File indexing completed on 2024-04-28 04:49:51

0001 /*
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bintvalidator.h"
0007 #include "k3b_i18n.h"
0008 
0009 #include <QDebug>
0010 #include <QString>
0011 #include <QWidget>
0012 
0013 
0014 K3b::IntValidator::IntValidator ( QWidget * parent )
0015     : QValidator(parent)
0016 {
0017     m_min = m_max = 0;
0018 }
0019 
0020 
0021 K3b::IntValidator::IntValidator ( int bottom, int top, QWidget * parent)
0022     : QValidator(parent)
0023 {
0024     m_min = bottom;
0025     m_max = top;
0026 }
0027 
0028 
0029 K3b::IntValidator::~IntValidator ()
0030 {
0031 }
0032 
0033 
0034 QValidator::State K3b::IntValidator::validate ( QString &str, int & ) const
0035 {
0036     bool ok;
0037     int  val = 0;
0038     QString newStr;
0039 
0040     newStr = str.trimmed();
0041     newStr = newStr.toUpper();
0042 
0043     if( newStr.length() ) {
0044         // check for < 0
0045         bool minus = newStr.startsWith( '-' );
0046         if( minus )
0047             newStr.remove( 0, 1 );
0048 
0049         // check for hex
0050         bool hex = newStr.startsWith( "0X" );
0051 
0052         if( hex )
0053             newStr.remove( 0, 2 );
0054 
0055         // a special case
0056         if( newStr.isEmpty() ) {
0057             if( minus && m_min && m_min >= 0)
0058                 ok = false;
0059             else
0060                 return QValidator::Acceptable;
0061         }
0062 
0063         val = newStr.toInt( &ok, hex ? 16 : 10 );
0064         if( minus )
0065             val *= -1;
0066     }
0067     else {
0068         val = 0;
0069         ok = true;
0070     }
0071 
0072     if( !ok )
0073         return QValidator::Invalid;
0074 
0075     if( m_min && val > 0 && val < m_min )
0076         return QValidator::Acceptable;
0077 
0078     if( m_max && val < 0 && val > m_max )
0079         return QValidator::Acceptable;
0080 
0081     if( (m_max && val > m_max) || (m_min && val < m_min) )
0082         return QValidator::Invalid;
0083 
0084     return QValidator::Intermediate;
0085 }
0086 
0087 
0088 void K3b::IntValidator::fixup ( QString& ) const
0089 {
0090     // TODO: remove preceding zeros
0091 }
0092 
0093 
0094 void K3b::IntValidator::setRange ( int bottom, int top )
0095 {
0096     m_min = bottom;
0097     m_max = top;
0098 
0099     if( m_max < m_min )
0100         m_max = m_min;
0101 }
0102 
0103 
0104 int K3b::IntValidator::bottom () const
0105 {
0106     return m_min;
0107 }
0108 
0109 
0110 int K3b::IntValidator::top () const
0111 {
0112     return m_max;
0113 }
0114 
0115 
0116 int K3b::IntValidator::toInt( const QString& s, bool* ok )
0117 {
0118     if( s.toLower().startsWith( "0x" ) )
0119         return s.right( s.length()-2 ).toInt( ok, 16 );
0120     else if( s.toLower().startsWith( "-0x" ) )
0121         return -1 * s.right( s.length()-3 ).toInt( ok, 16 );
0122     else
0123         return s.toInt( ok, 10 );
0124 }