diff options
author | Christian Strømme <christian.stromme@nokia.com> | 2011-05-27 14:55:12 +0200 |
---|---|---|
committer | Christian Strømme <christian.stromme@nokia.com> | 2011-06-03 12:11:13 +0200 |
commit | 4d0c295cfe31ee765b5019442dd1554839f7a766 (patch) | |
tree | 0a3d1fb8191dafe5578c8561c6fc64a2ccf242f5 | |
parent | 3d5fac039b8c23ca883b0363b4c9990b639bfa24 (diff) |
Added functionality to set the echo-mode on string properties (QLineEdit editor).
Reviewed-by: Martin Pejcoch
-rw-r--r-- | qtpropertybrowser/examples/simple/main.cpp | 5 | ||||
-rw-r--r-- | qtpropertybrowser/src/qteditorfactory.cpp | 30 | ||||
-rw-r--r-- | qtpropertybrowser/src/qteditorfactory.h | 1 | ||||
-rw-r--r-- | qtpropertybrowser/src/qtpropertybrowser.cpp | 48 | ||||
-rw-r--r-- | qtpropertybrowser/src/qtpropertybrowser.h | 5 | ||||
-rw-r--r-- | qtpropertybrowser/src/qtpropertymanager.cpp | 46 | ||||
-rw-r--r-- | qtpropertybrowser/src/qtpropertymanager.h | 5 | ||||
-rw-r--r-- | qtpropertybrowser/src/qttreepropertybrowser.cpp | 7 | ||||
-rw-r--r-- | qtpropertybrowser/src/qtvariantproperty.cpp | 21 | ||||
-rw-r--r-- | qtpropertybrowser/src/qtvariantproperty.h | 1 |
10 files changed, 160 insertions, 9 deletions
diff --git a/qtpropertybrowser/examples/simple/main.cpp b/qtpropertybrowser/examples/simple/main.cpp index d62673b..72cd56a 100644 --- a/qtpropertybrowser/examples/simple/main.cpp +++ b/qtpropertybrowser/examples/simple/main.cpp @@ -75,6 +75,11 @@ int main(int argc, char **argv) item->setValue("Value"); topItem->addSubProperty(item); + item = variantManager->addProperty(QVariant::String, QString::number(i++) + QLatin1String(" String Property (Password)")); + item->setAttribute(QLatin1String("echoMode"), QLineEdit::Password); + item->setValue("Password"); + topItem->addSubProperty(item); + item = variantManager->addProperty(QVariant::Date, QString::number(i++) + QLatin1String(" Date Property")); item->setValue(QDate::currentDate().addDays(2)); topItem->addSubProperty(item); diff --git a/qtpropertybrowser/src/qteditorfactory.cpp b/qtpropertybrowser/src/qteditorfactory.cpp index 43c6d79..c303fb0 100644 --- a/qtpropertybrowser/src/qteditorfactory.cpp +++ b/qtpropertybrowser/src/qteditorfactory.cpp @@ -906,6 +906,7 @@ public: void slotPropertyChanged(QtProperty *property, const QString &value); void slotRegExpChanged(QtProperty *property, const QRegExp ®Exp); void slotSetValue(const QString &value); + void slotEchoModeChanged(QtProperty *, int); }; void QtLineEditFactoryPrivate::slotPropertyChanged(QtProperty *property, @@ -948,6 +949,26 @@ void QtLineEditFactoryPrivate::slotRegExpChanged(QtProperty *property, } } +void QtLineEditFactoryPrivate::slotEchoModeChanged(QtProperty *property, int echoMode) +{ + if (!m_createdEditors.contains(property)) + return; + + QtStringPropertyManager *manager = q_ptr->propertyManager(property); + if (!manager) + return; + + QListIterator<QLineEdit *> itEditor(m_createdEditors[property]); + while (itEditor.hasNext()) { + QLineEdit *editor = itEditor.next(); + editor->blockSignals(true); + editor->setEchoMode((EchoMode)echoMode); + editor->blockSignals(false); + } + + +} + void QtLineEditFactoryPrivate::slotSetValue(const QString &value) { QObject *object = q_ptr->sender(); @@ -1000,9 +1021,11 @@ QtLineEditFactory::~QtLineEditFactory() void QtLineEditFactory::connectPropertyManager(QtStringPropertyManager *manager) { connect(manager, SIGNAL(valueChanged(QtProperty *, const QString &)), - this, SLOT(slotPropertyChanged(QtProperty *, const QString &))); + this, SLOT(slotPropertyChanged(QtProperty *, const QString &))); connect(manager, SIGNAL(regExpChanged(QtProperty *, const QRegExp &)), - this, SLOT(slotRegExpChanged(QtProperty *, const QRegExp &))); + this, SLOT(slotRegExpChanged(QtProperty *, const QRegExp &))); + connect(manager, SIGNAL(echoModeChanged(QtProperty*, int)), + this, SLOT(slotEchoModeChanged(QtProperty *, int))); } /*! @@ -1015,6 +1038,7 @@ QWidget *QtLineEditFactory::createEditor(QtStringPropertyManager *manager, { QLineEdit *editor = d_ptr->createEditor(property, parent); + editor->setEchoMode((EchoMode)manager->echoMode(property)); QRegExp regExp = manager->regExp(property); if (regExp.isValid()) { QValidator *validator = new QRegExpValidator(regExp, editor); @@ -1040,6 +1064,8 @@ void QtLineEditFactory::disconnectPropertyManager(QtStringPropertyManager *manag this, SLOT(slotPropertyChanged(QtProperty *, const QString &))); disconnect(manager, SIGNAL(regExpChanged(QtProperty *, const QRegExp &)), this, SLOT(slotRegExpChanged(QtProperty *, const QRegExp &))); + disconnect(manager, SIGNAL(echoModeChanged(QtProperty*,int)), + this, SLOT(slotEchoModeChanged(QtProperty *, int))); } // QtDateEditFactory diff --git a/qtpropertybrowser/src/qteditorfactory.h b/qtpropertybrowser/src/qteditorfactory.h index 5fe7cab..6af079c 100644 --- a/qtpropertybrowser/src/qteditorfactory.h +++ b/qtpropertybrowser/src/qteditorfactory.h @@ -185,6 +185,7 @@ private: Q_DISABLE_COPY(QtLineEditFactory) Q_PRIVATE_SLOT(d_func(), void slotPropertyChanged(QtProperty *, const QString &)) Q_PRIVATE_SLOT(d_func(), void slotRegExpChanged(QtProperty *, const QRegExp &)) + Q_PRIVATE_SLOT(d_func(), void slotEchoModeChanged(QtProperty *, int)) Q_PRIVATE_SLOT(d_func(), void slotSetValue(const QString &)) Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *)) }; diff --git a/qtpropertybrowser/src/qtpropertybrowser.cpp b/qtpropertybrowser/src/qtpropertybrowser.cpp index bfa5705..8fccc51 100644 --- a/qtpropertybrowser/src/qtpropertybrowser.cpp +++ b/qtpropertybrowser/src/qtpropertybrowser.cpp @@ -42,6 +42,7 @@ #include <QtCore/QSet> #include <QtCore/QMap> #include <QtGui/QIcon> +#include <QtGui/QLineEdit> #if defined(Q_CC_MSVC) # pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ @@ -54,7 +55,10 @@ QT_BEGIN_NAMESPACE class QtPropertyPrivate { public: - QtPropertyPrivate(QtAbstractPropertyManager *manager) : m_enabled(true), m_modified(false), m_manager(manager) {} + QtPropertyPrivate(QtAbstractPropertyManager *manager) + : m_enabled(true), + m_modified(false), + m_manager(manager) {} QtProperty *q_ptr; QSet<QtProperty *> m_parentItems; @@ -296,6 +300,19 @@ QString QtProperty::valueText() const } /*! + Returns the display text according to the echo-mode set on the editor. + + When the editor is a QLineEdit, this will return a string equal to what + is displayed. + + \sa QtAbstractPropertyManager::valueText() +*/ +QString QtProperty::displayText() const +{ + return d_ptr->m_manager->displayText(this); +} + +/*! Sets the property's tool tip to the given \a text. \sa toolTip() @@ -718,6 +735,35 @@ QString QtAbstractPropertyManager::valueText(const QtProperty *property) const } /*! + Returns a string representing the current state of the given \a + property. + + The default implementation of this function returns an empty + string. + + \sa QtProperty::valueText() +*/ +QString QtAbstractPropertyManager::displayText(const QtProperty *property) const +{ + Q_UNUSED(property) + return QString(); +} + +/*! + Returns the echo mode representing the current state of the given \a + property. + + The default implementation of this function returns QLineEdit::Normal. + + \sa QtProperty::valueText() +*/ +EchoMode QtAbstractPropertyManager::echoMode(const QtProperty *property) const +{ + Q_UNUSED(property) + return QLineEdit::Normal; +} + +/*! Creates a property with the given \a name which then is owned by this manager. Internally, this function calls the createProperty() and diff --git a/qtpropertybrowser/src/qtpropertybrowser.h b/qtpropertybrowser/src/qtpropertybrowser.h index 99d7396..3ebb802 100644 --- a/qtpropertybrowser/src/qtpropertybrowser.h +++ b/qtpropertybrowser/src/qtpropertybrowser.h @@ -43,6 +43,7 @@ #include <QtGui/QWidget> #include <QtCore/QSet> +#include <QtGui/QLineEdit> #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -64,6 +65,7 @@ QT_BEGIN_NAMESPACE # define QT_QTPROPERTYBROWSER_EXPORT #endif +typedef QLineEdit::EchoMode EchoMode; class QtAbstractPropertyManager; class QtPropertyPrivate; @@ -87,6 +89,7 @@ public: bool hasValue() const; QIcon valueIcon() const; QString valueText() const; + QString displayText() const; void setToolTip(const QString &text); void setStatusTip(const QString &text); @@ -131,6 +134,8 @@ protected: virtual bool hasValue(const QtProperty *property) const; virtual QIcon valueIcon(const QtProperty *property) const; virtual QString valueText(const QtProperty *property) const; + virtual QString displayText(const QtProperty *property) const; + virtual EchoMode echoMode(const QtProperty *) const; virtual void initializeProperty(QtProperty *property) = 0; virtual void uninitializeProperty(QtProperty *property); virtual QtProperty *createProperty(); diff --git a/qtpropertybrowser/src/qtpropertymanager.cpp b/qtpropertybrowser/src/qtpropertymanager.cpp index a634e6d..09a062d 100644 --- a/qtpropertybrowser/src/qtpropertymanager.cpp +++ b/qtpropertybrowser/src/qtpropertymanager.cpp @@ -53,6 +53,7 @@ #include <QtGui/QPainter> #include <QtGui/QLabel> #include <QtGui/QCheckBox> +#include <QtGui/QLineEdit> #include <limits.h> #include <float.h> @@ -1229,11 +1230,12 @@ public: struct Data { - Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard) + Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard), echoMode(QLineEdit::Normal) { } QString val; QRegExp regExp; + int echoMode; }; typedef QMap<const QtProperty *, Data> PropertyValueMap; @@ -1328,15 +1330,39 @@ QRegExp QtStringPropertyManager::regExp(const QtProperty *property) const /*! \reimp */ +EchoMode QtStringPropertyManager::echoMode(const QtProperty *property) const +{ + return (EchoMode)getData<int>(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::echoMode, property, 0); +} + +/*! + \reimp +*/ QString QtStringPropertyManager::valueText(const QtProperty *property) const { const QtStringPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); if (it == d_ptr->m_values.constEnd()) return QString(); + return it.value().val; } /*! + \reimp +*/ +QString QtStringPropertyManager::displayText(const QtProperty *property) const +{ + const QtStringPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + + QLineEdit edit; + edit.setEchoMode((EchoMode)it.value().echoMode); + edit.setText(it.value().val); + return edit.displayText(); +} + +/*! \fn void QtStringPropertyManager::setValue(QtProperty *property, const QString &value) Sets the value of the given \a property to \a value. @@ -1391,6 +1417,24 @@ void QtStringPropertyManager::setRegExp(QtProperty *property, const QRegExp ® emit regExpChanged(property, data.regExp); } +void QtStringPropertyManager::setEchoMode(QtProperty *property, EchoMode echoMode) +{ + const QtStringPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + QtStringPropertyManagerPrivate::Data data = it.value(); + + if (data.echoMode == echoMode) + return; + + data.echoMode = echoMode; + it.value() = data; + + emit propertyChanged(property); + emit echoModeChanged(property, data.echoMode); +} + /*! \reimp */ diff --git a/qtpropertybrowser/src/qtpropertymanager.h b/qtpropertybrowser/src/qtpropertymanager.h index f5d157b..d8abdaa 100644 --- a/qtpropertybrowser/src/qtpropertymanager.h +++ b/qtpropertybrowser/src/qtpropertymanager.h @@ -42,6 +42,7 @@ #define QTPROPERTYMANAGER_H #include "qtpropertybrowser.h" +#include <QLineEdit> #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -174,15 +175,19 @@ public: QString value(const QtProperty *property) const; QRegExp regExp(const QtProperty *property) const; + EchoMode echoMode(const QtProperty *property) const; public Q_SLOTS: void setValue(QtProperty *property, const QString &val); void setRegExp(QtProperty *property, const QRegExp ®Exp); + void setEchoMode(QtProperty *property, EchoMode echoMode); Q_SIGNALS: void valueChanged(QtProperty *property, const QString &val); void regExpChanged(QtProperty *property, const QRegExp ®Exp); + void echoModeChanged(QtProperty *property, const int); protected: QString valueText(const QtProperty *property) const; + QString displayText(const QtProperty *property) const; virtual void initializeProperty(QtProperty *property); virtual void uninitializeProperty(QtProperty *property); private: diff --git a/qtpropertybrowser/src/qttreepropertybrowser.cpp b/qtpropertybrowser/src/qttreepropertybrowser.cpp index 36783cf..3794dd3 100644 --- a/qtpropertybrowser/src/qttreepropertybrowser.cpp +++ b/qtpropertybrowser/src/qttreepropertybrowser.cpp @@ -371,8 +371,9 @@ void QtPropertyEditorDelegate::paint(QPainter *painter, const QStyleOptionViewIt QTreeWidgetItem *item = m_editorPrivate->indexToItem(index); if (m_editedItem && m_editedItem == item) m_disablePainting = true; - } + } QItemDelegate::paint(painter, opt, index); + if (option.type) m_disablePainting = false; opt.palette.setCurrentColorGroup(QPalette::Active); @@ -620,10 +621,10 @@ void QtTreePropertyBrowserPrivate::updateItem(QTreeWidgetItem *item) if (property->hasValue()) { QString toolTip = property->toolTip(); if (toolTip.isEmpty()) - toolTip = property->valueText(); + toolTip = property->displayText(); item->setToolTip(1, toolTip); item->setIcon(1, property->valueIcon()); - item->setText(1, property->valueText()); + property->displayText().isEmpty() ? item->setText(1, property->valueText()) : item->setText(1, property->displayText()); } else if (markPropertiesWithoutValue() && !m_treeWidget->rootIsDecorated()) { expandIcon = m_expandIcon; } diff --git a/qtpropertybrowser/src/qtvariantproperty.cpp b/qtpropertybrowser/src/qtvariantproperty.cpp index 6ea935a..3585e32 100644 --- a/qtpropertybrowser/src/qtvariantproperty.cpp +++ b/qtpropertybrowser/src/qtvariantproperty.cpp @@ -315,6 +315,7 @@ public: void slotValueChanged(QtProperty *property, bool val); void slotValueChanged(QtProperty *property, const QString &val); void slotRegExpChanged(QtProperty *property, const QRegExp ®Exp); + void slotEchoModeChanged(QtProperty *property, int); void slotValueChanged(QtProperty *property, const QDate &val); void slotRangeChanged(QtProperty *property, const QDate &min, const QDate &max); void slotValueChanged(QtProperty *property, const QTime &val); @@ -370,6 +371,7 @@ public: const QString m_maximumAttribute; const QString m_minimumAttribute; const QString m_regExpAttribute; + const QString m_echoModeAttribute; }; QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() : @@ -381,7 +383,8 @@ QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() : m_flagNamesAttribute(QLatin1String("flagNames")), m_maximumAttribute(QLatin1String("maximum")), m_minimumAttribute(QLatin1String("minimum")), - m_regExpAttribute(QLatin1String("regExp")) + m_regExpAttribute(QLatin1String("regExp")), + m_echoModeAttribute(QLatin1String("echoMode")) { } @@ -537,6 +540,12 @@ void QtVariantPropertyManagerPrivate::slotRegExpChanged(QtProperty *property, co emit q_ptr->attributeChanged(varProp, m_regExpAttribute, QVariant(regExp)); } +void QtVariantPropertyManagerPrivate::slotEchoModeChanged(QtProperty *property, int mode) +{ + if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + emit q_ptr->attributeChanged(varProp, m_echoModeAttribute, QVariant(mode)); +} + void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, const QDate &val) { valueChanged(property, QVariant(val)); @@ -976,10 +985,14 @@ QtVariantPropertyManager::QtVariantPropertyManager(QObject *parent) d_ptr->m_typeToValueType[QVariant::String] = QVariant::String; d_ptr->m_typeToAttributeToAttributeType[QVariant::String][d_ptr->m_regExpAttribute] = QVariant::RegExp; + d_ptr->m_typeToAttributeToAttributeType[QVariant::String][d_ptr->m_echoModeAttribute] = + QVariant::Int; connect(stringPropertyManager, SIGNAL(valueChanged(QtProperty *, const QString &)), this, SLOT(slotValueChanged(QtProperty *, const QString &))); connect(stringPropertyManager, SIGNAL(regExpChanged(QtProperty *, const QRegExp &)), this, SLOT(slotRegExpChanged(QtProperty *, const QRegExp &))); + connect(stringPropertyManager, SIGNAL(echoModeChanged(QtProperty*,int)), + this, SLOT(slotEchoModeChanged(QtProperty*, int))); // DatePropertyManager QtDatePropertyManager *datePropertyManager = new QtDatePropertyManager(this); d_ptr->m_typeToPropertyManager[QVariant::Date] = datePropertyManager; @@ -1461,6 +1474,8 @@ QVariant QtVariantPropertyManager::attributeValue(const QtProperty *property, co } else if (QtStringPropertyManager *stringManager = qobject_cast<QtStringPropertyManager *>(manager)) { if (attribute == d_ptr->m_regExpAttribute) return stringManager->regExp(internProp); + if (attribute == d_ptr->m_echoModeAttribute) + return stringManager->echoMode(internProp); return QVariant(); } else if (QtDatePropertyManager *dateManager = qobject_cast<QtDatePropertyManager *>(manager)) { if (attribute == d_ptr->m_maximumAttribute) @@ -1704,6 +1719,8 @@ void QtVariantPropertyManager::setAttribute(QtProperty *property, } else if (QtStringPropertyManager *stringManager = qobject_cast<QtStringPropertyManager *>(manager)) { if (attribute == d_ptr->m_regExpAttribute) stringManager->setRegExp(internProp, qVariantValue<QRegExp>(value)); + if (attribute == d_ptr->m_echoModeAttribute) + stringManager->setEchoMode(internProp, (EchoMode)qVariantValue<int>(value)); return; } else if (QtDatePropertyManager *dateManager = qobject_cast<QtDatePropertyManager *>(manager)) { if (attribute == d_ptr->m_maximumAttribute) @@ -1768,7 +1785,7 @@ bool QtVariantPropertyManager::hasValue(const QtProperty *property) const QString QtVariantPropertyManager::valueText(const QtProperty *property) const { const QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); - return internProp ? internProp->valueText() : QString(); + return internProp ? !internProp->displayText().isEmpty() ? internProp->displayText() : internProp->valueText() : QString(); } /*! diff --git a/qtpropertybrowser/src/qtvariantproperty.h b/qtpropertybrowser/src/qtvariantproperty.h index c488247..0d24004 100644 --- a/qtpropertybrowser/src/qtvariantproperty.h +++ b/qtpropertybrowser/src/qtvariantproperty.h @@ -126,6 +126,7 @@ private: Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, bool)) Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, const QString &)) Q_PRIVATE_SLOT(d_func(), void slotRegExpChanged(QtProperty *, const QRegExp &)) + Q_PRIVATE_SLOT(d_func(), void slotEchoModeChanged(QtProperty *, int)) Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, const QDate &)) Q_PRIVATE_SLOT(d_func(), void slotRangeChanged(QtProperty *, const QDate &, const QDate &)) Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, const QTime &)) |