• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • gui
  • liboktetawidgets
addressvalidator.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "addressvalidator.h"
24 
25 // Okteta core
26 #include <valuecodec.h>
27 // KDE
28 #include <KLocale>
29 #include <KDebug>
30 // Qt
31 #include <QtCore/QString>
32 #include <QtCore/QRegExp>
33 #include <QtScript/QScriptEngine>
34 #include <QtScript/QScriptValue>
35 
36 
37 namespace Okteta
38 {
39 
40 AddressValidator::AddressValidator( QObject* parent, Coding codecId )
41  : QValidator( parent ),
42  mCodecId( InvalidCoding ),
43  mValueCodec( 0 )
44 {
45  setCodec( codecId );
46 }
47 
48 void AddressValidator::setCodec( Coding codecId )
49 {
50  if( codecId == mCodecId )
51  return;
52 
53  mCodecId = codecId;
54 
55  delete mValueCodec;
56  mValueCodec = ValueCodec::createCodec( (Okteta::ValueCoding)mCodecId );
57 }
58 
59 const QRegExp AddressValidator::expressionRegex =
60  QRegExp(QLatin1String("[0-9a-fx\\+/\\s\\-\\*]+"),
61  Qt::CaseInsensitive, QRegExp::RegExp2); //FIXME this is way too simple, only there to test
62 
63 QValidator::State AddressValidator::validate( QString& string, int& pos ) const
64 {
65  Q_UNUSED( pos )
66 
67  State result = QValidator::Acceptable;
68  if( mCodecId == ExpressionCoding )
69  {
70  string = string.trimmed();
71  if( ! expressionRegex.exactMatch(string) )
72  result = QValidator::Invalid;
73  //only prefix has been typed:
74  if( string == QLatin1String("+")
75  || string == QLatin1String("-")
76  || string.endsWith(QLatin1Char('x')) ) // 0x at end
77  result = QValidator::Intermediate;
78  }
79  else
80  {
81  const int stringLength = string.length();
82  for( int i=0; i<stringLength; ++i )
83  {
84  const QChar c = string.at( i );
85  if( !mValueCodec->isValidDigit( c.toLatin1() ) && !c.isSpace() )
86  {
87  result = QValidator::Invalid;
88  break;
89  }
90  }
91  }
92  if( string.isEmpty() )
93  result = QValidator::Intermediate;
94  return result;
95 }
96 
97 Address AddressValidator::toAddress( const QString& string, AddressType* addressType) const
98 {
99  Address address;
100 
101  QString expression = string.trimmed();
102 
103  if( addressType )
104  {
105  const AddressType type =
106  expression.startsWith(QLatin1Char('+')) ? RelativeForwards :
107  expression.startsWith(QLatin1Char('-')) ? RelativeBackwards :
108  /* else */ AbsoluteAddress;
109 
110  if( type != AbsoluteAddress )
111  expression.remove( 0, 1 );
112 
113  *addressType = type;
114  }
115 
116  if( mCodecId == ExpressionCoding )
117  {
118  QScriptEngine evaluator;
119  QScriptValue value = evaluator.evaluate( expression );
120  address = value.toInt32();
121 kDebug() << "expression " << expression << " evaluated to: " << address;
122 
123  if( evaluator.hasUncaughtException() )
124  {
125  kWarning() << "evaluation error: "
126  << evaluator.uncaughtExceptionBacktrace();
127  if( addressType )
128  *addressType = InvalidAddressType;
129  }
130  }
131  else
132  {
133  const bool isHexadecimal = ( mCodecId == HexadecimalCoding );
134  const int base = isHexadecimal ? 16 : 10;
135  address = expression.toInt( 0, base );
136  }
137 
138  return address;
139 }
140 
141 
142 QString AddressValidator::toString( Address address, AddressType addressType ) const
143 {
144  //ExpressionCoding just uses base 10 so no need to adjust this code
145  const int isHexadecimal = ( mCodecId == HexadecimalCoding );
146  const int base = isHexadecimal ? 16 : 10;
147 
148  QString string = QString::number( address, base );
149 
150  if( addressType == RelativeForwards )
151  string.prepend( QLatin1Char('+') );
152  else if( addressType == RelativeBackwards )
153  string.prepend( QLatin1Char('-') );
154 
155  return string;
156 }
157 
158 AddressValidator::~AddressValidator()
159 {
160  delete mValueCodec;
161 }
162 
163 }
Okteta::InvalidCoding
Definition: oktetacore.h:35
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::ValueCodec::createCodec
static ValueCodec * createCodec(ValueCoding valueCoding)
Definition: valuecodec.cpp:36
Okteta::AddressValidator::AbsoluteAddress
Definition: addressvalidator.h:47
Okteta::AddressValidator::RelativeBackwards
Definition: addressvalidator.h:47
Okteta::ValueCodec::isValidDigit
virtual bool isValidDigit(unsigned char digit) const =0
Checks if the given digit is used in the encoding.
QObject
Okteta::AddressValidator::ExpressionCoding
Definition: addressvalidator.h:45
Okteta::AddressValidator::validate
virtual QValidator::State validate(QString &input, int &pos) const
Definition: addressvalidator.cpp:63
Okteta::AddressValidator::AddressValidator
AddressValidator(QObject *parent, Coding codecId=HexadecimalCoding)
Definition: addressvalidator.cpp:40
QValidator
Okteta::AddressValidator::toAddress
Address toAddress(const QString &string, AddressType *type=0) const
Definition: addressvalidator.cpp:97
Okteta::AddressValidator::RelativeForwards
Definition: addressvalidator.h:47
Okteta::AddressValidator::~AddressValidator
virtual ~AddressValidator()
Definition: addressvalidator.cpp:158
valuecodec.h
addressvalidator.h
Okteta::AddressValidator::setCodec
void setCodec(Coding codecId)
Sets one of the value codecs or any char codec.
Definition: addressvalidator.cpp:48
Okteta::AddressValidator::AddressType
AddressType
Definition: addressvalidator.h:47
Okteta::ValueCoding
ValueCoding
Definition: oktetacore.h:34
Okteta::AddressValidator::HexadecimalCoding
Definition: addressvalidator.h:45
Okteta::AddressValidator::Coding
Coding
Definition: addressvalidator.h:45
Okteta::AddressValidator::InvalidAddressType
Definition: addressvalidator.h:47
Okteta::AddressValidator::toString
QString toString(Address address, AddressType addressType) const
Definition: addressvalidator.cpp:142
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal