From 4184dbd1ebad1c4afd77d88a0940d007a4b09192 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 23 Apr 2017 14:18:58 +0300 Subject: [PATCH 01/39] Fixing "Set but not used" warnings --- src/qtbuttonpropertybrowser.cpp | 1 + src/qtgroupboxpropertybrowser.cpp | 1 + src/qtpropertybrowser.cpp | 1 + 3 files changed, 3 insertions(+) diff --git a/src/qtbuttonpropertybrowser.cpp b/src/qtbuttonpropertybrowser.cpp index 58cfc51..0cb22b0 100644 --- a/src/qtbuttonpropertybrowser.cpp +++ b/src/qtbuttonpropertybrowser.cpp @@ -287,6 +287,7 @@ void QtButtonPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBr w = q_ptr; l = m_mainLayout; } + Q_UNUSED(w); QFrame *container = new QFrame(); container->setFrameShape(QFrame::Panel); container->setFrameShadow(QFrame::Raised); diff --git a/src/qtgroupboxpropertybrowser.cpp b/src/qtgroupboxpropertybrowser.cpp index caf074d..ae38f03 100644 --- a/src/qtgroupboxpropertybrowser.cpp +++ b/src/qtgroupboxpropertybrowser.cpp @@ -326,6 +326,7 @@ void QtGroupBoxPropertyBrowserPrivate::propertyRemoved(QtBrowserItem *index) if (hasHeader(par)) oldRow += 2; } + Q_UNUSED(w); if (parentItem->widget) { parentItem->widget->hide(); diff --git a/src/qtpropertybrowser.cpp b/src/qtpropertybrowser.cpp index 2449fcb..3ac8723 100644 --- a/src/qtpropertybrowser.cpp +++ b/src/qtpropertybrowser.cpp @@ -1875,6 +1875,7 @@ QtBrowserItem *QtAbstractPropertyBrowser::insertProperty(QtProperty *property, int pos = 0; int newPos = 0; QtProperty *properAfterProperty = 0; + Q_UNUSED(properAfterProperty); while (pos < pendingList.count()) { QtProperty *prop = pendingList.at(pos); if (prop == property) From cafe071159d684f2aae3120ab38c86c9d80c509a Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 23 Apr 2017 14:25:25 +0300 Subject: [PATCH 02/39] Fixing the deprecated warnings because of QStyleOptionViewItemV3 (which are just typedefs over QStyleOptionViewItem class) --- src/qttreepropertybrowser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qttreepropertybrowser.cpp b/src/qttreepropertybrowser.cpp index 9e506af..a92ab53 100644 --- a/src/qttreepropertybrowser.cpp +++ b/src/qttreepropertybrowser.cpp @@ -145,7 +145,7 @@ QtPropertyEditorView::QtPropertyEditorView(QWidget *parent) : void QtPropertyEditorView::drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - QStyleOptionViewItemV3 opt = option; + QStyleOptionViewItem opt = option; bool hasValue = true; if (m_editorPrivate) { QtProperty *property = m_editorPrivate->indexToProperty(index); @@ -347,7 +347,7 @@ void QtPropertyEditorDelegate::paint(QPainter *painter, const QStyleOptionViewIt if (property) hasValue = property->hasValue(); } - QStyleOptionViewItemV3 opt = option; + QStyleOptionViewItem opt = option; if ((m_editorPrivate && index.column() == 0) || !hasValue) { QtProperty *property = m_editorPrivate->indexToProperty(index); if (property && property->isModified()) { From 9f24ddd570d8f0a9a78fefc083a31e64567d186d Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 7 Oct 2018 04:10:25 +0300 Subject: [PATCH 03/39] Added "Max Length" property for string field --- src/qteditorfactory.cpp | 46 +++++++++++++++++++++++++++++++++++++++ src/qteditorfactory.h | 2 ++ src/qtpropertymanager.cpp | 40 +++++++++++++++++++++++++++++++++- src/qtpropertymanager.h | 3 +++ src/qtvariantproperty.cpp | 17 +++++++++++++++ src/qtvariantproperty.h | 1 + 6 files changed, 108 insertions(+), 1 deletion(-) diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index a2ef86c..3a06f9e 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -905,7 +905,9 @@ class QtLineEditFactoryPrivate : public EditorFactoryPrivate void slotPropertyChanged(QtProperty *property, const QString &value); void slotRegExpChanged(QtProperty *property, const QRegExp ®Exp); + void slotMaxLengthChanged(QtProperty *property, int maxlen); void slotSetValue(const QString &value); + void slotSetMaxLength(int maxlen); }; void QtLineEditFactoryPrivate::slotPropertyChanged(QtProperty *property, @@ -948,6 +950,27 @@ void QtLineEditFactoryPrivate::slotRegExpChanged(QtProperty *property, } } +void QtLineEditFactoryPrivate::slotMaxLengthChanged(QtProperty *property, int maxlen) +{ + if (!m_createdEditors.contains(property)) + return; + + QtStringPropertyManager *manager = q_ptr->propertyManager(property); + if (!manager) + return; + + if(maxlen < 0) + maxlen = 32767; + + QListIterator itEditor(m_createdEditors[property]); + while (itEditor.hasNext()) { + QLineEdit *editor = itEditor.next(); + editor->blockSignals(true); + editor->setMaxLength(maxlen); + editor->blockSignals(false); + } +} + void QtLineEditFactoryPrivate::slotSetValue(const QString &value) { QObject *object = q_ptr->sender(); @@ -963,6 +986,21 @@ void QtLineEditFactoryPrivate::slotSetValue(const QString &value) } } +void QtLineEditFactoryPrivate::slotSetMaxLength(int maxlen) +{ + QObject *object = q_ptr->sender(); + const QMap::ConstIterator ecend = m_editorToProperty.constEnd(); + for (QMap::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor) + if (itEditor.key() == object) { + QtProperty *property = itEditor.value(); + QtStringPropertyManager *manager = q_ptr->propertyManager(property); + if (!manager) + return; + manager->setMaxLength(property, maxlen); + return; + } +} + /*! \class QtLineEditFactory @@ -1003,6 +1041,8 @@ void QtLineEditFactory::connectPropertyManager(QtStringPropertyManager *manager) this, SLOT(slotPropertyChanged(QtProperty *, const QString &))); connect(manager, SIGNAL(regExpChanged(QtProperty *, const QRegExp &)), this, SLOT(slotRegExpChanged(QtProperty *, const QRegExp &))); + connect(manager, SIGNAL(maxLenChanged(QtProperty*,int)), + this, SLOT(slotMaxLengthChanged(QtProperty *, int))); } /*! @@ -1020,6 +1060,10 @@ QWidget *QtLineEditFactory::createEditor(QtStringPropertyManager *manager, QValidator *validator = new QRegExpValidator(regExp, editor); editor->setValidator(validator); } + int maxlen = manager->maxLength(property); + if(maxlen < 0) + maxlen = 32767; + editor->setMaxLength(maxlen); editor->setText(manager->value(property)); connect(editor, SIGNAL(textEdited(const QString &)), @@ -1040,6 +1084,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(maxLenChanged(QtProperty*,int)), + this, SLOT(slotMaxLengthChanged(QtProperty *, int))); } // QtDateEditFactory diff --git a/src/qteditorfactory.h b/src/qteditorfactory.h index 5fe7cab..7f0a157 100644 --- a/src/qteditorfactory.h +++ b/src/qteditorfactory.h @@ -185,7 +185,9 @@ class QT_QTPROPERTYBROWSER_EXPORT QtLineEditFactory : public QtAbstractEditorFac 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 slotMaxLengthChanged(QtProperty *, int)) Q_PRIVATE_SLOT(d_func(), void slotSetValue(const QString &)) + Q_PRIVATE_SLOT(d_func(), void slotSetMaxLength(int)) Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *)) }; diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index ef627e9..39024e4 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -1229,11 +1229,12 @@ class QtStringPropertyManagerPrivate struct Data { - Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard) + Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard), maxLen(-1) { } QString val; QRegExp regExp; + int maxLen; }; typedef QMap PropertyValueMap; @@ -1325,6 +1326,19 @@ QRegExp QtStringPropertyManager::regExp(const QtProperty *property) const return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::regExp, property, QRegExp()); } +/*! + Returns the given \a property's currently set maximum length. + + If the given \a property is not managed by this manager, this + function returns an empty expression. + + \sa setMaxLength() +*/ +int QtStringPropertyManager::maxLength(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::maxLen, property, -1); +} + /*! \reimp */ @@ -1357,6 +1371,9 @@ void QtStringPropertyManager::setValue(QtProperty *property, const QString &val) if (data.val == val) return; + if ((data.maxLen > 0) && (val.length() > data.maxLen)) + return; + if (data.regExp.isValid() && !data.regExp.exactMatch(val)) return; @@ -1391,6 +1408,27 @@ void QtStringPropertyManager::setRegExp(QtProperty *property, const QRegExp ® emit regExpChanged(property, data.regExp); } +void QtStringPropertyManager::setMaxLength(QtProperty *property, int maxlen) +{ + 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.maxLen == maxlen) + return; + + if (maxlen < -1) + return; + + data.maxLen = maxlen; + + it.value() = data; + + emit maxLenChanged(property, data.maxLen); +} + /*! \reimp */ diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index f5d157b..413b555 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -174,13 +174,16 @@ class QT_QTPROPERTYBROWSER_EXPORT QtStringPropertyManager : public QtAbstractPro QString value(const QtProperty *property) const; QRegExp regExp(const QtProperty *property) const; + int maxLength(const QtProperty *property) const; public Q_SLOTS: void setValue(QtProperty *property, const QString &val); void setRegExp(QtProperty *property, const QRegExp ®Exp); + void setMaxLength(QtProperty *property, int maxlen); Q_SIGNALS: void valueChanged(QtProperty *property, const QString &val); void regExpChanged(QtProperty *property, const QRegExp ®Exp); + void maxLenChanged(QtProperty *property, int maxlen); protected: QString valueText(const QtProperty *property) const; virtual void initializeProperty(QtProperty *property); diff --git a/src/qtvariantproperty.cpp b/src/qtvariantproperty.cpp index c41730c..42bb8e6 100644 --- a/src/qtvariantproperty.cpp +++ b/src/qtvariantproperty.cpp @@ -328,6 +328,7 @@ class QtVariantPropertyManagerPrivate void slotValueChanged(QtProperty *property, bool val); void slotValueChanged(QtProperty *property, const QString &val); void slotRegExpChanged(QtProperty *property, const QRegExp ®Exp); + void slotMaxLengthChanged(QtProperty *property, int maxlen); void slotValueChanged(QtProperty *property, const QDate &val); void slotRangeChanged(QtProperty *property, const QDate &min, const QDate &max); void slotValueChanged(QtProperty *property, const QTime &val); @@ -382,6 +383,7 @@ class QtVariantPropertyManagerPrivate const QString m_flagNamesAttribute; const QString m_maximumAttribute; const QString m_minimumAttribute; + const QString m_maxlengthAttribute; const QString m_regExpAttribute; }; @@ -394,6 +396,7 @@ QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() : m_flagNamesAttribute(QLatin1String("flagNames")), m_maximumAttribute(QLatin1String("maximum")), m_minimumAttribute(QLatin1String("minimum")), + m_maxlengthAttribute(QLatin1String("maxlength")), m_regExpAttribute(QLatin1String("regExp")) { } @@ -550,6 +553,12 @@ void QtVariantPropertyManagerPrivate::slotRegExpChanged(QtProperty *property, co emit q_ptr->attributeChanged(varProp, m_regExpAttribute, QVariant(regExp)); } +void QtVariantPropertyManagerPrivate::slotMaxLengthChanged(QtProperty *property, int maxlen) +{ + if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + emit q_ptr->attributeChanged(varProp, m_maxlengthAttribute, QVariant(maxlen)); +} + void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, const QDate &val) { valueChanged(property, QVariant(val)); @@ -989,10 +998,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_maxlengthAttribute] = + 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(maxLenChanged(QtProperty*,int)), + this, SLOT(slotMaxLengthChanged(QtProperty *, int))); // DatePropertyManager QtDatePropertyManager *datePropertyManager = new QtDatePropertyManager(this); d_ptr->m_typeToPropertyManager[QVariant::Date] = datePropertyManager; @@ -1526,6 +1539,8 @@ QVariant QtVariantPropertyManager::attributeValue(const QtProperty *property, co } else if (QtStringPropertyManager *stringManager = qobject_cast(manager)) { if (attribute == d_ptr->m_regExpAttribute) return stringManager->regExp(internProp); + if (attribute == d_ptr->m_maxlengthAttribute) + return stringManager->maxLength(internProp); return QVariant(); } else if (QtDatePropertyManager *dateManager = qobject_cast(manager)) { if (attribute == d_ptr->m_maximumAttribute) @@ -1769,6 +1784,8 @@ void QtVariantPropertyManager::setAttribute(QtProperty *property, } else if (QtStringPropertyManager *stringManager = qobject_cast(manager)) { if (attribute == d_ptr->m_regExpAttribute) stringManager->setRegExp(internProp, value.value()); + if (attribute == d_ptr->m_maxlengthAttribute) + stringManager->setMaxLength(internProp, value.value()); return; } else if (QtDatePropertyManager *dateManager = qobject_cast(manager)) { if (attribute == d_ptr->m_maximumAttribute) diff --git a/src/qtvariantproperty.h b/src/qtvariantproperty.h index 5fb83d6..9eb7cf9 100644 --- a/src/qtvariantproperty.h +++ b/src/qtvariantproperty.h @@ -130,6 +130,7 @@ public Q_SLOTS: 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 slotMaxLengthChanged(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 &)) From 7f9da665786c087fd80672e20535202ca786a13f Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 7 Oct 2018 04:14:03 +0300 Subject: [PATCH 04/39] [WIP] Added example of properties made from JSON --- .gitignore | 2 + examples/CMakeLists.txt | 1 + examples/json/CMakeLists.txt | 15 +++ examples/json/json.cpp | 234 +++++++++++++++++++++++++++++++++++ examples/json/json.h | 21 ++++ examples/json/sample.json | 58 +++++++++ examples/json/setup.json | 7 ++ 7 files changed, 338 insertions(+) create mode 100644 examples/json/CMakeLists.txt create mode 100644 examples/json/json.cpp create mode 100644 examples/json/json.h create mode 100644 examples/json/sample.json create mode 100644 examples/json/setup.json diff --git a/.gitignore b/.gitignore index b667c89..6629825 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ # Ignores 'build' in any sub folder build/ +*.user* + diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index af43083..b192366 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,3 +13,4 @@ ADD_SUBDIRECTORY(demo) ADD_SUBDIRECTORY(extension) ADD_SUBDIRECTORY(object_controller) ADD_SUBDIRECTORY(simple) +ADD_SUBDIRECTORY(json) diff --git a/examples/json/CMakeLists.txt b/examples/json/CMakeLists.txt new file mode 100644 index 0000000..9279bfd --- /dev/null +++ b/examples/json/CMakeLists.txt @@ -0,0 +1,15 @@ +# Tell CMake to run moc when necessary: +set(CMAKE_AUTOMOC ON) + +# As moc files are generated in the binary dir, tell CMake +# to always look for includes there: +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +SET(example_name json) + +SET(KIT_SRCS + json.cpp + ) + +ADD_EXECUTABLE(${example_name} ${KIT_SRCS}) +TARGET_LINK_LIBRARIES(${example_name} ${PROJECT_NAME}) diff --git a/examples/json/json.cpp b/examples/json/json.cpp new file mode 100644 index 0000000..a6263b7 --- /dev/null +++ b/examples/json/json.cpp @@ -0,0 +1,234 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of a Qt Solutions component. +** +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +****************************************************************************/ + +#include "json.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include "qtpropertymanager.h" +#include "qtvariantproperty.h" +#include "qtgroupboxpropertybrowser.h" +#include "qtbuttonpropertybrowser.h" +#include "qttreepropertybrowser.h" + +#include + +const char *sample_config = "sample.json"; +const char *sample_settings = "setup.json"; + + +PropJsonIO::PropJsonIO(QObject *parent) : + QObject(parent) +{} + +PropJsonIO::~PropJsonIO() {} + +void PropJsonIO::currentItemChanged(QtBrowserItem *item) +{} + + +static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVariantPropertyManager *manager, QtProperty *target, QWidget *parent = nullptr, QString *err = nullptr) +{ + for(const QJsonValue &o : elements) + { + QString type = o["type"].toString("invalid"); + QString name = o["name"].toString(type); + QString title = o["title"].toString(name); + QString control = o["control"].toString(); + QtVariantProperty *item = nullptr; + + if(control.isEmpty()) + continue;//invalid + if(type.isEmpty() || (control != "group" && type == "invalid")) + continue; + if(name.isEmpty()) + continue;//invalid + + if(!control.compare("spinBox", Qt::CaseInsensitive)) + { + if(!type.compare("int", Qt::CaseInsensitive)) + { + int valueDefault = o["value-default"].toInt(); + int valueMin = o["value-min"].toInt(0); + int valueMax = o["value-max"].toInt(100); + int singleStep = o["single-step"].toInt(1); + item = manager->addProperty(QVariant::Int, title); + item->setValue(valueDefault); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setAttribute(QLatin1String("singleStep"), singleStep); + target->addSubProperty(item); + } + else if(!type.compare("double", Qt::CaseInsensitive) || !type.compare("float", Qt::CaseInsensitive)) + { + double valueDefault = o["value-default"].toDouble(); + double valueMin = o["value-min"].toDouble(0.0); + double valueMax = o["value-max"].toDouble(100.0); + double singleStep = o["single-step"].toDouble(1); + int decimals = o["decimals"].toInt(1); + item = manager->addProperty(QVariant::Double, title); + item->setValue(valueDefault); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setAttribute(QLatin1String("singleStep"), singleStep); + item->setAttribute(QLatin1String("decimals"), decimals); + target->addSubProperty(item); + } + } + if(!control.compare("lineEdit", Qt::CaseInsensitive)) + { + int maxLength = o["max-length"].toInt(-1); + QString valueDefault = o["value-default"].toString(); + QString validator = o["validator"].toString(); + item = manager->addProperty(QVariant::String, title); + item->setValue(valueDefault); + item->setAttribute(QLatin1String("maxlength"), maxLength); + if(!validator.isEmpty()) + item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); + target->addSubProperty(item); + } + else + if(!control.compare("group", Qt::CaseInsensitive)) + { + QJsonArray children = o["children"].toArray(); + if(!children.isEmpty()) + { + QtProperty *subGroup = manager->addProperty(QtVariantPropertyManager::groupTypeId(), title); + target->addSubProperty(subGroup); + loadPropertiesLoayout_fillElements(children, manager, subGroup, parent, err); + } + } + } +} + +static QtAbstractPropertyBrowser *loadPropertiesLoayout(const QByteArray &json, QWidget *parent = nullptr, QString *err = nullptr) +{ + QtAbstractPropertyBrowser *gui = nullptr; + QString style; + QString title; + + QJsonParseError errCode; + QJsonDocument layout = QJsonDocument::fromJson(json, &errCode); + + + if(errCode.error != QJsonParseError::NoError) + { + if(err) + *err = errCode.errorString(); + return nullptr; + } + + QJsonObject layoutData = layout.object(); + + style = layoutData["style"].toString(); + title = layoutData["title"].toString(); + if(style == "groupbox") + gui = new QtGroupBoxPropertyBrowser(parent); + else if(style == "button") + gui = new QtButtonPropertyBrowser(parent); + else // "tree" is default + { + QtTreePropertyBrowser *gui_t = new QtTreePropertyBrowser(parent); + gui_t->setPropertiesWithoutValueMarked(true); + gui_t->setRootIsDecorated(false); + gui = gui_t; + } + + QtVariantPropertyManager *variantManager = new QtVariantPropertyManager(gui); + + QtProperty *topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), title); + + QJsonArray layoutEntries = layoutData["layout"].toArray(); + loadPropertiesLoayout_fillElements(layoutEntries, variantManager, topItem, parent, err); + + QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(gui); + + gui->setFactoryForManager(variantManager, variantFactory); + gui->addProperty(topItem); + + return gui; +} + + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + QFile f; + f.setFileName(sample_config); + if(!f.open(QIODevice::ReadOnly)) + { + QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); + return 1; + } + + QByteArray layoutJson = f.readAll(); + f.close(); + +// QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(); + + QString error; + //QtTreePropertyBrowser *variantEditor = new QtTreePropertyBrowser(); + QtAbstractPropertyBrowser *variantEditor = loadPropertiesLoayout(layoutJson, nullptr, &error); + if(!variantEditor) + { + QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(error)); + return 1; + } + +// variantEditor->setFactoryForManager(variantManager, variantFactory); +// variantEditor->addProperty(topItem); + + variantEditor->show(); + + int ret = app.exec(); + + // delete variantManager; + // delete variantFactory; + delete variantEditor; + + return ret; +} diff --git a/examples/json/json.h b/examples/json/json.h new file mode 100644 index 0000000..d07d2dd --- /dev/null +++ b/examples/json/json.h @@ -0,0 +1,21 @@ +#ifndef JSONNNNNNN +#define JSONNNNNNN + +#include +#include +#include + +class PropJsonIO : public QObject +{ + Q_OBJECT + + QJsonDocument m_doc; +public: + explicit PropJsonIO(QObject *parent = nullptr); + virtual ~PropJsonIO(); + +public slots: + void currentItemChanged(QtBrowserItem *item); +}; + +#endif//JSONNNNNNN diff --git a/examples/json/sample.json b/examples/json/sample.json new file mode 100644 index 0000000..4341a49 --- /dev/null +++ b/examples/json/sample.json @@ -0,0 +1,58 @@ +{ + "style" : "tree", + "title" : "Sample setup", + "layout": [{ + "control": "spinBox", + "type": "int", + "name": "testNumber", + "title": "Test integer number value", + "value-default": 5, + "value-min": -42, + "value-max": 42, + "single-step": 2 + }, + { + "control": "spinBox", + "type": "double", + "name": "floatTest", + "title": "Test floating number value", + "value-default": 5.0, + "value-min": -42.0, + "value-max": 42.0, + "single-step": 0.1, + "decimals" : 3 + }, + { + "control": "group", + "name": "alienShit", + "title": "Some alien shit", + "children": [{ + "control": "spinBox", + "type": "int", + "name": "child1", + "title" : "Kek", + "value-default": 5, + "value-min": -42, + "value-max": 42 + }, + { + "control": "lineEdit", + "type": "string", + "name": "child2", + "title" : "Pek", + "value-default": "", + "max-length": 42, + "validator": "^a\\ sh\\dt$" + }, + { + "control": "lineEdit", + "type": "string", + "name": "child3", + "title" : "Tek", + "value-default": "", + "max-length": 5 + } + ] + } + ] +} diff --git a/examples/json/setup.json b/examples/json/setup.json new file mode 100644 index 0000000..b2cecd4 --- /dev/null +++ b/examples/json/setup.json @@ -0,0 +1,7 @@ +{ + "testNumber": 5, + "alienShit": { + "child1": 5, + "child2": "Kek" + } +} From da08e4a7a37c356e801b6541064d528f75bd7bdf Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Fri, 12 Oct 2018 02:40:50 +0300 Subject: [PATCH 05/39] JSON sample: added the data restorer from the setup json --- examples/json/json.cpp | 97 +++++++++++++++++++++++++++++++++++---- examples/json/sample.json | 29 +++++++++++- examples/json/setup.json | 13 ++++-- 3 files changed, 126 insertions(+), 13 deletions(-) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index a6263b7..03b0e31 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -56,6 +57,8 @@ #include +#include + const char *sample_config = "sample.json"; const char *sample_settings = "setup.json"; @@ -69,8 +72,75 @@ PropJsonIO::~PropJsonIO() {} void PropJsonIO::currentItemChanged(QtBrowserItem *item) {} +static QVariant retrieve_property(const QStack &setupTree, QString prop, const QVariant &defaultValue) +{ + /* + TODO: It's a crap! Put settings loader into separated place, but keep here the digging through already loaded tree + */ + QFile f; + f.setFileName(sample_settings); + if(!f.open(QIODevice::ReadOnly)) + { + QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); + return QVariant(); + } + + QByteArray layoutJson = f.readAll(); + f.close(); + QJsonParseError errCode; + QJsonDocument d = QJsonDocument::fromJson(layoutJson, &errCode); + if(errCode.error != QJsonParseError::NoError) + { + qDebug() << defaultValue << "ERROR" << errCode.errorString(); + return defaultValue; + } + + QJsonObject o = d.object(); + QVariant out; + QString outPr; + + for(const QString & t : setupTree) + { + outPr.append(t); + outPr.append(" << "); + if(!o.contains(t)) + { + qDebug() << outPr << prop << defaultValue << "DEFAULT-TREE"; + return defaultValue; + } + o = o[t].toObject(); + out = o[t].toVariant(); + } + + if(!o.contains(prop)) + { + qDebug() << outPr << prop << defaultValue << "DEFAULT-PROP"; + return defaultValue; + } + out = o[prop].toVariant(); + + qDebug() << outPr << prop << out << "INPUT"; + + return out; +} -static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVariantPropertyManager *manager, QtProperty *target, QWidget *parent = nullptr, QString *err = nullptr) +static QHash loadPropertiesLoayout_requiredTypes = +{ + {"group", false}, + {"checkbox", false}, + {"spinbox", true}, + {"lineedit", false}, +}; + +static bool loadPropertiesLoayout_hasType(const QString &type) +{ + QString l = type.toLower(); + if(!loadPropertiesLoayout_requiredTypes.contains(l)) + return false; + return loadPropertiesLoayout_requiredTypes[l]; +} + +static void loadPropertiesLoayout_fillElements(QStack setupTree, const QJsonArray &elements, QtVariantPropertyManager *manager, QtProperty *target, QWidget *parent = nullptr, QString *err = nullptr) { for(const QJsonValue &o : elements) { @@ -82,7 +152,7 @@ static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVar if(control.isEmpty()) continue;//invalid - if(type.isEmpty() || (control != "group" && type == "invalid")) + if(loadPropertiesLoayout_hasType(control) && (type.isEmpty() || type == "invalid")) continue; if(name.isEmpty()) continue;//invalid @@ -96,7 +166,7 @@ static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVar int valueMax = o["value-max"].toInt(100); int singleStep = o["single-step"].toInt(1); item = manager->addProperty(QVariant::Int, title); - item->setValue(valueDefault); + item->setValue(retrieve_property(setupTree, name, valueDefault)); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); item->setAttribute(QLatin1String("singleStep"), singleStep); @@ -110,7 +180,7 @@ static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVar double singleStep = o["single-step"].toDouble(1); int decimals = o["decimals"].toInt(1); item = manager->addProperty(QVariant::Double, title); - item->setValue(valueDefault); + item->setValue(retrieve_property(setupTree, name, valueDefault)); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); item->setAttribute(QLatin1String("singleStep"), singleStep); @@ -118,13 +188,20 @@ static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVar target->addSubProperty(item); } } - if(!control.compare("lineEdit", Qt::CaseInsensitive)) + else if(!control.compare("checkBox", Qt::CaseInsensitive)) + { + bool valueDefault = o["value-default"].toBool(); + item = manager->addProperty(QVariant::Bool, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + target->addSubProperty(item); + } + else if(!control.compare("lineEdit", Qt::CaseInsensitive)) { int maxLength = o["max-length"].toInt(-1); QString valueDefault = o["value-default"].toString(); QString validator = o["validator"].toString(); item = manager->addProperty(QVariant::String, title); - item->setValue(valueDefault); + item->setValue(retrieve_property(setupTree, name, valueDefault)); item->setAttribute(QLatin1String("maxlength"), maxLength); if(!validator.isEmpty()) item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); @@ -137,8 +214,10 @@ static void loadPropertiesLoayout_fillElements(const QJsonArray &elements, QtVar if(!children.isEmpty()) { QtProperty *subGroup = manager->addProperty(QtVariantPropertyManager::groupTypeId(), title); + setupTree.push(name); + loadPropertiesLoayout_fillElements(setupTree, children, manager, subGroup, parent, err); + setupTree.pop(); target->addSubProperty(subGroup); - loadPropertiesLoayout_fillElements(children, manager, subGroup, parent, err); } } } @@ -149,6 +228,7 @@ static QtAbstractPropertyBrowser *loadPropertiesLoayout(const QByteArray &json, QtAbstractPropertyBrowser *gui = nullptr; QString style; QString title; + QStack setupTree; QJsonParseError errCode; QJsonDocument layout = QJsonDocument::fromJson(json, &errCode); @@ -173,7 +253,6 @@ static QtAbstractPropertyBrowser *loadPropertiesLoayout(const QByteArray &json, { QtTreePropertyBrowser *gui_t = new QtTreePropertyBrowser(parent); gui_t->setPropertiesWithoutValueMarked(true); - gui_t->setRootIsDecorated(false); gui = gui_t; } @@ -182,7 +261,7 @@ static QtAbstractPropertyBrowser *loadPropertiesLoayout(const QByteArray &json, QtProperty *topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), title); QJsonArray layoutEntries = layoutData["layout"].toArray(); - loadPropertiesLoayout_fillElements(layoutEntries, variantManager, topItem, parent, err); + loadPropertiesLoayout_fillElements(setupTree, layoutEntries, variantManager, topItem, parent, err); QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(gui); diff --git a/examples/json/sample.json b/examples/json/sample.json index 4341a49..37a9ac1 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -1,5 +1,5 @@ { - "style" : "tree", + "style" : "groupbox", "title" : "Sample setup", "layout": [{ "control": "spinBox", @@ -11,6 +11,18 @@ "value-max": 42, "single-step": 2 }, + { + "control": "checkbox", + "name": "checkMe", + "title": "Check me", + "value-default": false + }, + { + "control": "checkbox", + "name": "checkMeNot", + "title": "Don't check me, jerk", + "value-default": true + }, { "control": "spinBox", "type": "double", @@ -53,6 +65,21 @@ "max-length": 5 } ] + }, + { + "control": "group", + "name": "someAnother", + "title": "Another...", + "children": [{ + "control": "spinBox", + "type": "int", + "name": "kekVision", + "title" : "Kek", + "value-default": 5, + "value-min": -42, + "value-max": 42 + } + ] } ] } diff --git a/examples/json/setup.json b/examples/json/setup.json index b2cecd4..64277ee 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -1,7 +1,14 @@ { - "testNumber": 5, + "testNumber": 41, + "checkMe" : true, + "checkMeNot" : false, + "floatTest" : 3.444, "alienShit": { - "child1": 5, - "child2": "Kek" + "child1": 42, + "child2": "a sh3t", + "child3" : "hekof" + }, + "someAnother" : { + "kekVision" : 32 } } From 785d318d59984599d26a48c4cd91b50f965d2d95 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sat, 13 Oct 2018 02:32:35 +0300 Subject: [PATCH 06/39] JSON: Combo-box support --- examples/json/json.cpp | 15 +++++++++++++-- examples/json/sample.json | 9 ++++++++- examples/json/setup.json | 1 + 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index 03b0e31..f5cde72 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -207,8 +207,19 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); target->addSubProperty(item); } - else - if(!control.compare("group", Qt::CaseInsensitive)) + else if(!control.compare("comboBox", Qt::CaseInsensitive)) + { + item = manager->addProperty(QtVariantPropertyManager::enumTypeId(), title); + QStringList enumList; + QVariantList children = o["elements"].toArray().toVariantList(); + for(QVariant &j : children) + enumList.push_back(j.toString()); + int valueDefault = o["value-default"].toInt(); + item->setAttribute(QLatin1String("enumNames"), enumList); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + target->addSubProperty(item); + } + else if(!control.compare("group", Qt::CaseInsensitive)) { QJsonArray children = o["children"].toArray(); if(!children.isEmpty()) diff --git a/examples/json/sample.json b/examples/json/sample.json index 37a9ac1..7053ac7 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -1,5 +1,5 @@ { - "style" : "groupbox", + "style" : "tree", "title" : "Sample setup", "layout": [{ "control": "spinBox", @@ -23,6 +23,13 @@ "title": "Don't check me, jerk", "value-default": true }, + { + "control": "combobox", + "name": "choice", + "title": "Choose me", + "elements": ["zero", "one", "two", "three", "four"], + "value-default": 1 + }, { "control": "spinBox", "type": "double", diff --git a/examples/json/setup.json b/examples/json/setup.json index 64277ee..0880193 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -3,6 +3,7 @@ "checkMe" : true, "checkMeNot" : false, "floatTest" : 3.444, + "choice" : 4, "alienShit": { "child1": 42, "child2": "a sh3t", From aec7c75bbfe18ff55a4c92e72e87c0ed17398ffb Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 14 Oct 2018 02:29:32 +0300 Subject: [PATCH 07/39] JSON: More additions - Flags box - Size box - Point box - Rectangle box --- examples/json/json.cpp | 54 +++++++++++++++++++++++++++++++++++++++ examples/json/sample.json | 28 ++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index f5cde72..ec7159a 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -219,6 +219,60 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const item->setValue(retrieve_property(setupTree, name, valueDefault)); target->addSubProperty(item); } + else if(!control.compare("flagBox", Qt::CaseInsensitive)) + { + item = manager->addProperty(QtVariantPropertyManager::flagTypeId(), title); + QStringList enumList; + QVariantList children = o["elements"].toArray().toVariantList(); + for(QVariant &j : children) + enumList.push_back(j.toString()); + int valueDefault = o["value-default"].toInt(); + item->setAttribute(QLatin1String("flagNames"), enumList); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + target->addSubProperty(item); + } + else if(!control.compare("sizeBox", Qt::CaseInsensitive)) + { + item = manager->addProperty(QVariant::Size, title); + QJsonArray defArr = o["value-default"].toArray(); + QJsonArray defMin = o["value-min"].toArray(); + QJsonArray defMax = o["value-max"].toArray(); + QSize valueDefault = QSize(defArr[0].toInt(), defArr[1].toInt()); + QSize valueMin = QSize(defMin[0].toInt(), defMin[1].toInt()); + QSize valueMax = QSize(defMax[0].toInt(), defMax[1].toInt()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + target->addSubProperty(item); + } + else if(!control.compare("pointbox", Qt::CaseInsensitive)) + { + QJsonArray defArr = o["value-default"].toArray(); + QPoint valueDefault = QPoint(defArr[0].toInt(), defArr[1].toInt()); + item = manager->addProperty(QVariant::Point, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + //TODO: Feed them with QPoint + //item->setAttribute(QLatin1String("minimum"), valueMin); + //item->setAttribute(QLatin1String("maximum"), valueMax); + target->addSubProperty(item); + } + else if(!control.compare("rectbox", Qt::CaseInsensitive)) + { + QJsonArray defArr = o["value-default"].toArray(); + QRect valueDefault = QRect(defArr[0].toInt(), defArr[1].toInt(), + defArr[2].toInt(), defArr[3].toInt()); + item = manager->addProperty(QVariant::Rect, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + //TODO: Feed them with QRect + //item->setAttribute(QLatin1String("minimum"), valueMin); + //item->setAttribute(QLatin1String("maximum"), valueMax); + target->addSubProperty(item); + } + /* TODO: + * - QSizeF + * - QRectF + * - QPointF + */ else if(!control.compare("group", Qt::CaseInsensitive)) { QJsonArray children = o["children"].toArray(); diff --git a/examples/json/sample.json b/examples/json/sample.json index 7053ac7..ca71875 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -1,5 +1,6 @@ { "style" : "tree", + "style-bak" : "groupbox", "title" : "Sample setup", "layout": [{ "control": "spinBox", @@ -30,6 +31,33 @@ "elements": ["zero", "one", "two", "three", "four"], "value-default": 1 }, + { + "control": "flagbox", + "name": "choice2", + "title": "Choose of:", + "elements": ["zero", "one", "two", "three", "four"], + "value-default": 21 + }, + { + "control": "sizebox", + "name": "sizeOfMyAss", + "title": "Size of?", + "value-min": [2, 2], + "value-max": [42, 42], + "value-default": [41, 34] + }, + { + "control": "pointBox", + "name": "whereIsMySombrero", + "title": "Where?", + "value-default":[12, 12] + }, + { + "control": "rectBox", + "name": "iCanSeeYou", + "title": "Rects, Rects.... rects!!!", + "value-default": [-12, 34, 12, 12] + }, { "control": "spinBox", "type": "double", From 9f71081aded6c5d409bf1cbdf7342d0999a7bafe Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 17 Oct 2018 02:24:07 +0300 Subject: [PATCH 08/39] Try to hook value changes --- examples/json/json.cpp | 87 ++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 24 deletions(-) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index ec7159a..27cb4e9 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -72,34 +72,54 @@ PropJsonIO::~PropJsonIO() {} void PropJsonIO::currentItemChanged(QtBrowserItem *item) {} -static QVariant retrieve_property(const QStack &setupTree, QString prop, const QVariant &defaultValue) +struct SetupStack { - /* - TODO: It's a crap! Put settings loader into separated place, but keep here the digging through already loaded tree - */ - QFile f; - f.setFileName(sample_settings); - if(!f.open(QIODevice::ReadOnly)) + QStack m_setupTree; + QJsonDocument m_setupCache; + + SetupStack() + {} + + QString getPropertyId(const QString &name) { - QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); - return QVariant(); + QString outPr; + for(const QString & t : m_setupTree) + { + outPr.append(t); + outPr.append('/'); + } + outPr.append(name); + return outPr; } - QByteArray layoutJson = f.readAll(); - f.close(); - QJsonParseError errCode; - QJsonDocument d = QJsonDocument::fromJson(layoutJson, &errCode); - if(errCode.error != QJsonParseError::NoError) + void loadSetup(const QString &path) { - qDebug() << defaultValue << "ERROR" << errCode.errorString(); - return defaultValue; + m_setupTree.clear(); + QFile f; + f.setFileName(path); + if(!f.open(QIODevice::ReadOnly)) + { + QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); + m_setupCache = QJsonDocument(); + } + + QByteArray layoutJson = f.readAll(); + f.close(); + QJsonParseError errCode; + m_setupCache = QJsonDocument::fromJson(layoutJson, &errCode); + if(errCode.error != QJsonParseError::NoError) + m_setupCache = QJsonDocument(); } +}; +static QVariant retrieve_property(const SetupStack &setupTree, QString prop, const QVariant &defaultValue) +{ + const QJsonDocument d = setupTree.m_setupCache; QJsonObject o = d.object(); QVariant out; QString outPr; - for(const QString & t : setupTree) + for(const QString & t : setupTree.m_setupTree) { outPr.append(t); outPr.append(" << "); @@ -140,7 +160,7 @@ static bool loadPropertiesLoayout_hasType(const QString &type) return loadPropertiesLoayout_requiredTypes[l]; } -static void loadPropertiesLoayout_fillElements(QStack setupTree, const QJsonArray &elements, QtVariantPropertyManager *manager, QtProperty *target, QWidget *parent = nullptr, QString *err = nullptr) +static void loadPropertiesLoayout_fillElements(SetupStack setupTree, const QJsonArray &elements, QtVariantPropertyManager *manager, QtProperty *target, QWidget *parent = nullptr, QString *err = nullptr) { for(const QJsonValue &o : elements) { @@ -157,6 +177,8 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const if(name.isEmpty()) continue;//invalid + qDebug() << "property" << setupTree.getPropertyId(name); + if(!control.compare("spinBox", Qt::CaseInsensitive)) { if(!type.compare("int", Qt::CaseInsensitive)) @@ -170,6 +192,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); item->setAttribute(QLatin1String("singleStep"), singleStep); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!type.compare("double", Qt::CaseInsensitive) || !type.compare("float", Qt::CaseInsensitive)) @@ -185,6 +208,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const item->setAttribute(QLatin1String("maximum"), valueMax); item->setAttribute(QLatin1String("singleStep"), singleStep); item->setAttribute(QLatin1String("decimals"), decimals); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } } @@ -205,6 +229,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const item->setAttribute(QLatin1String("maxlength"), maxLength); if(!validator.isEmpty()) item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("comboBox", Qt::CaseInsensitive)) @@ -217,6 +242,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const int valueDefault = o["value-default"].toInt(); item->setAttribute(QLatin1String("enumNames"), enumList); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("flagBox", Qt::CaseInsensitive)) @@ -229,6 +255,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const int valueDefault = o["value-default"].toInt(); item->setAttribute(QLatin1String("flagNames"), enumList); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("sizeBox", Qt::CaseInsensitive)) @@ -243,6 +270,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const item->setValue(retrieve_property(setupTree, name, valueDefault)); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("pointbox", Qt::CaseInsensitive)) @@ -254,6 +282,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const //TODO: Feed them with QPoint //item->setAttribute(QLatin1String("minimum"), valueMin); //item->setAttribute(QLatin1String("maximum"), valueMax); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("rectbox", Qt::CaseInsensitive)) @@ -266,6 +295,7 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const //TODO: Feed them with QRect //item->setAttribute(QLatin1String("minimum"), valueMin); //item->setAttribute(QLatin1String("maximum"), valueMax); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } /* TODO: @@ -279,21 +309,20 @@ static void loadPropertiesLoayout_fillElements(QStack setupTree, const if(!children.isEmpty()) { QtProperty *subGroup = manager->addProperty(QtVariantPropertyManager::groupTypeId(), title); - setupTree.push(name); + setupTree.m_setupTree.push(name); loadPropertiesLoayout_fillElements(setupTree, children, manager, subGroup, parent, err); - setupTree.pop(); + setupTree.m_setupTree.pop(); target->addSubProperty(subGroup); } } } } -static QtAbstractPropertyBrowser *loadPropertiesLoayout(const QByteArray &json, QWidget *parent = nullptr, QString *err = nullptr) +static QtAbstractPropertyBrowser *loadPropertiesLoayout(const SetupStack &stack, const QByteArray &json, QWidget *parent = nullptr, QString *err = nullptr) { QtAbstractPropertyBrowser *gui = nullptr; QString style; QString title; - QStack setupTree; QJsonParseError errCode; QJsonDocument layout = QJsonDocument::fromJson(json, &errCode); @@ -326,7 +355,14 @@ static QtAbstractPropertyBrowser *loadPropertiesLoayout(const QByteArray &json, QtProperty *topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), title); QJsonArray layoutEntries = layoutData["layout"].toArray(); - loadPropertiesLoayout_fillElements(setupTree, layoutEntries, variantManager, topItem, parent, err); + loadPropertiesLoayout_fillElements(stack, layoutEntries, variantManager, topItem, parent, err); + + variantManager->connect(variantManager, &QtVariantPropertyManager::valueChanged, + [](QtProperty *p,QVariant v) + { + qDebug() << "changed:" << p->propertyId() << v; + } + ); QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(gui); @@ -349,6 +385,9 @@ int main(int argc, char **argv) return 1; } + SetupStack stack; + stack.loadSetup(sample_settings); + QByteArray layoutJson = f.readAll(); f.close(); @@ -356,7 +395,7 @@ int main(int argc, char **argv) QString error; //QtTreePropertyBrowser *variantEditor = new QtTreePropertyBrowser(); - QtAbstractPropertyBrowser *variantEditor = loadPropertiesLoayout(layoutJson, nullptr, &error); + QtAbstractPropertyBrowser *variantEditor = loadPropertiesLoayout(stack, layoutJson, nullptr, &error); if(!variantEditor) { QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(error)); From a491d7b3b3ecfbec6c30aca9dbb1b1ec9decb76a Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 17 Oct 2018 03:25:05 +0300 Subject: [PATCH 09/39] Added support for save&restore --- examples/json/json.cpp | 165 +++++++++++++++++++++++++++++++++----- examples/json/sample.json | 10 +-- examples/json/setup.json | 40 ++++++--- 3 files changed, 176 insertions(+), 39 deletions(-) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index 27cb4e9..1107895 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -72,6 +72,35 @@ PropJsonIO::~PropJsonIO() {} void PropJsonIO::currentItemChanged(QtBrowserItem *item) {} +static QJsonObject rectToArray(QVariant r) +{ + QRect rekt = r.toRect(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; +} + +static QJsonObject sizeToArray(QVariant r) +{ + QSize rekt = r.toSize(); + QJsonObject a; + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; +} + +static QJsonObject pointToArray(QVariant r) +{ + QPoint rekt = r.toPoint(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + return a; +} + struct SetupStack { QStack m_setupTree; @@ -101,6 +130,7 @@ struct SetupStack { QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); m_setupCache = QJsonDocument(); + return; } QByteArray layoutJson = f.readAll(); @@ -110,6 +140,62 @@ struct SetupStack if(errCode.error != QJsonParseError::NoError) m_setupCache = QJsonDocument(); } + + void saveSetup(const QString &path) + { + QFile f; + f.setFileName(path); + if(!f.open(QIODevice::WriteOnly)) + { + QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); + return; + } + f.write(m_setupCache.toJson()); + f.close(); + } + + void setValue(const QString &propertyId, QVariant value) + { + QStringList stack = propertyId.split("/"); + QJsonObject o = m_setupCache.object(); + QStack stack_o; + QString top; + for(int i = 0; i < stack.size(); i++) + { + QString &t = stack[i]; + top = t; + if(i == stack.size() - 1) + break; + stack_o.push(o); + o = o[t].toObject(); + } + + switch(value.type()) + { + case QVariant::Rect: + o[top] = rectToArray(value); + break; + case QVariant::Point: + o[top] = pointToArray(value); + break; + case QVariant::Size: + o[top] = sizeToArray(value); + break; + default: + o[top] = QJsonValue::fromVariant(value); + break; + } + + for(int i = stack.size() - 2; i >= 0; i--) + { + QString &s = stack[i]; + QJsonObject oo = stack_o.pop(); + oo[s] = o; + o = oo; + } + + m_setupCache.setObject(o); + } }; static QVariant retrieve_property(const SetupStack &setupTree, QString prop, const QVariant &defaultValue) @@ -137,7 +223,30 @@ static QVariant retrieve_property(const SetupStack &setupTree, QString prop, con qDebug() << outPr << prop << defaultValue << "DEFAULT-PROP"; return defaultValue; } - out = o[prop].toVariant(); + switch(defaultValue.type()) + { + case QVariant::Size: + { + QJsonObject sz = o[prop].toObject(); + out = QSize(sz["w"].toInt(), sz["h"].toInt()); + } + break; + case QVariant::Point: + { + QJsonObject sz = o[prop].toObject(); + out = QPoint(sz["x"].toInt(), sz["y"].toInt()); + } + break; + case QVariant::Rect: + { + QJsonObject sz = o[prop].toObject(); + out = QRect(sz["x"].toInt(), sz["y"].toInt(), sz["w"].toInt(), sz["h"].toInt()); + } + break; + default: + out = o[prop].toVariant(); + break; + } qDebug() << outPr << prop << out << "INPUT"; @@ -217,6 +326,7 @@ static void loadPropertiesLoayout_fillElements(SetupStack setupTree, const QJson bool valueDefault = o["value-default"].toBool(); item = manager->addProperty(QVariant::Bool, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("lineEdit", Qt::CaseInsensitive)) @@ -261,12 +371,12 @@ static void loadPropertiesLoayout_fillElements(SetupStack setupTree, const QJson else if(!control.compare("sizeBox", Qt::CaseInsensitive)) { item = manager->addProperty(QVariant::Size, title); - QJsonArray defArr = o["value-default"].toArray(); - QJsonArray defMin = o["value-min"].toArray(); - QJsonArray defMax = o["value-max"].toArray(); - QSize valueDefault = QSize(defArr[0].toInt(), defArr[1].toInt()); - QSize valueMin = QSize(defMin[0].toInt(), defMin[1].toInt()); - QSize valueMax = QSize(defMax[0].toInt(), defMax[1].toInt()); + QJsonObject defArr = o["value-default"].toObject(); + QJsonObject defMin = o["value-min"].toObject(); + QJsonObject defMax = o["value-max"].toObject(); + QSize valueDefault = QSize(defArr["w"].toInt(), defArr["h"].toInt()); + QSize valueMin = QSize(defMin["w"].toInt(), defMin["h"].toInt()); + QSize valueMax = QSize(defMax["w"].toInt(), defMax["h"].toInt()); item->setValue(retrieve_property(setupTree, name, valueDefault)); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); @@ -275,26 +385,34 @@ static void loadPropertiesLoayout_fillElements(SetupStack setupTree, const QJson } else if(!control.compare("pointbox", Qt::CaseInsensitive)) { - QJsonArray defArr = o["value-default"].toArray(); - QPoint valueDefault = QPoint(defArr[0].toInt(), defArr[1].toInt()); + QJsonObject defArr = o["value-default"].toObject(); + QJsonObject defMin = o["value-min"].toObject(); + QJsonObject defMax = o["value-max"].toObject(); + QPoint valueDefault = QPoint(defArr["x"].toInt(), defArr["y"].toInt()); + QPoint valueMin = QPoint(defMin["x"].toInt(), defMin["y"].toInt()); + QPoint valueMax = QPoint(defMax["x"].toInt(), defMax["y"].toInt()); item = manager->addProperty(QVariant::Point, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); - //TODO: Feed them with QPoint - //item->setAttribute(QLatin1String("minimum"), valueMin); - //item->setAttribute(QLatin1String("maximum"), valueMax); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } else if(!control.compare("rectbox", Qt::CaseInsensitive)) { - QJsonArray defArr = o["value-default"].toArray(); - QRect valueDefault = QRect(defArr[0].toInt(), defArr[1].toInt(), - defArr[2].toInt(), defArr[3].toInt()); + QJsonObject defArr = o["value-default"].toObject(); + QJsonObject defMin = o["value-min"].toObject(); + QJsonObject defMax = o["value-max"].toObject(); + QRect valueDefault = QRect(defArr["x"].toInt(), defArr["y"].toInt(), + defArr["w"].toInt(), defArr["h"].toInt()); + QRect valueMin = QRect(defMin["x"].toInt(), defMin["y"].toInt(), + defMin["w"].toInt(), defMin["h"].toInt()); + QRect valueMax = QRect(defMax["x"].toInt(), defMax["y"].toInt(), + defMax["w"].toInt(), defMax["h"].toInt()); item = manager->addProperty(QVariant::Rect, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); - //TODO: Feed them with QRect - //item->setAttribute(QLatin1String("minimum"), valueMin); - //item->setAttribute(QLatin1String("maximum"), valueMax); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } @@ -318,7 +436,7 @@ static void loadPropertiesLoayout_fillElements(SetupStack setupTree, const QJson } } -static QtAbstractPropertyBrowser *loadPropertiesLoayout(const SetupStack &stack, const QByteArray &json, QWidget *parent = nullptr, QString *err = nullptr) +static QtAbstractPropertyBrowser *loadPropertiesLoayout(SetupStack &stack, const QByteArray &json, QWidget *parent = nullptr, QString *err = nullptr) { QtAbstractPropertyBrowser *gui = nullptr; QString style; @@ -358,9 +476,12 @@ static QtAbstractPropertyBrowser *loadPropertiesLoayout(const SetupStack &stack, loadPropertiesLoayout_fillElements(stack, layoutEntries, variantManager, topItem, parent, err); variantManager->connect(variantManager, &QtVariantPropertyManager::valueChanged, - [](QtProperty *p,QVariant v) + [&stack](QtProperty *p,QVariant v) { - qDebug() << "changed:" << p->propertyId() << v; + QString pid = p->propertyId(); + qDebug() << "changed:" << pid << v; + if(!pid.isEmpty()) + stack.setValue(p->propertyId(), v); } ); @@ -409,6 +530,8 @@ int main(int argc, char **argv) int ret = app.exec(); + stack.saveSetup(sample_settings); + // delete variantManager; // delete variantFactory; delete variantEditor; diff --git a/examples/json/sample.json b/examples/json/sample.json index ca71875..d8b9b9c 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -42,21 +42,21 @@ "control": "sizebox", "name": "sizeOfMyAss", "title": "Size of?", - "value-min": [2, 2], - "value-max": [42, 42], - "value-default": [41, 34] + "value-min": {"w": 2, "h" : 2}, + "value-max": {"w": 42, "h": 42}, + "value-default": {"w": 41, "h" : 34} }, { "control": "pointBox", "name": "whereIsMySombrero", "title": "Where?", - "value-default":[12, 12] + "value-default" : {"x":12, "y":12} }, { "control": "rectBox", "name": "iCanSeeYou", "title": "Rects, Rects.... rects!!!", - "value-default": [-12, 34, 12, 12] + "value-default": {"x":-12, "y":34, "w":12, "h":12} }, { "control": "spinBox", diff --git a/examples/json/setup.json b/examples/json/setup.json index 0880193..2f45825 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -1,15 +1,29 @@ { - "testNumber": 41, - "checkMe" : true, - "checkMeNot" : false, - "floatTest" : 3.444, - "choice" : 4, - "alienShit": { - "child1": 42, - "child2": "a sh3t", - "child3" : "hekof" - }, - "someAnother" : { - "kekVision" : 32 - } + "alienShit": { + "child2": "a sh9t", + "child3": "heck" + }, + "checkMe": true, + "checkMeNot": false, + "choice": 4, + "choice2": 0, + "floatTest": 4.100000000000003, + "iCanSeeYou": { + "h": 25, + "w": 25, + "x": 25, + "y": -25 + }, + "sizeOfMyAss": { + "h": 25, + "w": 25 + }, + "someAnother": { + "kekVision": 1 + }, + "testNumber": 0, + "whereIsMySombrero": { + "x": 25, + "y": -25 + } } From 9358594e5ca702b0be58ac631e9f66f0d077e0df Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 21 Oct 2018 04:25:51 +0300 Subject: [PATCH 10/39] JSON: Jail QVariant<->Rect/Size/Point converters TODO: - Be sure is need to save full dump of given settings, or it's fine to absent of some - Complete other types - Make a proper manager class for everything that can be re-used --- examples/json/json.cpp | 66 ++++++++++++++++++++-------------------- examples/json/json.h | 20 ++++++------ examples/json/setup.json | 13 ++++---- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index 1107895..51818c6 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -63,46 +63,46 @@ const char *sample_config = "sample.json"; const char *sample_settings = "setup.json"; -PropJsonIO::PropJsonIO(QObject *parent) : - QObject(parent) -{} +//PropJsonIO::PropJsonIO(QObject *parent) : +// QObject(parent) +//{} -PropJsonIO::~PropJsonIO() {} +//PropJsonIO::~PropJsonIO() {} -void PropJsonIO::currentItemChanged(QtBrowserItem *item) -{} +//void PropJsonIO::currentItemChanged(QtBrowserItem *item) +//{} -static QJsonObject rectToArray(QVariant r) +struct SetupStack { - QRect rekt = r.toRect(); - QJsonObject a; - a["x"] = QJsonValue::fromVariant(rekt.x()); - a["y"] = QJsonValue::fromVariant(rekt.y()); - a["w"] = QJsonValue::fromVariant(rekt.width()); - a["h"] = QJsonValue::fromVariant(rekt.height()); - return a; -} + static QJsonObject rectToArray(QVariant r) + { + QRect rekt = r.toRect(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; + } -static QJsonObject sizeToArray(QVariant r) -{ - QSize rekt = r.toSize(); - QJsonObject a; - a["w"] = QJsonValue::fromVariant(rekt.width()); - a["h"] = QJsonValue::fromVariant(rekt.height()); - return a; -} + static QJsonObject sizeToArray(QVariant r) + { + QSize rekt = r.toSize(); + QJsonObject a; + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; + } -static QJsonObject pointToArray(QVariant r) -{ - QPoint rekt = r.toPoint(); - QJsonObject a; - a["x"] = QJsonValue::fromVariant(rekt.x()); - a["y"] = QJsonValue::fromVariant(rekt.y()); - return a; -} + static QJsonObject pointToArray(QVariant r) + { + QPoint rekt = r.toPoint(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + return a; + } -struct SetupStack -{ QStack m_setupTree; QJsonDocument m_setupCache; diff --git a/examples/json/json.h b/examples/json/json.h index d07d2dd..0db0648 100644 --- a/examples/json/json.h +++ b/examples/json/json.h @@ -5,17 +5,17 @@ #include #include -class PropJsonIO : public QObject -{ - Q_OBJECT +//class PropJsonIO : public QObject +//{ +// Q_OBJECT - QJsonDocument m_doc; -public: - explicit PropJsonIO(QObject *parent = nullptr); - virtual ~PropJsonIO(); +// QJsonDocument m_doc; +//public: +// explicit PropJsonIO(QObject *parent = nullptr); +// virtual ~PropJsonIO(); -public slots: - void currentItemChanged(QtBrowserItem *item); -}; +//public slots: +// void currentItemChanged(QtBrowserItem *item); +//}; #endif//JSONNNNNNN diff --git a/examples/json/setup.json b/examples/json/setup.json index 2f45825..a22da4b 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -1,18 +1,19 @@ { "alienShit": { + "child1": -4, "child2": "a sh9t", "child3": "heck" }, - "checkMe": true, - "checkMeNot": false, - "choice": 4, - "choice2": 0, + "checkMe": false, + "checkMeNot": true, + "choice": 2, + "choice2": 22, "floatTest": 4.100000000000003, "iCanSeeYou": { "h": 25, "w": 25, "x": 25, - "y": -25 + "y": -27 }, "sizeOfMyAss": { "h": 25, @@ -21,7 +22,7 @@ "someAnother": { "kekVision": 1 }, - "testNumber": 0, + "testNumber": 8, "whereIsMySombrero": { "x": 25, "y": -25 From c08f33a69d96cb7b60d45d01e7ec196058254f04 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Mon, 22 Oct 2018 03:35:38 +0300 Subject: [PATCH 11/39] JSON: Organize the code into proper and handy class --- examples/json/CMakeLists.txt | 5 +- examples/json/json.cpp | 484 +--------------------- examples/json/json.h | 21 - examples/json/json_settings_widget.cpp | 542 +++++++++++++++++++++++++ examples/json/json_settings_widget.h | 78 ++++ examples/json/setup.json | 30 +- examples/json/wwsetup.json | 15 + 7 files changed, 661 insertions(+), 514 deletions(-) delete mode 100644 examples/json/json.h create mode 100644 examples/json/json_settings_widget.cpp create mode 100644 examples/json/json_settings_widget.h create mode 100644 examples/json/wwsetup.json diff --git a/examples/json/CMakeLists.txt b/examples/json/CMakeLists.txt index 9279bfd..ccdc2a0 100644 --- a/examples/json/CMakeLists.txt +++ b/examples/json/CMakeLists.txt @@ -8,8 +8,9 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) SET(example_name json) SET(KIT_SRCS - json.cpp - ) + json_settings_widget.cpp + json.cpp +) ADD_EXECUTABLE(${example_name} ${KIT_SRCS}) TARGET_LINK_LIBRARIES(${example_name} ${PROJECT_NAME}) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index 51818c6..24d1fb9 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -37,504 +37,36 @@ ** ****************************************************************************/ -#include "json.h" +#include "json_settings_widget.h" #include -#include -#include #include -#include - -#include -#include -#include - -#include "qtpropertymanager.h" -#include "qtvariantproperty.h" -#include "qtgroupboxpropertybrowser.h" -#include "qtbuttonpropertybrowser.h" -#include "qttreepropertybrowser.h" #include - #include -const char *sample_config = "sample.json"; +const char *sample_layout = "sample.json"; const char *sample_settings = "setup.json"; - -//PropJsonIO::PropJsonIO(QObject *parent) : -// QObject(parent) -//{} - -//PropJsonIO::~PropJsonIO() {} - -//void PropJsonIO::currentItemChanged(QtBrowserItem *item) -//{} - -struct SetupStack -{ - static QJsonObject rectToArray(QVariant r) - { - QRect rekt = r.toRect(); - QJsonObject a; - a["x"] = QJsonValue::fromVariant(rekt.x()); - a["y"] = QJsonValue::fromVariant(rekt.y()); - a["w"] = QJsonValue::fromVariant(rekt.width()); - a["h"] = QJsonValue::fromVariant(rekt.height()); - return a; - } - - static QJsonObject sizeToArray(QVariant r) - { - QSize rekt = r.toSize(); - QJsonObject a; - a["w"] = QJsonValue::fromVariant(rekt.width()); - a["h"] = QJsonValue::fromVariant(rekt.height()); - return a; - } - - static QJsonObject pointToArray(QVariant r) - { - QPoint rekt = r.toPoint(); - QJsonObject a; - a["x"] = QJsonValue::fromVariant(rekt.x()); - a["y"] = QJsonValue::fromVariant(rekt.y()); - return a; - } - - QStack m_setupTree; - QJsonDocument m_setupCache; - - SetupStack() - {} - - QString getPropertyId(const QString &name) - { - QString outPr; - for(const QString & t : m_setupTree) - { - outPr.append(t); - outPr.append('/'); - } - outPr.append(name); - return outPr; - } - - void loadSetup(const QString &path) - { - m_setupTree.clear(); - QFile f; - f.setFileName(path); - if(!f.open(QIODevice::ReadOnly)) - { - QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); - m_setupCache = QJsonDocument(); - return; - } - - QByteArray layoutJson = f.readAll(); - f.close(); - QJsonParseError errCode; - m_setupCache = QJsonDocument::fromJson(layoutJson, &errCode); - if(errCode.error != QJsonParseError::NoError) - m_setupCache = QJsonDocument(); - } - - void saveSetup(const QString &path) - { - QFile f; - f.setFileName(path); - if(!f.open(QIODevice::WriteOnly)) - { - QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); - return; - } - f.write(m_setupCache.toJson()); - f.close(); - } - - void setValue(const QString &propertyId, QVariant value) - { - QStringList stack = propertyId.split("/"); - QJsonObject o = m_setupCache.object(); - QStack stack_o; - QString top; - for(int i = 0; i < stack.size(); i++) - { - QString &t = stack[i]; - top = t; - if(i == stack.size() - 1) - break; - stack_o.push(o); - o = o[t].toObject(); - } - - switch(value.type()) - { - case QVariant::Rect: - o[top] = rectToArray(value); - break; - case QVariant::Point: - o[top] = pointToArray(value); - break; - case QVariant::Size: - o[top] = sizeToArray(value); - break; - default: - o[top] = QJsonValue::fromVariant(value); - break; - } - - for(int i = stack.size() - 2; i >= 0; i--) - { - QString &s = stack[i]; - QJsonObject oo = stack_o.pop(); - oo[s] = o; - o = oo; - } - - m_setupCache.setObject(o); - } -}; - -static QVariant retrieve_property(const SetupStack &setupTree, QString prop, const QVariant &defaultValue) -{ - const QJsonDocument d = setupTree.m_setupCache; - QJsonObject o = d.object(); - QVariant out; - QString outPr; - - for(const QString & t : setupTree.m_setupTree) - { - outPr.append(t); - outPr.append(" << "); - if(!o.contains(t)) - { - qDebug() << outPr << prop << defaultValue << "DEFAULT-TREE"; - return defaultValue; - } - o = o[t].toObject(); - out = o[t].toVariant(); - } - - if(!o.contains(prop)) - { - qDebug() << outPr << prop << defaultValue << "DEFAULT-PROP"; - return defaultValue; - } - switch(defaultValue.type()) - { - case QVariant::Size: - { - QJsonObject sz = o[prop].toObject(); - out = QSize(sz["w"].toInt(), sz["h"].toInt()); - } - break; - case QVariant::Point: - { - QJsonObject sz = o[prop].toObject(); - out = QPoint(sz["x"].toInt(), sz["y"].toInt()); - } - break; - case QVariant::Rect: - { - QJsonObject sz = o[prop].toObject(); - out = QRect(sz["x"].toInt(), sz["y"].toInt(), sz["w"].toInt(), sz["h"].toInt()); - } - break; - default: - out = o[prop].toVariant(); - break; - } - - qDebug() << outPr << prop << out << "INPUT"; - - return out; -} - -static QHash loadPropertiesLoayout_requiredTypes = -{ - {"group", false}, - {"checkbox", false}, - {"spinbox", true}, - {"lineedit", false}, -}; - -static bool loadPropertiesLoayout_hasType(const QString &type) -{ - QString l = type.toLower(); - if(!loadPropertiesLoayout_requiredTypes.contains(l)) - return false; - return loadPropertiesLoayout_requiredTypes[l]; -} - -static void loadPropertiesLoayout_fillElements(SetupStack setupTree, const QJsonArray &elements, QtVariantPropertyManager *manager, QtProperty *target, QWidget *parent = nullptr, QString *err = nullptr) -{ - for(const QJsonValue &o : elements) - { - QString type = o["type"].toString("invalid"); - QString name = o["name"].toString(type); - QString title = o["title"].toString(name); - QString control = o["control"].toString(); - QtVariantProperty *item = nullptr; - - if(control.isEmpty()) - continue;//invalid - if(loadPropertiesLoayout_hasType(control) && (type.isEmpty() || type == "invalid")) - continue; - if(name.isEmpty()) - continue;//invalid - - qDebug() << "property" << setupTree.getPropertyId(name); - - if(!control.compare("spinBox", Qt::CaseInsensitive)) - { - if(!type.compare("int", Qt::CaseInsensitive)) - { - int valueDefault = o["value-default"].toInt(); - int valueMin = o["value-min"].toInt(0); - int valueMax = o["value-max"].toInt(100); - int singleStep = o["single-step"].toInt(1); - item = manager->addProperty(QVariant::Int, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); - item->setAttribute(QLatin1String("singleStep"), singleStep); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!type.compare("double", Qt::CaseInsensitive) || !type.compare("float", Qt::CaseInsensitive)) - { - double valueDefault = o["value-default"].toDouble(); - double valueMin = o["value-min"].toDouble(0.0); - double valueMax = o["value-max"].toDouble(100.0); - double singleStep = o["single-step"].toDouble(1); - int decimals = o["decimals"].toInt(1); - item = manager->addProperty(QVariant::Double, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); - item->setAttribute(QLatin1String("singleStep"), singleStep); - item->setAttribute(QLatin1String("decimals"), decimals); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - } - else if(!control.compare("checkBox", Qt::CaseInsensitive)) - { - bool valueDefault = o["value-default"].toBool(); - item = manager->addProperty(QVariant::Bool, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!control.compare("lineEdit", Qt::CaseInsensitive)) - { - int maxLength = o["max-length"].toInt(-1); - QString valueDefault = o["value-default"].toString(); - QString validator = o["validator"].toString(); - item = manager->addProperty(QVariant::String, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("maxlength"), maxLength); - if(!validator.isEmpty()) - item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!control.compare("comboBox", Qt::CaseInsensitive)) - { - item = manager->addProperty(QtVariantPropertyManager::enumTypeId(), title); - QStringList enumList; - QVariantList children = o["elements"].toArray().toVariantList(); - for(QVariant &j : children) - enumList.push_back(j.toString()); - int valueDefault = o["value-default"].toInt(); - item->setAttribute(QLatin1String("enumNames"), enumList); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!control.compare("flagBox", Qt::CaseInsensitive)) - { - item = manager->addProperty(QtVariantPropertyManager::flagTypeId(), title); - QStringList enumList; - QVariantList children = o["elements"].toArray().toVariantList(); - for(QVariant &j : children) - enumList.push_back(j.toString()); - int valueDefault = o["value-default"].toInt(); - item->setAttribute(QLatin1String("flagNames"), enumList); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!control.compare("sizeBox", Qt::CaseInsensitive)) - { - item = manager->addProperty(QVariant::Size, title); - QJsonObject defArr = o["value-default"].toObject(); - QJsonObject defMin = o["value-min"].toObject(); - QJsonObject defMax = o["value-max"].toObject(); - QSize valueDefault = QSize(defArr["w"].toInt(), defArr["h"].toInt()); - QSize valueMin = QSize(defMin["w"].toInt(), defMin["h"].toInt()); - QSize valueMax = QSize(defMax["w"].toInt(), defMax["h"].toInt()); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!control.compare("pointbox", Qt::CaseInsensitive)) - { - QJsonObject defArr = o["value-default"].toObject(); - QJsonObject defMin = o["value-min"].toObject(); - QJsonObject defMax = o["value-max"].toObject(); - QPoint valueDefault = QPoint(defArr["x"].toInt(), defArr["y"].toInt()); - QPoint valueMin = QPoint(defMin["x"].toInt(), defMin["y"].toInt()); - QPoint valueMax = QPoint(defMax["x"].toInt(), defMax["y"].toInt()); - item = manager->addProperty(QVariant::Point, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - else if(!control.compare("rectbox", Qt::CaseInsensitive)) - { - QJsonObject defArr = o["value-default"].toObject(); - QJsonObject defMin = o["value-min"].toObject(); - QJsonObject defMax = o["value-max"].toObject(); - QRect valueDefault = QRect(defArr["x"].toInt(), defArr["y"].toInt(), - defArr["w"].toInt(), defArr["h"].toInt()); - QRect valueMin = QRect(defMin["x"].toInt(), defMin["y"].toInt(), - defMin["w"].toInt(), defMin["h"].toInt()); - QRect valueMax = QRect(defMax["x"].toInt(), defMax["y"].toInt(), - defMax["w"].toInt(), defMax["h"].toInt()); - item = manager->addProperty(QVariant::Rect, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); - item->setPropertyId(setupTree.getPropertyId(name)); - target->addSubProperty(item); - } - /* TODO: - * - QSizeF - * - QRectF - * - QPointF - */ - else if(!control.compare("group", Qt::CaseInsensitive)) - { - QJsonArray children = o["children"].toArray(); - if(!children.isEmpty()) - { - QtProperty *subGroup = manager->addProperty(QtVariantPropertyManager::groupTypeId(), title); - setupTree.m_setupTree.push(name); - loadPropertiesLoayout_fillElements(setupTree, children, manager, subGroup, parent, err); - setupTree.m_setupTree.pop(); - target->addSubProperty(subGroup); - } - } - } -} - -static QtAbstractPropertyBrowser *loadPropertiesLoayout(SetupStack &stack, const QByteArray &json, QWidget *parent = nullptr, QString *err = nullptr) -{ - QtAbstractPropertyBrowser *gui = nullptr; - QString style; - QString title; - - QJsonParseError errCode; - QJsonDocument layout = QJsonDocument::fromJson(json, &errCode); - - - if(errCode.error != QJsonParseError::NoError) - { - if(err) - *err = errCode.errorString(); - return nullptr; - } - - QJsonObject layoutData = layout.object(); - - style = layoutData["style"].toString(); - title = layoutData["title"].toString(); - if(style == "groupbox") - gui = new QtGroupBoxPropertyBrowser(parent); - else if(style == "button") - gui = new QtButtonPropertyBrowser(parent); - else // "tree" is default - { - QtTreePropertyBrowser *gui_t = new QtTreePropertyBrowser(parent); - gui_t->setPropertiesWithoutValueMarked(true); - gui = gui_t; - } - - QtVariantPropertyManager *variantManager = new QtVariantPropertyManager(gui); - - QtProperty *topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), title); - - QJsonArray layoutEntries = layoutData["layout"].toArray(); - loadPropertiesLoayout_fillElements(stack, layoutEntries, variantManager, topItem, parent, err); - - variantManager->connect(variantManager, &QtVariantPropertyManager::valueChanged, - [&stack](QtProperty *p,QVariant v) - { - QString pid = p->propertyId(); - qDebug() << "changed:" << pid << v; - if(!pid.isEmpty()) - stack.setValue(p->propertyId(), v); - } - ); - - QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(gui); - - gui->setFactoryForManager(variantManager, variantFactory); - gui->addProperty(topItem); - - return gui; -} - - int main(int argc, char **argv) { QApplication app(argc, argv); - QFile f; - f.setFileName(sample_config); - if(!f.open(QIODevice::ReadOnly)) - { - QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(f.errorString())); - return 1; - } - - SetupStack stack; - stack.loadSetup(sample_settings); - - QByteArray layoutJson = f.readAll(); - f.close(); + JsonSettingsWidget extraSettings; -// QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(); - - QString error; - //QtTreePropertyBrowser *variantEditor = new QtTreePropertyBrowser(); - QtAbstractPropertyBrowser *variantEditor = loadPropertiesLoayout(stack, layoutJson, nullptr, &error); - if(!variantEditor) + if(!extraSettings.loadLayoutFromFile(sample_settings, sample_layout)) { - QMessageBox::warning(nullptr, "oops", QString("Oops: %1").arg(error)); + QMessageBox::warning(nullptr, "oops", extraSettings.errorString()); return 1; } -// variantEditor->setFactoryForManager(variantManager, variantFactory); -// variantEditor->addProperty(topItem); - variantEditor->show(); + QWidget *w = extraSettings.getWidget(); + w->show(); int ret = app.exec(); - stack.saveSetup(sample_settings); - - // delete variantManager; - // delete variantFactory; - delete variantEditor; + extraSettings.saveSettingsIntoFile(sample_settings); return ret; } diff --git a/examples/json/json.h b/examples/json/json.h deleted file mode 100644 index 0db0648..0000000 --- a/examples/json/json.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef JSONNNNNNN -#define JSONNNNNNN - -#include -#include -#include - -//class PropJsonIO : public QObject -//{ -// Q_OBJECT - -// QJsonDocument m_doc; -//public: -// explicit PropJsonIO(QObject *parent = nullptr); -// virtual ~PropJsonIO(); - -//public slots: -// void currentItemChanged(QtBrowserItem *item); -//}; - -#endif//JSONNNNNNN diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp new file mode 100644 index 0000000..460d83b --- /dev/null +++ b/examples/json/json_settings_widget.cpp @@ -0,0 +1,542 @@ +#include "json_settings_widget.h" + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +QJsonObject JsonSettingsWidget::SetupStack::rectToArray(QVariant r) +{ + QRect rekt = r.toRect(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; +} + +QJsonObject JsonSettingsWidget::SetupStack::sizeToArray(QVariant r) +{ + QSize rekt = r.toSize(); + QJsonObject a; + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; +} + +QJsonObject JsonSettingsWidget::SetupStack::pointToArray(QVariant r) +{ + QPoint rekt = r.toPoint(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + return a; +} + +QString JsonSettingsWidget::SetupStack::getPropertyId(const QString &name) +{ + QString outPr; + for(const QString & t : m_setupTree) + { + outPr.append(t); + outPr.append('/'); + } + outPr.append(name); + return outPr; +} + +bool JsonSettingsWidget::SetupStack::loadSetup(const QByteArray &layoutJson, QString &errorString) +{ + QJsonParseError errCode; + m_setupTree.clear(); + m_setupCache = QJsonDocument::fromJson(layoutJson, &errCode); + if(errCode.error != QJsonParseError::NoError) + { + clear(); + errorString = errCode.errorString(); + return false; + } + return true; +} + +QByteArray JsonSettingsWidget::SetupStack::saveSetup() +{ + return m_setupCache.toJson(); +} + +void JsonSettingsWidget::SetupStack::clear() +{ + m_setupTree.clear(); + m_setupCache = QJsonDocument(); +} + +void JsonSettingsWidget::SetupStack::setValue(const QString &propertyId, QVariant value) +{ + QStringList stack = propertyId.split("/"); + QJsonObject o = m_setupCache.object(); + QStack stack_o; + QString top; + for(int i = 0; i < stack.size(); i++) + { + QString &t = stack[i]; + top = t; + if(i == stack.size() - 1) + break; + stack_o.push(o); + o = o[t].toObject(); + } + + switch(value.type()) + { + case QVariant::Rect: + o[top] = rectToArray(value); + break; + case QVariant::Point: + o[top] = pointToArray(value); + break; + case QVariant::Size: + o[top] = sizeToArray(value); + break; + default: + o[top] = QJsonValue::fromVariant(value); + break; + } + + for(int i = stack.size() - 2; i >= 0; i--) + { + QString &s = stack[i]; + QJsonObject oo = stack_o.pop(); + oo[s] = o; + o = oo; + } + + m_setupCache.setObject(o); +} + + +JsonSettingsWidget::JsonSettingsWidget(QWidget *parent) : + QObject(parent) +{} + +JsonSettingsWidget::JsonSettingsWidget(const QByteArray &layout, QWidget *parent) : + QObject(parent) +{ + loadLayout(layout); +} + +JsonSettingsWidget::~JsonSettingsWidget() +{ + if(m_browser) + delete m_browser; + m_browser = nullptr; +} + +bool JsonSettingsWidget::loadSettingsFromFile(const QString &path) +{ + QFile f; + f.setFileName(path); + if(!f.open(QIODevice::ReadOnly)) + { + m_setupStack.clear(); + return false; + } + + QByteArray layoutJson = f.readAll(); + f.close(); + + m_errorString.clear(); + return m_setupStack.loadSetup(layoutJson, m_errorString); +} + +bool JsonSettingsWidget::saveSettingsIntoFile(const QString &path) +{ + QFile f; + f.setFileName(path); + if(!f.open(QIODevice::WriteOnly)) + return false; + + QByteArray curSetup = m_setupStack.saveSetup(); + f.write(curSetup); + f.close(); + + return true; +} + +bool JsonSettingsWidget::loadSettings(const QByteArray &rawData) +{ + m_errorString.clear(); + return m_setupStack.loadSetup(rawData, m_errorString); +} + +QString JsonSettingsWidget::saveSettings() +{ + return m_setupStack.saveSetup(); +} + +bool JsonSettingsWidget::loadLayout(const QByteArray &layout) +{ + m_browser = loadLayoutDetail(m_setupStack, layout, m_errorString); + return m_browser != nullptr; +} + +bool JsonSettingsWidget::loadLayout(const QByteArray &settings, const QByteArray &layout) +{ + if(!loadSettings(settings)) + return false; + return loadLayout(layout); +} + +bool JsonSettingsWidget::loadLayoutFromFile(const QString &settings_path, const QString &layout_path) +{ + if(!loadSettingsFromFile(settings_path)) + return false; + + QFile f; + f.setFileName(layout_path); + if(!f.open(QIODevice::ReadOnly)) + { + m_errorString = f.errorString(); + return false; + } + + QByteArray a = f.readAll(); + if(a.isEmpty()) + { + m_errorString = f.errorString(); + return false; + } + + f.close(); + + return loadLayout(a); +} + +bool JsonSettingsWidget::isValid() +{ + return (m_browser != nullptr) && m_errorString.isEmpty(); +} + +QWidget *JsonSettingsWidget::getWidget() +{ + return m_browser; +} + +QString JsonSettingsWidget::errorString() +{ + return m_errorString; +} + +QVariant JsonSettingsWidget::retrieve_property(const JsonSettingsWidget::SetupStack &setupTree, + QString prop, + const QVariant &defaultValue) +{ + const QJsonDocument d = setupTree.m_setupCache; + QJsonObject o = d.object(); + QVariant out; + QString outPr; + + for(const QString & t : setupTree.m_setupTree) + { + outPr.append(t); + outPr.append(" << "); + if(!o.contains(t)) + { + qDebug() << outPr << prop << defaultValue << "DEFAULT-TREE"; + return defaultValue; + } + o = o[t].toObject(); + out = o[t].toVariant(); + } + + if(!o.contains(prop)) + { + qDebug() << outPr << prop << defaultValue << "DEFAULT-PROP"; + return defaultValue; + } + + switch(defaultValue.type()) + { + case QVariant::Size: + { + QJsonObject sz = o[prop].toObject(); + out = QSize(sz["w"].toInt(), sz["h"].toInt()); + } + break; + case QVariant::Point: + { + QJsonObject sz = o[prop].toObject(); + out = QPoint(sz["x"].toInt(), sz["y"].toInt()); + } + break; + case QVariant::Rect: + { + QJsonObject sz = o[prop].toObject(); + out = QRect(sz["x"].toInt(), sz["y"].toInt(), sz["w"].toInt(), sz["h"].toInt()); + } + break; + default: + out = o[prop].toVariant(); + break; + } + + qDebug() << outPr << prop << out << "INPUT"; + + return out; +} + +static const QHash loadPropertiesLoayout_requiredTypes = +{ + {"group", false}, + {"checkbox", false}, + {"spinbox", true}, + {"lineedit", false}, +}; + +bool JsonSettingsWidget::entryHasType(const QString &type) +{ + QString l = type.toLower(); + if(!loadPropertiesLoayout_requiredTypes.contains(l)) + return false; + return loadPropertiesLoayout_requiredTypes[l]; +} + +void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupTree, + const QJsonArray &elements, + QtVariantPropertyManager *manager, + QtProperty *target, + QString &err, + QWidget *parent) +{ + for(const QJsonValue &o : elements) + { + QString type = o["type"].toString("invalid"); + QString name = o["name"].toString(type); + QString title = o["title"].toString(name); + QString control = o["control"].toString(); + QtVariantProperty *item = nullptr; + + if(control.isEmpty()) + continue;//invalid + if(entryHasType(control) && (type.isEmpty() || type == "invalid")) + continue; + if(name.isEmpty()) + continue;//invalid + + qDebug() << "property" << setupTree.getPropertyId(name); + + if(!control.compare("spinBox", Qt::CaseInsensitive)) + { + if(!type.compare("int", Qt::CaseInsensitive)) + { + int valueDefault = o["value-default"].toInt(); + int valueMin = o["value-min"].toInt(0); + int valueMax = o["value-max"].toInt(100); + int singleStep = o["single-step"].toInt(1); + item = manager->addProperty(QVariant::Int, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setAttribute(QLatin1String("singleStep"), singleStep); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!type.compare("double", Qt::CaseInsensitive) || !type.compare("float", Qt::CaseInsensitive)) + { + double valueDefault = o["value-default"].toDouble(); + double valueMin = o["value-min"].toDouble(0.0); + double valueMax = o["value-max"].toDouble(100.0); + double singleStep = o["single-step"].toDouble(1); + int decimals = o["decimals"].toInt(1); + item = manager->addProperty(QVariant::Double, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setAttribute(QLatin1String("singleStep"), singleStep); + item->setAttribute(QLatin1String("decimals"), decimals); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + } + else if(!control.compare("checkBox", Qt::CaseInsensitive)) + { + bool valueDefault = o["value-default"].toBool(); + item = manager->addProperty(QVariant::Bool, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!control.compare("lineEdit", Qt::CaseInsensitive)) + { + int maxLength = o["max-length"].toInt(-1); + QString valueDefault = o["value-default"].toString(); + QString validator = o["validator"].toString(); + item = manager->addProperty(QVariant::String, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("maxlength"), maxLength); + if(!validator.isEmpty()) + item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!control.compare("comboBox", Qt::CaseInsensitive)) + { + item = manager->addProperty(QtVariantPropertyManager::enumTypeId(), title); + QStringList enumList; + QVariantList children = o["elements"].toArray().toVariantList(); + for(QVariant &j : children) + enumList.push_back(j.toString()); + int valueDefault = o["value-default"].toInt(); + item->setAttribute(QLatin1String("enumNames"), enumList); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!control.compare("flagBox", Qt::CaseInsensitive)) + { + item = manager->addProperty(QtVariantPropertyManager::flagTypeId(), title); + QStringList enumList; + QVariantList children = o["elements"].toArray().toVariantList(); + for(QVariant &j : children) + enumList.push_back(j.toString()); + int valueDefault = o["value-default"].toInt(); + item->setAttribute(QLatin1String("flagNames"), enumList); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!control.compare("sizeBox", Qt::CaseInsensitive)) + { + item = manager->addProperty(QVariant::Size, title); + QJsonObject defArr = o["value-default"].toObject(); + QJsonObject defMin = o["value-min"].toObject(); + QJsonObject defMax = o["value-max"].toObject(); + QSize valueDefault = QSize(defArr["w"].toInt(), defArr["h"].toInt()); + QSize valueMin = QSize(defMin["w"].toInt(), defMin["h"].toInt()); + QSize valueMax = QSize(defMax["w"].toInt(), defMax["h"].toInt()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!control.compare("pointbox", Qt::CaseInsensitive)) + { + QJsonObject defArr = o["value-default"].toObject(); + QJsonObject defMin = o["value-min"].toObject(); + QJsonObject defMax = o["value-max"].toObject(); + QPoint valueDefault = QPoint(defArr["x"].toInt(), defArr["y"].toInt()); + QPoint valueMin = QPoint(defMin["x"].toInt(), defMin["y"].toInt()); + QPoint valueMax = QPoint(defMax["x"].toInt(), defMax["y"].toInt()); + item = manager->addProperty(QVariant::Point, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + else if(!control.compare("rectbox", Qt::CaseInsensitive)) + { + QJsonObject defArr = o["value-default"].toObject(); + QJsonObject defMin = o["value-min"].toObject(); + QJsonObject defMax = o["value-max"].toObject(); + QRect valueDefault = QRect(defArr["x"].toInt(), defArr["y"].toInt(), + defArr["w"].toInt(), defArr["h"].toInt()); + QRect valueMin = QRect(defMin["x"].toInt(), defMin["y"].toInt(), + defMin["w"].toInt(), defMin["h"].toInt()); + QRect valueMax = QRect(defMax["x"].toInt(), defMax["y"].toInt(), + defMax["w"].toInt(), defMax["h"].toInt()); + item = manager->addProperty(QVariant::Rect, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } + /* TODO: + * - QSizeF + * - QRectF + * - QPointF + */ + else if(!control.compare("group", Qt::CaseInsensitive)) + { + QJsonArray children = o["children"].toArray(); + if(!children.isEmpty()) + { + QtProperty *subGroup = manager->addProperty(QtVariantPropertyManager::groupTypeId(), title); + setupTree.m_setupTree.push(name); + loadLayoutEntries(setupTree, children, manager, subGroup, err, parent); + setupTree.m_setupTree.pop(); + target->addSubProperty(subGroup); + } + } + } +} + +QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidget::SetupStack &stack, + const QByteArray &layoutJson, + QString &err) +{ + QtAbstractPropertyBrowser *gui = nullptr; + QString style; + QString title; + + QJsonParseError errCode; + QJsonDocument layout = QJsonDocument::fromJson(layoutJson, &errCode); + + + if(errCode.error != QJsonParseError::NoError) + { + err = errCode.errorString(); + return nullptr; + } + + QJsonObject layoutData = layout.object(); + + style = layoutData["style"].toString(); + title = layoutData["title"].toString(); + if(style == "groupbox") + gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent())); + else if(style == "button") + gui = new QtButtonPropertyBrowser(qobject_cast(parent())); + else // "tree" is default + { + QtTreePropertyBrowser *gui_t = new QtTreePropertyBrowser(qobject_cast(parent())); + gui_t->setPropertiesWithoutValueMarked(true); + gui = gui_t; + } + + QtVariantPropertyManager *variantManager = new QtVariantPropertyManager(gui); + + QtProperty *topItem = variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), title); + + QJsonArray layoutEntries = layoutData["layout"].toArray(); + loadLayoutEntries(stack, layoutEntries, variantManager, topItem, err, qobject_cast(parent())); + + variantManager->connect(variantManager, &QtVariantPropertyManager::valueChanged, + [this](QtProperty *p,QVariant v) + { + QString pid = p->propertyId(); + qDebug() << "changed:" << pid << v; + if(!pid.isEmpty()) + m_setupStack.setValue(p->propertyId(), v); + } + ); + + QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(gui); + + gui->setFactoryForManager(variantManager, variantFactory); + gui->addProperty(topItem); + + return gui; +} diff --git a/examples/json/json_settings_widget.h b/examples/json/json_settings_widget.h new file mode 100644 index 0000000..b5f80e6 --- /dev/null +++ b/examples/json/json_settings_widget.h @@ -0,0 +1,78 @@ +#ifndef JSON_SETTINGS_WIDGET_H +#define JSON_SETTINGS_WIDGET_H + +#include +#include +#include +#include + +class QtVariantPropertyManager; + +class JsonSettingsWidget : public QObject +{ + Q_OBJECT + +public: + explicit JsonSettingsWidget(QWidget *parent = nullptr); + explicit JsonSettingsWidget(const QByteArray &layout, QWidget *parent = nullptr); + virtual ~JsonSettingsWidget(); + + struct SetupStack + { + static QJsonObject rectToArray(QVariant r); + static QJsonObject sizeToArray(QVariant r); + static QJsonObject pointToArray(QVariant r); + + QStack m_setupTree; + QJsonDocument m_setupCache; + + SetupStack() + {} + + QString getPropertyId(const QString &name); + + bool loadSetup(const QByteArray &layoutJson, QString &errorString); + QByteArray saveSetup(); + + void clear(); + void setValue(const QString &propertyId, QVariant value); + }; + + bool loadSettingsFromFile(const QString &path); + bool saveSettingsIntoFile(const QString &path); + + bool loadSettings(const QByteArray &rawData); + QString saveSettings(); + + bool loadLayout(const QByteArray &layout); + bool loadLayout(const QByteArray &settings, const QByteArray &layout); + bool loadLayoutFromFile(const QString &settings_path, const QString &layout_path); + + bool isValid(); + + QWidget *getWidget(); + + QString errorString(); + +private: + QString m_errorString; + SetupStack m_setupStack; + QtAbstractPropertyBrowser *m_browser = nullptr; + + QVariant retrieve_property(const SetupStack &setupTree, QString prop, const QVariant &defaultValue); + + bool entryHasType(const QString &type); + void loadLayoutEntries(SetupStack setupTree, + const QJsonArray &elements, + QtVariantPropertyManager *manager, + QtProperty *target, + QString &err, + QWidget *parent = nullptr); + + QtAbstractPropertyBrowser *loadLayoutDetail(SetupStack &stack, + const QByteArray &layoutJson, + QString &err); + +}; + +#endif // JSON_SETTINGS_WIDGET_H diff --git a/examples/json/setup.json b/examples/json/setup.json index a22da4b..3b16685 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -1,30 +1,30 @@ { "alienShit": { - "child1": -4, + "child1": 17, "child2": "a sh9t", - "child3": "heck" + "child3": "meow!" }, "checkMe": false, "checkMeNot": true, - "choice": 2, - "choice2": 22, - "floatTest": 4.100000000000003, + "choice": 3, + "choice2": 7, + "floatTest": 3.6999999999999993, "iCanSeeYou": { - "h": 25, - "w": 25, - "x": 25, - "y": -27 + "h": 16, + "w": 11, + "x": 52, + "y": -53 }, "sizeOfMyAss": { - "h": 25, - "w": 25 + "h": 21, + "w": 4 }, "someAnother": { - "kekVision": 1 + "kekVision": 5 }, - "testNumber": 8, + "testNumber": 9, "whereIsMySombrero": { - "x": 25, - "y": -25 + "x": 6, + "y": -35 } } diff --git a/examples/json/wwsetup.json b/examples/json/wwsetup.json new file mode 100644 index 0000000..0880193 --- /dev/null +++ b/examples/json/wwsetup.json @@ -0,0 +1,15 @@ +{ + "testNumber": 41, + "checkMe" : true, + "checkMeNot" : false, + "floatTest" : 3.444, + "choice" : 4, + "alienShit": { + "child1": 42, + "child2": "a sh3t", + "child3" : "hekof" + }, + "someAnother" : { + "kekVision" : 32 + } +} From f2189ada907725ab8b0c25f4c941866ffec3f6b4 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 24 Oct 2018 01:46:18 +0300 Subject: [PATCH 12/39] JSON: Add double-type rects, sizes and points --- examples/json/json_settings_widget.cpp | 162 ++++++++++++++++++++----- examples/json/json_settings_widget.h | 3 + examples/json/sample.json | 26 ++++ examples/json/setup.json | 14 +++ 4 files changed, 176 insertions(+), 29 deletions(-) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index 460d83b..adc33ac 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -26,6 +26,17 @@ QJsonObject JsonSettingsWidget::SetupStack::rectToArray(QVariant r) return a; } +QJsonObject JsonSettingsWidget::SetupStack::rectfToArray(QVariant r) +{ + QRectF rekt = r.toRectF(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; +} + QJsonObject JsonSettingsWidget::SetupStack::sizeToArray(QVariant r) { QSize rekt = r.toSize(); @@ -35,6 +46,15 @@ QJsonObject JsonSettingsWidget::SetupStack::sizeToArray(QVariant r) return a; } +QJsonObject JsonSettingsWidget::SetupStack::sizefToArray(QVariant r) +{ + QSizeF rekt = r.toSizeF(); + QJsonObject a; + a["w"] = QJsonValue::fromVariant(rekt.width()); + a["h"] = QJsonValue::fromVariant(rekt.height()); + return a; +} + QJsonObject JsonSettingsWidget::SetupStack::pointToArray(QVariant r) { QPoint rekt = r.toPoint(); @@ -44,6 +64,15 @@ QJsonObject JsonSettingsWidget::SetupStack::pointToArray(QVariant r) return a; } +QJsonObject JsonSettingsWidget::SetupStack::pointfToArray(QVariant r) +{ + QPointF rekt = r.toPointF(); + QJsonObject a; + a["x"] = QJsonValue::fromVariant(rekt.x()); + a["y"] = QJsonValue::fromVariant(rekt.y()); + return a; +} + QString JsonSettingsWidget::SetupStack::getPropertyId(const QString &name) { QString outPr; @@ -102,12 +131,21 @@ void JsonSettingsWidget::SetupStack::setValue(const QString &propertyId, QVarian case QVariant::Rect: o[top] = rectToArray(value); break; + case QVariant::RectF: + o[top] = rectfToArray(value); + break; case QVariant::Point: o[top] = pointToArray(value); break; + case QVariant::PointF: + o[top] = pointfToArray(value); + break; case QVariant::Size: o[top] = sizeToArray(value); break; + case QVariant::SizeF: + o[top] = sizefToArray(value); + break; default: o[top] = QJsonValue::fromVariant(value); break; @@ -271,20 +309,38 @@ QVariant JsonSettingsWidget::retrieve_property(const JsonSettingsWidget::SetupSt { QJsonObject sz = o[prop].toObject(); out = QSize(sz["w"].toInt(), sz["h"].toInt()); + break; } + case QVariant::SizeF: + { + QJsonObject sz = o[prop].toObject(); + out = QSizeF(sz["w"].toDouble(), sz["h"].toDouble()); break; + } case QVariant::Point: { QJsonObject sz = o[prop].toObject(); out = QPoint(sz["x"].toInt(), sz["y"].toInt()); + break; } + case QVariant::PointF: + { + QJsonObject sz = o[prop].toObject(); + out = QPointF(sz["x"].toDouble(), sz["y"].toDouble()); break; + } case QVariant::Rect: { QJsonObject sz = o[prop].toObject(); out = QRect(sz["x"].toInt(), sz["y"].toInt(), sz["w"].toInt(), sz["h"].toInt()); + break; } + case QVariant::RectF: + { + QJsonObject sz = o[prop].toObject(); + out = QRectF(sz["x"].toDouble(), sz["y"].toDouble(), sz["w"].toDouble(), sz["h"].toDouble()); break; + } default: out = o[prop].toVariant(); break; @@ -417,16 +473,32 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT } else if(!control.compare("sizeBox", Qt::CaseInsensitive)) { - item = manager->addProperty(QVariant::Size, title); QJsonObject defArr = o["value-default"].toObject(); QJsonObject defMin = o["value-min"].toObject(); QJsonObject defMax = o["value-max"].toObject(); - QSize valueDefault = QSize(defArr["w"].toInt(), defArr["h"].toInt()); - QSize valueMin = QSize(defMin["w"].toInt(), defMin["h"].toInt()); - QSize valueMax = QSize(defMax["w"].toInt(), defMax["h"].toInt()); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); + if(!type.compare("double", Qt::CaseInsensitive)) + { + item = manager->addProperty(QVariant::SizeF, title); + QSizeF valueDefault = QSizeF(defArr["w"].toDouble(), defArr["h"].toDouble()); + QSizeF valueMin = QSizeF(defMin["w"].toDouble(), defMin["h"].toDouble()); + QSizeF valueMax = QSizeF(defMax["w"].toDouble(), defMax["h"].toDouble()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + int decimals = o["decimals"].toInt(2); + item->setAttribute(QLatin1String("decimals"), decimals); + } + else + { + item = manager->addProperty(QVariant::Size, title); + QSize valueDefault = QSize(defArr["w"].toInt(), defArr["h"].toInt()); + QSize valueMin = QSize(defMin["w"].toInt(), defMin["h"].toInt()); + QSize valueMax = QSize(defMax["w"].toInt(), defMax["h"].toInt()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + } + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } @@ -435,13 +507,30 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QJsonObject defArr = o["value-default"].toObject(); QJsonObject defMin = o["value-min"].toObject(); QJsonObject defMax = o["value-max"].toObject(); - QPoint valueDefault = QPoint(defArr["x"].toInt(), defArr["y"].toInt()); - QPoint valueMin = QPoint(defMin["x"].toInt(), defMin["y"].toInt()); - QPoint valueMax = QPoint(defMax["x"].toInt(), defMax["y"].toInt()); - item = manager->addProperty(QVariant::Point, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); + + if(!type.compare("double", Qt::CaseInsensitive)) + { + item = manager->addProperty(QVariant::PointF, title); + QPointF valueDefault = QPointF(defArr["x"].toDouble(), defArr["y"].toDouble()); + QPointF valueMin = QPointF(defMin["x"].toDouble(), defMin["y"].toDouble()); + QPointF valueMax = QPointF(defMax["x"].toDouble(), defMax["y"].toDouble()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + int decimals = o["decimals"].toInt(2); + item->setAttribute(QLatin1String("decimals"), decimals); + } + else + { + item = manager->addProperty(QVariant::Point, title); + QPoint valueDefault = QPoint(defArr["x"].toInt(), defArr["y"].toInt()); + QPoint valueMin = QPoint(defMin["x"].toInt(), defMin["y"].toInt()); + QPoint valueMax = QPoint(defMax["x"].toInt(), defMax["y"].toInt()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + } + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } @@ -450,24 +539,39 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QJsonObject defArr = o["value-default"].toObject(); QJsonObject defMin = o["value-min"].toObject(); QJsonObject defMax = o["value-max"].toObject(); - QRect valueDefault = QRect(defArr["x"].toInt(), defArr["y"].toInt(), - defArr["w"].toInt(), defArr["h"].toInt()); - QRect valueMin = QRect(defMin["x"].toInt(), defMin["y"].toInt(), - defMin["w"].toInt(), defMin["h"].toInt()); - QRect valueMax = QRect(defMax["x"].toInt(), defMax["y"].toInt(), - defMax["w"].toInt(), defMax["h"].toInt()); - item = manager->addProperty(QVariant::Rect, title); - item->setValue(retrieve_property(setupTree, name, valueDefault)); - item->setAttribute(QLatin1String("minimum"), valueMin); - item->setAttribute(QLatin1String("maximum"), valueMax); + + if(!type.compare("double", Qt::CaseInsensitive)) + { + item = manager->addProperty(QVariant::RectF, title); + QRectF valueDefault = QRectF(defArr["x"].toDouble(), defArr["y"].toDouble(), + defArr["w"].toDouble(), defArr["h"].toDouble()); + QRectF valueMin = QRectF(defMin["x"].toDouble(), defMin["y"].toDouble(), + defMin["w"].toDouble(), defMin["h"].toDouble()); + QRectF valueMax = QRectF(defMax["x"].toDouble(), defMax["y"].toDouble(), + defMax["w"].toDouble(), defMax["h"].toDouble()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + int decimals = o["decimals"].toInt(2); + item->setAttribute(QLatin1String("decimals"), decimals); + } + else + { + item = manager->addProperty(QVariant::Rect, title); + QRect valueDefault = QRect(defArr["x"].toInt(), defArr["y"].toInt(), + defArr["w"].toInt(), defArr["h"].toInt()); + QRect valueMin = QRect(defMin["x"].toInt(), defMin["y"].toInt(), + defMin["w"].toInt(), defMin["h"].toInt()); + QRect valueMax = QRect(defMax["x"].toInt(), defMax["y"].toInt(), + defMax["w"].toInt(), defMax["h"].toInt()); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setAttribute(QLatin1String("minimum"), valueMin); + item->setAttribute(QLatin1String("maximum"), valueMax); + } + item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } - /* TODO: - * - QSizeF - * - QRectF - * - QPointF - */ else if(!control.compare("group", Qt::CaseInsensitive)) { QJsonArray children = o["children"].toArray(); diff --git a/examples/json/json_settings_widget.h b/examples/json/json_settings_widget.h index b5f80e6..d7123cf 100644 --- a/examples/json/json_settings_widget.h +++ b/examples/json/json_settings_widget.h @@ -20,8 +20,11 @@ class JsonSettingsWidget : public QObject struct SetupStack { static QJsonObject rectToArray(QVariant r); + static QJsonObject rectfToArray(QVariant r); static QJsonObject sizeToArray(QVariant r); + static QJsonObject sizefToArray(QVariant r); static QJsonObject pointToArray(QVariant r); + static QJsonObject pointfToArray(QVariant r); QStack m_setupTree; QJsonDocument m_setupCache; diff --git a/examples/json/sample.json b/examples/json/sample.json index d8b9b9c..602ad9a 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -46,18 +46,44 @@ "value-max": {"w": 42, "h": 42}, "value-default": {"w": 41, "h" : 34} }, + { + "control": "sizebox", + "type": "double", + "decimals": 4, + "name": "floatingBrick", + "title": "Floating brick", + "value-min": {"w": 2.2, "h" : 2.0}, + "value-max": {"w": 42.0, "h": 42.1}, + "value-default": {"w": 41.52, "h" : 34.2} + }, { "control": "pointBox", "name": "whereIsMySombrero", "title": "Where?", "value-default" : {"x":12, "y":12} }, + { + "control": "pointBox", + "type": "double", + "decimals": 3, + "name": "floatOfWhere", + "title": "Float Where?", + "value-default" : {"x":12.02, "y":-12.1} + }, { "control": "rectBox", "name": "iCanSeeYou", "title": "Rects, Rects.... rects!!!", "value-default": {"x":-12, "y":34, "w":12, "h":12} }, + { + "control": "rectBox", + "type": "double", + "decimals": 5, + "name": "fanSeeYou", + "title": "Floating rects!", + "value-default": {"x":-1.02, "y":34.0, "w":12.054, "h":12.11} + }, { "control": "spinBox", "type": "double", diff --git a/examples/json/setup.json b/examples/json/setup.json index 3b16685..b4c80ec 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -8,7 +8,21 @@ "checkMeNot": true, "choice": 3, "choice2": 7, + "fanSeeYou": { + "h": 12.11, + "w": 12.054, + "x": -1.05224, + "y": 34 + }, + "floatOfWhere": { + "x": 12.02, + "y": -12.104 + }, "floatTest": 3.6999999999999993, + "floatingBrick": { + "h": 34.2, + "w": 40.22 + }, "iCanSeeYou": { "h": 16, "w": 11, From b0db023aa585c5f37c5c36dd50899d7a828edd9f Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 24 Oct 2018 02:27:14 +0300 Subject: [PATCH 13/39] JSON: Move SetupStack into the private section --- examples/json/json_settings_widget.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/json/json_settings_widget.h b/examples/json/json_settings_widget.h index d7123cf..b9ecd33 100644 --- a/examples/json/json_settings_widget.h +++ b/examples/json/json_settings_widget.h @@ -12,11 +12,6 @@ class JsonSettingsWidget : public QObject { Q_OBJECT -public: - explicit JsonSettingsWidget(QWidget *parent = nullptr); - explicit JsonSettingsWidget(const QByteArray &layout, QWidget *parent = nullptr); - virtual ~JsonSettingsWidget(); - struct SetupStack { static QJsonObject rectToArray(QVariant r); @@ -41,6 +36,11 @@ class JsonSettingsWidget : public QObject void setValue(const QString &propertyId, QVariant value); }; +public: + explicit JsonSettingsWidget(QWidget *parent = nullptr); + explicit JsonSettingsWidget(const QByteArray &layout, QWidget *parent = nullptr); + virtual ~JsonSettingsWidget(); + bool loadSettingsFromFile(const QString &path); bool saveSettingsIntoFile(const QString &path); From dcba408e5620bef9ac5c7232156a1948ab796097 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 24 Oct 2018 02:33:07 +0300 Subject: [PATCH 14/39] JSON: Added the signal to notify about settings change --- examples/json/json.cpp | 5 +++++ examples/json/json_settings_widget.cpp | 3 +++ examples/json/json_settings_widget.h | 3 +++ examples/json/setup.json | 18 +++++++++--------- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/examples/json/json.cpp b/examples/json/json.cpp index 24d1fb9..c4a2654 100644 --- a/examples/json/json.cpp +++ b/examples/json/json.cpp @@ -60,6 +60,11 @@ int main(int argc, char **argv) return 1; } + extraSettings.connect(&extraSettings, &JsonSettingsWidget::settingsChanged, + []() + { + qDebug() << "Setup Changed!"; + }); QWidget *w = extraSettings.getWidget(); w->show(); diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index adc33ac..a19c2d7 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -633,7 +633,10 @@ QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidg QString pid = p->propertyId(); qDebug() << "changed:" << pid << v; if(!pid.isEmpty()) + { m_setupStack.setValue(p->propertyId(), v); + emit settingsChanged(); + } } ); diff --git a/examples/json/json_settings_widget.h b/examples/json/json_settings_widget.h index b9ecd33..bc16ebc 100644 --- a/examples/json/json_settings_widget.h +++ b/examples/json/json_settings_widget.h @@ -57,6 +57,9 @@ class JsonSettingsWidget : public QObject QString errorString(); +signals: + void settingsChanged(); + private: QString m_errorString; SetupStack m_setupStack; diff --git a/examples/json/setup.json b/examples/json/setup.json index b4c80ec..beddce5 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -1,13 +1,13 @@ { "alienShit": { - "child1": 17, + "child1": 12, "child2": "a sh9t", - "child3": "meow!" + "child3": "1t4t1" }, - "checkMe": false, - "checkMeNot": true, + "checkMe": true, + "checkMeNot": false, "choice": 3, - "choice2": 7, + "choice2": 24, "fanSeeYou": { "h": 12.11, "w": 12.054, @@ -24,10 +24,10 @@ "w": 40.22 }, "iCanSeeYou": { - "h": 16, + "h": 18, "w": 11, - "x": 52, - "y": -53 + "x": 38, + "y": -64 }, "sizeOfMyAss": { "h": 21, @@ -36,7 +36,7 @@ "someAnother": { "kekVision": 5 }, - "testNumber": 9, + "testNumber": 27, "whereIsMySombrero": { "x": 6, "y": -35 From df512f11dfdc96858cfb95daa2715fc6ea28c5a0 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 14 Nov 2018 01:47:20 +0300 Subject: [PATCH 15/39] JSON: Fix some builds on Windows --- examples/json/json_settings_widget.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index a19c2d7..3d3618b 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -374,8 +374,9 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QString &err, QWidget *parent) { - for(const QJsonValue &o : elements) + for(const QJsonValue &ov : elements) { + QJsonObject o = ov.toObject(); QString type = o["type"].toString("invalid"); QString name = o["name"].toString(type); QString title = o["title"].toString(name); From 28f832963cf83a826ac7af03e75ed3ef642dc820 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 28 Nov 2018 01:34:24 +0300 Subject: [PATCH 16/39] JSON: Allow tooltips --- examples/json/json_settings_widget.cpp | 13 ++++++++ examples/json/sample.json | 42 +++++++++++++++++--------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index 3d3618b..2f85847 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -381,6 +381,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QString name = o["name"].toString(type); QString title = o["title"].toString(name); QString control = o["control"].toString(); + QString tooltip = o["tooltip"].toString(); QtVariantProperty *item = nullptr; if(control.isEmpty()) @@ -402,6 +403,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT int singleStep = o["single-step"].toInt(1); item = manager->addProperty(QVariant::Int, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); item->setAttribute(QLatin1String("singleStep"), singleStep); @@ -417,6 +419,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT int decimals = o["decimals"].toInt(1); item = manager->addProperty(QVariant::Double, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); item->setAttribute(QLatin1String("singleStep"), singleStep); @@ -430,6 +433,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT bool valueDefault = o["value-default"].toBool(); item = manager->addProperty(QVariant::Bool, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } @@ -440,6 +444,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QString validator = o["validator"].toString(); item = manager->addProperty(QVariant::String, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("maxlength"), maxLength); if(!validator.isEmpty()) item->setAttribute(QLatin1String("regExp"), QRegExp(validator)); @@ -456,6 +461,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT int valueDefault = o["value-default"].toInt(); item->setAttribute(QLatin1String("enumNames"), enumList); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } @@ -469,6 +475,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT int valueDefault = o["value-default"].toInt(); item->setAttribute(QLatin1String("flagNames"), enumList); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } @@ -484,6 +491,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QSizeF valueMin = QSizeF(defMin["w"].toDouble(), defMin["h"].toDouble()); QSizeF valueMax = QSizeF(defMax["w"].toDouble(), defMax["h"].toDouble()); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); int decimals = o["decimals"].toInt(2); @@ -496,6 +504,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QSize valueMin = QSize(defMin["w"].toInt(), defMin["h"].toInt()); QSize valueMax = QSize(defMax["w"].toInt(), defMax["h"].toInt()); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); } @@ -516,6 +525,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QPointF valueMin = QPointF(defMin["x"].toDouble(), defMin["y"].toDouble()); QPointF valueMax = QPointF(defMax["x"].toDouble(), defMax["y"].toDouble()); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); int decimals = o["decimals"].toInt(2); @@ -528,6 +538,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QPoint valueMin = QPoint(defMin["x"].toInt(), defMin["y"].toInt()); QPoint valueMax = QPoint(defMax["x"].toInt(), defMax["y"].toInt()); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); } @@ -551,6 +562,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QRectF valueMax = QRectF(defMax["x"].toDouble(), defMax["y"].toDouble(), defMax["w"].toDouble(), defMax["h"].toDouble()); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); int decimals = o["decimals"].toInt(2); @@ -566,6 +578,7 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QRect valueMax = QRect(defMax["x"].toInt(), defMax["y"].toInt(), defMax["w"].toInt(), defMax["h"].toInt()); item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); item->setAttribute(QLatin1String("minimum"), valueMin); item->setAttribute(QLatin1String("maximum"), valueMax); } diff --git a/examples/json/sample.json b/examples/json/sample.json index 602ad9a..241e733 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -10,33 +10,38 @@ "value-default": 5, "value-min": -42, "value-max": 42, - "single-step": 2 + "single-step": 2, + "tooltip" : "Keeek" }, { "control": "checkbox", "name": "checkMe", "title": "Check me", - "value-default": false + "value-default": false, + "tooltip" : "Keeek 1" }, { "control": "checkbox", "name": "checkMeNot", "title": "Don't check me, jerk", - "value-default": true + "value-default": true, + "tooltip" : "Keeek 2" }, { "control": "combobox", "name": "choice", "title": "Choose me", "elements": ["zero", "one", "two", "three", "four"], - "value-default": 1 + "value-default": 1, + "tooltip" : "Keeek 3" }, { "control": "flagbox", "name": "choice2", "title": "Choose of:", "elements": ["zero", "one", "two", "three", "four"], - "value-default": 21 + "value-default": 21, + "tooltip" : "Keeek 4" }, { "control": "sizebox", @@ -44,7 +49,8 @@ "title": "Size of?", "value-min": {"w": 2, "h" : 2}, "value-max": {"w": 42, "h": 42}, - "value-default": {"w": 41, "h" : 34} + "value-default": {"w": 41, "h" : 34}, + "tooltip" : "Keeek 5" }, { "control": "sizebox", @@ -54,13 +60,15 @@ "title": "Floating brick", "value-min": {"w": 2.2, "h" : 2.0}, "value-max": {"w": 42.0, "h": 42.1}, - "value-default": {"w": 41.52, "h" : 34.2} + "value-default": {"w": 41.52, "h" : 34.2}, + "tooltip" : "Keeek 6" }, { "control": "pointBox", "name": "whereIsMySombrero", "title": "Where?", - "value-default" : {"x":12, "y":12} + "value-default" : {"x":12, "y":12}, + "tooltip" : "Keeek 7" }, { "control": "pointBox", @@ -68,13 +76,15 @@ "decimals": 3, "name": "floatOfWhere", "title": "Float Where?", - "value-default" : {"x":12.02, "y":-12.1} + "value-default" : {"x":12.02, "y":-12.1}, + "tooltip" : "Keeek 8" }, { "control": "rectBox", "name": "iCanSeeYou", "title": "Rects, Rects.... rects!!!", - "value-default": {"x":-12, "y":34, "w":12, "h":12} + "value-default": {"x":-12, "y":34, "w":12, "h":12}, + "tooltip" : "Keeek 9" }, { "control": "rectBox", @@ -82,7 +92,8 @@ "decimals": 5, "name": "fanSeeYou", "title": "Floating rects!", - "value-default": {"x":-1.02, "y":34.0, "w":12.054, "h":12.11} + "value-default": {"x":-1.02, "y":34.0, "w":12.054, "h":12.11}, + "tooltip" : "Keeek 10" }, { "control": "spinBox", @@ -93,7 +104,8 @@ "value-min": -42.0, "value-max": 42.0, "single-step": 0.1, - "decimals" : 3 + "decimals" : 3, + "tooltip" : "Keeek 11" }, { "control": "group", @@ -115,7 +127,8 @@ "title" : "Pek", "value-default": "", "max-length": 42, - "validator": "^a\\ sh\\dt$" + "validator": "^a\\ sh\\dt$", + "tooltip" : "put a KEK here" }, { "control": "lineEdit", @@ -138,7 +151,8 @@ "title" : "Kek", "value-default": 5, "value-min": -42, - "value-max": 42 + "value-max": 42, + "tooltip" : "It's a NOOOOOnsese" } ] } From 380b2dfdc521adaf02ec60a3dc332708bc4f5f60 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Thu, 6 Dec 2018 03:29:18 +0300 Subject: [PATCH 17/39] JSON: Added support for "color" field --- examples/json/json_settings_widget.cpp | 9 +++++++++ examples/json/sample.json | 7 +++++++ examples/json/setup.json | 1 + 3 files changed, 17 insertions(+) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index 2f85847..e22fd2e 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -437,6 +437,15 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } + else if(!control.compare("color", Qt::CaseInsensitive)) + { + QColor valueDefault = QColor(o["value-default"].toString()); + item = manager->addProperty(QVariant::Color, title); + item->setValue(retrieve_property(setupTree, name, valueDefault)); + item->setToolTip(tooltip); + item->setPropertyId(setupTree.getPropertyId(name)); + target->addSubProperty(item); + } else if(!control.compare("lineEdit", Qt::CaseInsensitive)) { int maxLength = o["max-length"].toInt(-1); diff --git a/examples/json/sample.json b/examples/json/sample.json index 241e733..7fe0c58 100644 --- a/examples/json/sample.json +++ b/examples/json/sample.json @@ -35,6 +35,13 @@ "value-default": 1, "tooltip" : "Keeek 3" }, + { + "control": "color", + "name": "colorness", + "title": "Color of?", + "value-default": "#FF00FF", + "tooltip" : "Just a color" + }, { "control": "flagbox", "name": "choice2", diff --git a/examples/json/setup.json b/examples/json/setup.json index beddce5..fd84962 100644 --- a/examples/json/setup.json +++ b/examples/json/setup.json @@ -8,6 +8,7 @@ "checkMeNot": false, "choice": 3, "choice2": 24, + "colorness": "#1996ff", "fanSeeYou": { "h": 12.11, "w": 12.054, From b93e94a045d922086f130d9131db6117f2323b5f Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 13 Feb 2019 01:01:35 +0300 Subject: [PATCH 18/39] Remove jerkish optimization flags Those flags are making binary be non-portable and crashy on different CPUs --- cmake/CompileSettings.cmake | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmake/CompileSettings.cmake b/cmake/CompileSettings.cmake index f26e7ac..3aa57a4 100644 --- a/cmake/CompileSettings.cmake +++ b/cmake/CompileSettings.cmake @@ -13,21 +13,21 @@ IF(CMAKE_COMPILER_IS_GNUCC) SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") - SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native -funroll-loops -ffast-math") - SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -march=native -funroll-loops") + SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fomit-frame-pointer -funroll-loops -ffast-math") + SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -fomit-frame-pointer -funroll-loops") OPTION (USE_PEDANTIC_FLAGS "Use Pedantic Flags in GCC" ON) IF(USE_PEDANTIC_FLAGS) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long -Wno-variadic-macros") SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -pedantic -Wno-long-long -Wno-variadic-macros") ENDIF() - + OPTION (USE_DEBUG_SYMBOLS "Use Debug Symbols" OFF) IF(USE_DEBUG_SYMBOLS) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -g") ENDIF() - + ENDIF(CMAKE_COMPILER_IS_GNUCC) IF(NOT MSVC) @@ -38,9 +38,9 @@ IF(NOT MSVC) CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) IF(COMPILER_SUPPORTS_CXX11) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") ELSEIF(COMPILER_SUPPORTS_CXX0X) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") ELSE() SET(USE_CPP_11 OFF) MESSAGE(STATUS "Compiler ${CMAKE_CXX_COMPILER} has no C++11 support.") @@ -65,4 +65,4 @@ IF(USE_OpenMP) ENDIF() ENDIF() -MESSAGE(STATUS "===============================================================") \ No newline at end of file +MESSAGE(STATUS "===============================================================") From f3f769cfa1a6a041e47541a82a9c98d92ec7fbe5 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 30 Jun 2019 08:10:37 +0300 Subject: [PATCH 19/39] Fixed linking warnings on macOS --- cmake/CompileSettings.cmake | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmake/CompileSettings.cmake b/cmake/CompileSettings.cmake index 3aa57a4..6f05c87 100644 --- a/cmake/CompileSettings.cmake +++ b/cmake/CompileSettings.cmake @@ -9,6 +9,17 @@ MESSAGE(STATUS "===============================================================" MESSAGE(STATUS "============ Configuring CompileSettings =====================") +IF(APPLE) + + # Unify visibility to meet llvm's default. + INCLUDE(CheckCXXCompilerFlag) + check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG) + IF(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden") + ENDIF() + +ENDIF(APPLE) + IF(CMAKE_COMPILER_IS_GNUCC) SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall") From a2aca2bbda4f9728d845e22887b6739715280a8a Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 29 Sep 2019 02:43:19 +0300 Subject: [PATCH 20/39] Fixed two "deprecated" warnings --- src/qteditorfactory.cpp | 7 +++---- src/qttreepropertybrowser.cpp | 5 ++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index 3a06f9e..afa620f 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -2251,10 +2251,9 @@ void QtColorEditWidget::setValue(const QColor &c) void QtColorEditWidget::buttonClicked() { - bool ok = false; - QRgb oldRgba = m_color.rgba(); - QRgb newRgba = QColorDialog::getRgba(oldRgba, &ok, this); - if (ok && newRgba != oldRgba) { + QColor oldRgba = m_color; + QColor newRgba = QColorDialog::getColor(oldRgba, this); + if (newRgba.isValid() && newRgba != oldRgba) { setValue(QColor::fromRgba(newRgba)); emit valueChanged(m_color); } diff --git a/src/qttreepropertybrowser.cpp b/src/qttreepropertybrowser.cpp index a92ab53..4b5fa8a 100644 --- a/src/qttreepropertybrowser.cpp +++ b/src/qttreepropertybrowser.cpp @@ -127,6 +127,9 @@ class QtPropertyEditorView : public QTreeWidget QTreeWidgetItem *indexToItem(const QModelIndex &index) const { return itemFromIndex(index); } + void setExpandedItem(const QTreeWidgetItem *item, bool expand) // Made to replace "setExpandedItem" which is deprecated now + { this->setExpanded(this->indexFromItem(item), expand); } + protected: void keyPressEvent(QKeyEvent *event); void mousePressEvent(QMouseEvent *event); @@ -586,7 +589,7 @@ void QtTreePropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBrow m_indexToItem[index] = newItem; newItem->setFlags(newItem->flags() | Qt::ItemIsEditable); - m_treeWidget->setItemExpanded(newItem, true); + m_treeWidget->setExpandedItem(newItem, true); updateItem(newItem); } From 94988be3279e0a98aa0c94b385d6ca305f4558b6 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 29 Sep 2019 02:53:14 +0300 Subject: [PATCH 21/39] Ouch! --- src/qteditorfactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index afa620f..1350703 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -2254,7 +2254,7 @@ void QtColorEditWidget::buttonClicked() QColor oldRgba = m_color; QColor newRgba = QColorDialog::getColor(oldRgba, this); if (newRgba.isValid() && newRgba != oldRgba) { - setValue(QColor::fromRgba(newRgba)); + setValue(newRgba); emit valueChanged(m_color); } } From 357283962a619ca5b3e428f12b41dce9a401e0cf Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 29 Sep 2019 14:40:03 +0300 Subject: [PATCH 22/39] Fixed build for Qt lower than 5.7 --- src/qttreepropertybrowser.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/qttreepropertybrowser.cpp b/src/qttreepropertybrowser.cpp index 4b5fa8a..1268a42 100644 --- a/src/qttreepropertybrowser.cpp +++ b/src/qttreepropertybrowser.cpp @@ -128,7 +128,13 @@ class QtPropertyEditorView : public QTreeWidget { return itemFromIndex(index); } void setExpandedItem(const QTreeWidgetItem *item, bool expand) // Made to replace "setExpandedItem" which is deprecated now - { this->setExpanded(this->indexFromItem(item), expand); } + { +#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0) + this->setExpanded(this->indexFromItem(item), expand); +#else + this->setItemExpanded(item, expand); +#endif + } protected: void keyPressEvent(QKeyEvent *event); From 1a104eb8b05336d580f8629130e486bf4c8bef8c Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Thu, 10 Oct 2019 01:37:30 +0300 Subject: [PATCH 23/39] Added support for frameless group boxes (aka frames) --- examples/json/json_settings_widget.cpp | 2 ++ src/qtgroupboxpropertybrowser.cpp | 25 ++++++++++++++++++------- src/qtgroupboxpropertybrowser.h | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index e22fd2e..306d9e7 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -634,6 +634,8 @@ QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidg title = layoutData["title"].toString(); if(style == "groupbox") gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent())); + if(style == "frame") + gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent()), true); else if(style == "button") gui = new QtButtonPropertyBrowser(qobject_cast(parent())); else // "tree" is default diff --git a/src/qtgroupboxpropertybrowser.cpp b/src/qtgroupboxpropertybrowser.cpp index ae38f03..9f4e817 100644 --- a/src/qtgroupboxpropertybrowser.cpp +++ b/src/qtgroupboxpropertybrowser.cpp @@ -56,7 +56,7 @@ class QtGroupBoxPropertyBrowserPrivate Q_DECLARE_PUBLIC(QtGroupBoxPropertyBrowser) public: - void init(QWidget *parent); + void init(QWidget *parent, bool frameLess); void propertyInserted(QtBrowserItem *index, QtBrowserItem *afterIndex); void propertyRemoved(QtBrowserItem *index); @@ -70,11 +70,14 @@ class QtGroupBoxPropertyBrowserPrivate struct WidgetItem { WidgetItem() : widget(0), label(0), widgetLabel(0), + groupBoxGroup(0), groupBoxFrame(0), groupBox(0), layout(0), line(0), parent(0) { } QWidget *widget; // can be null QLabel *label; QLabel *widgetLabel; - QGroupBox *groupBox; + QGroupBox *groupBoxGroup; + QFrame *groupBoxFrame; + QWidget *groupBox; QGridLayout *layout; QFrame *line; WidgetItem *parent; @@ -88,6 +91,7 @@ class QtGroupBoxPropertyBrowserPrivate bool hasHeader(WidgetItem *item) const; + bool m_frameLess; QMap m_indexToItem; QMap m_itemToIndex; QMap m_widgetToItem; @@ -96,8 +100,9 @@ class QtGroupBoxPropertyBrowserPrivate QList m_recreateQueue; }; -void QtGroupBoxPropertyBrowserPrivate::init(QWidget *parent) +void QtGroupBoxPropertyBrowserPrivate::init(QWidget *parent, bool frameLess) { + m_frameLess = frameLess; m_mainLayout = new QGridLayout(); parent->setLayout(m_mainLayout); QLayoutItem *item = new QSpacerItem(0, 0, @@ -218,8 +223,13 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt if (hasHeader(par)) oldRow += 2; } - parentItem->groupBox = new QGroupBox(w); + if(m_frameLess) + parentItem->groupBox = parentItem->groupBoxFrame = new QFrame(w); + else + parentItem->groupBox = parentItem->groupBoxGroup = new QGroupBox(w); parentItem->layout = new QGridLayout(); + if(m_frameLess) + parentItem->layout->setMargin(0); parentItem->groupBox->setLayout(parentItem->layout); if (parentItem->label) { l->removeWidget(parentItem->label); @@ -414,7 +424,8 @@ void QtGroupBoxPropertyBrowserPrivate::updateItem(WidgetItem *item) QFont font = item->groupBox->font(); font.setUnderline(property->isModified()); item->groupBox->setFont(font); - item->groupBox->setTitle(property->propertyName()); + if(item->groupBoxGroup) + item->groupBoxGroup->setTitle(property->propertyName()); item->groupBox->setToolTip(property->toolTip()); item->groupBox->setStatusTip(property->statusTip()); item->groupBox->setWhatsThis(property->whatsThis()); @@ -479,13 +490,13 @@ void QtGroupBoxPropertyBrowserPrivate::updateItem(WidgetItem *item) /*! Creates a property browser with the given \a parent. */ -QtGroupBoxPropertyBrowser::QtGroupBoxPropertyBrowser(QWidget *parent) +QtGroupBoxPropertyBrowser::QtGroupBoxPropertyBrowser(QWidget *parent, bool frameLess) : QtAbstractPropertyBrowser(parent) { d_ptr = new QtGroupBoxPropertyBrowserPrivate; d_ptr->q_ptr = this; - d_ptr->init(this); + d_ptr->init(this, frameLess); } /*! diff --git a/src/qtgroupboxpropertybrowser.h b/src/qtgroupboxpropertybrowser.h index b02e87d..65a504a 100644 --- a/src/qtgroupboxpropertybrowser.h +++ b/src/qtgroupboxpropertybrowser.h @@ -54,7 +54,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtGroupBoxPropertyBrowser : public QtAbstractP Q_OBJECT public: - QtGroupBoxPropertyBrowser(QWidget *parent = 0); + QtGroupBoxPropertyBrowser(QWidget *parent = 0, bool frameLess = false); ~QtGroupBoxPropertyBrowser(); protected: From 36d0b684909604aaf62266d3609c2ea708fc0d0d Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Thu, 10 Oct 2019 03:47:19 +0300 Subject: [PATCH 24/39] Ability to control "True/False" visibility --- examples/json/json_settings_widget.cpp | 4 ++ src/qteditorfactory.cpp | 42 ++++++++++++++++++- src/qteditorfactory.h | 2 + src/qtgroupboxpropertybrowser.cpp | 2 +- src/qtpropertybrowserutils.cpp | 21 +++++++++- src/qtpropertybrowserutils_p.h | 6 +++ src/qtpropertymanager.cpp | 56 +++++++++++++++++++++----- src/qtpropertymanager.h | 3 ++ src/qtvariantproperty.cpp | 21 ++++++++++ src/qtvariantproperty.h | 1 + 10 files changed, 143 insertions(+), 15 deletions(-) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index 306d9e7..50891e8 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -431,9 +431,13 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT else if(!control.compare("checkBox", Qt::CaseInsensitive)) { bool valueDefault = o["value-default"].toBool(); + bool textVisible = true; + if(o.keys().contains("text-visible")) + textVisible = o["text-visible"].toBool(); item = manager->addProperty(QVariant::Bool, title); item->setValue(retrieve_property(setupTree, name, valueDefault)); item->setToolTip(tooltip); + item->setAttribute(QLatin1String("textVisible"), textVisible); item->setPropertyId(setupTree.getPropertyId(name)); target->addSubProperty(item); } diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index 1350703..e6aad8c 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -606,7 +606,9 @@ class QtCheckBoxFactoryPrivate : public EditorFactoryPrivate Q_DECLARE_PUBLIC(QtCheckBoxFactory) public: void slotPropertyChanged(QtProperty *property, bool value); + void slotTextVisibleChanged(QtProperty *property, bool); void slotSetValue(bool value); + void slotSetTextVisible(bool); }; void QtCheckBoxFactoryPrivate::slotPropertyChanged(QtProperty *property, bool value) @@ -623,6 +625,24 @@ void QtCheckBoxFactoryPrivate::slotPropertyChanged(QtProperty *property, bool va } } +void QtCheckBoxFactoryPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible) +{ + if (!m_createdEditors.contains(property)) + return; + + QtBoolPropertyManager *manager = q_ptr->propertyManager(property); + if (!manager) + return; + + QListIterator itEditor(m_createdEditors[property]); + while (itEditor.hasNext()) { + QtBoolEdit *editor = itEditor.next(); + editor->blockCheckBoxSignals(true); + editor->setTextVisible(textVisible); + editor->blockCheckBoxSignals(false); + } +} + void QtCheckBoxFactoryPrivate::slotSetValue(bool value) { QObject *object = q_ptr->sender(); @@ -639,6 +659,22 @@ void QtCheckBoxFactoryPrivate::slotSetValue(bool value) } } +void QtCheckBoxFactoryPrivate::slotSetTextVisible(bool textVisible) +{ + QObject *object = q_ptr->sender(); + + const QMap::ConstIterator ecend = m_editorToProperty.constEnd(); + for (QMap::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor) + if (itEditor.key() == object) { + QtProperty *property = itEditor.value(); + QtBoolPropertyManager *manager = q_ptr->propertyManager(property); + if (!manager) + return; + manager->setTextVisible(property, textVisible); + return; + } +} + /*! \class QtCheckBoxFactory @@ -677,6 +713,8 @@ void QtCheckBoxFactory::connectPropertyManager(QtBoolPropertyManager *manager) { connect(manager, SIGNAL(valueChanged(QtProperty *, bool)), this, SLOT(slotPropertyChanged(QtProperty *, bool))); + connect(manager, SIGNAL(textVisibleChanged(QtProperty *, bool)), + this, SLOT(slotTextVisibleChanged(QtProperty *, bool))); } /*! @@ -688,8 +726,8 @@ QWidget *QtCheckBoxFactory::createEditor(QtBoolPropertyManager *manager, QtPrope QWidget *parent) { QtBoolEdit *editor = d_ptr->createEditor(property, parent); + editor->setTextVisible(manager->textVisible(property)); editor->setChecked(manager->value(property)); - connect(editor, SIGNAL(toggled(bool)), this, SLOT(slotSetValue(bool))); connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(slotEditorDestroyed(QObject *))); @@ -705,6 +743,8 @@ void QtCheckBoxFactory::disconnectPropertyManager(QtBoolPropertyManager *manager { disconnect(manager, SIGNAL(valueChanged(QtProperty *, bool)), this, SLOT(slotPropertyChanged(QtProperty *, bool))); + disconnect(manager, SIGNAL(textVisibleChanged(QtProperty *, bool)), + this, SLOT(slotTextVisibleChanged(QtProperty *, bool))); } // QtDoubleSpinBoxFactory diff --git a/src/qteditorfactory.h b/src/qteditorfactory.h index 7f0a157..590a5b3 100644 --- a/src/qteditorfactory.h +++ b/src/qteditorfactory.h @@ -137,7 +137,9 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCheckBoxFactory : public QtAbstractEditorFac Q_DECLARE_PRIVATE(QtCheckBoxFactory) Q_DISABLE_COPY(QtCheckBoxFactory) Q_PRIVATE_SLOT(d_func(), void slotPropertyChanged(QtProperty *, bool)) + Q_PRIVATE_SLOT(d_func(), void slotTextVisibleChanged(QtProperty *, bool)) Q_PRIVATE_SLOT(d_func(), void slotSetValue(bool)) + Q_PRIVATE_SLOT(d_func(), void slotSetTextVisible(bool)) Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *)) }; diff --git a/src/qtgroupboxpropertybrowser.cpp b/src/qtgroupboxpropertybrowser.cpp index 9f4e817..0c2e252 100644 --- a/src/qtgroupboxpropertybrowser.cpp +++ b/src/qtgroupboxpropertybrowser.cpp @@ -204,7 +204,7 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt if (!parentItem) { layout = m_mainLayout; - parentWidget = q_ptr;; + parentWidget = q_ptr; } else { if (!parentItem->groupBox) { m_recreateQueue.removeAll(parentItem); diff --git a/src/qtpropertybrowserutils.cpp b/src/qtpropertybrowserutils.cpp index cd19583..ea47364 100644 --- a/src/qtpropertybrowserutils.cpp +++ b/src/qtpropertybrowserutils.cpp @@ -215,12 +215,29 @@ void QtBoolEdit::setTextVisible(bool textVisible) return; m_textVisible = textVisible; + updateText(); +} + +void QtBoolEdit::setLabel(QString label) +{ + m_checkBoxLabel = label; + updateText(); +} + +void QtBoolEdit::updateText() +{ if (m_textVisible) - m_checkBox->setText(isChecked() ? tr("True") : tr("False")); + { + if(m_checkBoxLabel.isEmpty()) + m_checkBox->setText(isChecked() ? tr("True") : tr("False")); + else + m_checkBox->setText(m_checkBoxLabel); + } else m_checkBox->setText(QString()); } + Qt::CheckState QtBoolEdit::checkState() const { return m_checkBox->checkState(); @@ -239,7 +256,7 @@ bool QtBoolEdit::isChecked() const void QtBoolEdit::setChecked(bool c) { m_checkBox->setChecked(c); - if (!m_textVisible) + if (!m_textVisible || !m_checkBoxLabel.isEmpty()) return; m_checkBox->setText(isChecked() ? tr("True") : tr("False")); } diff --git a/src/qtpropertybrowserutils_p.h b/src/qtpropertybrowserutils_p.h index 24c6500..eb5d682 100644 --- a/src/qtpropertybrowserutils_p.h +++ b/src/qtpropertybrowserutils_p.h @@ -105,6 +105,9 @@ class QtBoolEdit : public QWidget { bool textVisible() const { return m_textVisible; } void setTextVisible(bool textVisible); + QString label() const { return m_checkBoxLabel; } + void setLabel(QString label); + Qt::CheckState checkState() const; void setCheckState(Qt::CheckState state); @@ -121,7 +124,10 @@ class QtBoolEdit : public QWidget { void paintEvent(QPaintEvent *); private: + void updateText(); + QCheckBox *m_checkBox; + QString m_checkBoxLabel; bool m_textVisible; }; diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index 39024e4..36143ef 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -1453,7 +1453,15 @@ class QtBoolPropertyManagerPrivate Q_DECLARE_PUBLIC(QtBoolPropertyManager) public: - QMap m_values; + struct Data + { + Data() : val(false), textVisible(true) {} + bool val; + bool textVisible; + }; + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; }; /*! @@ -1498,6 +1506,11 @@ QtBoolPropertyManager::~QtBoolPropertyManager() delete d_ptr; } +bool QtBoolPropertyManager::textVisible(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtBoolPropertyManagerPrivate::Data::textVisible, property, true); +} + /*! Returns the given \a property's value. @@ -1508,7 +1521,22 @@ QtBoolPropertyManager::~QtBoolPropertyManager() */ bool QtBoolPropertyManager::value(const QtProperty *property) const { - return d_ptr->m_values.value(property, false); + return getValue(d_ptr->m_values, property, false); +} + +void QtBoolPropertyManager::setTextVisible(QtProperty *property, bool textVisible) +{ + const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + QtBoolPropertyManagerPrivate::Data data = it.value(); + if (data.textVisible == textVisible) + return; + + data.textVisible = textVisible; + it.value() = data; + emit textVisibleChanged(property, data.textVisible); } /*! @@ -1516,13 +1544,13 @@ bool QtBoolPropertyManager::value(const QtProperty *property) const */ QString QtBoolPropertyManager::valueText(const QtProperty *property) const { - const QMap::const_iterator it = d_ptr->m_values.constFind(property); + const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); if (it == d_ptr->m_values.constEnd()) return QString(); static const QString trueText = tr("True"); static const QString falseText = tr("False"); - return it.value() ? trueText : falseText; + return it.value().val ? trueText : falseText; } // Return an icon containing a check box indicator @@ -1561,13 +1589,13 @@ static QIcon drawCheckBox(bool value) */ QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const { - const QMap::const_iterator it = d_ptr->m_values.constFind(property); + const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); if (it == d_ptr->m_values.constEnd()) return QIcon(); static const QIcon checkedIcon = drawCheckBox(true); static const QIcon uncheckedIcon = drawCheckBox(false); - return it.value() ? checkedIcon : uncheckedIcon; + return it.value().val ? checkedIcon : uncheckedIcon; } /*! @@ -1579,10 +1607,16 @@ QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const */ void QtBoolPropertyManager::setValue(QtProperty *property, bool val) { - setSimpleValue(d_ptr->m_values, this, - &QtBoolPropertyManager::propertyChanged, - &QtBoolPropertyManager::valueChanged, - property, val); + QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + QtBoolPropertyManagerPrivate::Data data = it.value(); + if (data.val == val) + return; + + data.val = val; + it.value() = data; + emit valueChanged(property, data.val); } /*! @@ -1590,7 +1624,7 @@ void QtBoolPropertyManager::setValue(QtProperty *property, bool val) */ void QtBoolPropertyManager::initializeProperty(QtProperty *property) { - d_ptr->m_values[property] = false; + d_ptr->m_values[property].val = false; } /*! diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index 413b555..53384b3 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -110,11 +110,14 @@ class QT_QTPROPERTYBROWSER_EXPORT QtBoolPropertyManager : public QtAbstractPrope ~QtBoolPropertyManager(); bool value(const QtProperty *property) const; + bool textVisible(const QtProperty *property) const; public Q_SLOTS: void setValue(QtProperty *property, bool val); + void setTextVisible(QtProperty *property, bool textVisible); Q_SIGNALS: void valueChanged(QtProperty *property, bool val); + void textVisibleChanged(QtProperty *property, bool textVisible); protected: QString valueText(const QtProperty *property) const; QIcon valueIcon(const QtProperty *property) const; diff --git a/src/qtvariantproperty.cpp b/src/qtvariantproperty.cpp index 42bb8e6..7c4f038 100644 --- a/src/qtvariantproperty.cpp +++ b/src/qtvariantproperty.cpp @@ -326,6 +326,7 @@ class QtVariantPropertyManagerPrivate void slotSingleStepChanged(QtProperty *property, double step); void slotDecimalsChanged(QtProperty *property, int prec); void slotValueChanged(QtProperty *property, bool val); + void slotTextVisibleChanged(QtProperty *property, bool textVisible); void slotValueChanged(QtProperty *property, const QString &val); void slotRegExpChanged(QtProperty *property, const QRegExp ®Exp); void slotMaxLengthChanged(QtProperty *property, int maxlen); @@ -375,6 +376,7 @@ class QtVariantPropertyManagerPrivate QMap m_internalToProperty; + const QString m_textVisibleAttribute; const QString m_constraintAttribute; const QString m_singleStepAttribute; const QString m_decimalsAttribute; @@ -388,6 +390,7 @@ class QtVariantPropertyManagerPrivate }; QtVariantPropertyManagerPrivate::QtVariantPropertyManagerPrivate() : + m_textVisibleAttribute(QLatin1String("textVisible")), m_constraintAttribute(QLatin1String("constraint")), m_singleStepAttribute(QLatin1String("singleStep")), m_decimalsAttribute(QLatin1String("decimals")), @@ -542,6 +545,12 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, boo valueChanged(property, QVariant(val)); } +void QtVariantPropertyManagerPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible) +{ + if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + emit q_ptr->attributeChanged(varProp, m_textVisibleAttribute, QVariant(textVisible)); +} + void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, const QString &val) { valueChanged(property, QVariant(val)); @@ -990,8 +999,12 @@ QtVariantPropertyManager::QtVariantPropertyManager(QObject *parent) QtBoolPropertyManager *boolPropertyManager = new QtBoolPropertyManager(this); d_ptr->m_typeToPropertyManager[QVariant::Bool] = boolPropertyManager; d_ptr->m_typeToValueType[QVariant::Bool] = QVariant::Bool; + d_ptr->m_typeToAttributeToAttributeType[QVariant::Bool][d_ptr->m_textVisibleAttribute] = + QVariant::Bool; connect(boolPropertyManager, SIGNAL(valueChanged(QtProperty *, bool)), this, SLOT(slotValueChanged(QtProperty *, bool))); + connect(boolPropertyManager, SIGNAL(textVisibleChanged(QtProperty *, bool)), + this, SLOT(slotTextVisibleChanged(QtProperty *, bool))); // StringPropertyManager QtStringPropertyManager *stringPropertyManager = new QtStringPropertyManager(this); d_ptr->m_typeToPropertyManager[QVariant::String] = stringPropertyManager; @@ -1536,6 +1549,10 @@ QVariant QtVariantPropertyManager::attributeValue(const QtProperty *property, co if (attribute == d_ptr->m_decimalsAttribute) return doubleManager->decimals(internProp); return QVariant(); + } else if (QtBoolPropertyManager *boolManager = qobject_cast(manager)) { + if (attribute == d_ptr->m_textVisibleAttribute) + return boolManager->textVisible(internProp); + return QVariant(); } else if (QtStringPropertyManager *stringManager = qobject_cast(manager)) { if (attribute == d_ptr->m_regExpAttribute) return stringManager->regExp(internProp); @@ -1781,6 +1798,10 @@ void QtVariantPropertyManager::setAttribute(QtProperty *property, if (attribute == d_ptr->m_decimalsAttribute) doubleManager->setDecimals(internProp, value.value()); return; + } else if (QtBoolPropertyManager *boolManager = qobject_cast(manager)) { + if (attribute == d_ptr->m_textVisibleAttribute) + boolManager->setTextVisible(internProp, value.value()); + return; } else if (QtStringPropertyManager *stringManager = qobject_cast(manager)) { if (attribute == d_ptr->m_regExpAttribute) stringManager->setRegExp(internProp, value.value()); diff --git a/src/qtvariantproperty.h b/src/qtvariantproperty.h index 9eb7cf9..fbc9fed 100644 --- a/src/qtvariantproperty.h +++ b/src/qtvariantproperty.h @@ -128,6 +128,7 @@ public Q_SLOTS: Q_PRIVATE_SLOT(d_func(), void slotSingleStepChanged(QtProperty *, double)) Q_PRIVATE_SLOT(d_func(), void slotDecimalsChanged(QtProperty *, int)) Q_PRIVATE_SLOT(d_func(), void slotValueChanged(QtProperty *, bool)) + Q_PRIVATE_SLOT(d_func(), void slotTextVisibleChanged(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 slotMaxLengthChanged(QtProperty *, int)) From ddc0fe991b95af09982f997c240d9601123a75ba Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sat, 12 Oct 2019 23:41:09 +0300 Subject: [PATCH 25/39] Fixed JSON sample and "groupbox" style - missing "else" that makes it be inavailable --- examples/json/json_settings_widget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index 50891e8..6cf34dd 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -638,7 +638,7 @@ QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidg title = layoutData["title"].toString(); if(style == "groupbox") gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent())); - if(style == "frame") + else if(style == "frame") gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent()), true); else if(style == "button") gui = new QtButtonPropertyBrowser(qobject_cast(parent())); From c3a0fc69fec3c4cb8fda0bae18d4f9b646434905 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 13 Oct 2019 00:01:10 +0300 Subject: [PATCH 26/39] Color chooser: always show alpha-channel --- src/qteditorfactory.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index e6aad8c..a738184 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -2292,7 +2292,9 @@ void QtColorEditWidget::setValue(const QColor &c) void QtColorEditWidget::buttonClicked() { QColor oldRgba = m_color; - QColor newRgba = QColorDialog::getColor(oldRgba, this); + QColor newRgba = QColorDialog::getColor(oldRgba, this, + QString(), + QColorDialog::ShowAlphaChannel); if (newRgba.isValid() && newRgba != oldRgba) { setValue(newRgba); emit valueChanged(m_color); From b8f2085dabea0270cf529f937c3cbd9f7b13ff1f Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Sun, 13 Oct 2019 14:29:03 +0300 Subject: [PATCH 27/39] Color picker: Disable native dialog usage - It has more functionality include picking a color from screen. On some systems it may lack some important things. --- src/qteditorfactory.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index a738184..46397e8 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -2294,7 +2294,8 @@ void QtColorEditWidget::buttonClicked() QColor oldRgba = m_color; QColor newRgba = QColorDialog::getColor(oldRgba, this, QString(), - QColorDialog::ShowAlphaChannel); + QColorDialog::ShowAlphaChannel| + QColorDialog::DontUseNativeDialog); if (newRgba.isValid() && newRgba != oldRgba) { setValue(newRgba); emit valueChanged(m_color); From b450861680c674dc9134cf55d232f2db9b2ca0da Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Mon, 14 Oct 2019 02:32:10 +0300 Subject: [PATCH 28/39] Frameless groupbox: don't remove frame from children --- src/qtgroupboxpropertybrowser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qtgroupboxpropertybrowser.cpp b/src/qtgroupboxpropertybrowser.cpp index 0c2e252..6f198b7 100644 --- a/src/qtgroupboxpropertybrowser.cpp +++ b/src/qtgroupboxpropertybrowser.cpp @@ -223,12 +223,12 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt if (hasHeader(par)) oldRow += 2; } - if(m_frameLess) + if(m_frameLess && !par) parentItem->groupBox = parentItem->groupBoxFrame = new QFrame(w); else parentItem->groupBox = parentItem->groupBoxGroup = new QGroupBox(w); parentItem->layout = new QGridLayout(); - if(m_frameLess) + if(m_frameLess && !par) parentItem->layout->setMargin(0); parentItem->groupBox->setLayout(parentItem->layout); if (parentItem->label) { From cde6a8f8b1638514be13c27817d58d924052a05f Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Mon, 14 Oct 2019 03:37:10 +0300 Subject: [PATCH 29/39] Begin of internal refactoring Split biggies into separated parts, they are VERY inconvenient to manage --- src/CMakeLists.txt | 23 +- src/managers/bool_property.cpp | 214 +++++ src/managers/bool_property.h | 46 ++ src/managers/common.cpp | 38 + src/managers/common.h | 320 ++++++++ src/managers/double_property.cpp | 350 ++++++++ src/managers/double_property.h | 53 ++ src/managers/dummy.cpp | 20 + src/managers/dummy.h | 19 + src/managers/group_property.cpp | 73 ++ src/managers/group_property.h | 30 + src/managers/int_property.cpp | 297 +++++++ src/managers/int_property.h | 51 ++ src/qtpropertymanager.cpp | 1294 ++---------------------------- src/qtpropertymanager.h | 117 +-- 15 files changed, 1594 insertions(+), 1351 deletions(-) create mode 100644 src/managers/bool_property.cpp create mode 100644 src/managers/bool_property.h create mode 100644 src/managers/common.cpp create mode 100644 src/managers/common.h create mode 100644 src/managers/double_property.cpp create mode 100644 src/managers/double_property.h create mode 100644 src/managers/dummy.cpp create mode 100644 src/managers/dummy.h create mode 100644 src/managers/group_property.cpp create mode 100644 src/managers/group_property.h create mode 100644 src/managers/int_property.cpp create mode 100644 src/managers/int_property.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ac70956..f648456 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,15 +6,20 @@ set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(_SRCS - qtbuttonpropertybrowser.cpp - qteditorfactory.cpp - qtgroupboxpropertybrowser.cpp - qtpropertybrowser.cpp - qtpropertybrowserutils.cpp - qtpropertymanager.cpp - qttreepropertybrowser.cpp - qtvariantproperty.cpp - ) + qtbuttonpropertybrowser.cpp + qteditorfactory.cpp + qtgroupboxpropertybrowser.cpp + qtpropertybrowser.cpp + qtpropertybrowserutils.cpp + qtpropertymanager.cpp + qttreepropertybrowser.cpp + qtvariantproperty.cpp + managers/group_property.cpp + managers/int_property.cpp + managers/double_property.cpp + managers/bool_property.cpp + managers/common.cpp +) file(GLOB _IMPL_HDRS *.h) file(GLOB _PUBLIC_HDRS Qt*) diff --git a/src/managers/bool_property.cpp b/src/managers/bool_property.cpp new file mode 100644 index 0000000..9944d0a --- /dev/null +++ b/src/managers/bool_property.cpp @@ -0,0 +1,214 @@ + +#include "bool_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// QtBoolPropertyManager + +class QtBoolPropertyManagerPrivate +{ + QtBoolPropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtBoolPropertyManager) +public: + + struct Data + { + Data() : val(false), textVisible(true) {} + bool val; + bool textVisible; + }; + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; +}; + +/*! + \class QtBoolPropertyManager + + \brief The QtBoolPropertyManager class provides and manages boolean properties. + + The property's value can be retrieved using the value() function, + and set using the setValue() slot. + + In addition, QtBoolPropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes. + + \sa QtAbstractPropertyManager, QtCheckBoxFactory +*/ + +/*! + \fn void QtBoolPropertyManager::valueChanged(QtProperty *property, bool value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the + new \a value as parameters. +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtBoolPropertyManager::QtBoolPropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtBoolPropertyManagerPrivate; + d_ptr->q_ptr = this; +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtBoolPropertyManager::~QtBoolPropertyManager() +{ + clear(); + delete d_ptr; +} + +bool QtBoolPropertyManager::textVisible(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtBoolPropertyManagerPrivate::Data::textVisible, property, true); +} + +/*! + Returns the given \a property's value. + + If the given \a property is not managed by \e this manager, this + function returns false. + + \sa setValue() +*/ +bool QtBoolPropertyManager::value(const QtProperty *property) const +{ + return getValue(d_ptr->m_values, property, false); +} + +void QtBoolPropertyManager::setTextVisible(QtProperty *property, bool textVisible) +{ + const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + QtBoolPropertyManagerPrivate::Data data = it.value(); + if (data.textVisible == textVisible) + return; + + data.textVisible = textVisible; + it.value() = data; + emit textVisibleChanged(property, data.textVisible); +} + +/*! + \reimp +*/ +QString QtBoolPropertyManager::valueText(const QtProperty *property) const +{ + const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + + static const QString trueText = tr("True"); + static const QString falseText = tr("False"); + return it.value().val ? trueText : falseText; +} + +// Return an icon containing a check box indicator +static QIcon drawCheckBox(bool value) +{ + QStyleOptionButton opt; + opt.state |= value ? QStyle::State_On : QStyle::State_Off; + opt.state |= QStyle::State_Enabled; + const QStyle *style = QApplication::style(); + // Figure out size of an indicator and make sure it is not scaled down in a list view item + // by making the pixmap as big as a list view icon and centering the indicator in it. + // (if it is smaller, it can't be helped) + const int indicatorWidth = style->pixelMetric(QStyle::PM_IndicatorWidth, &opt); + const int indicatorHeight = style->pixelMetric(QStyle::PM_IndicatorHeight, &opt); + const int listViewIconSize = indicatorWidth; + const int pixmapWidth = indicatorWidth; + const int pixmapHeight = qMax(indicatorHeight, listViewIconSize); + + opt.rect = QRect(0, 0, indicatorWidth, indicatorHeight); + QPixmap pixmap = QPixmap(pixmapWidth, pixmapHeight); + pixmap.fill(Qt::transparent); + { + // Center? + const int xoff = (pixmapWidth > indicatorWidth) ? (pixmapWidth - indicatorWidth) / 2 : 0; + const int yoff = (pixmapHeight > indicatorHeight) ? (pixmapHeight - indicatorHeight) / 2 : 0; + QPainter painter(&pixmap); + painter.translate(xoff, yoff); + QCheckBox cb; + style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, &painter, &cb); + } + return QIcon(pixmap); +} + +/*! + \reimp +*/ +QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const +{ + const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QIcon(); + + static const QIcon checkedIcon = drawCheckBox(true); + static const QIcon uncheckedIcon = drawCheckBox(false); + return it.value().val ? checkedIcon : uncheckedIcon; +} + +/*! + \fn void QtBoolPropertyManager::setValue(QtProperty *property, bool value) + + Sets the value of the given \a property to \a value. + + \sa value() +*/ +void QtBoolPropertyManager::setValue(QtProperty *property, bool val) +{ + QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + QtBoolPropertyManagerPrivate::Data data = it.value(); + if (data.val == val) + return; + + data.val = val; + it.value() = data; + emit valueChanged(property, data.val); +} + +/*! + \reimp +*/ +void QtBoolPropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property].val = false; +} + +/*! + \reimp +*/ +void QtBoolPropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/bool_property.h b/src/managers/bool_property.h new file mode 100644 index 0000000..6cfe8b4 --- /dev/null +++ b/src/managers/bool_property.h @@ -0,0 +1,46 @@ + +#ifndef QtBoolPropertyManager_H +#define QtBoolPropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QtBoolPropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtBoolPropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtBoolPropertyManager(QObject *parent = 0); + ~QtBoolPropertyManager(); + + bool value(const QtProperty *property) const; + bool textVisible(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, bool val); + void setTextVisible(QtProperty *property, bool textVisible); +Q_SIGNALS: + void valueChanged(QtProperty *property, bool val); + void textVisibleChanged(QtProperty *property, bool textVisible); +protected: + QString valueText(const QtProperty *property) const; + QIcon valueIcon(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtBoolPropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtBoolPropertyManager) + Q_DISABLE_COPY(QtBoolPropertyManager) +}; + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/managers/common.cpp b/src/managers/common.cpp new file mode 100644 index 0000000..91d1974 --- /dev/null +++ b/src/managers/common.cpp @@ -0,0 +1,38 @@ + +#include + +#include "common.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +namespace sizeFunks +{ + +void orderBorders(QSize &minVal, QSize &maxVal) +{ + orderSizeBorders(minVal, maxVal); +} + +void orderBorders(QSizeF &minVal, QSizeF &maxVal) +{ + orderSizeBorders(minVal, maxVal); +} + +} + +QSize qBound(QSize minVal, QSize val, QSize maxVal) +{ + return qBoundSize(minVal, val, maxVal); +} + +QSizeF qBound(QSizeF minVal, QSizeF val, QSizeF maxVal) +{ + return qBoundSize(minVal, val, maxVal); +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/common.h b/src/managers/common.h new file mode 100644 index 0000000..f51b77b --- /dev/null +++ b/src/managers/common.h @@ -0,0 +1,320 @@ +#include +#include +#include + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QtProperty; + +template +static void setSimpleMinimumData(PrivateData *data, const Value &minVal) +{ + data->minVal = minVal; + if (data->maxVal < data->minVal) + data->maxVal = data->minVal; + + if (data->val < data->minVal) + data->val = data->minVal; +} + +template +static void setSimpleMaximumData(PrivateData *data, const Value &maxVal) +{ + data->maxVal = maxVal; + if (data->minVal > data->maxVal) + data->minVal = data->maxVal; + + if (data->val > data->maxVal) + data->val = data->maxVal; +} + +template +static void setSizeMinimumData(PrivateData *data, const Value &newMinVal) +{ + data->minVal = newMinVal; + if (data->maxVal.width() < data->minVal.width()) + data->maxVal.setWidth(data->minVal.width()); + if (data->maxVal.height() < data->minVal.height()) + data->maxVal.setHeight(data->minVal.height()); + + if (data->val.width() < data->minVal.width()) + data->val.setWidth(data->minVal.width()); + if (data->val.height() < data->minVal.height()) + data->val.setHeight(data->minVal.height()); +} + +template +static void setSizeMaximumData(PrivateData *data, const Value &newMaxVal) +{ + data->maxVal = newMaxVal; + if (data->minVal.width() > data->maxVal.width()) + data->minVal.setWidth(data->maxVal.width()); + if (data->minVal.height() > data->maxVal.height()) + data->minVal.setHeight(data->maxVal.height()); + + if (data->val.width() > data->maxVal.width()) + data->val.setWidth(data->maxVal.width()); + if (data->val.height() > data->maxVal.height()) + data->val.setHeight(data->maxVal.height()); +} + +template +static SizeValue qBoundSize(const SizeValue &minVal, const SizeValue &val, const SizeValue &maxVal) +{ + SizeValue croppedVal = val; + if (minVal.width() > val.width()) + croppedVal.setWidth(minVal.width()); + else if (maxVal.width() < val.width()) + croppedVal.setWidth(maxVal.width()); + + if (minVal.height() > val.height()) + croppedVal.setHeight(minVal.height()); + else if (maxVal.height() < val.height()) + croppedVal.setHeight(maxVal.height()); + + return croppedVal; +} + + +// Match the exact signature of qBound for VS 6. +QSize qBound(QSize minVal, QSize val, QSize maxVal); +QSizeF qBound(QSizeF minVal, QSizeF val, QSizeF maxVal); + + +namespace sizeFunks +{ + +template +void orderBorders(Value &minVal, Value &maxVal) +{ + if (minVal > maxVal) + qSwap(minVal, maxVal); +} + +template +static void orderSizeBorders(Value &minVal, Value &maxVal) +{ + Value fromSize = minVal; + Value toSize = maxVal; + if (fromSize.width() > toSize.width()) { + fromSize.setWidth(maxVal.width()); + toSize.setWidth(minVal.width()); + } + if (fromSize.height() > toSize.height()) { + fromSize.setHeight(maxVal.height()); + toSize.setHeight(minVal.height()); + } + minVal = fromSize; + maxVal = toSize; +} + +void orderBorders(QSize &minVal, QSize &maxVal); + +void orderBorders(QSizeF &minVal, QSizeF &maxVal); + +} +//////// + +template +static Value getData(const QMap &propertyMap, + Value PrivateData::*data, + const QtProperty *property, const Value &defaultValue = Value()) +{ + typedef QMap PropertyToData; + typedef typename PropertyToData::const_iterator PropertyToDataConstIterator; + const PropertyToDataConstIterator it = propertyMap.constFind(property); + if (it == propertyMap.constEnd()) + return defaultValue; + return it.value().*data; +} + +template +static Value getValue(const QMap &propertyMap, + const QtProperty *property, const Value &defaultValue = Value()) +{ + return getData(propertyMap, &PrivateData::val, property, defaultValue); +} + +template +static Value getMinimum(const QMap &propertyMap, + const QtProperty *property, const Value &defaultValue = Value()) +{ + return getData(propertyMap, &PrivateData::minVal, property, defaultValue); +} + +template +static Value getMaximum(const QMap &propertyMap, + const QtProperty *property, const Value &defaultValue = Value()) +{ + return getData(propertyMap, &PrivateData::maxVal, property, defaultValue); +} + +template +static void setSimpleValue(QMap &propertyMap, + PropertyManager *manager, + void (PropertyManager::*propertyChangedSignal)(QtProperty *), + void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), + QtProperty *property, const Value &val) +{ + typedef QMap PropertyToData; + typedef typename PropertyToData::iterator PropertyToDataIterator; + const PropertyToDataIterator it = propertyMap.find(property); + if (it == propertyMap.end()) + return; + + if (it.value() == val) + return; + + it.value() = val; + + emit (manager->*propertyChangedSignal)(property); + emit (manager->*valueChangedSignal)(property, val); +} + +template +static void setValueInRange(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, + void (PropertyManager::*propertyChangedSignal)(QtProperty *), + void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), + QtProperty *property, const Value &val, + void (PropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, ValueChangeParameter)) +{ + typedef typename PropertyManagerPrivate::Data PrivateData; + typedef QMap PropertyToData; + typedef typename PropertyToData::iterator PropertyToDataIterator; + const PropertyToDataIterator it = managerPrivate->m_values.find(property); + if (it == managerPrivate->m_values.end()) + return; + + PrivateData &data = it.value(); + + if (data.val == val) + return; + + const Value oldVal = data.val; + + data.val = qBound(data.minVal, val, data.maxVal); + + if (data.val == oldVal) + return; + + if (setSubPropertyValue) + (managerPrivate->*setSubPropertyValue)(property, data.val); + + emit (manager->*propertyChangedSignal)(property); + emit (manager->*valueChangedSignal)(property, data.val); +} + +template +static void setBorderValues(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, + void (PropertyManager::*propertyChangedSignal)(QtProperty *), + void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), + void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), + QtProperty *property, const Value &minVal, const Value &maxVal, + void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, + ValueChangeParameter, ValueChangeParameter, ValueChangeParameter)) +{ + typedef typename PropertyManagerPrivate::Data PrivateData; + typedef QMap PropertyToData; + typedef typename PropertyToData::iterator PropertyToDataIterator; + const PropertyToDataIterator it = managerPrivate->m_values.find(property); + if (it == managerPrivate->m_values.end()) + return; + + Value fromVal = minVal; + Value toVal = maxVal; + sizeFunks::orderBorders(fromVal, toVal); + + PrivateData &data = it.value(); + + if (data.minVal == fromVal && data.maxVal == toVal) + return; + + const Value oldVal = data.val; + + data.setMinimumValue(fromVal); + data.setMaximumValue(toVal); + + emit (manager->*rangeChangedSignal)(property, data.minVal, data.maxVal); + + if (setSubPropertyRange) + (managerPrivate->*setSubPropertyRange)(property, data.minVal, data.maxVal, data.val); + + if (data.val == oldVal) + return; + + emit (manager->*propertyChangedSignal)(property); + emit (manager->*valueChangedSignal)(property, data.val); +} + +template +static void setBorderValue(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, + void (PropertyManager::*propertyChangedSignal)(QtProperty *), + void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), + void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), + QtProperty *property, + Value (PrivateData::*getRangeVal)() const, + void (PrivateData::*setRangeVal)(ValueChangeParameter), const Value &borderVal, + void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, + ValueChangeParameter, ValueChangeParameter, ValueChangeParameter)) +{ + typedef QMap PropertyToData; + typedef typename PropertyToData::iterator PropertyToDataIterator; + const PropertyToDataIterator it = managerPrivate->m_values.find(property); + if (it == managerPrivate->m_values.end()) + return; + + PrivateData &data = it.value(); + + if ((data.*getRangeVal)() == borderVal) + return; + + const Value oldVal = data.val; + + (data.*setRangeVal)(borderVal); + + emit (manager->*rangeChangedSignal)(property, data.minVal, data.maxVal); + + if (setSubPropertyRange) + (managerPrivate->*setSubPropertyRange)(property, data.minVal, data.maxVal, data.val); + + if (data.val == oldVal) + return; + + emit (manager->*propertyChangedSignal)(property); + emit (manager->*valueChangedSignal)(property, data.val); +} + +template +static void setMinimumValue(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, + void (PropertyManager::*propertyChangedSignal)(QtProperty *), + void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), + void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), + QtProperty *property, const Value &minVal) +{ + void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, + ValueChangeParameter, ValueChangeParameter, ValueChangeParameter) = 0; + setBorderValue(manager, managerPrivate, + propertyChangedSignal, valueChangedSignal, rangeChangedSignal, + property, &PropertyManagerPrivate::Data::minimumValue, &PropertyManagerPrivate::Data::setMinimumValue, minVal, setSubPropertyRange); +} + +template +static void setMaximumValue(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, + void (PropertyManager::*propertyChangedSignal)(QtProperty *), + void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), + void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), + QtProperty *property, const Value &maxVal) +{ + void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, + ValueChangeParameter, ValueChangeParameter, ValueChangeParameter) = 0; + setBorderValue(manager, managerPrivate, + propertyChangedSignal, valueChangedSignal, rangeChangedSignal, + property, &PropertyManagerPrivate::Data::maximumValue, &PropertyManagerPrivate::Data::setMaximumValue, maxVal, setSubPropertyRange); +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/double_property.cpp b/src/managers/double_property.cpp new file mode 100644 index 0000000..f86ddef --- /dev/null +++ b/src/managers/double_property.cpp @@ -0,0 +1,350 @@ + +#include "double_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include + + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// QtDoublePropertyManager + +class QtDoublePropertyManagerPrivate +{ + QtDoublePropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtDoublePropertyManager) +public: + + struct Data + { + Data() : val(0), minVal(-INT_MAX), maxVal(INT_MAX), singleStep(1), decimals(2) {} + double val; + double minVal; + double maxVal; + double singleStep; + int decimals; + double minimumValue() const { return minVal; } + double maximumValue() const { return maxVal; } + void setMinimumValue(double newMinVal) { setSimpleMinimumData(this, newMinVal); } + void setMaximumValue(double newMaxVal) { setSimpleMaximumData(this, newMaxVal); } + }; + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; +}; + +/*! + \class QtDoublePropertyManager + + \brief The QtDoublePropertyManager provides and manages double properties. + + A double property has a current value, and a range specifying the + valid values. The range is defined by a minimum and a maximum + value. + + The property's value and range can be retrieved using the value(), + minimum() and maximum() functions, and can be set using the + setValue(), setMinimum() and setMaximum() slots. + Alternatively, the range can be defined in one go using the + setRange() slot. + + In addition, QtDoublePropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes, and the rangeChanged() signal which is emitted whenever + such a property changes its range of valid values. + + \sa QtAbstractPropertyManager, QtDoubleSpinBoxFactory +*/ + +/*! + \fn void QtDoublePropertyManager::valueChanged(QtProperty *property, double value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the new + \a value as parameters. + + \sa setValue() +*/ + +/*! + \fn void QtDoublePropertyManager::rangeChanged(QtProperty *property, double minimum, double maximum) + + This signal is emitted whenever a property created by this manager + changes its range of valid values, passing a pointer to the + \a property and the new \a minimum and \a maximum values + + \sa setRange() +*/ + +/*! + \fn void QtDoublePropertyManager::decimalsChanged(QtProperty *property, int prec) + + This signal is emitted whenever a property created by this manager + changes its precision of value, passing a pointer to the + \a property and the new \a prec value + + \sa setDecimals() +*/ + +/*! + \fn void QtDoublePropertyManager::singleStepChanged(QtProperty *property, double step) + + This signal is emitted whenever a property created by this manager + changes its single step property, passing a pointer to the + \a property and the new \a step value + + \sa setSingleStep() +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtDoublePropertyManager::QtDoublePropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtDoublePropertyManagerPrivate; + d_ptr->q_ptr = this; +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtDoublePropertyManager::~QtDoublePropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the given \a property's value. + + If the given property is not managed by this manager, this + function returns 0. + + \sa setValue() +*/ +double QtDoublePropertyManager::value(const QtProperty *property) const +{ + return getValue(d_ptr->m_values, property, 0.0); +} + +/*! + Returns the given \a property's minimum value. + + \sa maximum(), setRange() +*/ +double QtDoublePropertyManager::minimum(const QtProperty *property) const +{ + return getMinimum(d_ptr->m_values, property, 0.0); +} + +/*! + Returns the given \a property's maximum value. + + \sa minimum(), setRange() +*/ +double QtDoublePropertyManager::maximum(const QtProperty *property) const +{ + return getMaximum(d_ptr->m_values, property, 0.0); +} + +/*! + Returns the given \a property's step value. + + The step is typically used to increment or decrement a property value while pressing an arrow key. + + \sa setSingleStep() +*/ +double QtDoublePropertyManager::singleStep(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtDoublePropertyManagerPrivate::Data::singleStep, property, 0); +} + +/*! + Returns the given \a property's precision, in decimals. + + \sa setDecimals() +*/ +int QtDoublePropertyManager::decimals(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtDoublePropertyManagerPrivate::Data::decimals, property, 0); +} + +/*! + \reimp +*/ +QString QtDoublePropertyManager::valueText(const QtProperty *property) const +{ + const QtDoublePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + return QString::number(it.value().val, 'f', it.value().decimals); +} + +/*! + \fn void QtDoublePropertyManager::setValue(QtProperty *property, double value) + + Sets the value of the given \a property to \a value. + + If the specified \a value is not valid according to the given + \a property's range, the \a value is adjusted to the nearest valid value + within the range. + + \sa value(), setRange(), valueChanged() +*/ +void QtDoublePropertyManager::setValue(QtProperty *property, double val) +{ + void (QtDoublePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, double) = 0; + setValueInRange(this, d_ptr, + &QtDoublePropertyManager::propertyChanged, + &QtDoublePropertyManager::valueChanged, + property, val, setSubPropertyValue); +} + +/*! + Sets the step value for the given \a property to \a step. + + The step is typically used to increment or decrement a property value while pressing an arrow key. + + \sa singleStep() +*/ +void QtDoublePropertyManager::setSingleStep(QtProperty *property, double step) +{ + const QtDoublePropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + QtDoublePropertyManagerPrivate::Data data = it.value(); + + if (step < 0) + step = 0; + + if (data.singleStep == step) + return; + + data.singleStep = step; + + it.value() = data; + + emit singleStepChanged(property, data.singleStep); +} + +/*! + \fn void QtDoublePropertyManager::setDecimals(QtProperty *property, int prec) + + Sets the precision of the given \a property to \a prec. + + The valid decimal range is 0-13. The default is 2. + + \sa decimals() +*/ +void QtDoublePropertyManager::setDecimals(QtProperty *property, int prec) +{ + const QtDoublePropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + QtDoublePropertyManagerPrivate::Data data = it.value(); + + if (prec > 13) + prec = 13; + else if (prec < 0) + prec = 0; + + if (data.decimals == prec) + return; + + data.decimals = prec; + + it.value() = data; + + emit decimalsChanged(property, data.decimals); +} + +/*! + Sets the minimum value for the given \a property to \a minVal. + + When setting the minimum value, the maximum and current values are + adjusted if necessary (ensuring that the range remains valid and + that the current value is within in the range). + + \sa minimum(), setRange(), rangeChanged() +*/ +void QtDoublePropertyManager::setMinimum(QtProperty *property, double minVal) +{ + setMinimumValue(this, d_ptr, + &QtDoublePropertyManager::propertyChanged, + &QtDoublePropertyManager::valueChanged, + &QtDoublePropertyManager::rangeChanged, + property, minVal); +} + +/*! + Sets the maximum value for the given \a property to \a maxVal. + + When setting the maximum value, the minimum and current values are + adjusted if necessary (ensuring that the range remains valid and + that the current value is within in the range). + + \sa maximum(), setRange(), rangeChanged() +*/ +void QtDoublePropertyManager::setMaximum(QtProperty *property, double maxVal) +{ + setMaximumValue(this, d_ptr, + &QtDoublePropertyManager::propertyChanged, + &QtDoublePropertyManager::valueChanged, + &QtDoublePropertyManager::rangeChanged, + property, maxVal); +} + +/*! + \fn void QtDoublePropertyManager::setRange(QtProperty *property, double minimum, double maximum) + + Sets the range of valid values. + + This is a convenience function defining the range of valid values + in one go; setting the \a minimum and \a maximum values for the + given \a property with a single function call. + + When setting a new range, the current value is adjusted if + necessary (ensuring that the value remains within range). + + \sa setMinimum(), setMaximum(), rangeChanged() +*/ +void QtDoublePropertyManager::setRange(QtProperty *property, double minVal, double maxVal) +{ + void (QtDoublePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, double, double, double) = 0; + setBorderValues(this, d_ptr, + &QtDoublePropertyManager::propertyChanged, + &QtDoublePropertyManager::valueChanged, + &QtDoublePropertyManager::rangeChanged, + property, minVal, maxVal, setSubPropertyRange); +} + +/*! + \reimp +*/ +void QtDoublePropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property] = QtDoublePropertyManagerPrivate::Data(); +} + +/*! + \reimp +*/ +void QtDoublePropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/double_property.h b/src/managers/double_property.h new file mode 100644 index 0000000..bdfc1f9 --- /dev/null +++ b/src/managers/double_property.h @@ -0,0 +1,53 @@ + +#ifndef QtDoublePropertyManager_H +#define QtDoublePropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QtDoublePropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtDoublePropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtDoublePropertyManager(QObject *parent = 0); + ~QtDoublePropertyManager(); + + double value(const QtProperty *property) const; + double minimum(const QtProperty *property) const; + double maximum(const QtProperty *property) const; + double singleStep(const QtProperty *property) const; + int decimals(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, double val); + void setMinimum(QtProperty *property, double minVal); + void setMaximum(QtProperty *property, double maxVal); + void setRange(QtProperty *property, double minVal, double maxVal); + void setSingleStep(QtProperty *property, double step); + void setDecimals(QtProperty *property, int prec); +Q_SIGNALS: + void valueChanged(QtProperty *property, double val); + void rangeChanged(QtProperty *property, double minVal, double maxVal); + void singleStepChanged(QtProperty *property, double step); + void decimalsChanged(QtProperty *property, int prec); +protected: + QString valueText(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtDoublePropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtDoublePropertyManager) + Q_DISABLE_COPY(QtDoublePropertyManager) +}; + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/managers/dummy.cpp b/src/managers/dummy.cpp new file mode 100644 index 0000000..2310ae9 --- /dev/null +++ b/src/managers/dummy.cpp @@ -0,0 +1,20 @@ + +#include "dummy.h" + +#include + + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// PUT HERE + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/dummy.h b/src/managers/dummy.h new file mode 100644 index 0000000..79a16de --- /dev/null +++ b/src/managers/dummy.h @@ -0,0 +1,19 @@ + +#ifndef QtGroupPropertyManager_H +#define QtGroupPropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// PUT HERE + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/managers/group_property.cpp b/src/managers/group_property.cpp new file mode 100644 index 0000000..39048ef --- /dev/null +++ b/src/managers/group_property.cpp @@ -0,0 +1,73 @@ + +#include "group_property.h" + +#include + + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// QtGroupPropertyManager + +/*! + \class QtGroupPropertyManager + + \brief The QtGroupPropertyManager provides and manages group properties. + + This class is intended to provide a grouping element without any value. + + \sa QtAbstractPropertyManager +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtGroupPropertyManager::QtGroupPropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtGroupPropertyManager::~QtGroupPropertyManager() +{ + +} + +/*! + \reimp +*/ +bool QtGroupPropertyManager::hasValue(const QtProperty *property) const +{ + Q_UNUSED(property) + return false; +} + +/*! + \reimp +*/ +void QtGroupPropertyManager::initializeProperty(QtProperty *property) +{ + Q_UNUSED(property) +} + +/*! + \reimp +*/ +void QtGroupPropertyManager::uninitializeProperty(QtProperty *property) +{ + Q_UNUSED(property) +} + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/group_property.h b/src/managers/group_property.h new file mode 100644 index 0000000..06efdb4 --- /dev/null +++ b/src/managers/group_property.h @@ -0,0 +1,30 @@ + +#ifndef QtGroupPropertyManager_H +#define QtGroupPropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QT_QTPROPERTYBROWSER_EXPORT QtGroupPropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtGroupPropertyManager(QObject *parent = 0); + ~QtGroupPropertyManager(); + +protected: + virtual bool hasValue(const QtProperty *property) const; + + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +}; + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/managers/int_property.cpp b/src/managers/int_property.cpp new file mode 100644 index 0000000..fbbe32e --- /dev/null +++ b/src/managers/int_property.cpp @@ -0,0 +1,297 @@ + +#include "int_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include + + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + + +// QtIntPropertyManager + +class QtIntPropertyManagerPrivate +{ + QtIntPropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtIntPropertyManager) +public: + + struct Data + { + Data() : val(0), minVal(-INT_MAX), maxVal(INT_MAX), singleStep(1) {} + int val; + int minVal; + int maxVal; + int singleStep; + int minimumValue() const { return minVal; } + int maximumValue() const { return maxVal; } + void setMinimumValue(int newMinVal) { setSimpleMinimumData(this, newMinVal); } + void setMaximumValue(int newMaxVal) { setSimpleMaximumData(this, newMaxVal); } + }; + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; +}; + +/*! + \class QtIntPropertyManager + + \brief The QtIntPropertyManager provides and manages int properties. + + An int property has a current value, and a range specifying the + valid values. The range is defined by a minimum and a maximum + value. + + The property's value and range can be retrieved using the value(), + minimum() and maximum() functions, and can be set using the + setValue(), setMinimum() and setMaximum() slots. Alternatively, + the range can be defined in one go using the setRange() slot. + + In addition, QtIntPropertyManager provides the valueChanged() signal which + is emitted whenever a property created by this manager changes, + and the rangeChanged() signal which is emitted whenever such a + property changes its range of valid values. + + \sa QtAbstractPropertyManager, QtSpinBoxFactory, QtSliderFactory, QtScrollBarFactory +*/ + +/*! + \fn void QtIntPropertyManager::valueChanged(QtProperty *property, int value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the new + \a value as parameters. + + \sa setValue() +*/ + +/*! + \fn void QtIntPropertyManager::rangeChanged(QtProperty *property, int minimum, int maximum) + + This signal is emitted whenever a property created by this manager + changes its range of valid values, passing a pointer to the + \a property and the new \a minimum and \a maximum values. + + \sa setRange() +*/ + +/*! + \fn void QtIntPropertyManager::singleStepChanged(QtProperty *property, int step) + + This signal is emitted whenever a property created by this manager + changes its single step property, passing a pointer to the + \a property and the new \a step value + + \sa setSingleStep() +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtIntPropertyManager::QtIntPropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtIntPropertyManagerPrivate; + d_ptr->q_ptr = this; +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtIntPropertyManager::~QtIntPropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the given \a property's value. + + If the given property is not managed by this manager, this + function returns 0. + + \sa setValue() +*/ +int QtIntPropertyManager::value(const QtProperty *property) const +{ + return getValue(d_ptr->m_values, property, 0); +} + +/*! + Returns the given \a property's minimum value. + + \sa setMinimum(), maximum(), setRange() +*/ +int QtIntPropertyManager::minimum(const QtProperty *property) const +{ + return getMinimum(d_ptr->m_values, property, 0); +} + +/*! + Returns the given \a property's maximum value. + + \sa setMaximum(), minimum(), setRange() +*/ +int QtIntPropertyManager::maximum(const QtProperty *property) const +{ + return getMaximum(d_ptr->m_values, property, 0); +} + +/*! + Returns the given \a property's step value. + + The step is typically used to increment or decrement a property value while pressing an arrow key. + + \sa setSingleStep() +*/ +int QtIntPropertyManager::singleStep(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtIntPropertyManagerPrivate::Data::singleStep, property, 0); +} + +/*! + \reimp +*/ +QString QtIntPropertyManager::valueText(const QtProperty *property) const +{ + const QtIntPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + return QString::number(it.value().val); +} + +/*! + \fn void QtIntPropertyManager::setValue(QtProperty *property, int value) + + Sets the value of the given \a property to \a value. + + If the specified \a value is not valid according to the given \a + property's range, the \a value is adjusted to the nearest valid + value within the range. + + \sa value(), setRange(), valueChanged() +*/ +void QtIntPropertyManager::setValue(QtProperty *property, int val) +{ + void (QtIntPropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, int) = 0; + setValueInRange(this, d_ptr, + &QtIntPropertyManager::propertyChanged, + &QtIntPropertyManager::valueChanged, + property, val, setSubPropertyValue); +} + +/*! + Sets the minimum value for the given \a property to \a minVal. + + When setting the minimum value, the maximum and current values are + adjusted if necessary (ensuring that the range remains valid and + that the current value is within the range). + + \sa minimum(), setRange(), rangeChanged() +*/ +void QtIntPropertyManager::setMinimum(QtProperty *property, int minVal) +{ + setMinimumValue(this, d_ptr, + &QtIntPropertyManager::propertyChanged, + &QtIntPropertyManager::valueChanged, + &QtIntPropertyManager::rangeChanged, + property, minVal); +} + +/*! + Sets the maximum value for the given \a property to \a maxVal. + + When setting maximum value, the minimum and current values are + adjusted if necessary (ensuring that the range remains valid and + that the current value is within the range). + + \sa maximum(), setRange(), rangeChanged() +*/ +void QtIntPropertyManager::setMaximum(QtProperty *property, int maxVal) +{ + setMaximumValue(this, d_ptr, + &QtIntPropertyManager::propertyChanged, + &QtIntPropertyManager::valueChanged, + &QtIntPropertyManager::rangeChanged, + property, maxVal); +} + +/*! + \fn void QtIntPropertyManager::setRange(QtProperty *property, int minimum, int maximum) + + Sets the range of valid values. + + This is a convenience function defining the range of valid values + in one go; setting the \a minimum and \a maximum values for the + given \a property with a single function call. + + When setting a new range, the current value is adjusted if + necessary (ensuring that the value remains within range). + + \sa setMinimum(), setMaximum(), rangeChanged() +*/ +void QtIntPropertyManager::setRange(QtProperty *property, int minVal, int maxVal) +{ + void (QtIntPropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, int, int, int) = 0; + setBorderValues(this, d_ptr, + &QtIntPropertyManager::propertyChanged, + &QtIntPropertyManager::valueChanged, + &QtIntPropertyManager::rangeChanged, + property, minVal, maxVal, setSubPropertyRange); +} + +/*! + Sets the step value for the given \a property to \a step. + + The step is typically used to increment or decrement a property value while pressing an arrow key. + + \sa singleStep() +*/ +void QtIntPropertyManager::setSingleStep(QtProperty *property, int step) +{ + const QtIntPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + QtIntPropertyManagerPrivate::Data data = it.value(); + + if (step < 0) + step = 0; + + if (data.singleStep == step) + return; + + data.singleStep = step; + + it.value() = data; + + emit singleStepChanged(property, data.singleStep); +} + +/*! + \reimp +*/ +void QtIntPropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property] = QtIntPropertyManagerPrivate::Data(); +} + +/*! + \reimp +*/ +void QtIntPropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/int_property.h b/src/managers/int_property.h new file mode 100644 index 0000000..16bb898 --- /dev/null +++ b/src/managers/int_property.h @@ -0,0 +1,51 @@ + +#ifndef QtIntPropertyManager_H +#define QtIntPropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QtIntPropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtIntPropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtIntPropertyManager(QObject *parent = 0); + ~QtIntPropertyManager(); + + int value(const QtProperty *property) const; + int minimum(const QtProperty *property) const; + int maximum(const QtProperty *property) const; + int singleStep(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, int val); + void setMinimum(QtProperty *property, int minVal); + void setMaximum(QtProperty *property, int maxVal); + void setRange(QtProperty *property, int minVal, int maxVal); + void setSingleStep(QtProperty *property, int step); +Q_SIGNALS: + void valueChanged(QtProperty *property, int val); + void rangeChanged(QtProperty *property, int minVal, int maxVal); + void singleStepChanged(QtProperty *property, int step); +protected: + QString valueText(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtIntPropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtIntPropertyManager) + Q_DISABLE_COPY(QtIntPropertyManager) +}; + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index 36143ef..3212cc7 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -40,6 +40,7 @@ #include "qtpropertymanager.h" #include "qtpropertybrowserutils_p.h" +#include "managers/common.h" #include #include #include @@ -65,323 +66,6 @@ QT_BEGIN_NAMESPACE #endif -template -static void setSimpleMinimumData(PrivateData *data, const Value &minVal) -{ - data->minVal = minVal; - if (data->maxVal < data->minVal) - data->maxVal = data->minVal; - - if (data->val < data->minVal) - data->val = data->minVal; -} - -template -static void setSimpleMaximumData(PrivateData *data, const Value &maxVal) -{ - data->maxVal = maxVal; - if (data->minVal > data->maxVal) - data->minVal = data->maxVal; - - if (data->val > data->maxVal) - data->val = data->maxVal; -} - -template -static void setSizeMinimumData(PrivateData *data, const Value &newMinVal) -{ - data->minVal = newMinVal; - if (data->maxVal.width() < data->minVal.width()) - data->maxVal.setWidth(data->minVal.width()); - if (data->maxVal.height() < data->minVal.height()) - data->maxVal.setHeight(data->minVal.height()); - - if (data->val.width() < data->minVal.width()) - data->val.setWidth(data->minVal.width()); - if (data->val.height() < data->minVal.height()) - data->val.setHeight(data->minVal.height()); -} - -template -static void setSizeMaximumData(PrivateData *data, const Value &newMaxVal) -{ - data->maxVal = newMaxVal; - if (data->minVal.width() > data->maxVal.width()) - data->minVal.setWidth(data->maxVal.width()); - if (data->minVal.height() > data->maxVal.height()) - data->minVal.setHeight(data->maxVal.height()); - - if (data->val.width() > data->maxVal.width()) - data->val.setWidth(data->maxVal.width()); - if (data->val.height() > data->maxVal.height()) - data->val.setHeight(data->maxVal.height()); -} - -template -static SizeValue qBoundSize(const SizeValue &minVal, const SizeValue &val, const SizeValue &maxVal) -{ - SizeValue croppedVal = val; - if (minVal.width() > val.width()) - croppedVal.setWidth(minVal.width()); - else if (maxVal.width() < val.width()) - croppedVal.setWidth(maxVal.width()); - - if (minVal.height() > val.height()) - croppedVal.setHeight(minVal.height()); - else if (maxVal.height() < val.height()) - croppedVal.setHeight(maxVal.height()); - - return croppedVal; -} - -// Match the exact signature of qBound for VS 6. -QSize qBound(QSize minVal, QSize val, QSize maxVal) -{ - return qBoundSize(minVal, val, maxVal); -} - -QSizeF qBound(QSizeF minVal, QSizeF val, QSizeF maxVal) -{ - return qBoundSize(minVal, val, maxVal); -} - -namespace { - -namespace { -template -void orderBorders(Value &minVal, Value &maxVal) -{ - if (minVal > maxVal) - qSwap(minVal, maxVal); -} - -template -static void orderSizeBorders(Value &minVal, Value &maxVal) -{ - Value fromSize = minVal; - Value toSize = maxVal; - if (fromSize.width() > toSize.width()) { - fromSize.setWidth(maxVal.width()); - toSize.setWidth(minVal.width()); - } - if (fromSize.height() > toSize.height()) { - fromSize.setHeight(maxVal.height()); - toSize.setHeight(minVal.height()); - } - minVal = fromSize; - maxVal = toSize; -} - -void orderBorders(QSize &minVal, QSize &maxVal) -{ - orderSizeBorders(minVal, maxVal); -} - -void orderBorders(QSizeF &minVal, QSizeF &maxVal) -{ - orderSizeBorders(minVal, maxVal); -} - -} -} -//////// - -template -static Value getData(const QMap &propertyMap, - Value PrivateData::*data, - const QtProperty *property, const Value &defaultValue = Value()) -{ - typedef QMap PropertyToData; - typedef typename PropertyToData::const_iterator PropertyToDataConstIterator; - const PropertyToDataConstIterator it = propertyMap.constFind(property); - if (it == propertyMap.constEnd()) - return defaultValue; - return it.value().*data; -} - -template -static Value getValue(const QMap &propertyMap, - const QtProperty *property, const Value &defaultValue = Value()) -{ - return getData(propertyMap, &PrivateData::val, property, defaultValue); -} - -template -static Value getMinimum(const QMap &propertyMap, - const QtProperty *property, const Value &defaultValue = Value()) -{ - return getData(propertyMap, &PrivateData::minVal, property, defaultValue); -} - -template -static Value getMaximum(const QMap &propertyMap, - const QtProperty *property, const Value &defaultValue = Value()) -{ - return getData(propertyMap, &PrivateData::maxVal, property, defaultValue); -} - -template -static void setSimpleValue(QMap &propertyMap, - PropertyManager *manager, - void (PropertyManager::*propertyChangedSignal)(QtProperty *), - void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), - QtProperty *property, const Value &val) -{ - typedef QMap PropertyToData; - typedef typename PropertyToData::iterator PropertyToDataIterator; - const PropertyToDataIterator it = propertyMap.find(property); - if (it == propertyMap.end()) - return; - - if (it.value() == val) - return; - - it.value() = val; - - emit (manager->*propertyChangedSignal)(property); - emit (manager->*valueChangedSignal)(property, val); -} - -template -static void setValueInRange(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, - void (PropertyManager::*propertyChangedSignal)(QtProperty *), - void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), - QtProperty *property, const Value &val, - void (PropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, ValueChangeParameter)) -{ - typedef typename PropertyManagerPrivate::Data PrivateData; - typedef QMap PropertyToData; - typedef typename PropertyToData::iterator PropertyToDataIterator; - const PropertyToDataIterator it = managerPrivate->m_values.find(property); - if (it == managerPrivate->m_values.end()) - return; - - PrivateData &data = it.value(); - - if (data.val == val) - return; - - const Value oldVal = data.val; - - data.val = qBound(data.minVal, val, data.maxVal); - - if (data.val == oldVal) - return; - - if (setSubPropertyValue) - (managerPrivate->*setSubPropertyValue)(property, data.val); - - emit (manager->*propertyChangedSignal)(property); - emit (manager->*valueChangedSignal)(property, data.val); -} - -template -static void setBorderValues(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, - void (PropertyManager::*propertyChangedSignal)(QtProperty *), - void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), - void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), - QtProperty *property, const Value &minVal, const Value &maxVal, - void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, - ValueChangeParameter, ValueChangeParameter, ValueChangeParameter)) -{ - typedef typename PropertyManagerPrivate::Data PrivateData; - typedef QMap PropertyToData; - typedef typename PropertyToData::iterator PropertyToDataIterator; - const PropertyToDataIterator it = managerPrivate->m_values.find(property); - if (it == managerPrivate->m_values.end()) - return; - - Value fromVal = minVal; - Value toVal = maxVal; - orderBorders(fromVal, toVal); - - PrivateData &data = it.value(); - - if (data.minVal == fromVal && data.maxVal == toVal) - return; - - const Value oldVal = data.val; - - data.setMinimumValue(fromVal); - data.setMaximumValue(toVal); - - emit (manager->*rangeChangedSignal)(property, data.minVal, data.maxVal); - - if (setSubPropertyRange) - (managerPrivate->*setSubPropertyRange)(property, data.minVal, data.maxVal, data.val); - - if (data.val == oldVal) - return; - - emit (manager->*propertyChangedSignal)(property); - emit (manager->*valueChangedSignal)(property, data.val); -} - -template -static void setBorderValue(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, - void (PropertyManager::*propertyChangedSignal)(QtProperty *), - void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), - void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), - QtProperty *property, - Value (PrivateData::*getRangeVal)() const, - void (PrivateData::*setRangeVal)(ValueChangeParameter), const Value &borderVal, - void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, - ValueChangeParameter, ValueChangeParameter, ValueChangeParameter)) -{ - typedef QMap PropertyToData; - typedef typename PropertyToData::iterator PropertyToDataIterator; - const PropertyToDataIterator it = managerPrivate->m_values.find(property); - if (it == managerPrivate->m_values.end()) - return; - - PrivateData &data = it.value(); - - if ((data.*getRangeVal)() == borderVal) - return; - - const Value oldVal = data.val; - - (data.*setRangeVal)(borderVal); - - emit (manager->*rangeChangedSignal)(property, data.minVal, data.maxVal); - - if (setSubPropertyRange) - (managerPrivate->*setSubPropertyRange)(property, data.minVal, data.maxVal, data.val); - - if (data.val == oldVal) - return; - - emit (manager->*propertyChangedSignal)(property); - emit (manager->*valueChangedSignal)(property, data.val); -} - -template -static void setMinimumValue(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, - void (PropertyManager::*propertyChangedSignal)(QtProperty *), - void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), - void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), - QtProperty *property, const Value &minVal) -{ - void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, - ValueChangeParameter, ValueChangeParameter, ValueChangeParameter) = 0; - setBorderValue(manager, managerPrivate, - propertyChangedSignal, valueChangedSignal, rangeChangedSignal, - property, &PropertyManagerPrivate::Data::minimumValue, &PropertyManagerPrivate::Data::setMinimumValue, minVal, setSubPropertyRange); -} - -template -static void setMaximumValue(PropertyManager *manager, PropertyManagerPrivate *managerPrivate, - void (PropertyManager::*propertyChangedSignal)(QtProperty *), - void (PropertyManager::*valueChangedSignal)(QtProperty *, ValueChangeParameter), - void (PropertyManager::*rangeChangedSignal)(QtProperty *, ValueChangeParameter, ValueChangeParameter), - QtProperty *property, const Value &maxVal) -{ - void (PropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, - ValueChangeParameter, ValueChangeParameter, ValueChangeParameter) = 0; - setBorderValue(manager, managerPrivate, - propertyChangedSignal, valueChangedSignal, rangeChangedSignal, - property, &PropertyManagerPrivate::Data::maximumValue, &PropertyManagerPrivate::Data::setMaximumValue, maxVal, setSubPropertyRange); -} class QtMetaEnumWrapper : public QObject { @@ -559,151 +243,83 @@ void QtMetaEnumProvider::localeToIndex(QLocale::Language language, QLocale::Coun Q_GLOBAL_STATIC(QtMetaEnumProvider, metaEnumProvider) -// QtGroupPropertyManager - -/*! - \class QtGroupPropertyManager - - \brief The QtGroupPropertyManager provides and manages group properties. - - This class is intended to provide a grouping element without any value. - - \sa QtAbstractPropertyManager -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtGroupPropertyManager::QtGroupPropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtGroupPropertyManager::~QtGroupPropertyManager() -{ - -} - -/*! - \reimp -*/ -bool QtGroupPropertyManager::hasValue(const QtProperty *property) const -{ - Q_UNUSED(property) - return false; -} - -/*! - \reimp -*/ -void QtGroupPropertyManager::initializeProperty(QtProperty *property) -{ - Q_UNUSED(property) -} - -/*! - \reimp -*/ -void QtGroupPropertyManager::uninitializeProperty(QtProperty *property) -{ - Q_UNUSED(property) -} -// QtIntPropertyManager +// QtStringPropertyManager -class QtIntPropertyManagerPrivate +class QtStringPropertyManagerPrivate { - QtIntPropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtIntPropertyManager) + QtStringPropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtStringPropertyManager) public: struct Data { - Data() : val(0), minVal(-INT_MAX), maxVal(INT_MAX), singleStep(1) {} - int val; - int minVal; - int maxVal; - int singleStep; - int minimumValue() const { return minVal; } - int maximumValue() const { return maxVal; } - void setMinimumValue(int newMinVal) { setSimpleMinimumData(this, newMinVal); } - void setMaximumValue(int newMaxVal) { setSimpleMaximumData(this, newMaxVal); } + Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard), maxLen(-1) + { + } + QString val; + QRegExp regExp; + int maxLen; }; typedef QMap PropertyValueMap; - PropertyValueMap m_values; + QMap m_values; }; /*! - \class QtIntPropertyManager + \class QtStringPropertyManager - \brief The QtIntPropertyManager provides and manages int properties. + \brief The QtStringPropertyManager provides and manages QString properties. - An int property has a current value, and a range specifying the - valid values. The range is defined by a minimum and a maximum - value. + A string property's value can be retrieved using the value() + function, and set using the setValue() slot. - The property's value and range can be retrieved using the value(), - minimum() and maximum() functions, and can be set using the - setValue(), setMinimum() and setMaximum() slots. Alternatively, - the range can be defined in one go using the setRange() slot. + The current value can be checked against a regular expression. To + set the regular expression use the setRegExp() slot, use the + regExp() function to retrieve the currently set expression. - In addition, QtIntPropertyManager provides the valueChanged() signal which - is emitted whenever a property created by this manager changes, - and the rangeChanged() signal which is emitted whenever such a - property changes its range of valid values. + In addition, QtStringPropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes, and the regExpChanged() signal which is emitted whenever + such a property changes its currently set regular expression. - \sa QtAbstractPropertyManager, QtSpinBoxFactory, QtSliderFactory, QtScrollBarFactory + \sa QtAbstractPropertyManager, QtLineEditFactory */ /*! - \fn void QtIntPropertyManager::valueChanged(QtProperty *property, int value) + \fn void QtStringPropertyManager::valueChanged(QtProperty *property, const QString &value) This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the new - \a value as parameters. + changes its value, passing a pointer to the \a property and the + new \a value as parameters. \sa setValue() */ /*! - \fn void QtIntPropertyManager::rangeChanged(QtProperty *property, int minimum, int maximum) - - This signal is emitted whenever a property created by this manager - changes its range of valid values, passing a pointer to the - \a property and the new \a minimum and \a maximum values. - - \sa setRange() -*/ - -/*! - \fn void QtIntPropertyManager::singleStepChanged(QtProperty *property, int step) + \fn void QtStringPropertyManager::regExpChanged(QtProperty *property, const QRegExp ®Exp) This signal is emitted whenever a property created by this manager - changes its single step property, passing a pointer to the - \a property and the new \a step value + changes its currenlty set regular expression, passing a pointer to + the \a property and the new \a regExp as parameters. - \sa setSingleStep() + \sa setRegExp() */ /*! Creates a manager with the given \a parent. */ -QtIntPropertyManager::QtIntPropertyManager(QObject *parent) +QtStringPropertyManager::QtStringPropertyManager(QObject *parent) : QtAbstractPropertyManager(parent) { - d_ptr = new QtIntPropertyManagerPrivate; + d_ptr = new QtStringPropertyManagerPrivate; d_ptr->q_ptr = this; } /*! Destroys this manager, and all the properties it has created. */ -QtIntPropertyManager::~QtIntPropertyManager() +QtStringPropertyManager::~QtStringPropertyManager() { clear(); delete d_ptr; @@ -713,666 +329,75 @@ QtIntPropertyManager::~QtIntPropertyManager() Returns the given \a property's value. If the given property is not managed by this manager, this - function returns 0. + function returns an empty string. \sa setValue() */ -int QtIntPropertyManager::value(const QtProperty *property) const +QString QtStringPropertyManager::value(const QtProperty *property) const { - return getValue(d_ptr->m_values, property, 0); + return getValue(d_ptr->m_values, property); } /*! - Returns the given \a property's minimum value. - - \sa setMinimum(), maximum(), setRange() -*/ -int QtIntPropertyManager::minimum(const QtProperty *property) const -{ - return getMinimum(d_ptr->m_values, property, 0); -} + Returns the given \a property's currently set regular expression. -/*! - Returns the given \a property's maximum value. + If the given \a property is not managed by this manager, this + function returns an empty expression. - \sa setMaximum(), minimum(), setRange() + \sa setRegExp() */ -int QtIntPropertyManager::maximum(const QtProperty *property) const +QRegExp QtStringPropertyManager::regExp(const QtProperty *property) const { - return getMaximum(d_ptr->m_values, property, 0); + return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::regExp, property, QRegExp()); } /*! - Returns the given \a property's step value. + Returns the given \a property's currently set maximum length. - The step is typically used to increment or decrement a property value while pressing an arrow key. + If the given \a property is not managed by this manager, this + function returns an empty expression. - \sa setSingleStep() + \sa setMaxLength() */ -int QtIntPropertyManager::singleStep(const QtProperty *property) const +int QtStringPropertyManager::maxLength(const QtProperty *property) const { - return getData(d_ptr->m_values, &QtIntPropertyManagerPrivate::Data::singleStep, property, 0); + return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::maxLen, property, -1); } /*! \reimp */ -QString QtIntPropertyManager::valueText(const QtProperty *property) const +QString QtStringPropertyManager::valueText(const QtProperty *property) const { - const QtIntPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + const QtStringPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); if (it == d_ptr->m_values.constEnd()) return QString(); - return QString::number(it.value().val); + return it.value().val; } /*! - \fn void QtIntPropertyManager::setValue(QtProperty *property, int value) + \fn void QtStringPropertyManager::setValue(QtProperty *property, const QString &value) Sets the value of the given \a property to \a value. - If the specified \a value is not valid according to the given \a - property's range, the \a value is adjusted to the nearest valid - value within the range. + If the specified \a value doesn't match the given \a property's + regular expression, this function does nothing. - \sa value(), setRange(), valueChanged() + \sa value(), setRegExp(), valueChanged() */ -void QtIntPropertyManager::setValue(QtProperty *property, int val) +void QtStringPropertyManager::setValue(QtProperty *property, const QString &val) { - void (QtIntPropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, int) = 0; - setValueInRange(this, d_ptr, - &QtIntPropertyManager::propertyChanged, - &QtIntPropertyManager::valueChanged, - property, val, setSubPropertyValue); -} + const QtStringPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; -/*! - Sets the minimum value for the given \a property to \a minVal. + QtStringPropertyManagerPrivate::Data data = it.value(); - When setting the minimum value, the maximum and current values are - adjusted if necessary (ensuring that the range remains valid and - that the current value is within the range). + if (data.val == val) + return; - \sa minimum(), setRange(), rangeChanged() -*/ -void QtIntPropertyManager::setMinimum(QtProperty *property, int minVal) -{ - setMinimumValue(this, d_ptr, - &QtIntPropertyManager::propertyChanged, - &QtIntPropertyManager::valueChanged, - &QtIntPropertyManager::rangeChanged, - property, minVal); -} - -/*! - Sets the maximum value for the given \a property to \a maxVal. - - When setting maximum value, the minimum and current values are - adjusted if necessary (ensuring that the range remains valid and - that the current value is within the range). - - \sa maximum(), setRange(), rangeChanged() -*/ -void QtIntPropertyManager::setMaximum(QtProperty *property, int maxVal) -{ - setMaximumValue(this, d_ptr, - &QtIntPropertyManager::propertyChanged, - &QtIntPropertyManager::valueChanged, - &QtIntPropertyManager::rangeChanged, - property, maxVal); -} - -/*! - \fn void QtIntPropertyManager::setRange(QtProperty *property, int minimum, int maximum) - - Sets the range of valid values. - - This is a convenience function defining the range of valid values - in one go; setting the \a minimum and \a maximum values for the - given \a property with a single function call. - - When setting a new range, the current value is adjusted if - necessary (ensuring that the value remains within range). - - \sa setMinimum(), setMaximum(), rangeChanged() -*/ -void QtIntPropertyManager::setRange(QtProperty *property, int minVal, int maxVal) -{ - void (QtIntPropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, int, int, int) = 0; - setBorderValues(this, d_ptr, - &QtIntPropertyManager::propertyChanged, - &QtIntPropertyManager::valueChanged, - &QtIntPropertyManager::rangeChanged, - property, minVal, maxVal, setSubPropertyRange); -} - -/*! - Sets the step value for the given \a property to \a step. - - The step is typically used to increment or decrement a property value while pressing an arrow key. - - \sa singleStep() -*/ -void QtIntPropertyManager::setSingleStep(QtProperty *property, int step) -{ - const QtIntPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); - if (it == d_ptr->m_values.end()) - return; - - QtIntPropertyManagerPrivate::Data data = it.value(); - - if (step < 0) - step = 0; - - if (data.singleStep == step) - return; - - data.singleStep = step; - - it.value() = data; - - emit singleStepChanged(property, data.singleStep); -} - -/*! - \reimp -*/ -void QtIntPropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property] = QtIntPropertyManagerPrivate::Data(); -} - -/*! - \reimp -*/ -void QtIntPropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} - -// QtDoublePropertyManager - -class QtDoublePropertyManagerPrivate -{ - QtDoublePropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtDoublePropertyManager) -public: - - struct Data - { - Data() : val(0), minVal(-INT_MAX), maxVal(INT_MAX), singleStep(1), decimals(2) {} - double val; - double minVal; - double maxVal; - double singleStep; - int decimals; - double minimumValue() const { return minVal; } - double maximumValue() const { return maxVal; } - void setMinimumValue(double newMinVal) { setSimpleMinimumData(this, newMinVal); } - void setMaximumValue(double newMaxVal) { setSimpleMaximumData(this, newMaxVal); } - }; - - typedef QMap PropertyValueMap; - PropertyValueMap m_values; -}; - -/*! - \class QtDoublePropertyManager - - \brief The QtDoublePropertyManager provides and manages double properties. - - A double property has a current value, and a range specifying the - valid values. The range is defined by a minimum and a maximum - value. - - The property's value and range can be retrieved using the value(), - minimum() and maximum() functions, and can be set using the - setValue(), setMinimum() and setMaximum() slots. - Alternatively, the range can be defined in one go using the - setRange() slot. - - In addition, QtDoublePropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes, and the rangeChanged() signal which is emitted whenever - such a property changes its range of valid values. - - \sa QtAbstractPropertyManager, QtDoubleSpinBoxFactory -*/ - -/*! - \fn void QtDoublePropertyManager::valueChanged(QtProperty *property, double value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the new - \a value as parameters. - - \sa setValue() -*/ - -/*! - \fn void QtDoublePropertyManager::rangeChanged(QtProperty *property, double minimum, double maximum) - - This signal is emitted whenever a property created by this manager - changes its range of valid values, passing a pointer to the - \a property and the new \a minimum and \a maximum values - - \sa setRange() -*/ - -/*! - \fn void QtDoublePropertyManager::decimalsChanged(QtProperty *property, int prec) - - This signal is emitted whenever a property created by this manager - changes its precision of value, passing a pointer to the - \a property and the new \a prec value - - \sa setDecimals() -*/ - -/*! - \fn void QtDoublePropertyManager::singleStepChanged(QtProperty *property, double step) - - This signal is emitted whenever a property created by this manager - changes its single step property, passing a pointer to the - \a property and the new \a step value - - \sa setSingleStep() -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtDoublePropertyManager::QtDoublePropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtDoublePropertyManagerPrivate; - d_ptr->q_ptr = this; -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtDoublePropertyManager::~QtDoublePropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the given \a property's value. - - If the given property is not managed by this manager, this - function returns 0. - - \sa setValue() -*/ -double QtDoublePropertyManager::value(const QtProperty *property) const -{ - return getValue(d_ptr->m_values, property, 0.0); -} - -/*! - Returns the given \a property's minimum value. - - \sa maximum(), setRange() -*/ -double QtDoublePropertyManager::minimum(const QtProperty *property) const -{ - return getMinimum(d_ptr->m_values, property, 0.0); -} - -/*! - Returns the given \a property's maximum value. - - \sa minimum(), setRange() -*/ -double QtDoublePropertyManager::maximum(const QtProperty *property) const -{ - return getMaximum(d_ptr->m_values, property, 0.0); -} - -/*! - Returns the given \a property's step value. - - The step is typically used to increment or decrement a property value while pressing an arrow key. - - \sa setSingleStep() -*/ -double QtDoublePropertyManager::singleStep(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtDoublePropertyManagerPrivate::Data::singleStep, property, 0); -} - -/*! - Returns the given \a property's precision, in decimals. - - \sa setDecimals() -*/ -int QtDoublePropertyManager::decimals(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtDoublePropertyManagerPrivate::Data::decimals, property, 0); -} - -/*! - \reimp -*/ -QString QtDoublePropertyManager::valueText(const QtProperty *property) const -{ - const QtDoublePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QString(); - return QString::number(it.value().val, 'f', it.value().decimals); -} - -/*! - \fn void QtDoublePropertyManager::setValue(QtProperty *property, double value) - - Sets the value of the given \a property to \a value. - - If the specified \a value is not valid according to the given - \a property's range, the \a value is adjusted to the nearest valid value - within the range. - - \sa value(), setRange(), valueChanged() -*/ -void QtDoublePropertyManager::setValue(QtProperty *property, double val) -{ - void (QtDoublePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, double) = 0; - setValueInRange(this, d_ptr, - &QtDoublePropertyManager::propertyChanged, - &QtDoublePropertyManager::valueChanged, - property, val, setSubPropertyValue); -} - -/*! - Sets the step value for the given \a property to \a step. - - The step is typically used to increment or decrement a property value while pressing an arrow key. - - \sa singleStep() -*/ -void QtDoublePropertyManager::setSingleStep(QtProperty *property, double step) -{ - const QtDoublePropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); - if (it == d_ptr->m_values.end()) - return; - - QtDoublePropertyManagerPrivate::Data data = it.value(); - - if (step < 0) - step = 0; - - if (data.singleStep == step) - return; - - data.singleStep = step; - - it.value() = data; - - emit singleStepChanged(property, data.singleStep); -} - -/*! - \fn void QtDoublePropertyManager::setDecimals(QtProperty *property, int prec) - - Sets the precision of the given \a property to \a prec. - - The valid decimal range is 0-13. The default is 2. - - \sa decimals() -*/ -void QtDoublePropertyManager::setDecimals(QtProperty *property, int prec) -{ - const QtDoublePropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); - if (it == d_ptr->m_values.end()) - return; - - QtDoublePropertyManagerPrivate::Data data = it.value(); - - if (prec > 13) - prec = 13; - else if (prec < 0) - prec = 0; - - if (data.decimals == prec) - return; - - data.decimals = prec; - - it.value() = data; - - emit decimalsChanged(property, data.decimals); -} - -/*! - Sets the minimum value for the given \a property to \a minVal. - - When setting the minimum value, the maximum and current values are - adjusted if necessary (ensuring that the range remains valid and - that the current value is within in the range). - - \sa minimum(), setRange(), rangeChanged() -*/ -void QtDoublePropertyManager::setMinimum(QtProperty *property, double minVal) -{ - setMinimumValue(this, d_ptr, - &QtDoublePropertyManager::propertyChanged, - &QtDoublePropertyManager::valueChanged, - &QtDoublePropertyManager::rangeChanged, - property, minVal); -} - -/*! - Sets the maximum value for the given \a property to \a maxVal. - - When setting the maximum value, the minimum and current values are - adjusted if necessary (ensuring that the range remains valid and - that the current value is within in the range). - - \sa maximum(), setRange(), rangeChanged() -*/ -void QtDoublePropertyManager::setMaximum(QtProperty *property, double maxVal) -{ - setMaximumValue(this, d_ptr, - &QtDoublePropertyManager::propertyChanged, - &QtDoublePropertyManager::valueChanged, - &QtDoublePropertyManager::rangeChanged, - property, maxVal); -} - -/*! - \fn void QtDoublePropertyManager::setRange(QtProperty *property, double minimum, double maximum) - - Sets the range of valid values. - - This is a convenience function defining the range of valid values - in one go; setting the \a minimum and \a maximum values for the - given \a property with a single function call. - - When setting a new range, the current value is adjusted if - necessary (ensuring that the value remains within range). - - \sa setMinimum(), setMaximum(), rangeChanged() -*/ -void QtDoublePropertyManager::setRange(QtProperty *property, double minVal, double maxVal) -{ - void (QtDoublePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, double, double, double) = 0; - setBorderValues(this, d_ptr, - &QtDoublePropertyManager::propertyChanged, - &QtDoublePropertyManager::valueChanged, - &QtDoublePropertyManager::rangeChanged, - property, minVal, maxVal, setSubPropertyRange); -} - -/*! - \reimp -*/ -void QtDoublePropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property] = QtDoublePropertyManagerPrivate::Data(); -} - -/*! - \reimp -*/ -void QtDoublePropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} - -// QtStringPropertyManager - -class QtStringPropertyManagerPrivate -{ - QtStringPropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtStringPropertyManager) -public: - - struct Data - { - Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard), maxLen(-1) - { - } - QString val; - QRegExp regExp; - int maxLen; - }; - - typedef QMap PropertyValueMap; - QMap m_values; -}; - -/*! - \class QtStringPropertyManager - - \brief The QtStringPropertyManager provides and manages QString properties. - - A string property's value can be retrieved using the value() - function, and set using the setValue() slot. - - The current value can be checked against a regular expression. To - set the regular expression use the setRegExp() slot, use the - regExp() function to retrieve the currently set expression. - - In addition, QtStringPropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes, and the regExpChanged() signal which is emitted whenever - such a property changes its currently set regular expression. - - \sa QtAbstractPropertyManager, QtLineEditFactory -*/ - -/*! - \fn void QtStringPropertyManager::valueChanged(QtProperty *property, const QString &value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the - new \a value as parameters. - - \sa setValue() -*/ - -/*! - \fn void QtStringPropertyManager::regExpChanged(QtProperty *property, const QRegExp ®Exp) - - This signal is emitted whenever a property created by this manager - changes its currenlty set regular expression, passing a pointer to - the \a property and the new \a regExp as parameters. - - \sa setRegExp() -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtStringPropertyManager::QtStringPropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtStringPropertyManagerPrivate; - d_ptr->q_ptr = this; -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtStringPropertyManager::~QtStringPropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the given \a property's value. - - If the given property is not managed by this manager, this - function returns an empty string. - - \sa setValue() -*/ -QString QtStringPropertyManager::value(const QtProperty *property) const -{ - return getValue(d_ptr->m_values, property); -} - -/*! - Returns the given \a property's currently set regular expression. - - If the given \a property is not managed by this manager, this - function returns an empty expression. - - \sa setRegExp() -*/ -QRegExp QtStringPropertyManager::regExp(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::regExp, property, QRegExp()); -} - -/*! - Returns the given \a property's currently set maximum length. - - If the given \a property is not managed by this manager, this - function returns an empty expression. - - \sa setMaxLength() -*/ -int QtStringPropertyManager::maxLength(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::maxLen, property, -1); -} - -/*! - \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; -} - -/*! - \fn void QtStringPropertyManager::setValue(QtProperty *property, const QString &value) - - Sets the value of the given \a property to \a value. - - If the specified \a value doesn't match the given \a property's - regular expression, this function does nothing. - - \sa value(), setRegExp(), valueChanged() -*/ -void QtStringPropertyManager::setValue(QtProperty *property, const QString &val) -{ - 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.val == val) - return; - - if ((data.maxLen > 0) && (val.length() > data.maxLen)) - return; + if ((data.maxLen > 0) && (val.length() > data.maxLen)) + return; if (data.regExp.isValid() && !data.regExp.exactMatch(val)) return; @@ -1445,195 +470,6 @@ void QtStringPropertyManager::uninitializeProperty(QtProperty *property) d_ptr->m_values.remove(property); } -// QtBoolPropertyManager - -class QtBoolPropertyManagerPrivate -{ - QtBoolPropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtBoolPropertyManager) -public: - - struct Data - { - Data() : val(false), textVisible(true) {} - bool val; - bool textVisible; - }; - - typedef QMap PropertyValueMap; - PropertyValueMap m_values; -}; - -/*! - \class QtBoolPropertyManager - - \brief The QtBoolPropertyManager class provides and manages boolean properties. - - The property's value can be retrieved using the value() function, - and set using the setValue() slot. - - In addition, QtBoolPropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes. - - \sa QtAbstractPropertyManager, QtCheckBoxFactory -*/ - -/*! - \fn void QtBoolPropertyManager::valueChanged(QtProperty *property, bool value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the - new \a value as parameters. -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtBoolPropertyManager::QtBoolPropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtBoolPropertyManagerPrivate; - d_ptr->q_ptr = this; -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtBoolPropertyManager::~QtBoolPropertyManager() -{ - clear(); - delete d_ptr; -} - -bool QtBoolPropertyManager::textVisible(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtBoolPropertyManagerPrivate::Data::textVisible, property, true); -} - -/*! - Returns the given \a property's value. - - If the given \a property is not managed by \e this manager, this - function returns false. - - \sa setValue() -*/ -bool QtBoolPropertyManager::value(const QtProperty *property) const -{ - return getValue(d_ptr->m_values, property, false); -} - -void QtBoolPropertyManager::setTextVisible(QtProperty *property, bool textVisible) -{ - const QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); - if (it == d_ptr->m_values.end()) - return; - - QtBoolPropertyManagerPrivate::Data data = it.value(); - if (data.textVisible == textVisible) - return; - - data.textVisible = textVisible; - it.value() = data; - emit textVisibleChanged(property, data.textVisible); -} - -/*! - \reimp -*/ -QString QtBoolPropertyManager::valueText(const QtProperty *property) const -{ - const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QString(); - - static const QString trueText = tr("True"); - static const QString falseText = tr("False"); - return it.value().val ? trueText : falseText; -} - -// Return an icon containing a check box indicator -static QIcon drawCheckBox(bool value) -{ - QStyleOptionButton opt; - opt.state |= value ? QStyle::State_On : QStyle::State_Off; - opt.state |= QStyle::State_Enabled; - const QStyle *style = QApplication::style(); - // Figure out size of an indicator and make sure it is not scaled down in a list view item - // by making the pixmap as big as a list view icon and centering the indicator in it. - // (if it is smaller, it can't be helped) - const int indicatorWidth = style->pixelMetric(QStyle::PM_IndicatorWidth, &opt); - const int indicatorHeight = style->pixelMetric(QStyle::PM_IndicatorHeight, &opt); - const int listViewIconSize = indicatorWidth; - const int pixmapWidth = indicatorWidth; - const int pixmapHeight = qMax(indicatorHeight, listViewIconSize); - - opt.rect = QRect(0, 0, indicatorWidth, indicatorHeight); - QPixmap pixmap = QPixmap(pixmapWidth, pixmapHeight); - pixmap.fill(Qt::transparent); - { - // Center? - const int xoff = (pixmapWidth > indicatorWidth) ? (pixmapWidth - indicatorWidth) / 2 : 0; - const int yoff = (pixmapHeight > indicatorHeight) ? (pixmapHeight - indicatorHeight) / 2 : 0; - QPainter painter(&pixmap); - painter.translate(xoff, yoff); - QCheckBox cb; - style->drawPrimitive(QStyle::PE_IndicatorCheckBox, &opt, &painter, &cb); - } - return QIcon(pixmap); -} - -/*! - \reimp -*/ -QIcon QtBoolPropertyManager::valueIcon(const QtProperty *property) const -{ - const QtBoolPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QIcon(); - - static const QIcon checkedIcon = drawCheckBox(true); - static const QIcon uncheckedIcon = drawCheckBox(false); - return it.value().val ? checkedIcon : uncheckedIcon; -} - -/*! - \fn void QtBoolPropertyManager::setValue(QtProperty *property, bool value) - - Sets the value of the given \a property to \a value. - - \sa value() -*/ -void QtBoolPropertyManager::setValue(QtProperty *property, bool val) -{ - QtBoolPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); - if (it == d_ptr->m_values.end()) - return; - QtBoolPropertyManagerPrivate::Data data = it.value(); - if (data.val == val) - return; - - data.val = val; - it.value() = data; - emit valueChanged(property, data.val); -} - -/*! - \reimp -*/ -void QtBoolPropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property].val = false; -} - -/*! - \reimp -*/ -void QtBoolPropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} // QtDatePropertyManager diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index 53384b3..b99919a 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -42,6 +42,10 @@ #define QTPROPERTYMANAGER_H #include "qtpropertybrowser.h" +#include "managers/group_property.h" +#include "managers/int_property.h" +#include "managers/double_property.h" +#include "managers/bool_property.h" #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -52,119 +56,6 @@ class QTime; class QDateTime; class QLocale; -class QT_QTPROPERTYBROWSER_EXPORT QtGroupPropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtGroupPropertyManager(QObject *parent = 0); - ~QtGroupPropertyManager(); - -protected: - virtual bool hasValue(const QtProperty *property) const; - - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -}; - -class QtIntPropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtIntPropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtIntPropertyManager(QObject *parent = 0); - ~QtIntPropertyManager(); - - int value(const QtProperty *property) const; - int minimum(const QtProperty *property) const; - int maximum(const QtProperty *property) const; - int singleStep(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, int val); - void setMinimum(QtProperty *property, int minVal); - void setMaximum(QtProperty *property, int maxVal); - void setRange(QtProperty *property, int minVal, int maxVal); - void setSingleStep(QtProperty *property, int step); -Q_SIGNALS: - void valueChanged(QtProperty *property, int val); - void rangeChanged(QtProperty *property, int minVal, int maxVal); - void singleStepChanged(QtProperty *property, int step); -protected: - QString valueText(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtIntPropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtIntPropertyManager) - Q_DISABLE_COPY(QtIntPropertyManager) -}; - -class QtBoolPropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtBoolPropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtBoolPropertyManager(QObject *parent = 0); - ~QtBoolPropertyManager(); - - bool value(const QtProperty *property) const; - bool textVisible(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, bool val); - void setTextVisible(QtProperty *property, bool textVisible); -Q_SIGNALS: - void valueChanged(QtProperty *property, bool val); - void textVisibleChanged(QtProperty *property, bool textVisible); -protected: - QString valueText(const QtProperty *property) const; - QIcon valueIcon(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtBoolPropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtBoolPropertyManager) - Q_DISABLE_COPY(QtBoolPropertyManager) -}; - -class QtDoublePropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtDoublePropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtDoublePropertyManager(QObject *parent = 0); - ~QtDoublePropertyManager(); - - double value(const QtProperty *property) const; - double minimum(const QtProperty *property) const; - double maximum(const QtProperty *property) const; - double singleStep(const QtProperty *property) const; - int decimals(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, double val); - void setMinimum(QtProperty *property, double minVal); - void setMaximum(QtProperty *property, double maxVal); - void setRange(QtProperty *property, double minVal, double maxVal); - void setSingleStep(QtProperty *property, double step); - void setDecimals(QtProperty *property, int prec); -Q_SIGNALS: - void valueChanged(QtProperty *property, double val); - void rangeChanged(QtProperty *property, double minVal, double maxVal); - void singleStepChanged(QtProperty *property, double step); - void decimalsChanged(QtProperty *property, int prec); -protected: - QString valueText(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtDoublePropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtDoublePropertyManager) - Q_DISABLE_COPY(QtDoublePropertyManager) -}; class QtStringPropertyManagerPrivate; From fa83ec7889fa523a567136e9686675c9de04bbdf Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 22 Dec 2019 00:56:10 +0300 Subject: [PATCH 30/39] Move color property into separated file --- src/CMakeLists.txt | 1 + src/managers/color_property.cpp | 316 ++++++++++++++++++++++++++++++++ src/managers/color_property.h | 49 +++++ src/qtpropertymanager.cpp | 275 --------------------------- src/qtpropertymanager.h | 30 +-- 5 files changed, 367 insertions(+), 304 deletions(-) create mode 100644 src/managers/color_property.cpp create mode 100644 src/managers/color_property.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f648456..fa04032 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ set(_SRCS qtpropertymanager.cpp qttreepropertybrowser.cpp qtvariantproperty.cpp + managers/color_property.cpp managers/group_property.cpp managers/int_property.cpp managers/double_property.cpp diff --git a/src/managers/color_property.cpp b/src/managers/color_property.cpp new file mode 100644 index 0000000..3a5fea2 --- /dev/null +++ b/src/managers/color_property.cpp @@ -0,0 +1,316 @@ + +#include "color_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + + +// QtColorPropertyManager + +class QtColorPropertyManagerPrivate +{ + QtColorPropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtColorPropertyManager) +public: + + void slotIntChanged(QtProperty *property, int value); + void slotPropertyDestroyed(QtProperty *property); + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; + + QtIntPropertyManager *m_intPropertyManager; + + QMap m_propertyToR; + QMap m_propertyToG; + QMap m_propertyToB; + QMap m_propertyToA; + + QMap m_rToProperty; + QMap m_gToProperty; + QMap m_bToProperty; + QMap m_aToProperty; +}; + +void QtColorPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) +{ + if (QtProperty *prop = m_rToProperty.value(property, 0)) { + QColor c = m_values[prop]; + c.setRed(value); + q_ptr->setValue(prop, c); + } else if (QtProperty *prop = m_gToProperty.value(property, 0)) { + QColor c = m_values[prop]; + c.setGreen(value); + q_ptr->setValue(prop, c); + } else if (QtProperty *prop = m_bToProperty.value(property, 0)) { + QColor c = m_values[prop]; + c.setBlue(value); + q_ptr->setValue(prop, c); + } else if (QtProperty *prop = m_aToProperty.value(property, 0)) { + QColor c = m_values[prop]; + c.setAlpha(value); + q_ptr->setValue(prop, c); + } +} + +void QtColorPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) +{ + if (QtProperty *pointProp = m_rToProperty.value(property, 0)) { + m_propertyToR[pointProp] = 0; + m_rToProperty.remove(property); + } else if (QtProperty *pointProp = m_gToProperty.value(property, 0)) { + m_propertyToG[pointProp] = 0; + m_gToProperty.remove(property); + } else if (QtProperty *pointProp = m_bToProperty.value(property, 0)) { + m_propertyToB[pointProp] = 0; + m_bToProperty.remove(property); + } else if (QtProperty *pointProp = m_aToProperty.value(property, 0)) { + m_propertyToA[pointProp] = 0; + m_aToProperty.remove(property); + } +} + +/*! + \class QtColorPropertyManager + + \brief The QtColorPropertyManager provides and manages QColor properties. + + A color property has nested \e red, \e green and \e blue + subproperties. The top-level property's value can be retrieved + using the value() function, and set using the setValue() slot. + + The subproperties are created by a QtIntPropertyManager object. This + manager can be retrieved using the subIntPropertyManager() function. In + order to provide editing widgets for the subproperties in a + property browser widget, this manager must be associated with an + editor factory. + + In addition, QtColorPropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes. + + \sa QtAbstractPropertyManager, QtAbstractPropertyBrowser, QtIntPropertyManager +*/ + +/*! + \fn void QtColorPropertyManager::valueChanged(QtProperty *property, const QColor &value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the new + \a value as parameters. + + \sa setValue() +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtColorPropertyManager::QtColorPropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtColorPropertyManagerPrivate; + d_ptr->q_ptr = this; + + d_ptr->m_intPropertyManager = new QtIntPropertyManager(this); + connect(d_ptr->m_intPropertyManager, SIGNAL(valueChanged(QtProperty *, int)), + this, SLOT(slotIntChanged(QtProperty *, int))); + + connect(d_ptr->m_intPropertyManager, SIGNAL(propertyDestroyed(QtProperty *)), + this, SLOT(slotPropertyDestroyed(QtProperty *))); +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtColorPropertyManager::~QtColorPropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the manager that produces the nested \e red, \e green and + \e blue subproperties. + + In order to provide editing widgets for the subproperties in a + property browser widget, this manager must be associated with an + editor factory. + + \sa QtAbstractPropertyBrowser::setFactoryForManager() +*/ +QtIntPropertyManager *QtColorPropertyManager::subIntPropertyManager() const +{ + return d_ptr->m_intPropertyManager; +} + +/*! + Returns the given \a property's value. + + If the given \a property is not managed by \e this manager, this + function returns an invalid color. + + \sa setValue() +*/ +QColor QtColorPropertyManager::value(const QtProperty *property) const +{ + return d_ptr->m_values.value(property, QColor()); +} + +/*! + \reimp +*/ + +QString QtColorPropertyManager::valueText(const QtProperty *property) const +{ + const QtColorPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + + return QtPropertyBrowserUtils::colorValueText(it.value()); +} + +/*! + \reimp +*/ + +QIcon QtColorPropertyManager::valueIcon(const QtProperty *property) const +{ + const QtColorPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QIcon(); + return QtPropertyBrowserUtils::brushValueIcon(QBrush(it.value())); +} + +/*! + \fn void QtColorPropertyManager::setValue(QtProperty *property, const QColor &value) + + Sets the value of the given \a property to \a value. Nested + properties are updated automatically. + + \sa value(), valueChanged() +*/ +void QtColorPropertyManager::setValue(QtProperty *property, const QColor &val) +{ + const QtColorPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); + if (it == d_ptr->m_values.end()) + return; + + if (it.value() == val) + return; + + it.value() = val; + + d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToR[property], val.red()); + d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToG[property], val.green()); + d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToB[property], val.blue()); + d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToA[property], val.alpha()); + + emit propertyChanged(property); + emit valueChanged(property, val); +} + +/*! + \reimp +*/ +void QtColorPropertyManager::initializeProperty(QtProperty *property) +{ + QColor val; + d_ptr->m_values[property] = val; + + QtProperty *rProp = d_ptr->m_intPropertyManager->addProperty(); + rProp->setPropertyName(tr("Red")); + d_ptr->m_intPropertyManager->setValue(rProp, val.red()); + d_ptr->m_intPropertyManager->setRange(rProp, 0, 0xFF); + d_ptr->m_propertyToR[property] = rProp; + d_ptr->m_rToProperty[rProp] = property; + property->addSubProperty(rProp); + + QtProperty *gProp = d_ptr->m_intPropertyManager->addProperty(); + gProp->setPropertyName(tr("Green")); + d_ptr->m_intPropertyManager->setValue(gProp, val.green()); + d_ptr->m_intPropertyManager->setRange(gProp, 0, 0xFF); + d_ptr->m_propertyToG[property] = gProp; + d_ptr->m_gToProperty[gProp] = property; + property->addSubProperty(gProp); + + QtProperty *bProp = d_ptr->m_intPropertyManager->addProperty(); + bProp->setPropertyName(tr("Blue")); + d_ptr->m_intPropertyManager->setValue(bProp, val.blue()); + d_ptr->m_intPropertyManager->setRange(bProp, 0, 0xFF); + d_ptr->m_propertyToB[property] = bProp; + d_ptr->m_bToProperty[bProp] = property; + property->addSubProperty(bProp); + + QtProperty *aProp = d_ptr->m_intPropertyManager->addProperty(); + aProp->setPropertyName(tr("Alpha")); + d_ptr->m_intPropertyManager->setValue(aProp, val.alpha()); + d_ptr->m_intPropertyManager->setRange(aProp, 0, 0xFF); + d_ptr->m_propertyToA[property] = aProp; + d_ptr->m_aToProperty[aProp] = property; + property->addSubProperty(aProp); +} + +/*! + \reimp +*/ +void QtColorPropertyManager::uninitializeProperty(QtProperty *property) +{ + QtProperty *rProp = d_ptr->m_propertyToR[property]; + if (rProp) { + d_ptr->m_rToProperty.remove(rProp); + delete rProp; + } + d_ptr->m_propertyToR.remove(property); + + QtProperty *gProp = d_ptr->m_propertyToG[property]; + if (gProp) { + d_ptr->m_gToProperty.remove(gProp); + delete gProp; + } + d_ptr->m_propertyToG.remove(property); + + QtProperty *bProp = d_ptr->m_propertyToB[property]; + if (bProp) { + d_ptr->m_bToProperty.remove(bProp); + delete bProp; + } + d_ptr->m_propertyToB.remove(property); + + QtProperty *aProp = d_ptr->m_propertyToA[property]; + if (aProp) { + d_ptr->m_aToProperty.remove(aProp); + delete aProp; + } + d_ptr->m_propertyToA.remove(property); + + d_ptr->m_values.remove(property); +} + +void QtColorPropertyManager::slotIntChanged(QtProperty *p, int i) +{ + d_ptr->slotIntChanged(p, i); +} + +void QtColorPropertyManager::slotPropertyDestroyed(QtProperty *p) +{ + d_ptr->slotPropertyDestroyed(p); +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/color_property.h b/src/managers/color_property.h new file mode 100644 index 0000000..fdf153c --- /dev/null +++ b/src/managers/color_property.h @@ -0,0 +1,49 @@ + +#ifndef QtColorPropertyManager_H +#define QtColorPropertyManager_H + +#include "../qtpropertybrowser.h" +#include "int_property.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QtColorPropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtColorPropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtColorPropertyManager(QObject *parent = nullptr); + ~QtColorPropertyManager(); + + QtIntPropertyManager *subIntPropertyManager() const; + + QColor value(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, const QColor &val); +Q_SIGNALS: + void valueChanged(QtProperty *property, const QColor &val); +protected: + QString valueText(const QtProperty *property) const; + QIcon valueIcon(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtColorPropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtColorPropertyManager) + Q_DISABLE_COPY(QtColorPropertyManager) + +private Q_SLOTS: + void slotIntChanged(QtProperty *, int); + void slotPropertyDestroyed(QtProperty *); +}; + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index 3212cc7..0242983 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -4907,286 +4907,11 @@ void QtFontPropertyManager::uninitializeProperty(QtProperty *property) d_ptr->m_values.remove(property); } -// QtColorPropertyManager -class QtColorPropertyManagerPrivate -{ - QtColorPropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtColorPropertyManager) -public: - - void slotIntChanged(QtProperty *property, int value); - void slotPropertyDestroyed(QtProperty *property); - - typedef QMap PropertyValueMap; - PropertyValueMap m_values; - - QtIntPropertyManager *m_intPropertyManager; - - QMap m_propertyToR; - QMap m_propertyToG; - QMap m_propertyToB; - QMap m_propertyToA; - - QMap m_rToProperty; - QMap m_gToProperty; - QMap m_bToProperty; - QMap m_aToProperty; -}; - -void QtColorPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) -{ - if (QtProperty *prop = m_rToProperty.value(property, 0)) { - QColor c = m_values[prop]; - c.setRed(value); - q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_gToProperty.value(property, 0)) { - QColor c = m_values[prop]; - c.setGreen(value); - q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_bToProperty.value(property, 0)) { - QColor c = m_values[prop]; - c.setBlue(value); - q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_aToProperty.value(property, 0)) { - QColor c = m_values[prop]; - c.setAlpha(value); - q_ptr->setValue(prop, c); - } -} - -void QtColorPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) -{ - if (QtProperty *pointProp = m_rToProperty.value(property, 0)) { - m_propertyToR[pointProp] = 0; - m_rToProperty.remove(property); - } else if (QtProperty *pointProp = m_gToProperty.value(property, 0)) { - m_propertyToG[pointProp] = 0; - m_gToProperty.remove(property); - } else if (QtProperty *pointProp = m_bToProperty.value(property, 0)) { - m_propertyToB[pointProp] = 0; - m_bToProperty.remove(property); - } else if (QtProperty *pointProp = m_aToProperty.value(property, 0)) { - m_propertyToA[pointProp] = 0; - m_aToProperty.remove(property); - } -} - -/*! - \class QtColorPropertyManager - - \brief The QtColorPropertyManager provides and manages QColor properties. - - A color property has nested \e red, \e green and \e blue - subproperties. The top-level property's value can be retrieved - using the value() function, and set using the setValue() slot. - - The subproperties are created by a QtIntPropertyManager object. This - manager can be retrieved using the subIntPropertyManager() function. In - order to provide editing widgets for the subproperties in a - property browser widget, this manager must be associated with an - editor factory. - - In addition, QtColorPropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes. - - \sa QtAbstractPropertyManager, QtAbstractPropertyBrowser, QtIntPropertyManager -*/ - -/*! - \fn void QtColorPropertyManager::valueChanged(QtProperty *property, const QColor &value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the new - \a value as parameters. - - \sa setValue() -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtColorPropertyManager::QtColorPropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtColorPropertyManagerPrivate; - d_ptr->q_ptr = this; - - d_ptr->m_intPropertyManager = new QtIntPropertyManager(this); - connect(d_ptr->m_intPropertyManager, SIGNAL(valueChanged(QtProperty *, int)), - this, SLOT(slotIntChanged(QtProperty *, int))); - - connect(d_ptr->m_intPropertyManager, SIGNAL(propertyDestroyed(QtProperty *)), - this, SLOT(slotPropertyDestroyed(QtProperty *))); -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtColorPropertyManager::~QtColorPropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the manager that produces the nested \e red, \e green and - \e blue subproperties. - - In order to provide editing widgets for the subproperties in a - property browser widget, this manager must be associated with an - editor factory. - - \sa QtAbstractPropertyBrowser::setFactoryForManager() -*/ -QtIntPropertyManager *QtColorPropertyManager::subIntPropertyManager() const -{ - return d_ptr->m_intPropertyManager; -} -/*! - Returns the given \a property's value. - If the given \a property is not managed by \e this manager, this - function returns an invalid color. - \sa setValue() -*/ -QColor QtColorPropertyManager::value(const QtProperty *property) const -{ - return d_ptr->m_values.value(property, QColor()); -} -/*! - \reimp -*/ - -QString QtColorPropertyManager::valueText(const QtProperty *property) const -{ - const QtColorPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QString(); - - return QtPropertyBrowserUtils::colorValueText(it.value()); -} - -/*! - \reimp -*/ - -QIcon QtColorPropertyManager::valueIcon(const QtProperty *property) const -{ - const QtColorPropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QIcon(); - return QtPropertyBrowserUtils::brushValueIcon(QBrush(it.value())); -} - -/*! - \fn void QtColorPropertyManager::setValue(QtProperty *property, const QColor &value) - - Sets the value of the given \a property to \a value. Nested - properties are updated automatically. - - \sa value(), valueChanged() -*/ -void QtColorPropertyManager::setValue(QtProperty *property, const QColor &val) -{ - const QtColorPropertyManagerPrivate::PropertyValueMap::iterator it = d_ptr->m_values.find(property); - if (it == d_ptr->m_values.end()) - return; - - if (it.value() == val) - return; - - it.value() = val; - - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToR[property], val.red()); - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToG[property], val.green()); - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToB[property], val.blue()); - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToA[property], val.alpha()); - - emit propertyChanged(property); - emit valueChanged(property, val); -} - -/*! - \reimp -*/ -void QtColorPropertyManager::initializeProperty(QtProperty *property) -{ - QColor val; - d_ptr->m_values[property] = val; - - QtProperty *rProp = d_ptr->m_intPropertyManager->addProperty(); - rProp->setPropertyName(tr("Red")); - d_ptr->m_intPropertyManager->setValue(rProp, val.red()); - d_ptr->m_intPropertyManager->setRange(rProp, 0, 0xFF); - d_ptr->m_propertyToR[property] = rProp; - d_ptr->m_rToProperty[rProp] = property; - property->addSubProperty(rProp); - - QtProperty *gProp = d_ptr->m_intPropertyManager->addProperty(); - gProp->setPropertyName(tr("Green")); - d_ptr->m_intPropertyManager->setValue(gProp, val.green()); - d_ptr->m_intPropertyManager->setRange(gProp, 0, 0xFF); - d_ptr->m_propertyToG[property] = gProp; - d_ptr->m_gToProperty[gProp] = property; - property->addSubProperty(gProp); - - QtProperty *bProp = d_ptr->m_intPropertyManager->addProperty(); - bProp->setPropertyName(tr("Blue")); - d_ptr->m_intPropertyManager->setValue(bProp, val.blue()); - d_ptr->m_intPropertyManager->setRange(bProp, 0, 0xFF); - d_ptr->m_propertyToB[property] = bProp; - d_ptr->m_bToProperty[bProp] = property; - property->addSubProperty(bProp); - - QtProperty *aProp = d_ptr->m_intPropertyManager->addProperty(); - aProp->setPropertyName(tr("Alpha")); - d_ptr->m_intPropertyManager->setValue(aProp, val.alpha()); - d_ptr->m_intPropertyManager->setRange(aProp, 0, 0xFF); - d_ptr->m_propertyToA[property] = aProp; - d_ptr->m_aToProperty[aProp] = property; - property->addSubProperty(aProp); -} - -/*! - \reimp -*/ -void QtColorPropertyManager::uninitializeProperty(QtProperty *property) -{ - QtProperty *rProp = d_ptr->m_propertyToR[property]; - if (rProp) { - d_ptr->m_rToProperty.remove(rProp); - delete rProp; - } - d_ptr->m_propertyToR.remove(property); - - QtProperty *gProp = d_ptr->m_propertyToG[property]; - if (gProp) { - d_ptr->m_gToProperty.remove(gProp); - delete gProp; - } - d_ptr->m_propertyToG.remove(property); - - QtProperty *bProp = d_ptr->m_propertyToB[property]; - if (bProp) { - d_ptr->m_bToProperty.remove(bProp); - delete bProp; - } - d_ptr->m_propertyToB.remove(property); - - QtProperty *aProp = d_ptr->m_propertyToA[property]; - if (aProp) { - d_ptr->m_aToProperty.remove(aProp); - delete aProp; - } - d_ptr->m_propertyToA.remove(property); - - d_ptr->m_values.remove(property); -} // QtCursorPropertyManager diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index b99919a..d90fb80 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -46,6 +46,7 @@ #include "managers/int_property.h" #include "managers/double_property.h" #include "managers/bool_property.h" +#include "managers/color_property.h" #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -581,35 +582,6 @@ public Q_SLOTS: Q_PRIVATE_SLOT(d_func(), void slotFontDatabaseDelayedChange()) }; -class QtColorPropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtColorPropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtColorPropertyManager(QObject *parent = 0); - ~QtColorPropertyManager(); - - QtIntPropertyManager *subIntPropertyManager() const; - - QColor value(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, const QColor &val); -Q_SIGNALS: - void valueChanged(QtProperty *property, const QColor &val); -protected: - QString valueText(const QtProperty *property) const; - QIcon valueIcon(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtColorPropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtColorPropertyManager) - Q_DISABLE_COPY(QtColorPropertyManager) - Q_PRIVATE_SLOT(d_func(), void slotIntChanged(QtProperty *, int)) - Q_PRIVATE_SLOT(d_func(), void slotPropertyDestroyed(QtProperty *)) -}; class QtCursorPropertyManagerPrivate; From 01ba14b7bdde9c4ff406a2cfa7d69abf5618660b Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 22 Dec 2019 01:43:32 +0300 Subject: [PATCH 31/39] Replace 0 with nullptr and other minor fixes --- src/managers/color_property.cpp | 24 ++-- src/managers/common.h | 7 + src/managers/double_property.cpp | 6 +- src/managers/int_property.cpp | 4 +- src/qtbuttonpropertybrowser.cpp | 43 +++--- src/qtbuttonpropertybrowser.h | 2 +- src/qteditorfactory.h | 6 +- src/qtgroupboxpropertybrowser.cpp | 48 ++++--- src/qtgroupboxpropertybrowser.h | 2 +- src/qtpropertybrowser.cpp | 38 +++--- src/qtpropertybrowser.h | 8 +- src/qtpropertybrowserutils.cpp | 6 +- src/qtpropertybrowserutils_p.h | 4 +- src/qtpropertymanager.cpp | 212 +++++++++++++++--------------- src/qtpropertymanager.h | 36 ++--- src/qttreepropertybrowser.cpp | 46 ++++--- src/qttreepropertybrowser.h | 2 +- src/qtvariantproperty.cpp | 91 +++++++------ src/qtvariantproperty.h | 4 +- 19 files changed, 308 insertions(+), 281 deletions(-) diff --git a/src/managers/color_property.cpp b/src/managers/color_property.cpp index 3a5fea2..c5ab93c 100644 --- a/src/managers/color_property.cpp +++ b/src/managers/color_property.cpp @@ -48,19 +48,19 @@ class QtColorPropertyManagerPrivate void QtColorPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) { - if (QtProperty *prop = m_rToProperty.value(property, 0)) { + if (QtProperty *prop = m_rToProperty.value(property, nullptr)) { QColor c = m_values[prop]; c.setRed(value); q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_gToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_gToProperty.value(property, nullptr)) { QColor c = m_values[prop]; c.setGreen(value); q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_bToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_bToProperty.value(property, nullptr)) { QColor c = m_values[prop]; c.setBlue(value); q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_aToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_aToProperty.value(property, nullptr)) { QColor c = m_values[prop]; c.setAlpha(value); q_ptr->setValue(prop, c); @@ -69,17 +69,17 @@ void QtColorPropertyManagerPrivate::slotIntChanged(QtProperty *property, int val void QtColorPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_rToProperty.value(property, 0)) { - m_propertyToR[pointProp] = 0; + if (QtProperty *pointProp = m_rToProperty.value(property, nullptr)) { + m_propertyToR[pointProp] = nullptr; m_rToProperty.remove(property); - } else if (QtProperty *pointProp = m_gToProperty.value(property, 0)) { - m_propertyToG[pointProp] = 0; + } else if (QtProperty *pointProp = m_gToProperty.value(property, nullptr)) { + m_propertyToG[pointProp] = nullptr; m_gToProperty.remove(property); - } else if (QtProperty *pointProp = m_bToProperty.value(property, 0)) { - m_propertyToB[pointProp] = 0; + } else if (QtProperty *pointProp = m_bToProperty.value(property, nullptr)) { + m_propertyToB[pointProp] = nullptr; m_bToProperty.remove(property); - } else if (QtProperty *pointProp = m_aToProperty.value(property, 0)) { - m_propertyToA[pointProp] = 0; + } else if (QtProperty *pointProp = m_aToProperty.value(property, nullptr)) { + m_propertyToA[pointProp] = nullptr; m_aToProperty.remove(property); } } diff --git a/src/managers/common.h b/src/managers/common.h index f51b77b..140881b 100644 --- a/src/managers/common.h +++ b/src/managers/common.h @@ -77,6 +77,13 @@ static SizeValue qBoundSize(const SizeValue &minVal, const SizeValue &val, const return croppedVal; } +template +bool floatEqual(T x, T y) +{ + int64_t xi = int64_t(x * 10000000.0); + int64_t yi = int64_t(y * 10000000.0); + return (xi == yi); +} // Match the exact signature of qBound for VS 6. QSize qBound(QSize minVal, QSize val, QSize maxVal); diff --git a/src/managers/double_property.cpp b/src/managers/double_property.cpp index f86ddef..082acf2 100644 --- a/src/managers/double_property.cpp +++ b/src/managers/double_property.cpp @@ -201,7 +201,7 @@ QString QtDoublePropertyManager::valueText(const QtProperty *property) const */ void QtDoublePropertyManager::setValue(QtProperty *property, double val) { - void (QtDoublePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, double) = 0; + void (QtDoublePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, double) = nullptr; setValueInRange(this, d_ptr, &QtDoublePropertyManager::propertyChanged, &QtDoublePropertyManager::valueChanged, @@ -226,7 +226,7 @@ void QtDoublePropertyManager::setSingleStep(QtProperty *property, double step) if (step < 0) step = 0; - if (data.singleStep == step) + if (floatEqual(data.singleStep, step)) return; data.singleStep = step; @@ -320,7 +320,7 @@ void QtDoublePropertyManager::setMaximum(QtProperty *property, double maxVal) */ void QtDoublePropertyManager::setRange(QtProperty *property, double minVal, double maxVal) { - void (QtDoublePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, double, double, double) = 0; + void (QtDoublePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, double, double, double) = nullptr; setBorderValues(this, d_ptr, &QtDoublePropertyManager::propertyChanged, &QtDoublePropertyManager::valueChanged, diff --git a/src/managers/int_property.cpp b/src/managers/int_property.cpp index fbbe32e..c7ebcfa 100644 --- a/src/managers/int_property.cpp +++ b/src/managers/int_property.cpp @@ -180,7 +180,7 @@ QString QtIntPropertyManager::valueText(const QtProperty *property) const */ void QtIntPropertyManager::setValue(QtProperty *property, int val) { - void (QtIntPropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, int) = 0; + void (QtIntPropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, int) = nullptr; setValueInRange(this, d_ptr, &QtIntPropertyManager::propertyChanged, &QtIntPropertyManager::valueChanged, @@ -239,7 +239,7 @@ void QtIntPropertyManager::setMaximum(QtProperty *property, int maxVal) */ void QtIntPropertyManager::setRange(QtProperty *property, int minVal, int maxVal) { - void (QtIntPropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, int, int, int) = 0; + void (QtIntPropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, int, int, int) = nullptr; setBorderValues(this, d_ptr, &QtIntPropertyManager::propertyChanged, &QtIntPropertyManager::valueChanged, diff --git a/src/qtbuttonpropertybrowser.cpp b/src/qtbuttonpropertybrowser.cpp index 0cb22b0..b4f6b07 100644 --- a/src/qtbuttonpropertybrowser.cpp +++ b/src/qtbuttonpropertybrowser.cpp @@ -71,8 +71,17 @@ class QtButtonPropertyBrowserPrivate struct WidgetItem { - WidgetItem() : widget(0), label(0), widgetLabel(0), - button(0), container(0), layout(0), /*line(0), */parent(0), expanded(false) { } + WidgetItem() : + widget(nullptr), + label(nullptr), + widgetLabel(nullptr), + button(nullptr), + container(nullptr), + layout(nullptr), + /*line(0), */ + parent(nullptr), + expanded(false) + {} QWidget *widget; // can be null QLabel *label; // main label with property name QLabel *widgetLabel; // label substitute showing the current value if there is no widget @@ -91,7 +100,7 @@ class QtButtonPropertyBrowserPrivate int gridRow(WidgetItem *item) const; int gridSpan(WidgetItem *item) const; void setExpanded(WidgetItem *item, bool expanded); - QToolButton *createButton(QWidget *panret = 0) const; + QToolButton *createButton(QWidget *panret = nullptr) const; QMap m_indexToItem; QMap m_itemToIndex; @@ -161,7 +170,7 @@ void QtButtonPropertyBrowserPrivate::slotEditorDestroyed() return; if (!m_widgetToItem.contains(editor)) return; - m_widgetToItem[editor]->widget = 0; + m_widgetToItem[editor]->widget = nullptr; m_widgetToItem.remove(editor); } @@ -172,8 +181,8 @@ void QtButtonPropertyBrowserPrivate::slotUpdate() WidgetItem *item = itItem.next(); WidgetItem *parent = item->parent; - QWidget *w = 0; - QGridLayout *l = 0; + QWidget *w = nullptr; + QGridLayout *l = nullptr; const int oldRow = gridRow(item); if (parent) { w = parent->container; @@ -206,7 +215,7 @@ void QtButtonPropertyBrowserPrivate::setExpanded(WidgetItem *item, bool expanded item->expanded = expanded; const int row = gridRow(item); WidgetItem *parent = item->parent; - QGridLayout *l = 0; + QGridLayout *l = nullptr; if (parent) l = parent->layout; else @@ -253,8 +262,8 @@ void QtButtonPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBr WidgetItem *newItem = new WidgetItem(); newItem->parent = parentItem; - QGridLayout *layout = 0; - QWidget *parentWidget = 0; + QGridLayout *layout = nullptr; + QWidget *parentWidget = nullptr; int row = -1; if (!afterItem) { row = 0; @@ -277,8 +286,8 @@ void QtButtonPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBr if (!parentItem->container) { m_recreateQueue.removeAll(parentItem); WidgetItem *grandParent = parentItem->parent; - QWidget *w = 0; - QGridLayout *l = 0; + QWidget *w = nullptr; + QGridLayout *l = nullptr; const int oldRow = gridRow(parentItem); if (grandParent) { w = grandParent->container; @@ -287,7 +296,7 @@ void QtButtonPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBr w = q_ptr; l = m_mainLayout; } - Q_UNUSED(w); + Q_UNUSED(w) QFrame *container = new QFrame(); container->setFrameShape(QFrame::Panel); container->setFrameShadow(QFrame::Raised); @@ -300,7 +309,7 @@ void QtButtonPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBr if (parentItem->label) { l->removeWidget(parentItem->label); delete parentItem->label; - parentItem->label = 0; + parentItem->label = nullptr; } int span = 1; if (!parentItem->widget && !parentItem->widgetLabel) @@ -380,7 +389,7 @@ void QtButtonPropertyBrowserPrivate::propertyRemoved(QtBrowserItem *index) removeRow(parentItem->layout, row); } else { const WidgetItem *grandParent = parentItem->parent; - QGridLayout *l = 0; + QGridLayout *l = nullptr; if (grandParent) { l = grandParent->layout; } else { @@ -394,9 +403,9 @@ void QtButtonPropertyBrowserPrivate::propertyRemoved(QtBrowserItem *index) l->removeWidget(parentItem->container); delete parentItem->button; delete parentItem->container; - parentItem->button = 0; - parentItem->container = 0; - parentItem->layout = 0; + parentItem->button = nullptr; + parentItem->container = nullptr; + parentItem->layout = nullptr; if (!m_recreateQueue.contains(parentItem)) m_recreateQueue.append(parentItem); if (parentSpan > 1) diff --git a/src/qtbuttonpropertybrowser.h b/src/qtbuttonpropertybrowser.h index c46a458..6ce135a 100644 --- a/src/qtbuttonpropertybrowser.h +++ b/src/qtbuttonpropertybrowser.h @@ -54,7 +54,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtButtonPropertyBrowser : public QtAbstractPro Q_OBJECT public: - QtButtonPropertyBrowser(QWidget *parent = 0); + QtButtonPropertyBrowser(QWidget *parent = nullptr); ~QtButtonPropertyBrowser(); void setExpanded(QtBrowserItem *item, bool expanded); diff --git a/src/qteditorfactory.h b/src/qteditorfactory.h index 590a5b3..124c888 100644 --- a/src/qteditorfactory.h +++ b/src/qteditorfactory.h @@ -53,7 +53,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtSpinBoxFactory : public QtAbstractEditorFact { Q_OBJECT public: - QtSpinBoxFactory(QObject *parent = 0); + QtSpinBoxFactory(QObject *parent = nullptr); ~QtSpinBoxFactory(); protected: void connectPropertyManager(QtIntPropertyManager *manager); @@ -77,7 +77,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtSliderFactory : public QtAbstractEditorFacto { Q_OBJECT public: - QtSliderFactory(QObject *parent = 0); + QtSliderFactory(QObject *parent = nullptr); ~QtSliderFactory(); protected: void connectPropertyManager(QtIntPropertyManager *manager); @@ -381,7 +381,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtFontEditorFactory : public QtAbstractEditorF { Q_OBJECT public: - QtFontEditorFactory(QObject *parent = 0); + QtFontEditorFactory(QObject *parent = nullptr); ~QtFontEditorFactory(); protected: void connectPropertyManager(QtFontPropertyManager *manager); diff --git a/src/qtgroupboxpropertybrowser.cpp b/src/qtgroupboxpropertybrowser.cpp index 6f198b7..9304da9 100644 --- a/src/qtgroupboxpropertybrowser.cpp +++ b/src/qtgroupboxpropertybrowser.cpp @@ -69,9 +69,17 @@ class QtGroupBoxPropertyBrowserPrivate struct WidgetItem { - WidgetItem() : widget(0), label(0), widgetLabel(0), - groupBoxGroup(0), groupBoxFrame(0), - groupBox(0), layout(0), line(0), parent(0) { } + WidgetItem() : + widget(nullptr), + label(nullptr), + widgetLabel(nullptr), + groupBoxGroup(nullptr), + groupBoxFrame(nullptr), + groupBox(nullptr), + layout(nullptr), + line(nullptr), + parent(nullptr) + {} QWidget *widget; // can be null QLabel *label; QLabel *widgetLabel; @@ -117,7 +125,7 @@ void QtGroupBoxPropertyBrowserPrivate::slotEditorDestroyed() return; if (!m_widgetToItem.contains(editor)) return; - m_widgetToItem[editor]->widget = 0; + m_widgetToItem[editor]->widget = nullptr; m_widgetToItem.remove(editor); } @@ -128,8 +136,8 @@ void QtGroupBoxPropertyBrowserPrivate::slotUpdate() WidgetItem *item = itItem.next(); WidgetItem *par = item->parent; - QWidget *w = 0; - QGridLayout *l = 0; + QWidget *w = nullptr; + QGridLayout *l = nullptr; int oldRow = -1; if (!par) { w = q_ptr; @@ -181,8 +189,8 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt WidgetItem *newItem = new WidgetItem(); newItem->parent = parentItem; - QGridLayout *layout = 0; - QWidget *parentWidget = 0; + QGridLayout *layout = nullptr; + QWidget *parentWidget = nullptr; int row = -1; if (!afterItem) { row = 0; @@ -209,8 +217,8 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt if (!parentItem->groupBox) { m_recreateQueue.removeAll(parentItem); WidgetItem *par = parentItem->parent; - QWidget *w = 0; - QGridLayout *l = 0; + QWidget *w = nullptr; + QGridLayout *l = nullptr; int oldRow = -1; if (!par) { w = q_ptr; @@ -234,7 +242,7 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt if (parentItem->label) { l->removeWidget(parentItem->label); delete parentItem->label; - parentItem->label = 0; + parentItem->label = nullptr; } if (parentItem->widget) { l->removeWidget(parentItem->widget); @@ -244,7 +252,7 @@ void QtGroupBoxPropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, Qt } else if (parentItem->widgetLabel) { l->removeWidget(parentItem->widgetLabel); delete parentItem->widgetLabel; - parentItem->widgetLabel = 0; + parentItem->widgetLabel = nullptr; } if (parentItem->line) { parentItem->line->setFrameShape(QFrame::HLine); @@ -322,8 +330,8 @@ void QtGroupBoxPropertyBrowserPrivate::propertyRemoved(QtBrowserItem *index) removeRow(parentItem->layout, row); } else { WidgetItem *par = parentItem->parent; - QWidget *w = 0; - QGridLayout *l = 0; + QWidget *w = nullptr; + QGridLayout *l = nullptr; int oldRow = -1; if (!par) { w = q_ptr; @@ -336,22 +344,22 @@ void QtGroupBoxPropertyBrowserPrivate::propertyRemoved(QtBrowserItem *index) if (hasHeader(par)) oldRow += 2; } - Q_UNUSED(w); + Q_UNUSED(w) if (parentItem->widget) { parentItem->widget->hide(); - parentItem->widget->setParent(0); + parentItem->widget->setParent(nullptr); } else if (parentItem->widgetLabel) { parentItem->widgetLabel->hide(); - parentItem->widgetLabel->setParent(0); + parentItem->widgetLabel->setParent(nullptr); } else { //parentItem->widgetLabel = new QLabel(w); } l->removeWidget(parentItem->groupBox); delete parentItem->groupBox; - parentItem->groupBox = 0; - parentItem->line = 0; - parentItem->layout = 0; + parentItem->groupBox = nullptr; + parentItem->line = nullptr; + parentItem->layout = nullptr; if (!m_recreateQueue.contains(parentItem)) m_recreateQueue.append(parentItem); updateLater(); diff --git a/src/qtgroupboxpropertybrowser.h b/src/qtgroupboxpropertybrowser.h index 65a504a..a4c485f 100644 --- a/src/qtgroupboxpropertybrowser.h +++ b/src/qtgroupboxpropertybrowser.h @@ -54,7 +54,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtGroupBoxPropertyBrowser : public QtAbstractP Q_OBJECT public: - QtGroupBoxPropertyBrowser(QWidget *parent = 0, bool frameLess = false); + QtGroupBoxPropertyBrowser(QWidget *parent = nullptr, bool frameLess = false); ~QtGroupBoxPropertyBrowser(); protected: diff --git a/src/qtpropertybrowser.cpp b/src/qtpropertybrowser.cpp index 3ac8723..55c2acc 100644 --- a/src/qtpropertybrowser.cpp +++ b/src/qtpropertybrowser.cpp @@ -441,7 +441,7 @@ bool QtProperty::isSubProperty()const */ void QtProperty::addSubProperty(QtProperty *property) { - QtProperty *after = 0; + QtProperty *after = nullptr; if (d_ptr->m_subItems.count() > 0) after = d_ptr->m_subItems.last(); insertSubProperty(property, after); @@ -486,7 +486,7 @@ void QtProperty::insertSubProperty(QtProperty *property, pendingList = subProperties(); int pos = 0; int newPos = 0; - QtProperty *properAfterProperty = 0; + QtProperty *properAfterProperty = nullptr; while (pos < pendingList.count()) { QtProperty *i = pendingList.at(pos); if (i == property) @@ -800,7 +800,7 @@ QtProperty * QtAbstractPropertyManager::qtProperty(const QString &id)const return prop; } } - return 0; + return nullptr; } /*! @@ -1124,7 +1124,7 @@ class QtBrowserItemPrivate { public: QtBrowserItemPrivate(QtAbstractPropertyBrowser *browser, QtProperty *property, QtBrowserItem *parent) - : m_browser(browser), m_property(property), m_parent(parent), q_ptr(0) {} + : m_browser(browser), m_property(property), m_parent(parent), q_ptr(nullptr) {} void addChild(QtBrowserItem *index, QtBrowserItem *after); void removeChild(QtBrowserItem *index); @@ -1283,7 +1283,7 @@ class QtAbstractPropertyBrowserPrivate }; QtAbstractPropertyBrowserPrivate::QtAbstractPropertyBrowserPrivate() : - m_currentItem(0) + m_currentItem(nullptr) { } @@ -1392,10 +1392,10 @@ void QtAbstractPropertyBrowserPrivate::createBrowserIndexes(QtProperty *property QListIterator itIndex(indexes); while (itIndex.hasNext()) { QtBrowserItem *idx = itIndex.next(); - parentToAfter[idx] = 0; + parentToAfter[idx] = nullptr; } } else { - parentToAfter[0] = 0; + parentToAfter[nullptr] = nullptr; } const QMap::ConstIterator pcend = parentToAfter.constEnd(); @@ -1419,7 +1419,7 @@ QtBrowserItem *QtAbstractPropertyBrowserPrivate::createBrowserIndex(QtProperty * QList subItems = property->subProperties(); QListIterator itChild(subItems); - QtBrowserItem *afterChild = 0; + QtBrowserItem *afterChild = nullptr; while (itChild.hasNext()) { QtProperty *child = itChild.next(); afterChild = createBrowserIndex(child, newIndex, afterChild); @@ -1842,7 +1842,7 @@ void QtAbstractPropertyBrowser::clear() */ QtBrowserItem *QtAbstractPropertyBrowser::addProperty(QtProperty *property) { - QtProperty *afterProperty = 0; + QtProperty *afterProperty = nullptr; if (d_ptr->m_subItems.count() > 0) afterProperty = d_ptr->m_subItems.last(); return insertProperty(property, afterProperty); @@ -1868,28 +1868,28 @@ QtBrowserItem *QtAbstractPropertyBrowser::insertProperty(QtProperty *property, QtProperty *afterProperty) { if (!property) - return 0; + return nullptr; // if item is already inserted in this item then cannot add. QList pendingList = properties(); int pos = 0; int newPos = 0; - QtProperty *properAfterProperty = 0; - Q_UNUSED(properAfterProperty); + QtProperty *properAfterProperty = nullptr; + Q_UNUSED(properAfterProperty) while (pos < pendingList.count()) { QtProperty *prop = pendingList.at(pos); if (prop == property) - return 0; + return nullptr; if (prop == afterProperty) { newPos = pos + 1; properAfterProperty = afterProperty; } pos++; } - d_ptr->createBrowserIndexes(property, 0, afterProperty); + d_ptr->createBrowserIndexes(property, nullptr, afterProperty); // traverse inserted subtree and connect to manager's signals - d_ptr->insertSubTree(property, 0); + d_ptr->insertSubTree(property, nullptr); d_ptr->m_subItems.insert(newPos, property); //propertyInserted(property, 0, properAfterProperty); @@ -1917,10 +1917,10 @@ void QtAbstractPropertyBrowser::removeProperty(QtProperty *property) while (pos < pendingList.count()) { if (pendingList.at(pos) == property) { d_ptr->m_subItems.removeAt(pos); //perhaps this two lines - d_ptr->removeSubTree(property, 0); //should be moved down after propertyRemoved call. + d_ptr->removeSubTree(property, nullptr); //should be moved down after propertyRemoved call. //propertyRemoved(property, 0); - d_ptr->removeBrowserIndexes(property, 0); + d_ptr->removeBrowserIndexes(property, nullptr); // when item is deleted, item will call removeItem for top level items, // and itemRemoved for nested items. @@ -1952,7 +1952,7 @@ void QtAbstractPropertyBrowser::removeProperty(QtProperty *property) QWidget *QtAbstractPropertyBrowser::createEditor(QtProperty *property, QWidget *parent) { - QtAbstractEditorFactoryBase *factory = 0; + QtAbstractEditorFactoryBase *factory = nullptr; QtAbstractPropertyManager *manager = property->propertyManager(); if (m_viewToManagerToFactory()->contains(this) && @@ -1961,7 +1961,7 @@ QWidget *QtAbstractPropertyBrowser::createEditor(QtProperty *property, } if (!factory) - return 0; + return nullptr; return factory->createEditor(property, parent); } diff --git a/src/qtpropertybrowser.h b/src/qtpropertybrowser.h index c4c6275..be77551 100644 --- a/src/qtpropertybrowser.h +++ b/src/qtpropertybrowser.h @@ -118,7 +118,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtAbstractPropertyManager : public QObject Q_OBJECT public: - explicit QtAbstractPropertyManager(QObject *parent = 0); + explicit QtAbstractPropertyManager(QObject *parent = nullptr); ~QtAbstractPropertyManager(); QSet properties() const; @@ -153,7 +153,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtAbstractEditorFactoryBase : public QObject public: virtual QWidget *createEditor(QtProperty *property, QWidget *parent) = 0; protected: - explicit QtAbstractEditorFactoryBase(QObject *parent = 0) + explicit QtAbstractEditorFactoryBase(QObject *parent = nullptr) : QObject(parent) {} virtual void breakConnection(QtAbstractPropertyManager *manager) = 0; @@ -177,7 +177,7 @@ class QtAbstractEditorFactory : public QtAbstractEditorFactoryBase return createEditor(manager, property, parent); } } - return 0; + return nullptr; } void addPropertyManager(PropertyManager *manager) { @@ -270,7 +270,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtAbstractPropertyBrowser : public QWidget Q_OBJECT public: - explicit QtAbstractPropertyBrowser(QWidget *parent = 0); + explicit QtAbstractPropertyBrowser(QWidget *parent = nullptr); ~QtAbstractPropertyBrowser(); QList properties() const; diff --git a/src/qtpropertybrowserutils.cpp b/src/qtpropertybrowserutils.cpp index ea47364..128060d 100644 --- a/src/qtpropertybrowserutils.cpp +++ b/src/qtpropertybrowserutils.cpp @@ -156,7 +156,7 @@ QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b) QString QtPropertyBrowserUtils::colorValueText(const QColor &c) { - return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)", 0) + return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)", nullptr) .arg(QString::number(c.red())) .arg(QString::number(c.green())) .arg(QString::number(c.blue())) @@ -186,7 +186,7 @@ QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f) QString QtPropertyBrowserUtils::fontValueText(const QFont &f) { - return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2]", 0) + return QApplication::translate("QtPropertyBrowserUtils", "[%1, %2]", nullptr) .arg(f.family()) .arg(f.pointSize()); } @@ -315,7 +315,7 @@ bool QtKeySequenceEdit::eventFilter(QObject *o, QEvent *e) actionString.remove(pos, actionString.length() - pos); action->setText(actionString); } - QAction *actionBefore = 0; + QAction *actionBefore = nullptr; if (actions.count() > 0) actionBefore = actions[0]; QAction *clearAction = new QAction(tr("Clear Shortcut"), menu); diff --git a/src/qtpropertybrowserutils_p.h b/src/qtpropertybrowserutils_p.h index eb5d682..e3eb014 100644 --- a/src/qtpropertybrowserutils_p.h +++ b/src/qtpropertybrowserutils_p.h @@ -100,7 +100,7 @@ class QtPropertyBrowserUtils class QtBoolEdit : public QWidget { Q_OBJECT public: - QtBoolEdit(QWidget *parent = 0); + QtBoolEdit(QWidget *parent = nullptr); bool textVisible() const { return m_textVisible; } void setTextVisible(bool textVisible); @@ -135,7 +135,7 @@ class QtKeySequenceEdit : public QWidget { Q_OBJECT public: - QtKeySequenceEdit(QWidget *parent = 0); + QtKeySequenceEdit(QWidget *parent = nullptr); QKeySequence keySequence() const; bool eventFilter(QObject *o, QEvent *e); diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index 0242983..ac068b4 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -619,7 +619,7 @@ QString QtDatePropertyManager::valueText(const QtProperty *property) const */ void QtDatePropertyManager::setValue(QtProperty *property, const QDate &val) { - void (QtDatePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, const QDate &) = 0; + void (QtDatePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, const QDate &) = nullptr; setValueInRange(this, d_ptr, &QtDatePropertyManager::propertyChanged, &QtDatePropertyManager::valueChanged, @@ -679,7 +679,7 @@ void QtDatePropertyManager::setMaximum(QtProperty *property, const QDate &maxVal void QtDatePropertyManager::setRange(QtProperty *property, const QDate &minVal, const QDate &maxVal) { void (QtDatePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, const QDate &, - const QDate &, const QDate &) = 0; + const QDate &, const QDate &) = nullptr; setBorderValues(this, d_ptr, &QtDatePropertyManager::propertyChanged, &QtDatePropertyManager::valueChanged, @@ -1183,14 +1183,14 @@ QtLocalePropertyManagerPrivate::QtLocalePropertyManagerPrivate() void QtLocalePropertyManagerPrivate::slotEnumChanged(QtProperty *property, int value) { - if (QtProperty *prop = m_languageToProperty.value(property, 0)) { + if (QtProperty *prop = m_languageToProperty.value(property, nullptr)) { const QLocale loc = m_values[prop]; QLocale::Language newLanguage = loc.language(); QLocale::Country newCountry = loc.country(); - metaEnumProvider()->indexToLocale(value, 0, &newLanguage, 0); + metaEnumProvider()->indexToLocale(value, 0, &newLanguage, nullptr); QLocale newLoc(newLanguage, newCountry); q_ptr->setValue(prop, newLoc); - } else if (QtProperty *prop = m_countryToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_countryToProperty.value(property, nullptr)) { const QLocale loc = m_values[prop]; QLocale::Language newLanguage = loc.language(); QLocale::Country newCountry = loc.country(); @@ -1202,11 +1202,11 @@ void QtLocalePropertyManagerPrivate::slotEnumChanged(QtProperty *property, int v void QtLocalePropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *subProp = m_languageToProperty.value(property, 0)) { - m_propertyToLanguage[subProp] = 0; + if (QtProperty *subProp = m_languageToProperty.value(property, nullptr)) { + m_propertyToLanguage[subProp] = nullptr; m_languageToProperty.remove(property); - } else if (QtProperty *subProp = m_countryToProperty.value(property, 0)) { - m_propertyToCountry[subProp] = 0; + } else if (QtProperty *subProp = m_countryToProperty.value(property, nullptr)) { + m_propertyToCountry[subProp] = nullptr; m_countryToProperty.remove(property); } } @@ -1426,11 +1426,11 @@ class QtPointPropertyManagerPrivate void QtPointPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) { - if (QtProperty *xprop = m_xToProperty.value(property, 0)) { + if (QtProperty *xprop = m_xToProperty.value(property, nullptr)) { QPoint p = m_values[xprop]; p.setX(value); q_ptr->setValue(xprop, p); - } else if (QtProperty *yprop = m_yToProperty.value(property, 0)) { + } else if (QtProperty *yprop = m_yToProperty.value(property, nullptr)) { QPoint p = m_values[yprop]; p.setY(value); q_ptr->setValue(yprop, p); @@ -1439,11 +1439,11 @@ void QtPointPropertyManagerPrivate::slotIntChanged(QtProperty *property, int val void QtPointPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_xToProperty.value(property, 0)) { - m_propertyToX[pointProp] = 0; + if (QtProperty *pointProp = m_xToProperty.value(property, nullptr)) { + m_propertyToX[pointProp] = nullptr; m_xToProperty.remove(property); - } else if (QtProperty *pointProp = m_yToProperty.value(property, 0)) { - m_propertyToY[pointProp] = 0; + } else if (QtProperty *pointProp = m_yToProperty.value(property, nullptr)) { + m_propertyToY[pointProp] = nullptr; m_yToProperty.remove(property); } } @@ -1645,11 +1645,11 @@ class QtPointFPropertyManagerPrivate void QtPointFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, double value) { - if (QtProperty *prop = m_xToProperty.value(property, 0)) { + if (QtProperty *prop = m_xToProperty.value(property, nullptr)) { QPointF p = m_values[prop].val; p.setX(value); q_ptr->setValue(prop, p); - } else if (QtProperty *prop = m_yToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_yToProperty.value(property, nullptr)) { QPointF p = m_values[prop].val; p.setY(value); q_ptr->setValue(prop, p); @@ -1658,11 +1658,11 @@ void QtPointFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, dou void QtPointFPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_xToProperty.value(property, 0)) { - m_propertyToX[pointProp] = 0; + if (QtProperty *pointProp = m_xToProperty.value(property, nullptr)) { + m_propertyToX[pointProp] = nullptr; m_xToProperty.remove(property); - } else if (QtProperty *pointProp = m_yToProperty.value(property, 0)) { - m_propertyToY[pointProp] = 0; + } else if (QtProperty *pointProp = m_yToProperty.value(property, nullptr)) { + m_propertyToY[pointProp] = nullptr; m_yToProperty.remove(property); } } @@ -1929,11 +1929,11 @@ class QtSizePropertyManagerPrivate void QtSizePropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) { - if (QtProperty *prop = m_wToProperty.value(property, 0)) { + if (QtProperty *prop = m_wToProperty.value(property, nullptr)) { QSize s = m_values[prop].val; s.setWidth(value); q_ptr->setValue(prop, s); - } else if (QtProperty *prop = m_hToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_hToProperty.value(property, nullptr)) { QSize s = m_values[prop].val; s.setHeight(value); q_ptr->setValue(prop, s); @@ -1942,11 +1942,11 @@ void QtSizePropertyManagerPrivate::slotIntChanged(QtProperty *property, int valu void QtSizePropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_wToProperty.value(property, 0)) { - m_propertyToW[pointProp] = 0; + if (QtProperty *pointProp = m_wToProperty.value(property, nullptr)) { + m_propertyToW[pointProp] = nullptr; m_wToProperty.remove(property); - } else if (QtProperty *pointProp = m_hToProperty.value(property, 0)) { - m_propertyToH[pointProp] = 0; + } else if (QtProperty *pointProp = m_hToProperty.value(property, nullptr)) { + m_propertyToH[pointProp] = nullptr; m_hToProperty.remove(property); } } @@ -2274,11 +2274,11 @@ class QtSizeFPropertyManagerPrivate void QtSizeFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, double value) { - if (QtProperty *prop = m_wToProperty.value(property, 0)) { + if (QtProperty *prop = m_wToProperty.value(property, nullptr)) { QSizeF s = m_values[prop].val; s.setWidth(value); q_ptr->setValue(prop, s); - } else if (QtProperty *prop = m_hToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_hToProperty.value(property, nullptr)) { QSizeF s = m_values[prop].val; s.setHeight(value); q_ptr->setValue(prop, s); @@ -2287,11 +2287,11 @@ void QtSizeFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, doub void QtSizeFPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_wToProperty.value(property, 0)) { - m_propertyToW[pointProp] = 0; + if (QtProperty *pointProp = m_wToProperty.value(property, nullptr)) { + m_propertyToW[pointProp] = nullptr; m_wToProperty.remove(property); - } else if (QtProperty *pointProp = m_hToProperty.value(property, 0)) { - m_propertyToH[pointProp] = 0; + } else if (QtProperty *pointProp = m_hToProperty.value(property, nullptr)) { + m_propertyToH[pointProp] = nullptr; m_hToProperty.remove(property); } } @@ -2670,7 +2670,7 @@ class QtRectPropertyManagerPrivate void QtRectPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) { - if (QtProperty *prop = m_xToProperty.value(property, 0)) { + if (QtProperty *prop = m_xToProperty.value(property, nullptr)) { QRect r = m_values[prop].val; r.moveLeft(value); q_ptr->setValue(prop, r); @@ -2678,7 +2678,7 @@ void QtRectPropertyManagerPrivate::slotIntChanged(QtProperty *property, int valu QRect r = m_values[prop].val; r.moveTop(value); q_ptr->setValue(prop, r); - } else if (QtProperty *prop = m_wToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_wToProperty.value(property, nullptr)) { Data data = m_values[prop]; QRect r = data.val; r.setWidth(value); @@ -2686,7 +2686,7 @@ void QtRectPropertyManagerPrivate::slotIntChanged(QtProperty *property, int valu r.moveLeft(data.constraint.left() + data.constraint.width() - r.width()); } q_ptr->setValue(prop, r); - } else if (QtProperty *prop = m_hToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_hToProperty.value(property, nullptr)) { Data data = m_values[prop]; QRect r = data.val; r.setHeight(value); @@ -2699,17 +2699,17 @@ void QtRectPropertyManagerPrivate::slotIntChanged(QtProperty *property, int valu void QtRectPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_xToProperty.value(property, 0)) { - m_propertyToX[pointProp] = 0; + if (QtProperty *pointProp = m_xToProperty.value(property, nullptr)) { + m_propertyToX[pointProp] = nullptr; m_xToProperty.remove(property); - } else if (QtProperty *pointProp = m_yToProperty.value(property, 0)) { - m_propertyToY[pointProp] = 0; + } else if (QtProperty *pointProp = m_yToProperty.value(property, nullptr)) { + m_propertyToY[pointProp] = nullptr; m_yToProperty.remove(property); - } else if (QtProperty *pointProp = m_wToProperty.value(property, 0)) { - m_propertyToW[pointProp] = 0; + } else if (QtProperty *pointProp = m_wToProperty.value(property, nullptr)) { + m_propertyToW[pointProp] = nullptr; m_wToProperty.remove(property); - } else if (QtProperty *pointProp = m_hToProperty.value(property, 0)) { - m_propertyToH[pointProp] = 0; + } else if (QtProperty *pointProp = m_hToProperty.value(property, nullptr)) { + m_propertyToH[pointProp] = nullptr; m_hToProperty.remove(property); } } @@ -3079,15 +3079,15 @@ class QtRectFPropertyManagerPrivate void QtRectFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, double value) { - if (QtProperty *prop = m_xToProperty.value(property, 0)) { + if (QtProperty *prop = m_xToProperty.value(property, nullptr)) { QRectF r = m_values[prop].val; r.moveLeft(value); q_ptr->setValue(prop, r); - } else if (QtProperty *prop = m_yToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_yToProperty.value(property, nullptr)) { QRectF r = m_values[prop].val; r.moveTop(value); q_ptr->setValue(prop, r); - } else if (QtProperty *prop = m_wToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_wToProperty.value(property, nullptr)) { Data data = m_values[prop]; QRectF r = data.val; r.setWidth(value); @@ -3095,7 +3095,7 @@ void QtRectFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, doub r.moveLeft(data.constraint.left() + data.constraint.width() - r.width()); } q_ptr->setValue(prop, r); - } else if (QtProperty *prop = m_hToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_hToProperty.value(property, nullptr)) { Data data = m_values[prop]; QRectF r = data.val; r.setHeight(value); @@ -3108,17 +3108,17 @@ void QtRectFPropertyManagerPrivate::slotDoubleChanged(QtProperty *property, doub void QtRectFPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_xToProperty.value(property, 0)) { - m_propertyToX[pointProp] = 0; + if (QtProperty *pointProp = m_xToProperty.value(property, nullptr)) { + m_propertyToX[pointProp] = nullptr; m_xToProperty.remove(property); - } else if (QtProperty *pointProp = m_yToProperty.value(property, 0)) { - m_propertyToY[pointProp] = 0; + } else if (QtProperty *pointProp = m_yToProperty.value(property, nullptr)) { + m_propertyToY[pointProp] = nullptr; m_yToProperty.remove(property); - } else if (QtProperty *pointProp = m_wToProperty.value(property, 0)) { - m_propertyToW[pointProp] = 0; + } else if (QtProperty *pointProp = m_wToProperty.value(property, nullptr)) { + m_propertyToW[pointProp] = nullptr; m_wToProperty.remove(property); - } else if (QtProperty *pointProp = m_hToProperty.value(property, 0)) { - m_propertyToH[pointProp] = 0; + } else if (QtProperty *pointProp = m_hToProperty.value(property, nullptr)) { + m_propertyToH[pointProp] = nullptr; m_hToProperty.remove(property); } } @@ -3127,17 +3127,17 @@ void QtRectFPropertyManagerPrivate::setConstraint(QtProperty *property, const QRectF &constraint, const QRectF &val) { const bool isNull = constraint.isNull(); - const float left = isNull ? FLT_MIN : constraint.left(); - const float right = isNull ? FLT_MAX : constraint.left() + constraint.width(); - const float top = isNull ? FLT_MIN : constraint.top(); - const float bottom = isNull ? FLT_MAX : constraint.top() + constraint.height(); - const float width = isNull ? FLT_MAX : constraint.width(); - const float height = isNull ? FLT_MAX : constraint.height(); - - m_doublePropertyManager->setRange(m_propertyToX[property], left, right); - m_doublePropertyManager->setRange(m_propertyToY[property], top, bottom); - m_doublePropertyManager->setRange(m_propertyToW[property], 0, width); - m_doublePropertyManager->setRange(m_propertyToH[property], 0, height); + const float left = isNull ? FLT_MIN : float(constraint.left()); + const float right = isNull ? FLT_MAX : float(constraint.left() + constraint.width()); + const float top = isNull ? FLT_MIN : float(constraint.top()); + const float bottom = isNull ? FLT_MAX : float(constraint.top() + constraint.height()); + const float width = isNull ? FLT_MAX : float(constraint.width()); + const float height = isNull ? FLT_MAX : float(constraint.height()); + + m_doublePropertyManager->setRange(m_propertyToX[property], double(left), double(right)); + m_doublePropertyManager->setRange(m_propertyToY[property], double(top), double(bottom)); + m_doublePropertyManager->setRange(m_propertyToW[property], 0, double(width)); + m_doublePropertyManager->setRange(m_propertyToH[property], 0, double(height)); m_doublePropertyManager->setValue(m_propertyToX[property], val.x()); m_doublePropertyManager->setValue(m_propertyToY[property], val.y()); @@ -3810,8 +3810,8 @@ class QtFlagPropertyManagerPrivate void QtFlagPropertyManagerPrivate::slotBoolChanged(QtProperty *property, bool value) { - QtProperty *prop = m_flagToProperty.value(property, 0); - if (prop == 0) + QtProperty *prop = m_flagToProperty.value(property, nullptr); + if (prop == nullptr) return; QListIterator itProp(m_propertyToFlags[prop]); @@ -3834,11 +3834,11 @@ void QtFlagPropertyManagerPrivate::slotBoolChanged(QtProperty *property, bool va void QtFlagPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - QtProperty *flagProperty = m_flagToProperty.value(property, 0); - if (flagProperty == 0) + QtProperty *flagProperty = m_flagToProperty.value(property, nullptr); + if (flagProperty == nullptr) return; - m_propertyToFlags[flagProperty].replace(m_propertyToFlags[flagProperty].indexOf(property), 0); + m_propertyToFlags[flagProperty].replace(m_propertyToFlags[flagProperty].indexOf(property), nullptr); m_flagToProperty.remove(property); } @@ -4144,11 +4144,11 @@ QtSizePolicyPropertyManagerPrivate::QtSizePolicyPropertyManagerPrivate() void QtSizePolicyPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) { - if (QtProperty *prop = m_hStretchToProperty.value(property, 0)) { + if (QtProperty *prop = m_hStretchToProperty.value(property, nullptr)) { QSizePolicy sp = m_values[prop]; sp.setHorizontalStretch(value); q_ptr->setValue(prop, sp); - } else if (QtProperty *prop = m_vStretchToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_vStretchToProperty.value(property, nullptr)) { QSizePolicy sp = m_values[prop]; sp.setVerticalStretch(value); q_ptr->setValue(prop, sp); @@ -4157,11 +4157,11 @@ void QtSizePolicyPropertyManagerPrivate::slotIntChanged(QtProperty *property, in void QtSizePolicyPropertyManagerPrivate::slotEnumChanged(QtProperty *property, int value) { - if (QtProperty *prop = m_hPolicyToProperty.value(property, 0)) { + if (QtProperty *prop = m_hPolicyToProperty.value(property, nullptr)) { QSizePolicy sp = m_values[prop]; sp.setHorizontalPolicy(metaEnumProvider()->indexToSizePolicy(value)); q_ptr->setValue(prop, sp); - } else if (QtProperty *prop = m_vPolicyToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_vPolicyToProperty.value(property, nullptr)) { QSizePolicy sp = m_values[prop]; sp.setVerticalPolicy(metaEnumProvider()->indexToSizePolicy(value)); q_ptr->setValue(prop, sp); @@ -4170,17 +4170,17 @@ void QtSizePolicyPropertyManagerPrivate::slotEnumChanged(QtProperty *property, i void QtSizePolicyPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_hStretchToProperty.value(property, 0)) { - m_propertyToHStretch[pointProp] = 0; + if (QtProperty *pointProp = m_hStretchToProperty.value(property, nullptr)) { + m_propertyToHStretch[pointProp] = nullptr; m_hStretchToProperty.remove(property); - } else if (QtProperty *pointProp = m_vStretchToProperty.value(property, 0)) { - m_propertyToVStretch[pointProp] = 0; + } else if (QtProperty *pointProp = m_vStretchToProperty.value(property, nullptr)) { + m_propertyToVStretch[pointProp] = nullptr; m_vStretchToProperty.remove(property); - } else if (QtProperty *pointProp = m_hPolicyToProperty.value(property, 0)) { - m_propertyToHPolicy[pointProp] = 0; + } else if (QtProperty *pointProp = m_hPolicyToProperty.value(property, nullptr)) { + m_propertyToHPolicy[pointProp] = nullptr; m_hPolicyToProperty.remove(property); - } else if (QtProperty *pointProp = m_vPolicyToProperty.value(property, 0)) { - m_propertyToVPolicy[pointProp] = 0; + } else if (QtProperty *pointProp = m_vPolicyToProperty.value(property, nullptr)) { + m_propertyToVPolicy[pointProp] = nullptr; m_vPolicyToProperty.remove(property); } } @@ -4480,7 +4480,7 @@ class QtFontPropertyManagerPrivate QtFontPropertyManagerPrivate::QtFontPropertyManagerPrivate() : m_settingValue(false), - m_fontDatabaseChangeTimer(0) + m_fontDatabaseChangeTimer(nullptr) { } @@ -4488,7 +4488,7 @@ void QtFontPropertyManagerPrivate::slotIntChanged(QtProperty *property, int valu { if (m_settingValue) return; - if (QtProperty *prop = m_pointSizeToProperty.value(property, 0)) { + if (QtProperty *prop = m_pointSizeToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setPointSize(value); q_ptr->setValue(prop, f); @@ -4499,7 +4499,7 @@ void QtFontPropertyManagerPrivate::slotEnumChanged(QtProperty *property, int val { if (m_settingValue) return; - if (QtProperty *prop = m_familyToProperty.value(property, 0)) { + if (QtProperty *prop = m_familyToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setFamily(m_familyNames.at(value)); q_ptr->setValue(prop, f); @@ -4510,23 +4510,23 @@ void QtFontPropertyManagerPrivate::slotBoolChanged(QtProperty *property, bool va { if (m_settingValue) return; - if (QtProperty *prop = m_boldToProperty.value(property, 0)) { + if (QtProperty *prop = m_boldToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setBold(value); q_ptr->setValue(prop, f); - } else if (QtProperty *prop = m_italicToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_italicToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setItalic(value); q_ptr->setValue(prop, f); - } else if (QtProperty *prop = m_underlineToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_underlineToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setUnderline(value); q_ptr->setValue(prop, f); - } else if (QtProperty *prop = m_strikeOutToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_strikeOutToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setStrikeOut(value); q_ptr->setValue(prop, f); - } else if (QtProperty *prop = m_kerningToProperty.value(property, 0)) { + } else if (QtProperty *prop = m_kerningToProperty.value(property, nullptr)) { QFont f = m_values[prop]; f.setKerning(value); q_ptr->setValue(prop, f); @@ -4535,26 +4535,26 @@ void QtFontPropertyManagerPrivate::slotBoolChanged(QtProperty *property, bool va void QtFontPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_pointSizeToProperty.value(property, 0)) { - m_propertyToPointSize[pointProp] = 0; + if (QtProperty *pointProp = m_pointSizeToProperty.value(property, nullptr)) { + m_propertyToPointSize[pointProp] = nullptr; m_pointSizeToProperty.remove(property); - } else if (QtProperty *pointProp = m_familyToProperty.value(property, 0)) { - m_propertyToFamily[pointProp] = 0; + } else if (QtProperty *pointProp = m_familyToProperty.value(property, nullptr)) { + m_propertyToFamily[pointProp] = nullptr; m_familyToProperty.remove(property); - } else if (QtProperty *pointProp = m_boldToProperty.value(property, 0)) { - m_propertyToBold[pointProp] = 0; + } else if (QtProperty *pointProp = m_boldToProperty.value(property, nullptr)) { + m_propertyToBold[pointProp] = nullptr; m_boldToProperty.remove(property); - } else if (QtProperty *pointProp = m_italicToProperty.value(property, 0)) { - m_propertyToItalic[pointProp] = 0; + } else if (QtProperty *pointProp = m_italicToProperty.value(property, nullptr)) { + m_propertyToItalic[pointProp] = nullptr; m_italicToProperty.remove(property); - } else if (QtProperty *pointProp = m_underlineToProperty.value(property, 0)) { - m_propertyToUnderline[pointProp] = 0; + } else if (QtProperty *pointProp = m_underlineToProperty.value(property, nullptr)) { + m_propertyToUnderline[pointProp] = nullptr; m_underlineToProperty.remove(property); - } else if (QtProperty *pointProp = m_strikeOutToProperty.value(property, 0)) { - m_propertyToStrikeOut[pointProp] = 0; + } else if (QtProperty *pointProp = m_strikeOutToProperty.value(property, nullptr)) { + m_propertyToStrikeOut[pointProp] = nullptr; m_strikeOutToProperty.remove(property); - } else if (QtProperty *pointProp = m_kerningToProperty.value(property, 0)) { - m_propertyToKerning[pointProp] = 0; + } else if (QtProperty *pointProp = m_kerningToProperty.value(property, nullptr)) { + m_propertyToKerning[pointProp] = nullptr; m_kerningToProperty.remove(property); } } diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index d90fb80..e19073e 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -64,7 +64,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtStringPropertyManager : public QtAbstractPro { Q_OBJECT public: - QtStringPropertyManager(QObject *parent = 0); + QtStringPropertyManager(QObject *parent = nullptr); ~QtStringPropertyManager(); QString value(const QtProperty *property) const; @@ -95,7 +95,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtDatePropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtDatePropertyManager(QObject *parent = 0); + QtDatePropertyManager(QObject *parent = nullptr); ~QtDatePropertyManager(); QDate value(const QtProperty *property) const; @@ -126,7 +126,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtTimePropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtTimePropertyManager(QObject *parent = 0); + QtTimePropertyManager(QObject *parent = nullptr); ~QtTimePropertyManager(); QTime value(const QtProperty *property) const; @@ -151,7 +151,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtDateTimePropertyManager : public QtAbstractP { Q_OBJECT public: - QtDateTimePropertyManager(QObject *parent = 0); + QtDateTimePropertyManager(QObject *parent = nullptr); ~QtDateTimePropertyManager(); QDateTime value(const QtProperty *property) const; @@ -176,7 +176,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtKeySequencePropertyManager : public QtAbstra { Q_OBJECT public: - QtKeySequencePropertyManager(QObject *parent = 0); + QtKeySequencePropertyManager(QObject *parent = nullptr); ~QtKeySequencePropertyManager(); QKeySequence value(const QtProperty *property) const; @@ -201,7 +201,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCharPropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtCharPropertyManager(QObject *parent = 0); + QtCharPropertyManager(QObject *parent = nullptr); ~QtCharPropertyManager(); QChar value(const QtProperty *property) const; @@ -227,7 +227,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtLocalePropertyManager : public QtAbstractPro { Q_OBJECT public: - QtLocalePropertyManager(QObject *parent = 0); + QtLocalePropertyManager(QObject *parent = nullptr); ~QtLocalePropertyManager(); QtEnumPropertyManager *subEnumPropertyManager() const; @@ -256,7 +256,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtPointPropertyManager : public QtAbstractProp { Q_OBJECT public: - QtPointPropertyManager(QObject *parent = 0); + QtPointPropertyManager(QObject *parent = nullptr); ~QtPointPropertyManager(); QtIntPropertyManager *subIntPropertyManager() const; @@ -285,7 +285,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtPointFPropertyManager : public QtAbstractPro { Q_OBJECT public: - QtPointFPropertyManager(QObject *parent = 0); + QtPointFPropertyManager(QObject *parent = nullptr); ~QtPointFPropertyManager(); QtDoublePropertyManager *subDoublePropertyManager() const; @@ -317,7 +317,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtSizePropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtSizePropertyManager(QObject *parent = 0); + QtSizePropertyManager(QObject *parent = nullptr); ~QtSizePropertyManager(); QtIntPropertyManager *subIntPropertyManager() const; @@ -352,7 +352,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtSizeFPropertyManager : public QtAbstractProp { Q_OBJECT public: - QtSizeFPropertyManager(QObject *parent = 0); + QtSizeFPropertyManager(QObject *parent = nullptr); ~QtSizeFPropertyManager(); QtDoublePropertyManager *subDoublePropertyManager() const; @@ -390,7 +390,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtRectPropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtRectPropertyManager(QObject *parent = 0); + QtRectPropertyManager(QObject *parent = nullptr); ~QtRectPropertyManager(); QtIntPropertyManager *subIntPropertyManager() const; @@ -422,7 +422,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtRectFPropertyManager : public QtAbstractProp { Q_OBJECT public: - QtRectFPropertyManager(QObject *parent = 0); + QtRectFPropertyManager(QObject *parent = nullptr); ~QtRectFPropertyManager(); QtDoublePropertyManager *subDoublePropertyManager() const; @@ -457,7 +457,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtEnumPropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtEnumPropertyManager(QObject *parent = 0); + QtEnumPropertyManager(QObject *parent = nullptr); ~QtEnumPropertyManager(); int value(const QtProperty *property) const; @@ -489,7 +489,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtFlagPropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtFlagPropertyManager(QObject *parent = 0); + QtFlagPropertyManager(QObject *parent = nullptr); ~QtFlagPropertyManager(); QtBoolPropertyManager *subBoolPropertyManager() const; @@ -521,7 +521,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtSizePolicyPropertyManager : public QtAbstrac { Q_OBJECT public: - QtSizePolicyPropertyManager(QObject *parent = 0); + QtSizePolicyPropertyManager(QObject *parent = nullptr); ~QtSizePolicyPropertyManager(); QtIntPropertyManager *subIntPropertyManager() const; @@ -552,7 +552,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtFontPropertyManager : public QtAbstractPrope { Q_OBJECT public: - QtFontPropertyManager(QObject *parent = 0); + QtFontPropertyManager(QObject *parent = nullptr); ~QtFontPropertyManager(); QtIntPropertyManager *subIntPropertyManager() const; @@ -589,7 +589,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCursorPropertyManager : public QtAbstractPro { Q_OBJECT public: - QtCursorPropertyManager(QObject *parent = 0); + QtCursorPropertyManager(QObject *parent = nullptr); ~QtCursorPropertyManager(); #ifndef QT_NO_CURSOR diff --git a/src/qttreepropertybrowser.cpp b/src/qttreepropertybrowser.cpp index 1268a42..e953d37 100644 --- a/src/qttreepropertybrowser.cpp +++ b/src/qttreepropertybrowser.cpp @@ -119,7 +119,7 @@ class QtPropertyEditorView : public QTreeWidget { Q_OBJECT public: - QtPropertyEditorView(QWidget *parent = 0); + QtPropertyEditorView(QWidget *parent = nullptr); void setEditorPrivate(QtTreePropertyBrowserPrivate *editorPrivate) { m_editorPrivate = editorPrivate; } @@ -147,7 +147,7 @@ class QtPropertyEditorView : public QTreeWidget QtPropertyEditorView::QtPropertyEditorView(QWidget *parent) : QTreeWidget(parent), - m_editorPrivate(0) + m_editorPrivate(nullptr) { connect(header(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(resizeColumnToContents(int))); } @@ -228,8 +228,12 @@ class QtPropertyEditorDelegate : public QItemDelegate { Q_OBJECT public: - QtPropertyEditorDelegate(QObject *parent = 0) - : QItemDelegate(parent), m_editorPrivate(0), m_editedItem(0), m_editedWidget(0), m_disablePainting(false) + QtPropertyEditorDelegate(QObject *parent = nullptr) : + QItemDelegate(parent), + m_editorPrivate(nullptr), + m_editedItem(nullptr), + m_editedWidget(nullptr), + m_disablePainting(false) {} void setEditorPrivate(QtTreePropertyBrowserPrivate *editorPrivate) @@ -305,15 +309,15 @@ void QtPropertyEditorDelegate::slotEditorDestroyed(QObject *object) m_editorToProperty.erase(it); } if (m_editedWidget == w) { - m_editedWidget = 0; - m_editedItem = 0; + m_editedWidget = nullptr; + m_editedItem = nullptr; } } } void QtPropertyEditorDelegate::closeEditor(QtProperty *property) { - if (QWidget *w = m_propertyToEditor.value(property, 0)) + if (QWidget *w = m_propertyToEditor.value(property, nullptr)) w->deleteLater(); } @@ -337,7 +341,7 @@ QWidget *QtPropertyEditorDelegate::createEditor(QWidget *parent, return editor; } } - return 0; + return nullptr; } void QtPropertyEditorDelegate::updateEditorGeometry(QWidget *editor, @@ -370,7 +374,7 @@ void QtPropertyEditorDelegate::paint(QPainter *painter, const QStyleOptionViewIt opt.palette.setColor(QPalette::Text, opt.palette.color(QPalette::BrightText)); } else { c = m_editorPrivate->calculatedBackgroundColor(m_editorPrivate->indexToBrowserItem(index)); - if (c.isValid() && (opt.features & QStyleOptionViewItemV2::Alternate)) + if (c.isValid() && (opt.features & QStyleOptionViewItem::Alternate)) c = c.lighter(112); } if (c.isValid()) @@ -431,10 +435,10 @@ bool QtPropertyEditorDelegate::eventFilter(QObject *object, QEvent *event) // -------- QtTreePropertyBrowserPrivate implementation QtTreePropertyBrowserPrivate::QtTreePropertyBrowserPrivate() : - m_treeWidget(0), + m_treeWidget(nullptr), m_headerVisible(true), m_resizeMode(QtTreePropertyBrowser::Stretch), - m_delegate(0), + m_delegate(nullptr), m_markPropertiesWithoutValue(false), m_browserChangedBlocked(false) { @@ -481,8 +485,8 @@ void QtTreePropertyBrowserPrivate::init(QWidget *parent) m_treeWidget->setColumnCount(2); QStringList labels; - labels.append(QApplication::translate("QtTreePropertyBrowser", "Property", 0)); - labels.append(QApplication::translate("QtTreePropertyBrowser", "Value", 0)); + labels.append(QApplication::translate("QtTreePropertyBrowser", "Property", nullptr)); + labels.append(QApplication::translate("QtTreePropertyBrowser", "Value", nullptr)); m_treeWidget->setHeaderLabels(labels); m_treeWidget->setAlternatingRowColors(true); m_treeWidget->setEditTriggers(QAbstractItemView::EditKeyPressed); @@ -503,14 +507,14 @@ QtBrowserItem *QtTreePropertyBrowserPrivate::currentItem() const { if (QTreeWidgetItem *treeItem = m_treeWidget->currentItem()) return m_itemToIndex.value(treeItem); - return 0; + return nullptr; } void QtTreePropertyBrowserPrivate::setCurrentItem(QtBrowserItem *browserItem, bool block) { const bool blocked = block ? m_treeWidget->blockSignals(true) : false; - if (browserItem == 0) - m_treeWidget->setCurrentItem(0); + if (browserItem == nullptr) + m_treeWidget->setCurrentItem(nullptr); else m_treeWidget->setCurrentItem(m_indexToItem.value(browserItem)); if (block) @@ -523,7 +527,7 @@ QtProperty *QtTreePropertyBrowserPrivate::indexToProperty(const QModelIndex &ind QtBrowserItem *idx = m_itemToIndex.value(item); if (idx) return idx->property(); - return 0; + return nullptr; } QtBrowserItem *QtTreePropertyBrowserPrivate::indexToBrowserItem(const QModelIndex &index) const @@ -585,7 +589,7 @@ void QtTreePropertyBrowserPrivate::propertyInserted(QtBrowserItem *index, QtBrow QTreeWidgetItem *afterItem = m_indexToItem.value(afterIndex); QTreeWidgetItem *parentItem = m_indexToItem.value(index->parent()); - QTreeWidgetItem *newItem = 0; + QTreeWidgetItem *newItem = nullptr; if (parentItem) { newItem = new QTreeWidgetItem(parentItem, afterItem); } else { @@ -605,7 +609,7 @@ void QtTreePropertyBrowserPrivate::propertyRemoved(QtBrowserItem *index) QTreeWidgetItem *item = m_indexToItem.value(index); if (m_treeWidget->currentItem() == item) { - m_treeWidget->setCurrentItem(0); + m_treeWidget->setCurrentItem(nullptr); } delete item; @@ -699,7 +703,7 @@ void QtTreePropertyBrowserPrivate::slotCurrentBrowserItemChanged(QtBrowserItem * void QtTreePropertyBrowserPrivate::slotCurrentTreeItemChanged(QTreeWidgetItem *newItem, QTreeWidgetItem *) { - QtBrowserItem *browserItem = newItem ? m_itemToIndex.value(newItem) : 0; + QtBrowserItem *browserItem = newItem ? m_itemToIndex.value(newItem) : nullptr; m_browserChangedBlocked = true; q_ptr->setCurrentItem(browserItem); m_browserChangedBlocked = false; @@ -712,7 +716,7 @@ QTreeWidgetItem *QtTreePropertyBrowserPrivate::editedItem() const void QtTreePropertyBrowserPrivate::editItem(QtBrowserItem *browserItem) { - if (QTreeWidgetItem *treeItem = m_indexToItem.value(browserItem, 0)) { + if (QTreeWidgetItem *treeItem = m_indexToItem.value(browserItem, nullptr)) { m_treeWidget->setCurrentItem (treeItem, 1); m_treeWidget->editItem(treeItem, 1); } diff --git a/src/qttreepropertybrowser.h b/src/qttreepropertybrowser.h index c5f7fa8..03c6d36 100644 --- a/src/qttreepropertybrowser.h +++ b/src/qttreepropertybrowser.h @@ -71,7 +71,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtTreePropertyBrowser : public QtAbstractPrope ResizeToContents }; - QtTreePropertyBrowser(QWidget *parent = 0); + QtTreePropertyBrowser(QWidget *parent = nullptr); ~QtTreePropertyBrowser(); int indentation() const; diff --git a/src/qtvariantproperty.cpp b/src/qtvariantproperty.cpp index 7c4f038..83f7a23 100644 --- a/src/qtvariantproperty.cpp +++ b/src/qtvariantproperty.cpp @@ -140,12 +140,12 @@ Q_GLOBAL_STATIC(PropertyMap, propertyToWrappedProperty) static QtProperty *wrappedProperty(QtProperty *property) { - return propertyToWrappedProperty()->value(property, 0); + return propertyToWrappedProperty()->value(property, nullptr); } class QtVariantPropertyPrivate { - QtVariantProperty *q_ptr; +// QtVariantProperty *q_ptr; public: QtVariantPropertyPrivate(QtVariantPropertyManager *m) : manager(m) {} @@ -424,7 +424,7 @@ QtVariantProperty *QtVariantPropertyManagerPrivate::createSubProperty(QtVariantP { int type = internalPropertyToType(internal); if (!type) - return 0; + return nullptr; bool wasCreatingSubProperties = m_creatingSubProperties; m_creatingSubProperties = true; @@ -462,13 +462,13 @@ void QtVariantPropertyManagerPrivate::slotPropertyInserted(QtProperty *property, if (m_creatingProperty) return; - QtVariantProperty *varParent = m_internalToProperty.value(parent, 0); + QtVariantProperty *varParent = m_internalToProperty.value(parent, nullptr); if (!varParent) return; - QtVariantProperty *varAfter = 0; + QtVariantProperty *varAfter = nullptr; if (after) { - varAfter = m_internalToProperty.value(after, 0); + varAfter = m_internalToProperty.value(after, nullptr); if (!varAfter) return; } @@ -480,7 +480,7 @@ void QtVariantPropertyManagerPrivate::slotPropertyRemoved(QtProperty *property, { Q_UNUSED(parent) - QtVariantProperty *varProperty = m_internalToProperty.value(property, 0); + QtVariantProperty *varProperty = m_internalToProperty.value(property, nullptr); if (!varProperty) return; @@ -489,7 +489,7 @@ void QtVariantPropertyManagerPrivate::slotPropertyRemoved(QtProperty *property, void QtVariantPropertyManagerPrivate::valueChanged(QtProperty *property, const QVariant &val) { - QtVariantProperty *varProp = m_internalToProperty.value(property, 0); + QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr); if (!varProp) return; emit q_ptr->valueChanged(varProp, val); @@ -503,7 +503,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, int void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, int min, int max) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) { + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) { emit q_ptr->attributeChanged(varProp, m_minimumAttribute, QVariant(min)); emit q_ptr->attributeChanged(varProp, m_maximumAttribute, QVariant(max)); } @@ -511,7 +511,7 @@ void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, int void QtVariantPropertyManagerPrivate::slotSingleStepChanged(QtProperty *property, int step) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_singleStepAttribute, QVariant(step)); } @@ -522,7 +522,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, dou void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, double min, double max) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) { + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) { emit q_ptr->attributeChanged(varProp, m_minimumAttribute, QVariant(min)); emit q_ptr->attributeChanged(varProp, m_maximumAttribute, QVariant(max)); } @@ -530,13 +530,13 @@ void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, dou void QtVariantPropertyManagerPrivate::slotSingleStepChanged(QtProperty *property, double step) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_singleStepAttribute, QVariant(step)); } void QtVariantPropertyManagerPrivate::slotDecimalsChanged(QtProperty *property, int prec) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_decimalsAttribute, QVariant(prec)); } @@ -547,7 +547,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, boo void QtVariantPropertyManagerPrivate::slotTextVisibleChanged(QtProperty *property, bool textVisible) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_textVisibleAttribute, QVariant(textVisible)); } @@ -558,13 +558,13 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotRegExpChanged(QtProperty *property, const QRegExp ®Exp) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_regExpAttribute, QVariant(regExp)); } void QtVariantPropertyManagerPrivate::slotMaxLengthChanged(QtProperty *property, int maxlen) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_maxlengthAttribute, QVariant(maxlen)); } @@ -575,7 +575,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, const QDate &min, const QDate &max) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) { + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) { emit q_ptr->attributeChanged(varProp, m_minimumAttribute, QVariant(min)); emit q_ptr->attributeChanged(varProp, m_maximumAttribute, QVariant(max)); } @@ -625,7 +625,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, const QSize &min, const QSize &max) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) { + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) { emit q_ptr->attributeChanged(varProp, m_minimumAttribute, QVariant(min)); emit q_ptr->attributeChanged(varProp, m_maximumAttribute, QVariant(max)); } @@ -638,7 +638,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotRangeChanged(QtProperty *property, const QSizeF &min, const QSizeF &max) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) { + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) { emit q_ptr->attributeChanged(varProp, m_minimumAttribute, QVariant(min)); emit q_ptr->attributeChanged(varProp, m_maximumAttribute, QVariant(max)); } @@ -651,7 +651,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotConstraintChanged(QtProperty *property, const QRect &constraint) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_constraintAttribute, QVariant(constraint)); } @@ -662,7 +662,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotConstraintChanged(QtProperty *property, const QRectF &constraint) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_constraintAttribute, QVariant(constraint)); } @@ -673,13 +673,13 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotEnumNamesChanged(QtProperty *property, const QStringList &enumNames) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_enumNamesAttribute, QVariant(enumNames)); } void QtVariantPropertyManagerPrivate::slotEnumIconsChanged(QtProperty *property, const QMap &enumIcons) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) { + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) { QVariant v; qVariantSetValue(v, enumIcons); emit q_ptr->attributeChanged(varProp, m_enumIconsAttribute, v); @@ -705,7 +705,7 @@ void QtVariantPropertyManagerPrivate::slotValueChanged(QtProperty *property, con void QtVariantPropertyManagerPrivate::slotFlagNamesChanged(QtProperty *property, const QStringList &flagNames) { - if (QtVariantProperty *varProp = m_internalToProperty.value(property, 0)) + if (QtVariantProperty *varProp = m_internalToProperty.value(property, nullptr)) emit q_ptr->attributeChanged(varProp, m_flagNamesAttribute, QVariant(flagNames)); } @@ -1297,7 +1297,7 @@ QtVariantProperty *QtVariantPropertyManager::variantProperty(const QtProperty *p { const QMap >::const_iterator it = d_ptr->m_propertyToType.constFind(property); if (it == d_ptr->m_propertyToType.constEnd()) - return 0; + return nullptr; return it.value().first; } @@ -1331,7 +1331,7 @@ bool QtVariantPropertyManager::isPropertyTypeSupported(int propertyType) const QtVariantProperty *QtVariantPropertyManager::addProperty(int propertyType, const QString &name) { if (!isPropertyTypeSupported(propertyType)) - return 0; + return nullptr; bool wasCreating = d_ptr->m_creatingProperty; d_ptr->m_creatingProperty = true; @@ -1341,14 +1341,14 @@ QtVariantProperty *QtVariantPropertyManager::addProperty(int propertyType, const d_ptr->m_propertyType = 0; if (!property) - return 0; + return nullptr; return variantProperty(property); } namespace{ void addPropertyRecusively(QtVariantPropertyManager * manager, - QtVariantProperty * prop, QtVariantProperty * newProp = 0) + QtVariantProperty * prop, QtVariantProperty * newProp = nullptr) { if (!newProp) { @@ -1408,8 +1408,8 @@ void QtVariantPropertyManager::setProperties(QSet properties) */ QVariant QtVariantPropertyManager::value(const QtProperty *property) const { - QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); - if (internProp == 0) + QtProperty *internProp = propertyToWrappedProperty()->value(property, nullptr); + if (internProp == nullptr) return QVariant(); QtAbstractPropertyManager *manager = internProp->propertyManager(); @@ -1526,8 +1526,8 @@ QVariant QtVariantPropertyManager::attributeValue(const QtProperty *property, co if (itAttr == attributes.constEnd()) return QVariant(); - QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); - if (internProp == 0) + QtProperty *internProp = propertyToWrappedProperty()->value(property, nullptr); + if (internProp == nullptr) return QVariant(); QtAbstractPropertyManager *manager = internProp->propertyManager(); @@ -1668,11 +1668,11 @@ void QtVariantPropertyManager::setValue(QtProperty *property, const QVariant &va int valType = valueType(property); - if (propType != valType && !val.canConvert(static_cast(valType))) + if (propType != valType && !val.canConvert(valType)) return; - QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); - if (internProp == 0) + QtProperty *internProp = propertyToWrappedProperty()->value(property, nullptr); + if (internProp == nullptr) return; @@ -1771,12 +1771,11 @@ void QtVariantPropertyManager::setAttribute(QtProperty *property, if (!attrType) return; - if (attrType != attributeType(propertyType(property), attribute) && - !value.canConvert((QVariant::Type)attrType)) + if (attrType != attributeType(propertyType(property), attribute) && !value.canConvert(attrType)) return; - QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); - if (internProp == 0) + QtProperty *internProp = propertyToWrappedProperty()->value(property, nullptr); + if (internProp == nullptr) return; QtAbstractPropertyManager *manager = internProp->propertyManager(); @@ -1870,7 +1869,7 @@ bool QtVariantPropertyManager::hasValue(const QtProperty *property) const */ QString QtVariantPropertyManager::valueText(const QtProperty *property) const { - const QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); + const QtProperty *internProp = propertyToWrappedProperty()->value(property, nullptr); return internProp ? internProp->valueText() : QString(); } @@ -1879,7 +1878,7 @@ QString QtVariantPropertyManager::valueText(const QtProperty *property) const */ QIcon QtVariantPropertyManager::valueIcon(const QtProperty *property) const { - const QtProperty *internProp = propertyToWrappedProperty()->value(property, 0); + const QtProperty *internProp = propertyToWrappedProperty()->value(property, nullptr); return internProp ? internProp->valueIcon() : QIcon(); } @@ -1895,7 +1894,7 @@ void QtVariantPropertyManager::initializeProperty(QtProperty *property) QMap::ConstIterator it = d_ptr->m_typeToPropertyManager.find(d_ptr->m_propertyType); if (it != d_ptr->m_typeToPropertyManager.constEnd()) { - QtProperty *internProp = 0; + QtProperty *internProp = nullptr; if (!d_ptr->m_creatingSubProperties) { QtAbstractPropertyManager *manager = it.value(); internProp = manager->addProperty(); @@ -1905,7 +1904,7 @@ void QtVariantPropertyManager::initializeProperty(QtProperty *property) if (internProp) { QList children = internProp->subProperties(); QListIterator itChild(children); - QtVariantProperty *lastProperty = 0; + QtVariantProperty *lastProperty = nullptr; while (itChild.hasNext()) { QtVariantProperty *prop = d_ptr->createSubProperty(varProp, lastProperty, itChild.next()); lastProperty = prop ? prop : lastProperty; @@ -1943,7 +1942,7 @@ void QtVariantPropertyManager::uninitializeProperty(QtProperty *property) QtProperty *QtVariantPropertyManager::createProperty() { if (!d_ptr->m_creatingProperty) - return 0; + return nullptr; QtVariantProperty *property = new QtVariantProperty(this); d_ptr->m_propertyToType.insert(property, qMakePair(property, d_ptr->m_propertyType)); @@ -2243,9 +2242,9 @@ QWidget *QtVariantEditorFactory::createEditor(QtVariantPropertyManager *manager, QWidget *parent) { const int propType = manager->propertyType(property); - QtAbstractEditorFactoryBase *factory = d_ptr->m_typeToFactory.value(propType, 0); + QtAbstractEditorFactoryBase *factory = d_ptr->m_typeToFactory.value(propType, nullptr); if (!factory) - return 0; + return nullptr; return factory->createEditor(wrappedProperty(property), parent); } diff --git a/src/qtvariantproperty.h b/src/qtvariantproperty.h index fbc9fed..1f1a252 100644 --- a/src/qtvariantproperty.h +++ b/src/qtvariantproperty.h @@ -80,7 +80,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtVariantPropertyManager : public QtAbstractPr { Q_OBJECT public: - QtVariantPropertyManager(QObject *parent = 0); + QtVariantPropertyManager(QObject *parent = nullptr); ~QtVariantPropertyManager(); virtual QtVariantProperty *addProperty(int propertyType, const QString &name = QString()); @@ -169,7 +169,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtVariantEditorFactory : public QtAbstractEdit { Q_OBJECT public: - QtVariantEditorFactory(QObject *parent = 0); + QtVariantEditorFactory(QObject *parent = nullptr); ~QtVariantEditorFactory(); protected: void connectPropertyManager(QtVariantPropertyManager *manager); From 1be6d491df302a7dbfd27c0691a8a65c188dfd70 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 22 Dec 2019 02:20:10 +0300 Subject: [PATCH 32/39] Split up the string property --- src/CMakeLists.txt | 1 + src/managers/string_property.cpp | 252 +++++++++++++++++++++++++++++++ src/managers/string_property.h | 47 ++++++ src/qtpropertymanager.cpp | 223 --------------------------- src/qtpropertymanager.h | 31 +--- 5 files changed, 301 insertions(+), 253 deletions(-) create mode 100644 src/managers/string_property.cpp create mode 100644 src/managers/string_property.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa04032..ddbb1d9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,6 +16,7 @@ set(_SRCS qtvariantproperty.cpp managers/color_property.cpp managers/group_property.cpp + managers/string_property.cpp managers/int_property.cpp managers/double_property.cpp managers/bool_property.cpp diff --git a/src/managers/string_property.cpp b/src/managers/string_property.cpp new file mode 100644 index 0000000..ebcef57 --- /dev/null +++ b/src/managers/string_property.cpp @@ -0,0 +1,252 @@ + +#include "string_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + + +// QtStringPropertyManager + +class QtStringPropertyManagerPrivate +{ + QtStringPropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtStringPropertyManager) +public: + + struct Data + { + Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard), maxLen(-1) + { + } + QString val; + QRegExp regExp; + int maxLen; + }; + + typedef QMap PropertyValueMap; + QMap m_values; +}; + +/*! + \class QtStringPropertyManager + + \brief The QtStringPropertyManager provides and manages QString properties. + + A string property's value can be retrieved using the value() + function, and set using the setValue() slot. + + The current value can be checked against a regular expression. To + set the regular expression use the setRegExp() slot, use the + regExp() function to retrieve the currently set expression. + + In addition, QtStringPropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes, and the regExpChanged() signal which is emitted whenever + such a property changes its currently set regular expression. + + \sa QtAbstractPropertyManager, QtLineEditFactory +*/ + +/*! + \fn void QtStringPropertyManager::valueChanged(QtProperty *property, const QString &value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the + new \a value as parameters. + + \sa setValue() +*/ + +/*! + \fn void QtStringPropertyManager::regExpChanged(QtProperty *property, const QRegExp ®Exp) + + This signal is emitted whenever a property created by this manager + changes its currenlty set regular expression, passing a pointer to + the \a property and the new \a regExp as parameters. + + \sa setRegExp() +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtStringPropertyManager::QtStringPropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtStringPropertyManagerPrivate; + d_ptr->q_ptr = this; +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtStringPropertyManager::~QtStringPropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the given \a property's value. + + If the given property is not managed by this manager, this + function returns an empty string. + + \sa setValue() +*/ +QString QtStringPropertyManager::value(const QtProperty *property) const +{ + return getValue(d_ptr->m_values, property); +} + +/*! + Returns the given \a property's currently set regular expression. + + If the given \a property is not managed by this manager, this + function returns an empty expression. + + \sa setRegExp() +*/ +QRegExp QtStringPropertyManager::regExp(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::regExp, property, QRegExp()); +} + +/*! + Returns the given \a property's currently set maximum length. + + If the given \a property is not managed by this manager, this + function returns an empty expression. + + \sa setMaxLength() +*/ +int QtStringPropertyManager::maxLength(const QtProperty *property) const +{ + return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::maxLen, property, -1); +} + +/*! + \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; +} + +/*! + \fn void QtStringPropertyManager::setValue(QtProperty *property, const QString &value) + + Sets the value of the given \a property to \a value. + + If the specified \a value doesn't match the given \a property's + regular expression, this function does nothing. + + \sa value(), setRegExp(), valueChanged() +*/ +void QtStringPropertyManager::setValue(QtProperty *property, const QString &val) +{ + 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.val == val) + return; + + if ((data.maxLen > 0) && (val.length() > data.maxLen)) + return; + + if (data.regExp.isValid() && !data.regExp.exactMatch(val)) + return; + + data.val = val; + + it.value() = data; + + emit propertyChanged(property); + emit valueChanged(property, data.val); +} + +/*! + Sets the regular expression of the given \a property to \a regExp. + + \sa regExp(), setValue(), regExpChanged() +*/ +void QtStringPropertyManager::setRegExp(QtProperty *property, const QRegExp ®Exp) +{ + 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.regExp == regExp) + return; + + data.regExp = regExp; + + it.value() = data; + + emit regExpChanged(property, data.regExp); +} + +void QtStringPropertyManager::setMaxLength(QtProperty *property, int maxlen) +{ + 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.maxLen == maxlen) + return; + + if (maxlen < -1) + return; + + data.maxLen = maxlen; + + it.value() = data; + + emit maxLenChanged(property, data.maxLen); +} + +/*! + \reimp +*/ +void QtStringPropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property] = QtStringPropertyManagerPrivate::Data(); +} + +/*! + \reimp +*/ +void QtStringPropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/string_property.h b/src/managers/string_property.h new file mode 100644 index 0000000..dc5216e --- /dev/null +++ b/src/managers/string_property.h @@ -0,0 +1,47 @@ + +#ifndef QtStringPropertyManager_H +#define QtStringPropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QtStringPropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtStringPropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtStringPropertyManager(QObject *parent = nullptr); + ~QtStringPropertyManager(); + + QString value(const QtProperty *property) const; + QRegExp regExp(const QtProperty *property) const; + int maxLength(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, const QString &val); + void setRegExp(QtProperty *property, const QRegExp ®Exp); + void setMaxLength(QtProperty *property, int maxlen); +Q_SIGNALS: + void valueChanged(QtProperty *property, const QString &val); + void regExpChanged(QtProperty *property, const QRegExp ®Exp); + void maxLenChanged(QtProperty *property, int maxlen); +protected: + QString valueText(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtStringPropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtStringPropertyManager) + Q_DISABLE_COPY(QtStringPropertyManager) +}; + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index ac068b4..958e9fc 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -244,231 +244,8 @@ void QtMetaEnumProvider::localeToIndex(QLocale::Language language, QLocale::Coun Q_GLOBAL_STATIC(QtMetaEnumProvider, metaEnumProvider) -// QtStringPropertyManager -class QtStringPropertyManagerPrivate -{ - QtStringPropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtStringPropertyManager) -public: - - struct Data - { - Data() : regExp(QString(QLatin1Char('*')), Qt::CaseSensitive, QRegExp::Wildcard), maxLen(-1) - { - } - QString val; - QRegExp regExp; - int maxLen; - }; - - typedef QMap PropertyValueMap; - QMap m_values; -}; - -/*! - \class QtStringPropertyManager - - \brief The QtStringPropertyManager provides and manages QString properties. - - A string property's value can be retrieved using the value() - function, and set using the setValue() slot. - - The current value can be checked against a regular expression. To - set the regular expression use the setRegExp() slot, use the - regExp() function to retrieve the currently set expression. - In addition, QtStringPropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes, and the regExpChanged() signal which is emitted whenever - such a property changes its currently set regular expression. - - \sa QtAbstractPropertyManager, QtLineEditFactory -*/ - -/*! - \fn void QtStringPropertyManager::valueChanged(QtProperty *property, const QString &value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the - new \a value as parameters. - - \sa setValue() -*/ - -/*! - \fn void QtStringPropertyManager::regExpChanged(QtProperty *property, const QRegExp ®Exp) - - This signal is emitted whenever a property created by this manager - changes its currenlty set regular expression, passing a pointer to - the \a property and the new \a regExp as parameters. - - \sa setRegExp() -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtStringPropertyManager::QtStringPropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtStringPropertyManagerPrivate; - d_ptr->q_ptr = this; -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtStringPropertyManager::~QtStringPropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the given \a property's value. - - If the given property is not managed by this manager, this - function returns an empty string. - - \sa setValue() -*/ -QString QtStringPropertyManager::value(const QtProperty *property) const -{ - return getValue(d_ptr->m_values, property); -} - -/*! - Returns the given \a property's currently set regular expression. - - If the given \a property is not managed by this manager, this - function returns an empty expression. - - \sa setRegExp() -*/ -QRegExp QtStringPropertyManager::regExp(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::regExp, property, QRegExp()); -} - -/*! - Returns the given \a property's currently set maximum length. - - If the given \a property is not managed by this manager, this - function returns an empty expression. - - \sa setMaxLength() -*/ -int QtStringPropertyManager::maxLength(const QtProperty *property) const -{ - return getData(d_ptr->m_values, &QtStringPropertyManagerPrivate::Data::maxLen, property, -1); -} - -/*! - \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; -} - -/*! - \fn void QtStringPropertyManager::setValue(QtProperty *property, const QString &value) - - Sets the value of the given \a property to \a value. - - If the specified \a value doesn't match the given \a property's - regular expression, this function does nothing. - - \sa value(), setRegExp(), valueChanged() -*/ -void QtStringPropertyManager::setValue(QtProperty *property, const QString &val) -{ - 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.val == val) - return; - - if ((data.maxLen > 0) && (val.length() > data.maxLen)) - return; - - if (data.regExp.isValid() && !data.regExp.exactMatch(val)) - return; - - data.val = val; - - it.value() = data; - - emit propertyChanged(property); - emit valueChanged(property, data.val); -} - -/*! - Sets the regular expression of the given \a property to \a regExp. - - \sa regExp(), setValue(), regExpChanged() -*/ -void QtStringPropertyManager::setRegExp(QtProperty *property, const QRegExp ®Exp) -{ - 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.regExp == regExp) - return; - - data.regExp = regExp; - - it.value() = data; - - emit regExpChanged(property, data.regExp); -} - -void QtStringPropertyManager::setMaxLength(QtProperty *property, int maxlen) -{ - 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.maxLen == maxlen) - return; - - if (maxlen < -1) - return; - - data.maxLen = maxlen; - - it.value() = data; - - emit maxLenChanged(property, data.maxLen); -} - -/*! - \reimp -*/ -void QtStringPropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property] = QtStringPropertyManagerPrivate::Data(); -} - -/*! - \reimp -*/ -void QtStringPropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} // QtDatePropertyManager diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index e19073e..6a7f17a 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -47,6 +47,7 @@ #include "managers/double_property.h" #include "managers/bool_property.h" #include "managers/color_property.h" +#include "managers/string_property.h" #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -58,36 +59,6 @@ class QDateTime; class QLocale; -class QtStringPropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtStringPropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtStringPropertyManager(QObject *parent = nullptr); - ~QtStringPropertyManager(); - - QString value(const QtProperty *property) const; - QRegExp regExp(const QtProperty *property) const; - int maxLength(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, const QString &val); - void setRegExp(QtProperty *property, const QRegExp ®Exp); - void setMaxLength(QtProperty *property, int maxlen); -Q_SIGNALS: - void valueChanged(QtProperty *property, const QString &val); - void regExpChanged(QtProperty *property, const QRegExp ®Exp); - void maxLenChanged(QtProperty *property, int maxlen); -protected: - QString valueText(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtStringPropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtStringPropertyManager) - Q_DISABLE_COPY(QtStringPropertyManager) -}; class QtDatePropertyManagerPrivate; From 8f82c798bdb5c99fe8f5dd1f291b21c6c69d10e0 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Sun, 22 Dec 2019 02:28:17 +0300 Subject: [PATCH 33/39] Split up Date, Time, DateTime properties --- src/CMakeLists.txt | 3 + src/managers/date_property.cpp | 261 ++++++++++++++++ src/managers/date_property.h | 49 +++ src/managers/datetime_property.cpp | 140 +++++++++ src/managers/datetime_property.h | 44 +++ src/managers/time_property.cpp | 144 +++++++++ src/managers/time_property.h | 43 +++ src/qtpropertymanager.cpp | 458 ----------------------------- src/qtpropertymanager.h | 84 +----- 9 files changed, 687 insertions(+), 539 deletions(-) create mode 100644 src/managers/date_property.cpp create mode 100644 src/managers/date_property.h create mode 100644 src/managers/datetime_property.cpp create mode 100644 src/managers/datetime_property.h create mode 100644 src/managers/time_property.cpp create mode 100644 src/managers/time_property.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ddbb1d9..1bdd0db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,6 +17,9 @@ set(_SRCS managers/color_property.cpp managers/group_property.cpp managers/string_property.cpp + managers/date_property.cpp + managers/time_property.cpp + managers/datetime_property.cpp managers/int_property.cpp managers/double_property.cpp managers/bool_property.cpp diff --git a/src/managers/date_property.cpp b/src/managers/date_property.cpp new file mode 100644 index 0000000..fe3d879 --- /dev/null +++ b/src/managers/date_property.cpp @@ -0,0 +1,261 @@ + +#include "date_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include +#include + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + + + +// QtDatePropertyManager + +class QtDatePropertyManagerPrivate +{ + QtDatePropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtDatePropertyManager) +public: + + struct Data + { + Data() : val(QDate::currentDate()), minVal(QDate(1752, 9, 14)), + maxVal(QDate(7999, 12, 31)) {} + QDate val; + QDate minVal; + QDate maxVal; + QDate minimumValue() const { return minVal; } + QDate maximumValue() const { return maxVal; } + void setMinimumValue(const QDate &newMinVal) { setSimpleMinimumData(this, newMinVal); } + void setMaximumValue(const QDate &newMaxVal) { setSimpleMaximumData(this, newMaxVal); } + }; + + QString m_format; + + typedef QMap PropertyValueMap; + QMap m_values; +}; + +/*! + \class QtDatePropertyManager + + \brief The QtDatePropertyManager provides and manages QDate properties. + + A date property has a current value, and a range specifying the + valid dates. The range is defined by a minimum and a maximum + value. + + The property's values can be retrieved using the minimum(), + maximum() and value() functions, and can be set using the + setMinimum(), setMaximum() and setValue() slots. Alternatively, + the range can be defined in one go using the setRange() slot. + + In addition, QtDatePropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes, and the rangeChanged() signal which is emitted whenever + such a property changes its range of valid dates. + + \sa QtAbstractPropertyManager, QtDateEditFactory, QtDateTimePropertyManager +*/ + +/*! + \fn void QtDatePropertyManager::valueChanged(QtProperty *property, const QDate &value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the new + \a value as parameters. + + \sa setValue() +*/ + +/*! + \fn void QtDatePropertyManager::rangeChanged(QtProperty *property, const QDate &minimum, const QDate &maximum) + + This signal is emitted whenever a property created by this manager + changes its range of valid dates, passing a pointer to the \a + property and the new \a minimum and \a maximum dates. + + \sa setRange() +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtDatePropertyManager::QtDatePropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtDatePropertyManagerPrivate; + d_ptr->q_ptr = this; + + QLocale loc; + d_ptr->m_format = loc.dateFormat(QLocale::ShortFormat); +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtDatePropertyManager::~QtDatePropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the given \a property's value. + + If the given \a property is not managed by \e this manager, this + function returns an invalid date. + + \sa setValue() +*/ +QDate QtDatePropertyManager::value(const QtProperty *property) const +{ + return getValue(d_ptr->m_values, property); +} + +/*! + Returns the given \a property's minimum date. + + \sa maximum(), setRange() +*/ +QDate QtDatePropertyManager::minimum(const QtProperty *property) const +{ + return getMinimum(d_ptr->m_values, property); +} + +/*! + Returns the given \a property's maximum date. + + \sa minimum(), setRange() +*/ +QDate QtDatePropertyManager::maximum(const QtProperty *property) const +{ + return getMaximum(d_ptr->m_values, property); +} + +/*! + \reimp +*/ +QString QtDatePropertyManager::valueText(const QtProperty *property) const +{ + const QtDatePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + return it.value().val.toString(d_ptr->m_format); +} + +/*! + \fn void QtDatePropertyManager::setValue(QtProperty *property, const QDate &value) + + Sets the value of the given \a property to \a value. + + If the specified \a value is not a valid date according to the + given \a property's range, the value is adjusted to the nearest + valid value within the range. + + \sa value(), setRange(), valueChanged() +*/ +void QtDatePropertyManager::setValue(QtProperty *property, const QDate &val) +{ + void (QtDatePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, const QDate &) = nullptr; + setValueInRange(this, d_ptr, + &QtDatePropertyManager::propertyChanged, + &QtDatePropertyManager::valueChanged, + property, val, setSubPropertyValue); +} + +/*! + Sets the minimum value for the given \a property to \a minVal. + + When setting the minimum value, the maximum and current values are + adjusted if necessary (ensuring that the range remains valid and + that the current value is within in the range). + + \sa minimum(), setRange() +*/ +void QtDatePropertyManager::setMinimum(QtProperty *property, const QDate &minVal) +{ + setMinimumValue(this, d_ptr, + &QtDatePropertyManager::propertyChanged, + &QtDatePropertyManager::valueChanged, + &QtDatePropertyManager::rangeChanged, + property, minVal); +} + +/*! + Sets the maximum value for the given \a property to \a maxVal. + + When setting the maximum value, the minimum and current + values are adjusted if necessary (ensuring that the range remains + valid and that the current value is within in the range). + + \sa maximum(), setRange() +*/ +void QtDatePropertyManager::setMaximum(QtProperty *property, const QDate &maxVal) +{ + setMaximumValue(this, d_ptr, + &QtDatePropertyManager::propertyChanged, + &QtDatePropertyManager::valueChanged, + &QtDatePropertyManager::rangeChanged, + property, maxVal); +} + +/*! + \fn void QtDatePropertyManager::setRange(QtProperty *property, const QDate &minimum, const QDate &maximum) + + Sets the range of valid dates. + + This is a convenience function defining the range of valid dates + in one go; setting the \a minimum and \a maximum values for the + given \a property with a single function call. + + When setting a new date range, the current value is adjusted if + necessary (ensuring that the value remains in date range). + + \sa setMinimum(), setMaximum(), rangeChanged() +*/ +void QtDatePropertyManager::setRange(QtProperty *property, const QDate &minVal, const QDate &maxVal) +{ + void (QtDatePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, const QDate &, + const QDate &, const QDate &) = nullptr; + setBorderValues(this, d_ptr, + &QtDatePropertyManager::propertyChanged, + &QtDatePropertyManager::valueChanged, + &QtDatePropertyManager::rangeChanged, + property, minVal, maxVal, setSubPropertyRange); +} + +/*! + \reimp +*/ +void QtDatePropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property] = QtDatePropertyManagerPrivate::Data(); +} + +/*! + \reimp +*/ +void QtDatePropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/date_property.h b/src/managers/date_property.h new file mode 100644 index 0000000..bc3d21a --- /dev/null +++ b/src/managers/date_property.h @@ -0,0 +1,49 @@ + +#ifndef QtDatePropertyManager_H +#define QtDatePropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QDate; + +class QtDatePropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtDatePropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtDatePropertyManager(QObject *parent = nullptr); + ~QtDatePropertyManager(); + + QDate value(const QtProperty *property) const; + QDate minimum(const QtProperty *property) const; + QDate maximum(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, const QDate &val); + void setMinimum(QtProperty *property, const QDate &minVal); + void setMaximum(QtProperty *property, const QDate &maxVal); + void setRange(QtProperty *property, const QDate &minVal, const QDate &maxVal); +Q_SIGNALS: + void valueChanged(QtProperty *property, const QDate &val); + void rangeChanged(QtProperty *property, const QDate &minVal, const QDate &maxVal); +protected: + QString valueText(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtDatePropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtDatePropertyManager) + Q_DISABLE_COPY(QtDatePropertyManager) +}; + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif diff --git a/src/managers/datetime_property.cpp b/src/managers/datetime_property.cpp new file mode 100644 index 0000000..782778e --- /dev/null +++ b/src/managers/datetime_property.cpp @@ -0,0 +1,140 @@ + +#include "datetime_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include +#include + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// QtDateTimePropertyManager + +class QtDateTimePropertyManagerPrivate +{ + QtDateTimePropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtDateTimePropertyManager) +public: + + QString m_format; + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; +}; + +/*! \class QtDateTimePropertyManager + + \brief The QtDateTimePropertyManager provides and manages QDateTime properties. + + A date and time property has a current value which can be + retrieved using the value() function, and set using the setValue() + slot. In addition, QtDateTimePropertyManager provides the + valueChanged() signal which is emitted whenever a property created + by this manager changes. + + \sa QtAbstractPropertyManager, QtDateTimeEditFactory, QtDatePropertyManager +*/ + +/*! + \fn void QtDateTimePropertyManager::valueChanged(QtProperty *property, const QDateTime &value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the new + \a value as parameters. +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtDateTimePropertyManager::QtDateTimePropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtDateTimePropertyManagerPrivate; + d_ptr->q_ptr = this; + + QLocale loc; + d_ptr->m_format = loc.dateFormat(QLocale::ShortFormat); + d_ptr->m_format += QLatin1Char(' '); + d_ptr->m_format += loc.timeFormat(QLocale::ShortFormat); +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtDateTimePropertyManager::~QtDateTimePropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the given \a property's value. + + If the given \a property is not managed by this manager, this + function returns an invalid QDateTime object. + + \sa setValue() +*/ +QDateTime QtDateTimePropertyManager::value(const QtProperty *property) const +{ + return d_ptr->m_values.value(property, QDateTime()); +} + +/*! + \reimp +*/ +QString QtDateTimePropertyManager::valueText(const QtProperty *property) const +{ + const QtDateTimePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + return it.value().toString(d_ptr->m_format); +} + +/*! + \fn void QtDateTimePropertyManager::setValue(QtProperty *property, const QDateTime &value) + + Sets the value of the given \a property to \a value. + + \sa value(), valueChanged() +*/ +void QtDateTimePropertyManager::setValue(QtProperty *property, const QDateTime &val) +{ + setSimpleValue(d_ptr->m_values, this, + &QtDateTimePropertyManager::propertyChanged, + &QtDateTimePropertyManager::valueChanged, + property, val); +} + +/*! + \reimp +*/ +void QtDateTimePropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property] = QDateTime::currentDateTime(); +} + +/*! + \reimp +*/ +void QtDateTimePropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/datetime_property.h b/src/managers/datetime_property.h new file mode 100644 index 0000000..17538af --- /dev/null +++ b/src/managers/datetime_property.h @@ -0,0 +1,44 @@ + +#ifndef QtDateTimePropertyManager_H +#define QtDateTimePropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QDateTime; + +class QtDateTimePropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtDateTimePropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtDateTimePropertyManager(QObject *parent = nullptr); + ~QtDateTimePropertyManager(); + + QDateTime value(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, const QDateTime &val); +Q_SIGNALS: + void valueChanged(QtProperty *property, const QDateTime &val); +protected: + QString valueText(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtDateTimePropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtDateTimePropertyManager) + Q_DISABLE_COPY(QtDateTimePropertyManager) +}; + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/managers/time_property.cpp b/src/managers/time_property.cpp new file mode 100644 index 0000000..b591f3a --- /dev/null +++ b/src/managers/time_property.cpp @@ -0,0 +1,144 @@ + +#include "time_property.h" +#include "../qtpropertybrowserutils_p.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include +#include + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + + +// QtTimePropertyManager + +class QtTimePropertyManagerPrivate +{ + QtTimePropertyManager *q_ptr; + Q_DECLARE_PUBLIC(QtTimePropertyManager) +public: + + QString m_format; + + typedef QMap PropertyValueMap; + PropertyValueMap m_values; +}; + +/*! + \class QtTimePropertyManager + + \brief The QtTimePropertyManager provides and manages QTime properties. + + A time property's value can be retrieved using the value() + function, and set using the setValue() slot. + + In addition, QtTimePropertyManager provides the valueChanged() signal + which is emitted whenever a property created by this manager + changes. + + \sa QtAbstractPropertyManager, QtTimeEditFactory +*/ + +/*! + \fn void QtTimePropertyManager::valueChanged(QtProperty *property, const QTime &value) + + This signal is emitted whenever a property created by this manager + changes its value, passing a pointer to the \a property and the + new \a value as parameters. + + \sa setValue() +*/ + +/*! + Creates a manager with the given \a parent. +*/ +QtTimePropertyManager::QtTimePropertyManager(QObject *parent) + : QtAbstractPropertyManager(parent) +{ + d_ptr = new QtTimePropertyManagerPrivate; + d_ptr->q_ptr = this; + + QLocale loc; + d_ptr->m_format = loc.timeFormat(QLocale::ShortFormat); +} + +/*! + Destroys this manager, and all the properties it has created. +*/ +QtTimePropertyManager::~QtTimePropertyManager() +{ + clear(); + delete d_ptr; +} + +/*! + Returns the given \a property's value. + + If the given property is not managed by this manager, this + function returns an invalid time object. + + \sa setValue() +*/ +QTime QtTimePropertyManager::value(const QtProperty *property) const +{ + return d_ptr->m_values.value(property, QTime()); +} + +/*! + \reimp +*/ +QString QtTimePropertyManager::valueText(const QtProperty *property) const +{ + const QtTimePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); + if (it == d_ptr->m_values.constEnd()) + return QString(); + return it.value().toString(d_ptr->m_format); +} + +/*! + \fn void QtTimePropertyManager::setValue(QtProperty *property, const QTime &value) + + Sets the value of the given \a property to \a value. + + \sa value(), valueChanged() +*/ +void QtTimePropertyManager::setValue(QtProperty *property, const QTime &val) +{ + setSimpleValue(d_ptr->m_values, this, + &QtTimePropertyManager::propertyChanged, + &QtTimePropertyManager::valueChanged, + property, val); +} + +/*! + \reimp +*/ +void QtTimePropertyManager::initializeProperty(QtProperty *property) +{ + d_ptr->m_values[property] = QTime::currentTime(); +} + +/*! + \reimp +*/ +void QtTimePropertyManager::uninitializeProperty(QtProperty *property) +{ + d_ptr->m_values.remove(property); +} + + + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + diff --git a/src/managers/time_property.h b/src/managers/time_property.h new file mode 100644 index 0000000..177afdc --- /dev/null +++ b/src/managers/time_property.h @@ -0,0 +1,43 @@ + +#ifndef QtTimePropertyManager_H +#define QtTimePropertyManager_H + +#include "../qtpropertybrowser.h" + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +class QTime; + +class QtTimePropertyManagerPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtTimePropertyManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + QtTimePropertyManager(QObject *parent = nullptr); + ~QtTimePropertyManager(); + + QTime value(const QtProperty *property) const; + +public Q_SLOTS: + void setValue(QtProperty *property, const QTime &val); +Q_SIGNALS: + void valueChanged(QtProperty *property, const QTime &val); +protected: + QString valueText(const QtProperty *property) const; + virtual void initializeProperty(QtProperty *property); + virtual void uninitializeProperty(QtProperty *property); +private: + QtTimePropertyManagerPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtTimePropertyManager) + Q_DISABLE_COPY(QtTimePropertyManager) +}; + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif + diff --git a/src/qtpropertymanager.cpp b/src/qtpropertymanager.cpp index 958e9fc..fe146d0 100644 --- a/src/qtpropertymanager.cpp +++ b/src/qtpropertymanager.cpp @@ -248,467 +248,9 @@ Q_GLOBAL_STATIC(QtMetaEnumProvider, metaEnumProvider) -// QtDatePropertyManager -class QtDatePropertyManagerPrivate -{ - QtDatePropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtDatePropertyManager) -public: - - struct Data - { - Data() : val(QDate::currentDate()), minVal(QDate(1752, 9, 14)), - maxVal(QDate(7999, 12, 31)) {} - QDate val; - QDate minVal; - QDate maxVal; - QDate minimumValue() const { return minVal; } - QDate maximumValue() const { return maxVal; } - void setMinimumValue(const QDate &newMinVal) { setSimpleMinimumData(this, newMinVal); } - void setMaximumValue(const QDate &newMaxVal) { setSimpleMaximumData(this, newMaxVal); } - }; - - QString m_format; - - typedef QMap PropertyValueMap; - QMap m_values; -}; - -/*! - \class QtDatePropertyManager - - \brief The QtDatePropertyManager provides and manages QDate properties. - - A date property has a current value, and a range specifying the - valid dates. The range is defined by a minimum and a maximum - value. - - The property's values can be retrieved using the minimum(), - maximum() and value() functions, and can be set using the - setMinimum(), setMaximum() and setValue() slots. Alternatively, - the range can be defined in one go using the setRange() slot. - - In addition, QtDatePropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes, and the rangeChanged() signal which is emitted whenever - such a property changes its range of valid dates. - - \sa QtAbstractPropertyManager, QtDateEditFactory, QtDateTimePropertyManager -*/ - -/*! - \fn void QtDatePropertyManager::valueChanged(QtProperty *property, const QDate &value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the new - \a value as parameters. - - \sa setValue() -*/ - -/*! - \fn void QtDatePropertyManager::rangeChanged(QtProperty *property, const QDate &minimum, const QDate &maximum) - - This signal is emitted whenever a property created by this manager - changes its range of valid dates, passing a pointer to the \a - property and the new \a minimum and \a maximum dates. - - \sa setRange() -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtDatePropertyManager::QtDatePropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtDatePropertyManagerPrivate; - d_ptr->q_ptr = this; - - QLocale loc; - d_ptr->m_format = loc.dateFormat(QLocale::ShortFormat); -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtDatePropertyManager::~QtDatePropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the given \a property's value. - - If the given \a property is not managed by \e this manager, this - function returns an invalid date. - - \sa setValue() -*/ -QDate QtDatePropertyManager::value(const QtProperty *property) const -{ - return getValue(d_ptr->m_values, property); -} - -/*! - Returns the given \a property's minimum date. - - \sa maximum(), setRange() -*/ -QDate QtDatePropertyManager::minimum(const QtProperty *property) const -{ - return getMinimum(d_ptr->m_values, property); -} - -/*! - Returns the given \a property's maximum date. - - \sa minimum(), setRange() -*/ -QDate QtDatePropertyManager::maximum(const QtProperty *property) const -{ - return getMaximum(d_ptr->m_values, property); -} -/*! - \reimp -*/ -QString QtDatePropertyManager::valueText(const QtProperty *property) const -{ - const QtDatePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QString(); - return it.value().val.toString(d_ptr->m_format); -} - -/*! - \fn void QtDatePropertyManager::setValue(QtProperty *property, const QDate &value) - - Sets the value of the given \a property to \a value. - - If the specified \a value is not a valid date according to the - given \a property's range, the value is adjusted to the nearest - valid value within the range. - - \sa value(), setRange(), valueChanged() -*/ -void QtDatePropertyManager::setValue(QtProperty *property, const QDate &val) -{ - void (QtDatePropertyManagerPrivate::*setSubPropertyValue)(QtProperty *, const QDate &) = nullptr; - setValueInRange(this, d_ptr, - &QtDatePropertyManager::propertyChanged, - &QtDatePropertyManager::valueChanged, - property, val, setSubPropertyValue); -} - -/*! - Sets the minimum value for the given \a property to \a minVal. - - When setting the minimum value, the maximum and current values are - adjusted if necessary (ensuring that the range remains valid and - that the current value is within in the range). - - \sa minimum(), setRange() -*/ -void QtDatePropertyManager::setMinimum(QtProperty *property, const QDate &minVal) -{ - setMinimumValue(this, d_ptr, - &QtDatePropertyManager::propertyChanged, - &QtDatePropertyManager::valueChanged, - &QtDatePropertyManager::rangeChanged, - property, minVal); -} - -/*! - Sets the maximum value for the given \a property to \a maxVal. - - When setting the maximum value, the minimum and current - values are adjusted if necessary (ensuring that the range remains - valid and that the current value is within in the range). - - \sa maximum(), setRange() -*/ -void QtDatePropertyManager::setMaximum(QtProperty *property, const QDate &maxVal) -{ - setMaximumValue(this, d_ptr, - &QtDatePropertyManager::propertyChanged, - &QtDatePropertyManager::valueChanged, - &QtDatePropertyManager::rangeChanged, - property, maxVal); -} - -/*! - \fn void QtDatePropertyManager::setRange(QtProperty *property, const QDate &minimum, const QDate &maximum) - - Sets the range of valid dates. - - This is a convenience function defining the range of valid dates - in one go; setting the \a minimum and \a maximum values for the - given \a property with a single function call. - - When setting a new date range, the current value is adjusted if - necessary (ensuring that the value remains in date range). - - \sa setMinimum(), setMaximum(), rangeChanged() -*/ -void QtDatePropertyManager::setRange(QtProperty *property, const QDate &minVal, const QDate &maxVal) -{ - void (QtDatePropertyManagerPrivate::*setSubPropertyRange)(QtProperty *, const QDate &, - const QDate &, const QDate &) = nullptr; - setBorderValues(this, d_ptr, - &QtDatePropertyManager::propertyChanged, - &QtDatePropertyManager::valueChanged, - &QtDatePropertyManager::rangeChanged, - property, minVal, maxVal, setSubPropertyRange); -} - -/*! - \reimp -*/ -void QtDatePropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property] = QtDatePropertyManagerPrivate::Data(); -} - -/*! - \reimp -*/ -void QtDatePropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} - -// QtTimePropertyManager - -class QtTimePropertyManagerPrivate -{ - QtTimePropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtTimePropertyManager) -public: - - QString m_format; - - typedef QMap PropertyValueMap; - PropertyValueMap m_values; -}; - -/*! - \class QtTimePropertyManager - - \brief The QtTimePropertyManager provides and manages QTime properties. - - A time property's value can be retrieved using the value() - function, and set using the setValue() slot. - - In addition, QtTimePropertyManager provides the valueChanged() signal - which is emitted whenever a property created by this manager - changes. - - \sa QtAbstractPropertyManager, QtTimeEditFactory -*/ -/*! - \fn void QtTimePropertyManager::valueChanged(QtProperty *property, const QTime &value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the - new \a value as parameters. - - \sa setValue() -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtTimePropertyManager::QtTimePropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtTimePropertyManagerPrivate; - d_ptr->q_ptr = this; - - QLocale loc; - d_ptr->m_format = loc.timeFormat(QLocale::ShortFormat); -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtTimePropertyManager::~QtTimePropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the given \a property's value. - - If the given property is not managed by this manager, this - function returns an invalid time object. - - \sa setValue() -*/ -QTime QtTimePropertyManager::value(const QtProperty *property) const -{ - return d_ptr->m_values.value(property, QTime()); -} - -/*! - \reimp -*/ -QString QtTimePropertyManager::valueText(const QtProperty *property) const -{ - const QtTimePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QString(); - return it.value().toString(d_ptr->m_format); -} - -/*! - \fn void QtTimePropertyManager::setValue(QtProperty *property, const QTime &value) - - Sets the value of the given \a property to \a value. - - \sa value(), valueChanged() -*/ -void QtTimePropertyManager::setValue(QtProperty *property, const QTime &val) -{ - setSimpleValue(d_ptr->m_values, this, - &QtTimePropertyManager::propertyChanged, - &QtTimePropertyManager::valueChanged, - property, val); -} - -/*! - \reimp -*/ -void QtTimePropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property] = QTime::currentTime(); -} - -/*! - \reimp -*/ -void QtTimePropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} - -// QtDateTimePropertyManager - -class QtDateTimePropertyManagerPrivate -{ - QtDateTimePropertyManager *q_ptr; - Q_DECLARE_PUBLIC(QtDateTimePropertyManager) -public: - - QString m_format; - - typedef QMap PropertyValueMap; - PropertyValueMap m_values; -}; - -/*! \class QtDateTimePropertyManager - - \brief The QtDateTimePropertyManager provides and manages QDateTime properties. - - A date and time property has a current value which can be - retrieved using the value() function, and set using the setValue() - slot. In addition, QtDateTimePropertyManager provides the - valueChanged() signal which is emitted whenever a property created - by this manager changes. - - \sa QtAbstractPropertyManager, QtDateTimeEditFactory, QtDatePropertyManager -*/ - -/*! - \fn void QtDateTimePropertyManager::valueChanged(QtProperty *property, const QDateTime &value) - - This signal is emitted whenever a property created by this manager - changes its value, passing a pointer to the \a property and the new - \a value as parameters. -*/ - -/*! - Creates a manager with the given \a parent. -*/ -QtDateTimePropertyManager::QtDateTimePropertyManager(QObject *parent) - : QtAbstractPropertyManager(parent) -{ - d_ptr = new QtDateTimePropertyManagerPrivate; - d_ptr->q_ptr = this; - - QLocale loc; - d_ptr->m_format = loc.dateFormat(QLocale::ShortFormat); - d_ptr->m_format += QLatin1Char(' '); - d_ptr->m_format += loc.timeFormat(QLocale::ShortFormat); -} - -/*! - Destroys this manager, and all the properties it has created. -*/ -QtDateTimePropertyManager::~QtDateTimePropertyManager() -{ - clear(); - delete d_ptr; -} - -/*! - Returns the given \a property's value. - - If the given \a property is not managed by this manager, this - function returns an invalid QDateTime object. - - \sa setValue() -*/ -QDateTime QtDateTimePropertyManager::value(const QtProperty *property) const -{ - return d_ptr->m_values.value(property, QDateTime()); -} - -/*! - \reimp -*/ -QString QtDateTimePropertyManager::valueText(const QtProperty *property) const -{ - const QtDateTimePropertyManagerPrivate::PropertyValueMap::const_iterator it = d_ptr->m_values.constFind(property); - if (it == d_ptr->m_values.constEnd()) - return QString(); - return it.value().toString(d_ptr->m_format); -} - -/*! - \fn void QtDateTimePropertyManager::setValue(QtProperty *property, const QDateTime &value) - - Sets the value of the given \a property to \a value. - - \sa value(), valueChanged() -*/ -void QtDateTimePropertyManager::setValue(QtProperty *property, const QDateTime &val) -{ - setSimpleValue(d_ptr->m_values, this, - &QtDateTimePropertyManager::propertyChanged, - &QtDateTimePropertyManager::valueChanged, - property, val); -} - -/*! - \reimp -*/ -void QtDateTimePropertyManager::initializeProperty(QtProperty *property) -{ - d_ptr->m_values[property] = QDateTime::currentDateTime(); -} - -/*! - \reimp -*/ -void QtDateTimePropertyManager::uninitializeProperty(QtProperty *property) -{ - d_ptr->m_values.remove(property); -} // QtKeySequencePropertyManager diff --git a/src/qtpropertymanager.h b/src/qtpropertymanager.h index 6a7f17a..9949e07 100644 --- a/src/qtpropertymanager.h +++ b/src/qtpropertymanager.h @@ -44,6 +44,9 @@ #include "qtpropertybrowser.h" #include "managers/group_property.h" #include "managers/int_property.h" +#include "managers/date_property.h" +#include "managers/time_property.h" +#include "managers/datetime_property.h" #include "managers/double_property.h" #include "managers/bool_property.h" #include "managers/color_property.h" @@ -60,87 +63,6 @@ class QLocale; -class QtDatePropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtDatePropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtDatePropertyManager(QObject *parent = nullptr); - ~QtDatePropertyManager(); - - QDate value(const QtProperty *property) const; - QDate minimum(const QtProperty *property) const; - QDate maximum(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, const QDate &val); - void setMinimum(QtProperty *property, const QDate &minVal); - void setMaximum(QtProperty *property, const QDate &maxVal); - void setRange(QtProperty *property, const QDate &minVal, const QDate &maxVal); -Q_SIGNALS: - void valueChanged(QtProperty *property, const QDate &val); - void rangeChanged(QtProperty *property, const QDate &minVal, const QDate &maxVal); -protected: - QString valueText(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtDatePropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtDatePropertyManager) - Q_DISABLE_COPY(QtDatePropertyManager) -}; - -class QtTimePropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtTimePropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtTimePropertyManager(QObject *parent = nullptr); - ~QtTimePropertyManager(); - - QTime value(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, const QTime &val); -Q_SIGNALS: - void valueChanged(QtProperty *property, const QTime &val); -protected: - QString valueText(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtTimePropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtTimePropertyManager) - Q_DISABLE_COPY(QtTimePropertyManager) -}; - -class QtDateTimePropertyManagerPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtDateTimePropertyManager : public QtAbstractPropertyManager -{ - Q_OBJECT -public: - QtDateTimePropertyManager(QObject *parent = nullptr); - ~QtDateTimePropertyManager(); - - QDateTime value(const QtProperty *property) const; - -public Q_SLOTS: - void setValue(QtProperty *property, const QDateTime &val); -Q_SIGNALS: - void valueChanged(QtProperty *property, const QDateTime &val); -protected: - QString valueText(const QtProperty *property) const; - virtual void initializeProperty(QtProperty *property); - virtual void uninitializeProperty(QtProperty *property); -private: - QtDateTimePropertyManagerPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtDateTimePropertyManager) - Q_DISABLE_COPY(QtDateTimePropertyManager) -}; - class QtKeySequencePropertyManagerPrivate; class QT_QTPROPERTYBROWSER_EXPORT QtKeySequencePropertyManager : public QtAbstractPropertyManager From 515c0b18c1e397fbe3782109cfc1190ef65812fd Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 23 Dec 2019 02:37:04 +0300 Subject: [PATCH 34/39] More refactoring and improve for a color picker --- examples/canvas_typed/mainwindow.cpp | 6 +- src/editor_factory_private.hpp | 80 +++++ src/managers/color_property.cpp | 465 +++++++++++++++++++++------ src/managers/color_property.h | 84 ++++- src/managers/common.h | 15 + src/qteditorfactory.cpp | 269 +--------------- src/qteditorfactory.h | 41 +-- src/qtpropertybrowserutils.cpp | 37 ++- src/qtpropertybrowserutils_p.h | 1 + src/qtvariantproperty.cpp | 10 +- 10 files changed, 599 insertions(+), 409 deletions(-) create mode 100644 src/editor_factory_private.hpp diff --git a/examples/canvas_typed/mainwindow.cpp b/examples/canvas_typed/mainwindow.cpp index e6ec196..3ac64ff 100644 --- a/examples/canvas_typed/mainwindow.cpp +++ b/examples/canvas_typed/mainwindow.cpp @@ -151,7 +151,7 @@ MainWindow::MainWindow(QWidget *parent) propertyEditor = new QtTreePropertyBrowser(dock); propertyEditor->setFactoryForManager(doubleManager, doubleSpinBoxFactory); propertyEditor->setFactoryForManager(stringManager, lineEditFactory); - propertyEditor->setFactoryForManager(colorManager->subIntPropertyManager(), spinBoxFactory); + propertyEditor->setFactoryForManager(colorManager->subStringPropertyManager(), lineEditFactory); propertyEditor->setFactoryForManager(fontManager->subIntPropertyManager(), spinBoxFactory); propertyEditor->setFactoryForManager(fontManager->subBoolPropertyManager(), checkBoxFactory); propertyEditor->setFactoryForManager(fontManager->subEnumPropertyManager(), comboBoxFactory); @@ -159,7 +159,7 @@ MainWindow::MainWindow(QWidget *parent) propertyEditor->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory); dock->setWidget(propertyEditor); - currentItem = 0; + currentItem = nullptr; connect(canvasView, SIGNAL(itemClicked(QtCanvasItem *)), this, SLOT(itemClicked(QtCanvasItem *))); @@ -167,7 +167,7 @@ MainWindow::MainWindow(QWidget *parent) this, SLOT(itemMoved(QtCanvasItem *))); fillView(); - itemClicked(0); + itemClicked(nullptr); } void MainWindow::newRectangle() diff --git a/src/editor_factory_private.hpp b/src/editor_factory_private.hpp new file mode 100644 index 0000000..3dd75ef --- /dev/null +++ b/src/editor_factory_private.hpp @@ -0,0 +1,80 @@ +#ifndef EditorFactoryPrivate_Hpp +#define EditorFactoryPrivate_Hpp + +#include +#include + + +#if defined(Q_CC_MSVC) +# pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ +#endif + +#if QT_VERSION >= 0x040400 +QT_BEGIN_NAMESPACE +#endif + +// ---------- EditorFactoryPrivate : +// Base class for editor factory private classes. Manages mapping of properties to editors and vice versa. + +class QtProperty; + +template +class EditorFactoryPrivate +{ +public: + + typedef QList EditorList; + typedef QMap PropertyToEditorListMap; + typedef QMap EditorToPropertyMap; + + Editor *createEditor(QtProperty *property, QWidget *parent); + void initializeEditor(QtProperty *property, Editor *e); + void slotEditorDestroyed(QObject *object); + + PropertyToEditorListMap m_createdEditors; + EditorToPropertyMap m_editorToProperty; +}; + +template +Editor *EditorFactoryPrivate::createEditor(QtProperty *property, QWidget *parent) +{ + Editor *editor = new Editor(parent); + initializeEditor(property, editor); + return editor; +} + +template +void EditorFactoryPrivate::initializeEditor(QtProperty *property, Editor *editor) +{ + typename PropertyToEditorListMap::iterator it = m_createdEditors.find(property); + if (it == m_createdEditors.end()) + it = m_createdEditors.insert(property, EditorList()); + it.value().append(editor); + m_editorToProperty.insert(editor, property); +} + +template +void EditorFactoryPrivate::slotEditorDestroyed(QObject *object) +{ + const typename EditorToPropertyMap::iterator ecend = m_editorToProperty.end(); + for (typename EditorToPropertyMap::iterator itEditor = m_editorToProperty.begin(); itEditor != ecend; ++itEditor) { + if (itEditor.key() == object) { + Editor *editor = itEditor.key(); + QtProperty *property = itEditor.value(); + const typename PropertyToEditorListMap::iterator pit = m_createdEditors.find(property); + if (pit != m_createdEditors.end()) { + pit.value().removeAll(editor); + if (pit.value().empty()) + m_createdEditors.erase(pit); + } + m_editorToProperty.erase(itEditor); + return; + } + } +} + +#if QT_VERSION >= 0x040400 +QT_END_NAMESPACE +#endif + +#endif // EditorFactoryPrivate_Hpp diff --git a/src/managers/color_property.cpp b/src/managers/color_property.cpp index c5ab93c..9a7070f 100644 --- a/src/managers/color_property.cpp +++ b/src/managers/color_property.cpp @@ -4,9 +4,14 @@ #include "common.h" #include +#include +#include +#include #include #include #include +#include +#include #include #include @@ -27,49 +32,66 @@ class QtColorPropertyManagerPrivate Q_DECLARE_PUBLIC(QtColorPropertyManager) public: - void slotIntChanged(QtProperty *property, int value); + void slotStringChanged(QtProperty *property, const QString &value); +// void slotIntChanged(QtProperty *property, int value); void slotPropertyDestroyed(QtProperty *property); typedef QMap PropertyValueMap; PropertyValueMap m_values; - QtIntPropertyManager *m_intPropertyManager; +// QtIntPropertyManager *m_intPropertyManager; + QtStringPropertyManager *m_stringPropertyManager; - QMap m_propertyToR; - QMap m_propertyToG; - QMap m_propertyToB; - QMap m_propertyToA; +// QMap m_propertyToR; +// QMap m_propertyToG; +// QMap m_propertyToB; +// QMap m_propertyToA; - QMap m_rToProperty; - QMap m_gToProperty; - QMap m_bToProperty; - QMap m_aToProperty; +// QMap m_rToProperty; +// QMap m_gToProperty; +// QMap m_bToProperty; +// QMap m_aToProperty; + + QMap m_propertyToRgba; + QMap m_rgbaToProperty; }; -void QtColorPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) +void QtColorPropertyManagerPrivate::slotStringChanged(QtProperty *property, const QString &value) { - if (QtProperty *prop = m_rToProperty.value(property, nullptr)) { - QColor c = m_values[prop]; - c.setRed(value); - q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_gToProperty.value(property, nullptr)) { - QColor c = m_values[prop]; - c.setGreen(value); - q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_bToProperty.value(property, nullptr)) { - QColor c = m_values[prop]; - c.setBlue(value); - q_ptr->setValue(prop, c); - } else if (QtProperty *prop = m_aToProperty.value(property, nullptr)) { + if (QtProperty *prop = m_rgbaToProperty.value(property, nullptr)) { QColor c = m_values[prop]; - c.setAlpha(value); + c.setNamedColor(value); q_ptr->setValue(prop, c); } } +//void QtColorPropertyManagerPrivate::slotIntChanged(QtProperty *property, int value) +//{ +// if (QtProperty *prop = m_rToProperty.value(property, nullptr)) { +// QColor c = m_values[prop]; +// c.setRed(value); +// q_ptr->setValue(prop, c); +// } else if (QtProperty *prop = m_gToProperty.value(property, nullptr)) { +// QColor c = m_values[prop]; +// c.setGreen(value); +// q_ptr->setValue(prop, c); +// } else if (QtProperty *prop = m_bToProperty.value(property, nullptr)) { +// QColor c = m_values[prop]; +// c.setBlue(value); +// q_ptr->setValue(prop, c); +// } else if (QtProperty *prop = m_aToProperty.value(property, nullptr)) { +// QColor c = m_values[prop]; +// c.setAlpha(value); +// q_ptr->setValue(prop, c); +// } +//} + void QtColorPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) { - if (QtProperty *pointProp = m_rToProperty.value(property, nullptr)) { + if (QtProperty *pointProp = m_rgbaToProperty.value(property, nullptr)) { + m_propertyToRgba[pointProp] = nullptr; + m_rgbaToProperty.remove(property); + } /*else if (QtProperty *pointProp = m_rToProperty.value(property, nullptr)) { m_propertyToR[pointProp] = nullptr; m_rToProperty.remove(property); } else if (QtProperty *pointProp = m_gToProperty.value(property, nullptr)) { @@ -81,7 +103,7 @@ void QtColorPropertyManagerPrivate::slotPropertyDestroyed(QtProperty *property) } else if (QtProperty *pointProp = m_aToProperty.value(property, nullptr)) { m_propertyToA[pointProp] = nullptr; m_aToProperty.remove(property); - } + }*/ } /*! @@ -125,11 +147,18 @@ QtColorPropertyManager::QtColorPropertyManager(QObject *parent) d_ptr = new QtColorPropertyManagerPrivate; d_ptr->q_ptr = this; - d_ptr->m_intPropertyManager = new QtIntPropertyManager(this); - connect(d_ptr->m_intPropertyManager, SIGNAL(valueChanged(QtProperty *, int)), - this, SLOT(slotIntChanged(QtProperty *, int))); +// d_ptr->m_intPropertyManager = new QtIntPropertyManager(this); +// connect(d_ptr->m_intPropertyManager, SIGNAL(valueChanged(QtProperty *, int)), +// this, SLOT(slotIntChanged(QtProperty *, int))); + +// connect(d_ptr->m_intPropertyManager, SIGNAL(propertyDestroyed(QtProperty *)), +// this, SLOT(slotPropertyDestroyed(QtProperty *))); - connect(d_ptr->m_intPropertyManager, SIGNAL(propertyDestroyed(QtProperty *)), + d_ptr->m_stringPropertyManager = new QtStringPropertyManager(this); + connect(d_ptr->m_stringPropertyManager, SIGNAL(valueChanged(QtProperty *, const QString &)), + this, SLOT(slotStringChanged(QtProperty *, const QString &))); + + connect(d_ptr->m_stringPropertyManager, SIGNAL(propertyDestroyed(QtProperty *)), this, SLOT(slotPropertyDestroyed(QtProperty *))); } @@ -152,9 +181,14 @@ QtColorPropertyManager::~QtColorPropertyManager() \sa QtAbstractPropertyBrowser::setFactoryForManager() */ -QtIntPropertyManager *QtColorPropertyManager::subIntPropertyManager() const +//QtIntPropertyManager *QtColorPropertyManager::subIntPropertyManager() const +//{ +// return d_ptr->m_intPropertyManager; +//} + +QtStringPropertyManager *QtColorPropertyManager::subStringPropertyManager() const { - return d_ptr->m_intPropertyManager; + return d_ptr->m_stringPropertyManager; } /*! @@ -180,7 +214,7 @@ QString QtColorPropertyManager::valueText(const QtProperty *property) const if (it == d_ptr->m_values.constEnd()) return QString(); - return QtPropertyBrowserUtils::colorValueText(it.value()); + return QtPropertyBrowserUtils::colorHexValueText(it.value()); } /*! @@ -214,10 +248,14 @@ void QtColorPropertyManager::setValue(QtProperty *property, const QColor &val) it.value() = val; - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToR[property], val.red()); - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToG[property], val.green()); - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToB[property], val.blue()); - d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToA[property], val.alpha()); +// d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToR[property], val.red()); +// d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToG[property], val.green()); +// d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToB[property], val.blue()); +// d_ptr->m_intPropertyManager->setValue(d_ptr->m_propertyToA[property], val.alpha()); + + d_ptr->m_stringPropertyManager->blockSignals(true); + d_ptr->m_stringPropertyManager->setValue(d_ptr->m_propertyToRgba[property], QtPropertyBrowserUtils::colorHexValueText(val)); + d_ptr->m_stringPropertyManager->blockSignals(false); emit propertyChanged(property); emit valueChanged(property, val); @@ -231,37 +269,44 @@ void QtColorPropertyManager::initializeProperty(QtProperty *property) QColor val; d_ptr->m_values[property] = val; - QtProperty *rProp = d_ptr->m_intPropertyManager->addProperty(); - rProp->setPropertyName(tr("Red")); - d_ptr->m_intPropertyManager->setValue(rProp, val.red()); - d_ptr->m_intPropertyManager->setRange(rProp, 0, 0xFF); - d_ptr->m_propertyToR[property] = rProp; - d_ptr->m_rToProperty[rProp] = property; - property->addSubProperty(rProp); - - QtProperty *gProp = d_ptr->m_intPropertyManager->addProperty(); - gProp->setPropertyName(tr("Green")); - d_ptr->m_intPropertyManager->setValue(gProp, val.green()); - d_ptr->m_intPropertyManager->setRange(gProp, 0, 0xFF); - d_ptr->m_propertyToG[property] = gProp; - d_ptr->m_gToProperty[gProp] = property; - property->addSubProperty(gProp); - - QtProperty *bProp = d_ptr->m_intPropertyManager->addProperty(); - bProp->setPropertyName(tr("Blue")); - d_ptr->m_intPropertyManager->setValue(bProp, val.blue()); - d_ptr->m_intPropertyManager->setRange(bProp, 0, 0xFF); - d_ptr->m_propertyToB[property] = bProp; - d_ptr->m_bToProperty[bProp] = property; - property->addSubProperty(bProp); - - QtProperty *aProp = d_ptr->m_intPropertyManager->addProperty(); - aProp->setPropertyName(tr("Alpha")); - d_ptr->m_intPropertyManager->setValue(aProp, val.alpha()); - d_ptr->m_intPropertyManager->setRange(aProp, 0, 0xFF); - d_ptr->m_propertyToA[property] = aProp; - d_ptr->m_aToProperty[aProp] = property; - property->addSubProperty(aProp); + QtProperty *rgbaProp = d_ptr->m_stringPropertyManager->addProperty(); + rgbaProp->setPropertyName("#"); + d_ptr->m_stringPropertyManager->setValue(rgbaProp, QtPropertyBrowserUtils::colorHexValueText(val)); + d_ptr->m_propertyToRgba[property] = rgbaProp; + d_ptr->m_rgbaToProperty[rgbaProp] = property; + property->addSubProperty(rgbaProp); + +// QtProperty *rProp = d_ptr->m_intPropertyManager->addProperty(); +// rProp->setPropertyName(tr("Red")); +// d_ptr->m_intPropertyManager->setValue(rProp, val.red()); +// d_ptr->m_intPropertyManager->setRange(rProp, 0, 0xFF); +// d_ptr->m_propertyToR[property] = rProp; +// d_ptr->m_rToProperty[rProp] = property; +// property->addSubProperty(rProp); + +// QtProperty *gProp = d_ptr->m_intPropertyManager->addProperty(); +// gProp->setPropertyName(tr("Green")); +// d_ptr->m_intPropertyManager->setValue(gProp, val.green()); +// d_ptr->m_intPropertyManager->setRange(gProp, 0, 0xFF); +// d_ptr->m_propertyToG[property] = gProp; +// d_ptr->m_gToProperty[gProp] = property; +// property->addSubProperty(gProp); + +// QtProperty *bProp = d_ptr->m_intPropertyManager->addProperty(); +// bProp->setPropertyName(tr("Blue")); +// d_ptr->m_intPropertyManager->setValue(bProp, val.blue()); +// d_ptr->m_intPropertyManager->setRange(bProp, 0, 0xFF); +// d_ptr->m_propertyToB[property] = bProp; +// d_ptr->m_bToProperty[bProp] = property; +// property->addSubProperty(bProp); + +// QtProperty *aProp = d_ptr->m_intPropertyManager->addProperty(); +// aProp->setPropertyName(tr("Alpha")); +// d_ptr->m_intPropertyManager->setValue(aProp, val.alpha()); +// d_ptr->m_intPropertyManager->setRange(aProp, 0, 0xFF); +// d_ptr->m_propertyToA[property] = aProp; +// d_ptr->m_aToProperty[aProp] = property; +// property->addSubProperty(aProp); } /*! @@ -269,47 +314,279 @@ void QtColorPropertyManager::initializeProperty(QtProperty *property) */ void QtColorPropertyManager::uninitializeProperty(QtProperty *property) { - QtProperty *rProp = d_ptr->m_propertyToR[property]; - if (rProp) { - d_ptr->m_rToProperty.remove(rProp); - delete rProp; + QtProperty *rgbaProp = d_ptr->m_propertyToRgba[property]; + if (rgbaProp) { + d_ptr->m_rgbaToProperty.remove(rgbaProp); + delete rgbaProp; } - d_ptr->m_propertyToR.remove(property); + d_ptr->m_propertyToRgba.remove(property); + +// QtProperty *rProp = d_ptr->m_propertyToR[property]; +// if (rProp) { +// d_ptr->m_rToProperty.remove(rProp); +// delete rProp; +// } +// d_ptr->m_propertyToR.remove(property); + +// QtProperty *gProp = d_ptr->m_propertyToG[property]; +// if (gProp) { +// d_ptr->m_gToProperty.remove(gProp); +// delete gProp; +// } +// d_ptr->m_propertyToG.remove(property); + +// QtProperty *bProp = d_ptr->m_propertyToB[property]; +// if (bProp) { +// d_ptr->m_bToProperty.remove(bProp); +// delete bProp; +// } +// d_ptr->m_propertyToB.remove(property); + +// QtProperty *aProp = d_ptr->m_propertyToA[property]; +// if (aProp) { +// d_ptr->m_aToProperty.remove(aProp); +// delete aProp; +// } +// d_ptr->m_propertyToA.remove(property); + + d_ptr->m_values.remove(property); +} + +void QtColorPropertyManager::slotStringChanged(QtProperty *p, const QString &val) +{ + d_ptr->slotStringChanged(p, val); +} + +//void QtColorPropertyManager::slotIntChanged(QtProperty *p, int i) +//{ +// d_ptr->slotIntChanged(p, i); +//} + +void QtColorPropertyManager::slotPropertyDestroyed(QtProperty *p) +{ + d_ptr->slotPropertyDestroyed(p); +} + - QtProperty *gProp = d_ptr->m_propertyToG[property]; - if (gProp) { - d_ptr->m_gToProperty.remove(gProp); - delete gProp; + + + + + + + + +QtColorEditWidget::QtColorEditWidget(QWidget *parent) : + QWidget(parent), + m_pixmapLabel(new QLabel), + m_label(nullptr), + m_lineEdit(new QLineEdit), + m_button(new QToolButton) +{ + QHBoxLayout *lt = new QHBoxLayout(this); + setupTreeViewEditorMargin(lt); + lt->setSpacing(4); + lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored)); + lt->addWidget(m_pixmapLabel); + lt->addWidget(m_lineEdit); + m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); + m_button->setFixedWidth(20); + setFocusProxy(m_button); + setFocusPolicy(m_button->focusPolicy()); + m_button->setText(tr("...")); + m_button->installEventFilter(this); + connect(m_button, SIGNAL(clicked()), this, SLOT(buttonClicked())); + connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(editFinished())); + lt->addWidget(m_button); + m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(m_color))); + m_lineEdit->setText(QtPropertyBrowserUtils::colorHexValueText(m_color)); + m_lineEdit->setMaxLength(9); + m_lineEdit->setMaximumWidth(80); + m_lineEdit->setValidator(new QRegExpValidator(QRegExp("#[0-9a-fA-F]{6,8}"), m_lineEdit)); +} + +void QtColorEditWidget::setValue(const QColor &c) +{ + if (m_color != c) { + m_color = c; + m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(c))); + m_lineEdit->setText(QtPropertyBrowserUtils::colorHexValueText(c)); } - d_ptr->m_propertyToG.remove(property); +} - QtProperty *bProp = d_ptr->m_propertyToB[property]; - if (bProp) { - d_ptr->m_bToProperty.remove(bProp); - delete bProp; +void QtColorEditWidget::buttonClicked() +{ + QColor &oldRgba = m_color; + QColor newRgba = QColorDialog::getColor(oldRgba, this, + QString(), + QColorDialog::ShowAlphaChannel| + QColorDialog::DontUseNativeDialog); + if (newRgba.isValid() && newRgba != oldRgba) { + setValue(newRgba); + emit valueChanged(m_color); } - d_ptr->m_propertyToB.remove(property); +} + +void QtColorEditWidget::editFinished() +{ + QColor &oldRgba = m_color; + QColor newRgba = m_color; + QString color = m_lineEdit->text(); - QtProperty *aProp = d_ptr->m_propertyToA[property]; - if (aProp) { - d_ptr->m_aToProperty.remove(aProp); - delete aProp; + if(color.size() < 7) + { + m_lineEdit->setText(QtPropertyBrowserUtils::colorHexValueText(m_color)); + return; } - d_ptr->m_propertyToA.remove(property); - d_ptr->m_values.remove(property); + color.remove(0, 1); + + if(color.size() >= 6) + { + newRgba.setRed(color.mid(0, 2).toInt(nullptr, 16)); + newRgba.setGreen(color.mid(2, 2).toInt(nullptr, 16)); + newRgba.setBlue(color.mid(4, 2).toInt(nullptr, 16)); + } + if(color.size() == 8) + newRgba.setAlpha(color.mid(6, 2).toInt(nullptr, 16)); + + if (newRgba.isValid() && newRgba != oldRgba) { + setValue(newRgba); + emit valueChanged(m_color); + } } -void QtColorPropertyManager::slotIntChanged(QtProperty *p, int i) +bool QtColorEditWidget::eventFilter(QObject *obj, QEvent *ev) { - d_ptr->slotIntChanged(p, i); + if (obj == m_button) { + switch (ev->type()) { + case QEvent::KeyPress: + case QEvent::KeyRelease: { // Prevent the QToolButton from handling Enter/Escape meant control the delegate + switch (static_cast(ev)->key()) { + case Qt::Key_Escape: + case Qt::Key_Enter: + case Qt::Key_Return: + ev->ignore(); + return true; + default: + break; + } + } + break; + default: + break; + } + } + return QWidget::eventFilter(obj, ev); } -void QtColorPropertyManager::slotPropertyDestroyed(QtProperty *p) +void QtColorEditWidget::paintEvent(QPaintEvent *) { - d_ptr->slotPropertyDestroyed(p); + QStyleOption opt; + opt.init(this); + QPainter p(this); + style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); +} + + + + + +// QtColorEditorFactoryPrivate + + +void QtColorEditorFactoryPrivate::slotPropertyChanged(QtProperty *property, + const QColor &value) +{ + const PropertyToEditorListMap::iterator it = m_createdEditors.find(property); + if (it == m_createdEditors.end()) + return; + QListIterator itEditor(it.value()); + + while (itEditor.hasNext()) + itEditor.next()->setValue(value); +} + +void QtColorEditorFactoryPrivate::slotSetValue(const QColor &value) +{ + QObject *object = q_ptr->sender(); + const EditorToPropertyMap::ConstIterator ecend = m_editorToProperty.constEnd(); + for (EditorToPropertyMap::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor) + if (itEditor.key() == object) { + QtProperty *property = itEditor.value(); + QtColorPropertyManager *manager = q_ptr->propertyManager(property); + if (!manager) + return; + manager->setValue(property, value); + return; + } +} + +/*! + \class QtColorEditorFactory + + \brief The QtColorEditorFactory class provides color editing for + properties created by QtColorPropertyManager objects. + + \sa QtAbstractEditorFactory, QtColorPropertyManager +*/ + +/*! + Creates a factory with the given \a parent. +*/ +QtColorEditorFactory::QtColorEditorFactory(QObject *parent) : + QtAbstractEditorFactory(parent), + d_ptr(new QtColorEditorFactoryPrivate()) +{ + d_ptr->q_ptr = this; } +/*! + Destroys this factory, and all the widgets it has created. +*/ +QtColorEditorFactory::~QtColorEditorFactory() +{ + qDeleteAll(d_ptr->m_editorToProperty.keys()); + delete d_ptr; +} + +/*! + \internal + + Reimplemented from the QtAbstractEditorFactory class. +*/ +void QtColorEditorFactory::connectPropertyManager(QtColorPropertyManager *manager) +{ + connect(manager, SIGNAL(valueChanged(QtProperty*,QColor)), + this, SLOT(slotPropertyChanged(QtProperty*,QColor))); +} + +/*! + \internal + + Reimplemented from the QtAbstractEditorFactory class. +*/ +QWidget *QtColorEditorFactory::createEditor(QtColorPropertyManager *manager, + QtProperty *property, QWidget *parent) +{ + QtColorEditWidget *editor = d_ptr->createEditor(property, parent); + editor->setValue(manager->value(property)); + connect(editor, SIGNAL(valueChanged(QColor)), this, SLOT(slotSetValue(QColor))); + connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(slotEditorDestroyed(QObject *))); + return editor; +} + +/*! + \internal + + Reimplemented from the QtAbstractEditorFactory class. +*/ +void QtColorEditorFactory::disconnectPropertyManager(QtColorPropertyManager *manager) +{ + disconnect(manager, SIGNAL(valueChanged(QtProperty*,QColor)), this, SLOT(slotPropertyChanged(QtProperty*,QColor))); +} + + #if QT_VERSION >= 0x040400 QT_END_NAMESPACE #endif diff --git a/src/managers/color_property.h b/src/managers/color_property.h index fdf153c..b029fd4 100644 --- a/src/managers/color_property.h +++ b/src/managers/color_property.h @@ -3,7 +3,9 @@ #define QtColorPropertyManager_H #include "../qtpropertybrowser.h" +#include "../editor_factory_private.hpp" #include "int_property.h" +#include "string_property.h" #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -19,6 +21,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtColorPropertyManager : public QtAbstractProp ~QtColorPropertyManager(); QtIntPropertyManager *subIntPropertyManager() const; + QtStringPropertyManager *subStringPropertyManager() const; QColor value(const QtProperty *property) const; @@ -37,10 +40,89 @@ public Q_SLOTS: Q_DISABLE_COPY(QtColorPropertyManager) private Q_SLOTS: - void slotIntChanged(QtProperty *, int); + void slotStringChanged(QtProperty *, const QString &val); +// void slotIntChanged(QtProperty *, int); void slotPropertyDestroyed(QtProperty *); }; + + +// QtColorEditWidget + +class QLabel; +class QLineEdit; +class QToolButton; + +class QtColorEditWidget : public QWidget { + Q_OBJECT + +public: + QtColorEditWidget(QWidget *parent); + + bool eventFilter(QObject *obj, QEvent *ev); + +public Q_SLOTS: + void setValue(const QColor &value); + +Q_SIGNALS: + void valueChanged(const QColor &value); + +protected: + void paintEvent(QPaintEvent *); + +private Q_SLOTS: + void buttonClicked(); + void editFinished(); + +private: + QColor m_color; + QLabel *m_pixmapLabel; + QLabel *m_label; + QLineEdit *m_lineEdit; + QToolButton *m_button; +}; + + + + +class QtColorEditorFactoryPrivate; + +class QT_QTPROPERTYBROWSER_EXPORT QtColorEditorFactory : public QtAbstractEditorFactory +{ + Q_OBJECT +public: + QtColorEditorFactory(QObject *parent = nullptr); + ~QtColorEditorFactory(); +protected: + void connectPropertyManager(QtColorPropertyManager *manager); + QWidget *createEditor(QtColorPropertyManager *manager, QtProperty *property, + QWidget *parent); + void disconnectPropertyManager(QtColorPropertyManager *manager); +private: + QtColorEditorFactoryPrivate *d_ptr; + Q_DECLARE_PRIVATE(QtColorEditorFactory) + Q_DISABLE_COPY(QtColorEditorFactory) + Q_PRIVATE_SLOT(d_func(), void slotPropertyChanged(QtProperty *, const QColor &)) + Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *)) + Q_PRIVATE_SLOT(d_func(), void slotSetValue(const QColor &)) +}; + + + + +// QtColorEditorFactoryPrivate +class QtColorEditorFactoryPrivate : public EditorFactoryPrivate +{ + QtColorEditorFactory *q_ptr; + Q_DECLARE_PUBLIC(QtColorEditorFactory) +public: + + void slotPropertyChanged(QtProperty *property, const QColor &value); + void slotSetValue(const QColor &value); +}; + + + #if QT_VERSION >= 0x040400 QT_END_NAMESPACE #endif diff --git a/src/managers/common.h b/src/managers/common.h index 140881b..84d2532 100644 --- a/src/managers/common.h +++ b/src/managers/common.h @@ -1,6 +1,8 @@ #include #include #include +#include +#include #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE @@ -321,6 +323,19 @@ static void setMaximumValue(PropertyManager *manager, PropertyManagerPrivate *ma property, &PropertyManagerPrivate::Data::maximumValue, &PropertyManagerPrivate::Data::setMaximumValue, maxVal, setSubPropertyRange); } + +// Set a hard coded left margin to account for the indentation +// of the tree view icon when switching to an editor + +static inline void setupTreeViewEditorMargin(QLayout *lt) +{ + enum { DecorationMargin = 4 }; + if (QApplication::layoutDirection() == Qt::LeftToRight) + lt->setContentsMargins(DecorationMargin, 0, 0, 0); + else + lt->setContentsMargins(0, 0, DecorationMargin, 0); +} + #if QT_VERSION >= 0x040400 QT_END_NAMESPACE #endif diff --git a/src/qteditorfactory.cpp b/src/qteditorfactory.cpp index 46397e8..53005a6 100644 --- a/src/qteditorfactory.cpp +++ b/src/qteditorfactory.cpp @@ -63,6 +63,8 @@ # pragma warning(disable: 4786) /* MS VS 6: truncating debug info after 255 characters */ #endif +#include "editor_factory_private.hpp" + #if QT_VERSION >= 0x040400 QT_BEGIN_NAMESPACE #endif @@ -79,64 +81,6 @@ static inline void setupTreeViewEditorMargin(QLayout *lt) lt->setContentsMargins(0, 0, DecorationMargin, 0); } -// ---------- EditorFactoryPrivate : -// Base class for editor factory private classes. Manages mapping of properties to editors and vice versa. - -template -class EditorFactoryPrivate -{ -public: - - typedef QList EditorList; - typedef QMap PropertyToEditorListMap; - typedef QMap EditorToPropertyMap; - - Editor *createEditor(QtProperty *property, QWidget *parent); - void initializeEditor(QtProperty *property, Editor *e); - void slotEditorDestroyed(QObject *object); - - PropertyToEditorListMap m_createdEditors; - EditorToPropertyMap m_editorToProperty; -}; - -template -Editor *EditorFactoryPrivate::createEditor(QtProperty *property, QWidget *parent) -{ - Editor *editor = new Editor(parent); - initializeEditor(property, editor); - return editor; -} - -template -void EditorFactoryPrivate::initializeEditor(QtProperty *property, Editor *editor) -{ - typename PropertyToEditorListMap::iterator it = m_createdEditors.find(property); - if (it == m_createdEditors.end()) - it = m_createdEditors.insert(property, EditorList()); - it.value().append(editor); - m_editorToProperty.insert(editor, property); -} - -template -void EditorFactoryPrivate::slotEditorDestroyed(QObject *object) -{ - const typename EditorToPropertyMap::iterator ecend = m_editorToProperty.end(); - for (typename EditorToPropertyMap::iterator itEditor = m_editorToProperty.begin(); itEditor != ecend; ++itEditor) { - if (itEditor.key() == object) { - Editor *editor = itEditor.key(); - QtProperty *property = itEditor.value(); - const typename PropertyToEditorListMap::iterator pit = m_createdEditors.find(property); - if (pit != m_createdEditors.end()) { - pit.value().removeAll(editor); - if (pit.value().empty()) - m_createdEditors.erase(pit); - } - m_editorToProperty.erase(itEditor); - return; - } - } -} - // ------------ QtSpinBoxFactory class QtSpinBoxFactoryPrivate : public EditorFactoryPrivate @@ -2226,216 +2170,7 @@ void QtCursorEditorFactory::disconnectPropertyManager(QtCursorPropertyManager *m this, SLOT(slotPropertyChanged(QtProperty *, const QCursor &))); } -// QtColorEditWidget - -class QtColorEditWidget : public QWidget { - Q_OBJECT - -public: - QtColorEditWidget(QWidget *parent); - - bool eventFilter(QObject *obj, QEvent *ev); - -public Q_SLOTS: - void setValue(const QColor &value); - -Q_SIGNALS: - void valueChanged(const QColor &value); - -protected: - void paintEvent(QPaintEvent *); - -private Q_SLOTS: - void buttonClicked(); - -private: - QColor m_color; - QLabel *m_pixmapLabel; - QLabel *m_label; - QToolButton *m_button; -}; - -QtColorEditWidget::QtColorEditWidget(QWidget *parent) : - QWidget(parent), - m_pixmapLabel(new QLabel), - m_label(new QLabel), - m_button(new QToolButton) -{ - QHBoxLayout *lt = new QHBoxLayout(this); - setupTreeViewEditorMargin(lt); - lt->setSpacing(0); - lt->addWidget(m_pixmapLabel); - lt->addWidget(m_label); - lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored)); - - m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); - m_button->setFixedWidth(20); - setFocusProxy(m_button); - setFocusPolicy(m_button->focusPolicy()); - m_button->setText(tr("...")); - m_button->installEventFilter(this); - connect(m_button, SIGNAL(clicked()), this, SLOT(buttonClicked())); - lt->addWidget(m_button); - m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(m_color))); - m_label->setText(QtPropertyBrowserUtils::colorValueText(m_color)); -} - -void QtColorEditWidget::setValue(const QColor &c) -{ - if (m_color != c) { - m_color = c; - m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(c))); - m_label->setText(QtPropertyBrowserUtils::colorValueText(c)); - } -} - -void QtColorEditWidget::buttonClicked() -{ - QColor oldRgba = m_color; - QColor newRgba = QColorDialog::getColor(oldRgba, this, - QString(), - QColorDialog::ShowAlphaChannel| - QColorDialog::DontUseNativeDialog); - if (newRgba.isValid() && newRgba != oldRgba) { - setValue(newRgba); - emit valueChanged(m_color); - } -} - -bool QtColorEditWidget::eventFilter(QObject *obj, QEvent *ev) -{ - if (obj == m_button) { - switch (ev->type()) { - case QEvent::KeyPress: - case QEvent::KeyRelease: { // Prevent the QToolButton from handling Enter/Escape meant control the delegate - switch (static_cast(ev)->key()) { - case Qt::Key_Escape: - case Qt::Key_Enter: - case Qt::Key_Return: - ev->ignore(); - return true; - default: - break; - } - } - break; - default: - break; - } - } - return QWidget::eventFilter(obj, ev); -} - -void QtColorEditWidget::paintEvent(QPaintEvent *) -{ - QStyleOption opt; - opt.init(this); - QPainter p(this); - style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); -} - -// QtColorEditorFactoryPrivate - -class QtColorEditorFactoryPrivate : public EditorFactoryPrivate -{ - QtColorEditorFactory *q_ptr; - Q_DECLARE_PUBLIC(QtColorEditorFactory) -public: - - void slotPropertyChanged(QtProperty *property, const QColor &value); - void slotSetValue(const QColor &value); -}; - -void QtColorEditorFactoryPrivate::slotPropertyChanged(QtProperty *property, - const QColor &value) -{ - const PropertyToEditorListMap::iterator it = m_createdEditors.find(property); - if (it == m_createdEditors.end()) - return; - QListIterator itEditor(it.value()); - - while (itEditor.hasNext()) - itEditor.next()->setValue(value); -} - -void QtColorEditorFactoryPrivate::slotSetValue(const QColor &value) -{ - QObject *object = q_ptr->sender(); - const EditorToPropertyMap::ConstIterator ecend = m_editorToProperty.constEnd(); - for (EditorToPropertyMap::ConstIterator itEditor = m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor) - if (itEditor.key() == object) { - QtProperty *property = itEditor.value(); - QtColorPropertyManager *manager = q_ptr->propertyManager(property); - if (!manager) - return; - manager->setValue(property, value); - return; - } -} - -/*! - \class QtColorEditorFactory - - \brief The QtColorEditorFactory class provides color editing for - properties created by QtColorPropertyManager objects. - - \sa QtAbstractEditorFactory, QtColorPropertyManager -*/ - -/*! - Creates a factory with the given \a parent. -*/ -QtColorEditorFactory::QtColorEditorFactory(QObject *parent) : - QtAbstractEditorFactory(parent), - d_ptr(new QtColorEditorFactoryPrivate()) -{ - d_ptr->q_ptr = this; -} - -/*! - Destroys this factory, and all the widgets it has created. -*/ -QtColorEditorFactory::~QtColorEditorFactory() -{ - qDeleteAll(d_ptr->m_editorToProperty.keys()); - delete d_ptr; -} - -/*! - \internal - - Reimplemented from the QtAbstractEditorFactory class. -*/ -void QtColorEditorFactory::connectPropertyManager(QtColorPropertyManager *manager) -{ - connect(manager, SIGNAL(valueChanged(QtProperty*,QColor)), - this, SLOT(slotPropertyChanged(QtProperty*,QColor))); -} - -/*! - \internal - Reimplemented from the QtAbstractEditorFactory class. -*/ -QWidget *QtColorEditorFactory::createEditor(QtColorPropertyManager *manager, - QtProperty *property, QWidget *parent) -{ - QtColorEditWidget *editor = d_ptr->createEditor(property, parent); - editor->setValue(manager->value(property)); - connect(editor, SIGNAL(valueChanged(QColor)), this, SLOT(slotSetValue(QColor))); - connect(editor, SIGNAL(destroyed(QObject *)), this, SLOT(slotEditorDestroyed(QObject *))); - return editor; -} - -/*! - \internal - - Reimplemented from the QtAbstractEditorFactory class. -*/ -void QtColorEditorFactory::disconnectPropertyManager(QtColorPropertyManager *manager) -{ - disconnect(manager, SIGNAL(valueChanged(QtProperty*,QColor)), this, SLOT(slotPropertyChanged(QtProperty*,QColor))); -} // QtFontEditWidget diff --git a/src/qteditorfactory.h b/src/qteditorfactory.h index 124c888..0eac73e 100644 --- a/src/qteditorfactory.h +++ b/src/qteditorfactory.h @@ -101,7 +101,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtScrollBarFactory : public QtAbstractEditorFa { Q_OBJECT public: - QtScrollBarFactory(QObject *parent = 0); + QtScrollBarFactory(QObject *parent = nullptr); ~QtScrollBarFactory(); protected: void connectPropertyManager(QtIntPropertyManager *manager); @@ -125,7 +125,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCheckBoxFactory : public QtAbstractEditorFac { Q_OBJECT public: - QtCheckBoxFactory(QObject *parent = 0); + QtCheckBoxFactory(QObject *parent = nullptr); ~QtCheckBoxFactory(); protected: void connectPropertyManager(QtBoolPropertyManager *manager); @@ -149,7 +149,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtDoubleSpinBoxFactory : public QtAbstractEdit { Q_OBJECT public: - QtDoubleSpinBoxFactory(QObject *parent = 0); + QtDoubleSpinBoxFactory(QObject *parent = nullptr); ~QtDoubleSpinBoxFactory(); protected: void connectPropertyManager(QtDoublePropertyManager *manager); @@ -174,7 +174,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtLineEditFactory : public QtAbstractEditorFac { Q_OBJECT public: - QtLineEditFactory(QObject *parent = 0); + QtLineEditFactory(QObject *parent = nullptr); ~QtLineEditFactory(); protected: void connectPropertyManager(QtStringPropertyManager *manager); @@ -199,7 +199,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtDateEditFactory : public QtAbstractEditorFac { Q_OBJECT public: - QtDateEditFactory(QObject *parent = 0); + QtDateEditFactory(QObject *parent = nullptr); ~QtDateEditFactory(); protected: void connectPropertyManager(QtDatePropertyManager *manager); @@ -223,7 +223,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtTimeEditFactory : public QtAbstractEditorFac { Q_OBJECT public: - QtTimeEditFactory(QObject *parent = 0); + QtTimeEditFactory(QObject *parent = nullptr); ~QtTimeEditFactory(); protected: void connectPropertyManager(QtTimePropertyManager *manager); @@ -245,7 +245,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtDateTimeEditFactory : public QtAbstractEdito { Q_OBJECT public: - QtDateTimeEditFactory(QObject *parent = 0); + QtDateTimeEditFactory(QObject *parent = nullptr); ~QtDateTimeEditFactory(); protected: void connectPropertyManager(QtDateTimePropertyManager *manager); @@ -267,7 +267,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtKeySequenceEditorFactory : public QtAbstract { Q_OBJECT public: - QtKeySequenceEditorFactory(QObject *parent = 0); + QtKeySequenceEditorFactory(QObject *parent = nullptr); ~QtKeySequenceEditorFactory(); protected: void connectPropertyManager(QtKeySequencePropertyManager *manager); @@ -289,7 +289,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCharEditorFactory : public QtAbstractEditorF { Q_OBJECT public: - QtCharEditorFactory(QObject *parent = 0); + QtCharEditorFactory(QObject *parent = nullptr); ~QtCharEditorFactory(); protected: void connectPropertyManager(QtCharPropertyManager *manager); @@ -311,7 +311,7 @@ class QT_QTPROPERTYBROWSER_EXPORT QtEnumEditorFactory : public QtAbstractEditorF { Q_OBJECT public: - QtEnumEditorFactory(QObject *parent = 0); + QtEnumEditorFactory(QObject *parent = nullptr); ~QtEnumEditorFactory(); protected: void connectPropertyManager(QtEnumPropertyManager *manager); @@ -353,27 +353,6 @@ class QT_QTPROPERTYBROWSER_EXPORT QtCursorEditorFactory : public QtAbstractEdito Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *)) }; -class QtColorEditorFactoryPrivate; - -class QT_QTPROPERTYBROWSER_EXPORT QtColorEditorFactory : public QtAbstractEditorFactory -{ - Q_OBJECT -public: - QtColorEditorFactory(QObject *parent = 0); - ~QtColorEditorFactory(); -protected: - void connectPropertyManager(QtColorPropertyManager *manager); - QWidget *createEditor(QtColorPropertyManager *manager, QtProperty *property, - QWidget *parent); - void disconnectPropertyManager(QtColorPropertyManager *manager); -private: - QtColorEditorFactoryPrivate *d_ptr; - Q_DECLARE_PRIVATE(QtColorEditorFactory) - Q_DISABLE_COPY(QtColorEditorFactory) - Q_PRIVATE_SLOT(d_func(), void slotPropertyChanged(QtProperty *, const QColor &)) - Q_PRIVATE_SLOT(d_func(), void slotEditorDestroyed(QObject *)) - Q_PRIVATE_SLOT(d_func(), void slotSetValue(const QColor &)) -}; class QtFontEditorFactoryPrivate; diff --git a/src/qtpropertybrowserutils.cpp b/src/qtpropertybrowserutils.cpp index 128060d..56c0700 100644 --- a/src/qtpropertybrowserutils.cpp +++ b/src/qtpropertybrowserutils.cpp @@ -134,17 +134,27 @@ QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b) QImage img(16, 16, QImage::Format_ARGB32_Premultiplied); img.fill(0); + QBrush lg = QBrush(Qt::lightGray); + QBrush dg = QBrush(Qt::darkGray); + QPen noPen = QPen(Qt::NoPen); + QPainter painter(&img); - painter.setCompositionMode(QPainter::CompositionMode_Source); - painter.fillRect(0, 0, img.width(), img.height(), b); - QColor color = b.color(); - if (color.alpha() != 255) { // indicate alpha by an inset - QBrush opaqueBrush = b; - color.setAlpha(255); - opaqueBrush.setColor(color); - painter.fillRect(img.width() / 4, img.height() / 4, - img.width() / 2, img.height() / 2, opaqueBrush); + painter.setCompositionMode(QPainter::CompositionMode_SourceOver); + painter.setPen(noPen); + painter.setBrush(dg); + + painter.fillRect(0, 0, img.width(), img.height(), lg); + for(int i = 0; i < 2; i++) + { + for(int j = 0; j < 2; j++) + { + painter.drawRect(i * 8, j * 8, 4, 4); + painter.drawRect(i * 8 + 4, j * 8 + 4, 4, 4); + } } + + painter.fillRect(0, 0, img.width(), img.height(), b); + painter.end(); return QPixmap::fromImage(img); } @@ -163,6 +173,15 @@ QString QtPropertyBrowserUtils::colorValueText(const QColor &c) .arg(QString::number(c.alpha())); } +QString QtPropertyBrowserUtils::colorHexValueText(const QColor &c) +{ + return QString("#%1%2%3%4") + .arg(c.red(), 2, 16, QChar('0')) + .arg(c.green(), 2, 16, QChar('0')) + .arg(c.blue(), 2, 16, QChar('0')) + .arg(c.alpha(), 2, 16, QChar('0')); +} + QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font) { QFont f = font; diff --git a/src/qtpropertybrowserutils_p.h b/src/qtpropertybrowserutils_p.h index e3eb014..908b806 100644 --- a/src/qtpropertybrowserutils_p.h +++ b/src/qtpropertybrowserutils_p.h @@ -92,6 +92,7 @@ class QtPropertyBrowserUtils static QPixmap brushValuePixmap(const QBrush &b); static QIcon brushValueIcon(const QBrush &b); static QString colorValueText(const QColor &c); + static QString colorHexValueText(const QColor &c); static QPixmap fontValuePixmap(const QFont &f); static QIcon fontValueIcon(const QFont &f); static QString fontValueText(const QFont &f); diff --git a/src/qtvariantproperty.cpp b/src/qtvariantproperty.cpp index 83f7a23..580a906 100644 --- a/src/qtvariantproperty.cpp +++ b/src/qtvariantproperty.cpp @@ -1185,8 +1185,10 @@ QtVariantPropertyManager::QtVariantPropertyManager(QObject *parent) d_ptr->m_typeToValueType[QVariant::Color] = QVariant::Color; connect(colorPropertyManager, SIGNAL(valueChanged(QtProperty *, const QColor &)), this, SLOT(slotValueChanged(QtProperty *, const QColor &))); - connect(colorPropertyManager->subIntPropertyManager(), SIGNAL(valueChanged(QtProperty *, int)), - this, SLOT(slotValueChanged(QtProperty *, int))); +// connect(colorPropertyManager->subIntPropertyManager(), SIGNAL(valueChanged(QtProperty *, int)), +// this, SLOT(slotValueChanged(QtProperty *, int))); + connect(colorPropertyManager->subStringPropertyManager(), SIGNAL(valueChanged(QtProperty *, const QString&)), + this, SLOT(slotValueChanged(QtProperty *, const QString&))); connect(colorPropertyManager, SIGNAL(propertyInserted(QtProperty *, QtProperty *, QtProperty *)), this, SLOT(slotPropertyInserted(QtProperty *, QtProperty *, QtProperty *))); connect(colorPropertyManager, SIGNAL(propertyRemoved(QtProperty *, QtProperty *)), @@ -2196,7 +2198,7 @@ void QtVariantEditorFactory::connectPropertyManager(QtVariantPropertyManager *ma while (itColor.hasNext()) { QtColorPropertyManager *manager = itColor.next(); d_ptr->m_colorEditorFactory->addPropertyManager(manager); - d_ptr->m_spinBoxFactory->addPropertyManager(manager->subIntPropertyManager()); + d_ptr->m_lineEditFactory->addPropertyManager(manager->subStringPropertyManager()); } QList enumPropertyManagers = manager->findChildren(); @@ -2351,7 +2353,7 @@ void QtVariantEditorFactory::disconnectPropertyManager(QtVariantPropertyManager while (itColor.hasNext()) { QtColorPropertyManager *manager = itColor.next(); d_ptr->m_colorEditorFactory->removePropertyManager(manager); - d_ptr->m_spinBoxFactory->removePropertyManager(manager->subIntPropertyManager()); + d_ptr->m_lineEditFactory->removePropertyManager(manager->subStringPropertyManager()); } QList enumPropertyManagers = manager->findChildren(); From e34c4ae5d6e6ae42abeec9f789f2f9cae9d368a2 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 23 Dec 2019 03:13:06 +0300 Subject: [PATCH 35/39] Fixed an installing of an important headers --- src/CMakeLists.txt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1bdd0db..0c5e8a8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,7 @@ set(_SRCS ) file(GLOB _IMPL_HDRS *.h) +file(GLOB _IMPL_HDRS_MGRS managers/*.h) file(GLOB _PUBLIC_HDRS Qt*) set(_UI_FORMS @@ -35,7 +36,7 @@ set(_UI_FORMS set(_RESOURCES qtpropertybrowser.qrc ) - + QT5_WRAP_UI(_UI_SRCS ${_UI_FORMS}) QT5_ADD_RESOURCES(_QRC_SRCS ${_RESOURCES}) @@ -72,3 +73,12 @@ install( Devel ) +install( + FILES + ${_IMPL_HDRS_MGRS} + DESTINATION + ${INSTALL_INCLUDE_DIR}/managers + COMPONENT + Devel +) + From 7accd9922bf6acdb91354bbdcc01fcace2849884 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Mon, 23 Dec 2019 03:19:12 +0300 Subject: [PATCH 36/39] Also install hpp files --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0c5e8a8..b055328 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,7 +26,7 @@ set(_SRCS managers/common.cpp ) -file(GLOB _IMPL_HDRS *.h) +file(GLOB _IMPL_HDRS *.h *.hpp) file(GLOB _IMPL_HDRS_MGRS managers/*.h) file(GLOB _PUBLIC_HDRS Qt*) From c7507feab1e859f5ab7426ce58d80ca68dc863ce Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 24 Dec 2019 02:42:52 +0300 Subject: [PATCH 37/39] ColorPicker: minor UI style polishing - Remove unneeded margin - Make the field width fit with a text - Draw a border around color indicator box --- src/managers/color_property.cpp | 16 ++++++++++++---- src/qtpropertybrowserutils.cpp | 20 ++++++++++++++++---- src/qtpropertybrowserutils_p.h | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/managers/color_property.cpp b/src/managers/color_property.cpp index 9a7070f..0ec7968 100644 --- a/src/managers/color_property.cpp +++ b/src/managers/color_property.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -386,8 +387,9 @@ QtColorEditWidget::QtColorEditWidget(QWidget *parent) : QHBoxLayout *lt = new QHBoxLayout(this); setupTreeViewEditorMargin(lt); lt->setSpacing(4); - lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored)); + lt->setMargin(0); lt->addWidget(m_pixmapLabel); + lt->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored)); lt->addWidget(m_lineEdit); m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); m_button->setFixedWidth(20); @@ -398,18 +400,24 @@ QtColorEditWidget::QtColorEditWidget(QWidget *parent) : connect(m_button, SIGNAL(clicked()), this, SLOT(buttonClicked())); connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(editFinished())); lt->addWidget(m_button); - m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(m_color))); m_lineEdit->setText(QtPropertyBrowserUtils::colorHexValueText(m_color)); m_lineEdit->setMaxLength(9); - m_lineEdit->setMaximumWidth(80); +#ifdef _WIN32 + m_lineEdit->setFont(QFont("Courier New")); +#else + m_lineEdit->setFont(QFont("Monospace")); +#endif + QFontMetrics meter = QFontMetrics(m_lineEdit->font()); m_lineEdit->setValidator(new QRegExpValidator(QRegExp("#[0-9a-fA-F]{6,8}"), m_lineEdit)); + m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(m_color))); + m_lineEdit->setMaximumWidth(meter.width("#ffffffff", 9) + 10); } void QtColorEditWidget::setValue(const QColor &c) { if (m_color != c) { m_color = c; - m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(c))); + m_pixmapLabel->setPixmap(QtPropertyBrowserUtils::brushValuePixmap(QBrush(c), m_lineEdit->sizeHint())); m_lineEdit->setText(QtPropertyBrowserUtils::colorHexValueText(c)); } } diff --git a/src/qtpropertybrowserutils.cpp b/src/qtpropertybrowserutils.cpp index 56c0700..802c9e3 100644 --- a/src/qtpropertybrowserutils.cpp +++ b/src/qtpropertybrowserutils.cpp @@ -129,13 +129,21 @@ QCursor QtCursorDatabase::valueToCursor(int value) const } #endif -QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b) +QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b, QSize fieldSizeHint) { - QImage img(16, 16, QImage::Format_ARGB32_Premultiplied); + int w = 16, h = 16; + if(fieldSizeHint.isValid()) + { + w = fieldSizeHint.height(); + h = w; + } + + QImage img(w, h, QImage::Format_ARGB32_Premultiplied); img.fill(0); QBrush lg = QBrush(Qt::lightGray); QBrush dg = QBrush(Qt::darkGray); + QBrush bl = QBrush(Qt::black); QPen noPen = QPen(Qt::NoPen); QPainter painter(&img); @@ -144,9 +152,9 @@ QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b) painter.setBrush(dg); painter.fillRect(0, 0, img.width(), img.height(), lg); - for(int i = 0; i < 2; i++) + for(int i = 0; i < h / 4; i++) { - for(int j = 0; j < 2; j++) + for(int j = 0; j < w / 4; j++) { painter.drawRect(i * 8, j * 8, 4, 4); painter.drawRect(i * 8 + 4, j * 8 + 4, 4, 4); @@ -155,6 +163,10 @@ QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b) painter.fillRect(0, 0, img.width(), img.height(), b); + painter.setPen(QPen(Qt::black)); + painter.setBrush(QBrush(Qt::NoBrush)); + painter.drawRect(0, 0, img.width() - 1, img.height() - 1); + painter.end(); return QPixmap::fromImage(img); } diff --git a/src/qtpropertybrowserutils_p.h b/src/qtpropertybrowserutils_p.h index 908b806..3fedde3 100644 --- a/src/qtpropertybrowserutils_p.h +++ b/src/qtpropertybrowserutils_p.h @@ -89,7 +89,7 @@ class QtCursorDatabase class QtPropertyBrowserUtils { public: - static QPixmap brushValuePixmap(const QBrush &b); + static QPixmap brushValuePixmap(const QBrush &b, QSize fieldSizeHint = QSize()); static QIcon brushValueIcon(const QBrush &b); static QString colorValueText(const QColor &c); static QString colorHexValueText(const QColor &c); From 8715f57ace16fdcb0a1a5962f00ec4d6d2f73164 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 24 Dec 2019 02:43:05 +0300 Subject: [PATCH 38/39] CheckBox: set zero margin --- src/qtpropertybrowserutils.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/qtpropertybrowserutils.cpp b/src/qtpropertybrowserutils.cpp index 802c9e3..3841ba6 100644 --- a/src/qtpropertybrowserutils.cpp +++ b/src/qtpropertybrowserutils.cpp @@ -233,6 +233,7 @@ QtBoolEdit::QtBoolEdit(QWidget *parent) : lt->setContentsMargins(4, 0, 0, 0); else lt->setContentsMargins(0, 0, 4, 0); + lt->setMargin(0); lt->addWidget(m_checkBox); setLayout(lt); connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool))); From c249a27e0dd18817d038ba9033110d492bc76f85 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 7 Jan 2020 02:04:39 +0300 Subject: [PATCH 39/39] Update JSON example: Allow groups with ".." name If you made a group with a name that equals to "..", all content of this branch will be dropped into a parent branch in the settings dump. --- examples/json/CMakeLists.txt | 1 + examples/json/json_settings_widget.cpp | 81 ++++++++++++++++++++++++-- examples/json/json_settings_widget.h | 10 +++- 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/examples/json/CMakeLists.txt b/examples/json/CMakeLists.txt index ccdc2a0..887f57a 100644 --- a/examples/json/CMakeLists.txt +++ b/examples/json/CMakeLists.txt @@ -14,3 +14,4 @@ SET(KIT_SRCS ADD_EXECUTABLE(${example_name} ${KIT_SRCS}) TARGET_LINK_LIBRARIES(${example_name} ${PROJECT_NAME}) +target_compile_definitions(${example_name} PRIVATE -DDEBUG_BUILD) diff --git a/examples/json/json_settings_widget.cpp b/examples/json/json_settings_widget.cpp index 6cf34dd..2d02686 100644 --- a/examples/json/json_settings_widget.cpp +++ b/examples/json/json_settings_widget.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -13,7 +14,9 @@ #include #include +#ifdef DEBUG_BUILD #include +#endif QJsonObject JsonSettingsWidget::SetupStack::rectToArray(QVariant r) { @@ -87,8 +90,15 @@ QString JsonSettingsWidget::SetupStack::getPropertyId(const QString &name) bool JsonSettingsWidget::SetupStack::loadSetup(const QByteArray &layoutJson, QString &errorString) { - QJsonParseError errCode; + QJsonParseError errCode = QJsonParseError(); + m_setupTree.clear(); + if(layoutJson.isEmpty()) + { + clear(); + return true; + } + m_setupCache = QJsonDocument::fromJson(layoutJson, &errCode); if(errCode.error != QJsonParseError::NoError) { @@ -217,11 +227,30 @@ bool JsonSettingsWidget::loadSettings(const QByteArray &rawData) return m_setupStack.loadSetup(rawData, m_errorString); } +bool JsonSettingsWidget::loadSettings(const QString &rawData) +{ + m_errorString.clear(); + return m_setupStack.loadSetup(rawData.toUtf8(), m_errorString); +} + +bool JsonSettingsWidget::loadSettings(const QJsonDocument &rawData) +{ + m_errorString.clear(); + m_setupStack.m_setupTree.clear(); + m_setupStack.m_setupCache = rawData; + return true; +} + QString JsonSettingsWidget::saveSettings() { return m_setupStack.saveSetup(); } +QJsonDocument JsonSettingsWidget::getSettings() +{ + return m_setupStack.m_setupCache; +} + bool JsonSettingsWidget::loadLayout(const QByteArray &layout) { m_browser = loadLayoutDetail(m_setupStack, layout, m_errorString); @@ -235,6 +264,13 @@ bool JsonSettingsWidget::loadLayout(const QByteArray &settings, const QByteArray return loadLayout(layout); } +bool JsonSettingsWidget::loadLayout(const QJsonDocument &settings, const QByteArray &layout) +{ + if(!loadSettings(settings)) + return false; + return loadLayout(layout); +} + bool JsonSettingsWidget::loadLayoutFromFile(const QString &settings_path, const QString &layout_path) { if(!loadSettingsFromFile(settings_path)) @@ -260,6 +296,11 @@ bool JsonSettingsWidget::loadLayoutFromFile(const QString &settings_path, const return loadLayout(a); } +bool JsonSettingsWidget::spacerNeeded() +{ + return m_spacerNeeded; +} + bool JsonSettingsWidget::isValid() { return (m_browser != nullptr) && m_errorString.isEmpty(); @@ -290,7 +331,9 @@ QVariant JsonSettingsWidget::retrieve_property(const JsonSettingsWidget::SetupSt outPr.append(" << "); if(!o.contains(t)) { +#ifdef DEBUG_BUILD qDebug() << outPr << prop << defaultValue << "DEFAULT-TREE"; +#endif return defaultValue; } o = o[t].toObject(); @@ -299,7 +342,9 @@ QVariant JsonSettingsWidget::retrieve_property(const JsonSettingsWidget::SetupSt if(!o.contains(prop)) { +#ifdef DEBUG_BUILD qDebug() << outPr << prop << defaultValue << "DEFAULT-PROP"; +#endif return defaultValue; } @@ -346,7 +391,9 @@ QVariant JsonSettingsWidget::retrieve_property(const JsonSettingsWidget::SetupSt break; } +#ifdef DEBUG_BUILD qDebug() << outPr << prop << out << "INPUT"; +#endif return out; } @@ -391,7 +438,9 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT if(name.isEmpty()) continue;//invalid +#ifdef DEBUG_BUILD qDebug() << "property" << setupTree.getPropertyId(name); +#endif if(!control.compare("spinBox", Qt::CaseInsensitive)) { @@ -604,10 +653,19 @@ void JsonSettingsWidget::loadLayoutEntries(JsonSettingsWidget::SetupStack setupT QJsonArray children = o["children"].toArray(); if(!children.isEmpty()) { + bool noBranch = (name == ".."); + if(noBranch && title == name) + title.clear(); + QtProperty *subGroup = manager->addProperty(QtVariantPropertyManager::groupTypeId(), title); - setupTree.m_setupTree.push(name); + + if(!noBranch) + setupTree.m_setupTree.push(name); + loadLayoutEntries(setupTree, children, manager, subGroup, err, parent); - setupTree.m_setupTree.pop(); + + if(!noBranch) + setupTree.m_setupTree.pop(); target->addSubProperty(subGroup); } } @@ -622,10 +680,9 @@ QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidg QString style; QString title; - QJsonParseError errCode; + QJsonParseError errCode = QJsonParseError(); QJsonDocument layout = QJsonDocument::fromJson(layoutJson, &errCode); - if(errCode.error != QJsonParseError::NoError) { err = errCode.errorString(); @@ -637,16 +694,26 @@ QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidg style = layoutData["style"].toString(); title = layoutData["title"].toString(); if(style == "groupbox") + { gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent())); + m_spacerNeeded = true; + } else if(style == "frame") + { gui = new QtGroupBoxPropertyBrowser(qobject_cast(parent()), true); + m_spacerNeeded = true; + } else if(style == "button") + { gui = new QtButtonPropertyBrowser(qobject_cast(parent())); + m_spacerNeeded = true; + } else // "tree" is default { QtTreePropertyBrowser *gui_t = new QtTreePropertyBrowser(qobject_cast(parent())); gui_t->setPropertiesWithoutValueMarked(true); gui = gui_t; + m_spacerNeeded = false; } QtVariantPropertyManager *variantManager = new QtVariantPropertyManager(gui); @@ -656,11 +723,13 @@ QtAbstractPropertyBrowser *JsonSettingsWidget::loadLayoutDetail(JsonSettingsWidg QJsonArray layoutEntries = layoutData["layout"].toArray(); loadLayoutEntries(stack, layoutEntries, variantManager, topItem, err, qobject_cast(parent())); - variantManager->connect(variantManager, &QtVariantPropertyManager::valueChanged, + QtVariantPropertyManager::connect(variantManager, &QtVariantPropertyManager::valueChanged, [this](QtProperty *p,QVariant v) { QString pid = p->propertyId(); +#ifdef DEBUG_BUILD qDebug() << "changed:" << pid << v; +#endif if(!pid.isEmpty()) { m_setupStack.setValue(p->propertyId(), v); diff --git a/examples/json/json_settings_widget.h b/examples/json/json_settings_widget.h index bc16ebc..9d46260 100644 --- a/examples/json/json_settings_widget.h +++ b/examples/json/json_settings_widget.h @@ -24,8 +24,7 @@ class JsonSettingsWidget : public QObject QStack m_setupTree; QJsonDocument m_setupCache; - SetupStack() - {} + SetupStack() = default; QString getPropertyId(const QString &name); @@ -45,12 +44,18 @@ class JsonSettingsWidget : public QObject bool saveSettingsIntoFile(const QString &path); bool loadSettings(const QByteArray &rawData); + bool loadSettings(const QString &rawData); + bool loadSettings(const QJsonDocument &rawData); QString saveSettings(); + QJsonDocument getSettings(); bool loadLayout(const QByteArray &layout); bool loadLayout(const QByteArray &settings, const QByteArray &layout); + bool loadLayout(const QJsonDocument &settings, const QByteArray &layout); bool loadLayoutFromFile(const QString &settings_path, const QString &layout_path); + bool spacerNeeded(); + bool isValid(); QWidget *getWidget(); @@ -64,6 +69,7 @@ class JsonSettingsWidget : public QObject QString m_errorString; SetupStack m_setupStack; QtAbstractPropertyBrowser *m_browser = nullptr; + bool m_spacerNeeded = false; QVariant retrieve_property(const SetupStack &setupTree, QString prop, const QVariant &defaultValue);