[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts
From: George Goldberg <grundleborg () googlemail ! com>
Date: 2009-09-14 8:51:45
Message-ID: 1252918305.917826.29279.nullmailer () svn ! kde ! org
[Download RAW message or body]
SVN commit 1023212 by gberg:
Reduce the amount of NIH (by using QValidators for int/uint types, rather than some \
crappy broken homebrew (yeah, it was my fault) validation code. Also make the \
validation results available through the model.
M +28 -42 integer-edit.cpp
M +9 -5 integer-edit.h
M +16 -0 parameter-edit-delegate.cpp
M +22 -6 parameter-edit-model.cpp
M +3 -2 parameter-edit-model.h
M +13 -0 parameter-item.cpp
M +6 -2 parameter-item.h
M +29 -28 unsigned-integer-edit.cpp
M +9 -5 unsigned-integer-edit.h
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/integer-edit.cpp \
#1023211:1023212 @@ -22,12 +22,15 @@
#include <KDebug>
-#include <QtGui/QKeyEvent>
+#include <QtGui/QIntValidator>
IntegerEdit::IntegerEdit(QWidget *parent)
: QLineEdit(parent)
{
connect(this, SIGNAL(textChanged(QString)), SLOT(onTextChanged(QString)));
+
+ // Set the validator range to the range of values in a 32 bit integer (dbus-type \
'i'). + setValidator(new QIntValidator(-2147483648, 2147483647, this));
}
IntegerEdit::~IntegerEdit()
@@ -35,64 +38,47 @@
}
-int IntegerEdit::value() const
+qint32 IntegerEdit::value() const
{
return text().toInt();
}
-void IntegerEdit::setValue(int integer)
+void IntegerEdit::setValue(qint32 integer)
{
setText(QString::number(integer));
}
-void IntegerEdit::keyPressEvent(QKeyEvent *event)
+QValidator::State IntegerEdit::validity() const
{
- kDebug() << "Key:" << event->key() << "Text:" << event->text();
+ int cursorPos = cursorPosition();
+ QString txt = text();
+ return validator()->validate(txt, cursorPos);
+}
- // If the text is empty or a modifier, allow the keypress
- if ((event->text().isEmpty()) || (event->key() < Qt::Key_Space)) {
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
+QPair<qint32, qint32> IntegerEdit::validRange() const
+{
+ QPair<qint32, qint32> ret;
+ ret.first = 0;
+ ret.second = 0;
- // If the key is backspace or delete, allow it
- if ((event->key() == Qt::Key_Delete) || (event->key() == Qt::Key_Backspace)) {
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
+ const QIntValidator *intValidator = qobject_cast<const \
QIntValidator*>(validator());
- // Check for numbers (and ensure maximum input length is not expended
- // FIXME: Have a better check to make sure the user doesn't enter a number too \
large.
- QString validKeys("0123456789");
- if (validKeys.contains(event->text())) {
- if (((text().contains(QString("-"))) && ((text().length() + 1) <= 5)) ||
- ((text().length() + 1) <= 4))
- {
- kDebug() << "Key is a number.";
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
+ if (!intValidator) {
+ kWarning() << "Somehow this is not an int validator :/";
+ return ret;
}
- // Check for minus sign as the first character
- if (event->text() == QString("-")) {
- if (cursorPosition() == 0) {
- if (!text().contains(QString("-"))) {
- kDebug() << "Key is a minus-sign at the start.";
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
- }
- }
+ ret.first = intValidator->bottom();
+ ret.second = intValidator->top();
- // Anything else, reject the keypress.
- event->accept();
+ return ret;
}
+void IntegerEdit::setValidRange(qint32 minimum, qint32 maximum)
+{
+ setValidator(new QIntValidator(static_cast<int>(minimum), \
static_cast<int>(maximum), this)); +}
+
void IntegerEdit::onTextChanged(const QString &text)
{
Q_EMIT integerChanged(text.toInt());
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/integer-edit.h \
#1023211:1023212 @@ -23,7 +23,9 @@
#include <kdemacros.h>
+#include <QtCore/QPair>
#include <QtGui/QLineEdit>
+#include <QtGui/QValidator>
class KDE_EXPORT IntegerEdit : public QLineEdit
{
@@ -33,14 +35,16 @@
explicit IntegerEdit(QWidget *parent = 0);
virtual ~IntegerEdit();
- int value() const;
- void setValue(int integer);
+ qint32 value() const;
+ void setValue(qint32 integer);
-protected:
- void keyPressEvent(QKeyEvent *event);
+ QValidator::State validity() const;
+ QPair<qint32, qint32> validRange() const;
+ void setValidRange(qint32 minimum, qint32 maximum);
+
Q_SIGNALS:
- void integerChanged(int integer);
+ void integerChanged(qint32 integer);
private Q_SLOTS:
void onTextChanged(const QString &text);
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/parameter-edit-delegate.cpp \
#1023211:1023212 @@ -247,18 +247,34 @@
{
kDebug();
+ IntegerEdit *widget = qobject_cast<IntegerEdit*>(sender());
+
+ if (!widget) {
+ kWarning() << "Slot called by object of the wrong type.";
+ return;
+ }
+
QModelIndex index = focusedIndex();
Q_EMIT dataChanged(index, QVariant(text), ParameterEditModel::ValueRole);
+ Q_EMIT dataChanged(index, QVariant(widget->validity()), \
ParameterEditModel::ValidityRole); }
void ParameterEditDelegate::onUnsignedIntegerEditTextChanged(const QString &text)
{
kDebug();
+ UnsignedIntegerEdit *widget = qobject_cast<UnsignedIntegerEdit*>(sender());
+
+ if (!widget) {
+ kWarning() << "Slot called by object of the wrong type.";
+ return;
+ }
+
QModelIndex index = focusedIndex();
Q_EMIT dataChanged(index, QVariant(text), ParameterEditModel::ValueRole);
+ Q_EMIT dataChanged(index, QVariant(widget->validity()), \
ParameterEditModel::ValidityRole); }
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/parameter-edit-model.cpp \
#1023211:1023212 @@ -24,6 +24,8 @@
#include <KDebug>
+#include <QtGui/QValidator>
+
#include <TelepathyQt4/ConnectionManager>
ParameterEditModel::ParameterEditModel(QObject *parent)
@@ -82,6 +84,9 @@
case ParameterEditModel::RequiredForRegistrationRole:
data = QVariant(m_items.at(index.row())->isRequiredForRegistration());
break;
+ case ParameterEditModel::ValidityRole:
+ data = QVariant(m_items.at(index.row())->validity());
+ break;
default:
break;
}
@@ -101,16 +106,27 @@
return false;
}
- switch(role)
- {
- case ParameterEditModel::ValueRole:
+ if (role == ParameterEditModel::ValueRole) {
+
m_items.at(index.row())->setValue(value);
Q_EMIT dataChanged(index, index);
return true;
- break;
- default:
+
+ } else if (ParameterEditModel::ValidityRole) {
+
+ if (value.toInt() == QValidator::Acceptable) {
+ m_items.at(index.row())->setValidity(QValidator::Acceptable);
+ } else if (value.toInt() == QValidator::Intermediate) {
+ m_items.at(index.row())->setValidity(QValidator::Intermediate);
+ } else {
+ m_items.at(index.row())->setValidity(QValidator::Invalid);
+ }
+
+ Q_EMIT dataChanged(index, index);
+ return true;
+
+ } else {
return false;
- break;
}
}
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/parameter-edit-model.h \
#1023211:1023212 @@ -18,8 +18,8 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#ifndef TELEPATHY_ACCOUNTS_KCM_PARAMETER_EDIT_MODEL_H
-#define TELEPATHY_ACCOUNTS_KCM_PARAMETER_EDIT_MODEL_H
+#ifndef LIB_KCM_TELEPATHY_ACCOUNTS_PARAMETER_EDIT_MODEL_H
+#define LIB_KCM_TELEPATHY_ACCOUNTS_PARAMETER_EDIT_MODEL_H
#include <QtCore/QAbstractListModel>
@@ -43,6 +43,7 @@
SecretRole,
RequiredRole,
RequiredForRegistrationRole,
+ ValidityRole,
UserRole = Qt::UserRole + 42
};
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/parameter-item.cpp \
#1023211:1023212 @@ -42,6 +42,9 @@
if (m_localizedName.isEmpty()) {
m_localizedName = parameter->name();
}
+
+ // Assume the default/un-altered value is valid.
+ m_validity = QValidator::Acceptable;
}
ParameterItem::~ParameterItem()
@@ -89,6 +92,16 @@
return m_parameter;
}
+QValidator::State ParameterItem::validity() const
+{
+ return m_validity;
+}
+
+void ParameterItem::setValidity(QValidator::State validity)
+{
+ m_validity = validity;
+}
+
void ParameterItem::setValue(const QVariant &value)
{
m_currentValue = value;
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/parameter-item.h \
#1023211:1023212 @@ -18,11 +18,12 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#ifndef TELEPATHY_ACCOUNTS_KCM_PARAMETER_ITEM_H
-#define TELEPATHY_ACCOUNTS_KCM_PARAMETER_ITEM_H
+#ifndef LIB_KCM_TELEPATHY_ACCOUNTS_PARAMETER_ITEM_H
+#define LIB_KCM_TELEPATHY_ACCOUNTS_PARAMETER_ITEM_H
#include <QtCore/QObject>
#include <QtCore/QVariant>
+#include <QtGui/QValidator>
#include <TelepathyQt4/ConnectionManager>
@@ -45,14 +46,17 @@
bool isRequired() const;
bool isRequiredForRegistration() const;
Tp::ProtocolParameter *parameter();
+ QValidator::State validity() const;
void setValue(const QVariant &value);
+ void setValidity(QValidator::State validity);
private:
Tp::ProtocolParameter *m_parameter;
const QVariant m_originalValue;
QVariant m_currentValue;
QString m_localizedName;
+ QValidator::State m_validity;
};
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/unsigned-integer-edit.cpp \
#1023211:1023212 @@ -22,12 +22,16 @@
#include <KDebug>
-#include <QtGui/QKeyEvent>
+#include <QtGui/QIntValidator>
UnsignedIntegerEdit::UnsignedIntegerEdit(QWidget *parent)
: QLineEdit(parent)
{
connect(this, SIGNAL(textChanged(QString)), SLOT(onTextChanged(QString)));
+
+ // Set the validator range to the range of values in a 32 bit unsigned integer \
(dbus-type 'u'). + // FIXME: Maximum value must be a valid type "int" for the \
validator to work. What a POS + setValidator(new QIntValidator(0, 2147483647, \
this)); }
UnsignedIntegerEdit::~UnsignedIntegerEdit()
@@ -45,41 +49,38 @@
setText(QString::number(unsignedInteger));
}
-void UnsignedIntegerEdit::keyPressEvent(QKeyEvent *event)
+QValidator::State UnsignedIntegerEdit::validity() const
{
- kDebug() << "Key:" << event->key() << "Text:" << event->text();
+ int cursorPos = cursorPosition();
+ QString txt = text();
+ return validator()->validate(txt, cursorPos);
+}
- // If the text is empty or a modifier, allow the keypress
- if ((event->text().isEmpty()) || (event->key() < Qt::Key_Space)) {
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
+QPair<quint32, quint32> UnsignedIntegerEdit::validRange() const
+{
+ QPair<quint32, quint32> ret;
+ ret.first = 0;
+ ret.second = 0;
- // If the key is backspace or delete, allow it
- if ((event->key() == Qt::Key_Delete) || (event->key() == Qt::Key_Backspace)) {
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
+ QIntValidator const *intValidator = qobject_cast<const \
QIntValidator*>(validator());
- // Check for numbers (and ensure maximum input length is not expended
- // FIXME: Have a better check to make sure the user doesn't enter a number too \
large.
- QString validKeys("0123456789");
- if (validKeys.contains(event->text())) {
- if ((text().length() + 1) <= 4)
- {
- kDebug() << "Key is a number.";
- event->ignore();
- QLineEdit::keyPressEvent(event);
- return;
- }
+ if (!intValidator) {
+ kWarning() << "Somehow this is not an int validator :/";
+ return ret;
}
- // Anything else, reject the keypress.
- event->accept();
+ ret.first = intValidator->bottom();
+ ret.second = intValidator->top();
+
+ return ret;
}
+// WARNING: Don't set a range outside of that supported by "int"!!!
+void UnsignedIntegerEdit::setValidRange(quint32 minimum, quint32 maximum)
+{
+ setValidator(new QIntValidator(static_cast<int>(minimum), \
static_cast<int>(maximum), this)); +}
+
void UnsignedIntegerEdit::onTextChanged(const QString &text)
{
Q_EMIT unsignedIntegerChanged(text.toUInt());
--- trunk/playground/network/telepathy-accounts-kcm/src/KCMTelepathyAccounts/unsigned-integer-edit.h \
#1023211:1023212 @@ -23,7 +23,9 @@
#include <kdemacros.h>
+#include <QtCore/QPair>
#include <QtGui/QLineEdit>
+#include <QtGui/QValidator>
class KDE_EXPORT UnsignedIntegerEdit : public QLineEdit
{
@@ -33,14 +35,16 @@
explicit UnsignedIntegerEdit(QWidget *parent = 0);
virtual ~UnsignedIntegerEdit();
- uint value() const;
- void setValue(uint unsignedInteger);
+ quint32 value() const;
+ void setValue(quint32 unsignedInteger);
-protected:
- void keyPressEvent(QKeyEvent *event);
+ QValidator::State validity() const;
+ QPair<quint32, quint32> validRange() const;
+ void setValidRange(quint32 minimum, quint32 maximum);
+
Q_SIGNALS:
- void unsignedIntegerChanged(uint unsignedInteger);
+ void unsignedIntegerChanged(quint32 unsignedInteger);
private Q_SLOTS:
void onTextChanged(const QString &text);
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic