kexi

longlongvalidator.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    Based on KIntValidator code by Glen Parker <glenebob@nwlink.com>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "longlongvalidator.h"
00023 
00024 #include <qwidget.h>
00025 
00026 using namespace KexiUtils;
00027 
00028 LongLongValidator::LongLongValidator( QWidget * parent, int base, const char * name )
00029  : QValidator(parent, name)
00030  , m_min(0), m_max(0)
00031 {
00032     setBase(base);
00033 }
00034 
00035 LongLongValidator::LongLongValidator( Q_LLONG bottom, Q_LLONG top, QWidget * parent, int base, const char * name )
00036   : QValidator(parent, name)
00037 {
00038     setBase(base);
00039     setRange( bottom, top );
00040 }
00041 
00042 LongLongValidator::~LongLongValidator()
00043 {
00044 }
00045 
00046 QValidator::State LongLongValidator::validate( QString &str, int & ) const
00047 {
00048     bool ok;
00049     Q_LLONG val = 0;
00050     QString newStr;
00051 
00052     newStr = str.stripWhiteSpace();
00053     if (m_base > 10)
00054         newStr = newStr.upper();
00055 
00056     if (newStr == QString::fromLatin1("-")) {// a special case
00057         if ((m_min || m_max) && m_min >= 0)
00058             ok = false;
00059         else
00060             return QValidator::Acceptable;
00061     }
00062     else if (!newStr.isEmpty())
00063         val = newStr.toLongLong(&ok, m_base);
00064     else {
00065         val = 0;
00066         ok = true;
00067     }
00068 
00069     if (! ok)
00070         return QValidator::Invalid;
00071 
00072     if ((! m_min && ! m_max) || (val >= m_min && val <= m_max))
00073         return QValidator::Acceptable;
00074 
00075     if (m_max && m_min >= 0 && val < 0)
00076         return QValidator::Invalid;
00077 
00078     return QValidator::Valid;
00079 }
00080 
00081 void LongLongValidator::fixup( QString &str ) const
00082 {
00083     int dummy;
00084     Q_LLONG val;
00085     QValidator::State state;
00086 
00087     state = validate(str, dummy);
00088 
00089     if (state == QValidator::Invalid || state == QValidator::Acceptable)
00090         return;
00091 
00092     if (! m_min && ! m_max)
00093         return;
00094 
00095     val = str.toLongLong(0, m_base);
00096 
00097     if (val < m_min)
00098         val = m_min;
00099     if (val > m_max)
00100         val = m_max;
00101 
00102     str.setNum(val, m_base);
00103 }
00104 
00105 void LongLongValidator::setRange( Q_LLONG bottom, Q_LLONG top )
00106 {
00107     m_min = bottom;
00108     m_max = top;
00109 
00110     if (m_max < m_min)
00111         m_max = m_min;
00112 }
00113 
00114 void LongLongValidator::setBase( int base )
00115 {
00116     m_base = base;
00117     if (m_base < 2)
00118         m_base = 2;
00119     if (m_base > 36)
00120         m_base = 36;
00121 }
00122 
00123 Q_LLONG LongLongValidator::bottom() const
00124 {
00125     return m_min;
00126 }
00127 
00128 Q_LLONG LongLongValidator::top() const
00129 {
00130     return m_max;
00131 }
00132 
00133 int LongLongValidator::base() const
00134 {
00135     return m_base;
00136 }
KDE Home | KDE Accessibility Home | Description of Access Keys