summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3bintvalidator.cpp
blob: 77e499c359d35c0105a0e6f1475ed9e944148bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 
 *
 * $Id: k3bintvalidator.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2004 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */


#include <tqwidget.h>
#include <tqstring.h>

#include "k3bintvalidator.h"

#include <tdelocale.h>
#include <tdeglobal.h>
#include <kdebug.h>


K3bIntValidator::K3bIntValidator ( TQWidget * parent, const char * name )
  : TQValidator(parent, name)
{
  m_min = m_max = 0;
}


K3bIntValidator::K3bIntValidator ( int bottom, int top, TQWidget * parent, const char * name )
  : TQValidator(parent, name)
{
  m_min = bottom;
  m_max = top;
}


K3bIntValidator::~K3bIntValidator ()
{
}


TQValidator::State K3bIntValidator::validate ( TQString &str, int & ) const
{
  bool ok;
  int  val = 0;
  TQString newStr;

  newStr = str.stripWhiteSpace();
  newStr = newStr.upper();

  if( newStr.length() ) {
    // check for < 0
    bool minus = newStr.startsWith( "-" );
    if( minus )
      newStr.remove( 0, 1 );

    // check for hex
    bool hex = newStr.startsWith( "0X" );

    if( hex )
      newStr.remove( 0, 2 );

    // a special case
    if( newStr.isEmpty() ) {
      if( minus && m_min && m_min >= 0)
	ok = false;
      else
	return TQValidator::Acceptable;
    }

    val = newStr.toInt( &ok, hex ? 16 : 10 );
    if( minus )
      val *= -1;
  }
  else {
    val = 0;
    ok = true;
  }

  if( !ok )
    return TQValidator::Invalid;

  if( m_min && val > 0 && val < m_min )
    return TQValidator::Acceptable;

  if( m_max && val < 0 && val > m_max )
    return TQValidator::Acceptable;

  if( (m_max && val > m_max) || (m_min && val < m_min) )
    return TQValidator::Invalid;

  return TQValidator::Valid;
}


void K3bIntValidator::fixup ( TQString& ) const
{
  // TODO: remove preceding zeros
}


void K3bIntValidator::setRange ( int bottom, int top )
{
  m_min = bottom;
  m_max = top;

  if( m_max < m_min )
    m_max = m_min;
}


int K3bIntValidator::bottom () const
{
  return m_min;
}


int K3bIntValidator::top () const
{
  return m_max;
}


int K3bIntValidator::toInt( const TQString& s, bool* ok )
{
  if( s.lower().startsWith( "0x" ) )
    return s.right( s.length()-2 ).toInt( ok, 16 );
  else if( s.lower().startsWith( "-0x" ) )
    return -1 * s.right( s.length()-3 ).toInt( ok, 16 );
  else
    return s.toInt( ok, 10 );
}