~ubuntu-branches/ubuntu/maverick/kdeutils/maverick-proposed

« back to all changes in this revision

Viewing changes to okteta/kasten/gui/liboktetawidgets/addressvalidator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-05-28 09:49:30 UTC
  • mfrom: (1.2.44 upstream)
  • Revision ID: james.westby@ubuntu.com-20100528094930-jzynf0obv1n2v13a
Tags: 4:4.4.80-0ubuntu1~ppa1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <valuecodec.h>
27
27
// KDE
28
28
#include <KLocale>
 
29
#include <KDebug>
29
30
// Qt
30
31
#include <QtCore/QString>
 
32
#include <QtCore/QRegExp>
 
33
#include <QtScript/QScriptEngine>
 
34
#include <QtScript/QScriptValue>
31
35
 
32
36
 
33
37
namespace Okteta
52
56
    mValueCodec = ValueCodec::createCodec( (Okteta::ValueCoding)mCodecId );
53
57
}
54
58
 
 
59
const QRegExp AddressValidator::expressionRegex = QRegExp("[0-9a-fx\\+/\\s\\-\\*]+",
 
60
        Qt::CaseInsensitive, QRegExp::RegExp2); //FIXME this is way too simple, only there to test
 
61
 
55
62
QValidator::State AddressValidator::validate( QString& string, int& pos ) const
56
63
{
57
64
    Q_UNUSED( pos )
58
65
 
59
66
    State result = QValidator::Acceptable;
60
 
 
61
 
    const int stringLength = string.length();
62
 
    for( int i=0; i<stringLength; ++i )
 
67
    if( mCodecId == ExpressionCoding )
63
68
    {
64
 
        const QChar c = string.at( i );
65
 
        if( !mValueCodec->isValidDigit(c.toLatin1()) && !c.isSpace() )
66
 
        {
 
69
        string = string.trimmed();
 
70
        if( ! expressionRegex.exactMatch(string) )
67
71
            result = QValidator::Invalid;
68
 
            break;
 
72
        //only prefix has been typed:
 
73
        if( string == QLatin1String("+")
 
74
            || string == QLatin1String("-")
 
75
            || string.endsWith('x') ) // 0x at end
 
76
            result = QValidator::Intermediate;
 
77
    }
 
78
    else
 
79
    {
 
80
        const int stringLength = string.length();
 
81
        for( int i=0; i<stringLength; ++i )
 
82
        {
 
83
            const QChar c = string.at( i );
 
84
            if( !mValueCodec->isValidDigit( c.toLatin1() ) && !c.isSpace() )
 
85
            {
 
86
                result = QValidator::Invalid;
 
87
                break;
 
88
            }
69
89
        }
70
90
    }
71
 
 
 
91
    if( string.isEmpty() )
 
92
        result = QValidator::Intermediate;
72
93
    return result;
73
94
}
74
95
 
75
 
 
76
 
Address AddressValidator::toAddress( const QString& string ) const
 
96
Address AddressValidator::toAddress( const QString& string, AddressType* addressType) const
77
97
{
78
 
    const int isHexadecimal = ( mCodecId == HexadecimalCoding );
79
 
    const int base = isHexadecimal ? 16 : 10;
80
 
    const Okteta::Address address = string.toInt( 0, base );
 
98
    Address address;
 
99
 
 
100
    QString expression = string.trimmed();
 
101
 
 
102
    if( addressType )
 
103
    {
 
104
        const AddressType type =
 
105
            expression.startsWith('+') ? RelativeForwards :
 
106
            expression.startsWith('-') ? RelativeBackwards :
 
107
            /* else */                   AbsoluteAddress;
 
108
 
 
109
        if( type != AbsoluteAddress )
 
110
            expression.remove( 0, 1 );
 
111
 
 
112
        *addressType = type;
 
113
    }
 
114
 
 
115
    if( mCodecId == ExpressionCoding )
 
116
    {
 
117
        QScriptEngine evaluator;
 
118
        QScriptValue value = evaluator.evaluate( expression );
 
119
        address = value.toInt32();
 
120
kDebug() << "expression " << expression << " evaluated to: " << address;
 
121
 
 
122
        if( evaluator.hasUncaughtException() )
 
123
        {
 
124
            kWarning() << "evaluation error: "
 
125
                    << evaluator.uncaughtExceptionBacktrace();
 
126
            if( addressType )
 
127
                *addressType = InvalidAddressType;
 
128
        }
 
129
    }
 
130
    else
 
131
    {
 
132
        const bool isHexadecimal = ( mCodecId == HexadecimalCoding );
 
133
        const int base = isHexadecimal ? 16 : 10;
 
134
        address = expression.toInt( 0, base );
 
135
    }
81
136
 
82
137
    return address;
83
138
}
84
139
 
85
 
QString AddressValidator::toString( Address address ) const
 
140
 
 
141
QString AddressValidator::toString( Address address, AddressType addressType ) const
86
142
{
 
143
    //ExpressionCoding just uses base 10 so no need to adjust this code
87
144
    const int isHexadecimal = ( mCodecId == HexadecimalCoding );
88
145
    const int base = isHexadecimal ? 16 : 10;
89
 
    const QString string = QString::number( address, base );
 
146
 
 
147
    QString string = QString::number( address, base );
 
148
 
 
149
    if( addressType == RelativeForwards )
 
150
        string.prepend( '+' );
 
151
    else if( addressType == RelativeBackwards )
 
152
        string.prepend( '-' );
 
153
 
90
154
    return string;
91
155
}
92
156