From de401b2d06311d706373cd6559f9dbc2cad53e0f Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 27 Oct 2020 18:26:33 +0200 Subject: [PATCH] Add notes for the base point tool to property browser. --- .../core/vtooloptionspropertybrowser.cpp | 11 +- .../plugins/vtextproperty.cpp | 134 ++++++++++++++++++ .../vpropertyexplorer/plugins/vtextproperty.h | 88 ++++++++++++ src/libs/vpropertyexplorer/vproperties.h | 1 + .../vpropertyexplorer/vpropertyexplorer.pri | 2 + .../vpropertyexplorer/vpropertyformwidget.cpp | 43 ++++-- src/libs/vtools/dialogs/dialogtoolbox.cpp | 21 +++ src/libs/vtools/dialogs/dialogtoolbox.h | 1 + .../dialogs/tools/dialogsinglepoint.cpp | 5 +- .../vtools/dialogs/tools/dialogsinglepoint.ui | 2 +- .../toolsinglepoint/vtoolbasepoint.cpp | 3 +- 11 files changed, 295 insertions(+), 16 deletions(-) create mode 100644 src/libs/vpropertyexplorer/plugins/vtextproperty.cpp create mode 100644 src/libs/vpropertyexplorer/plugins/vtextproperty.h diff --git a/src/app/valentina/core/vtooloptionspropertybrowser.cpp b/src/app/valentina/core/vtooloptionspropertybrowser.cpp index 874c154cc..f83e7a944 100644 --- a/src/app/valentina/core/vtooloptionspropertybrowser.cpp +++ b/src/app/valentina/core/vtooloptionspropertybrowser.cpp @@ -897,6 +897,9 @@ void VToolOptionsPropertyBrowser::ChangeDataToolSinglePoint(VPE::VProperty *prop case 1: // QLatin1String("position") i->SetBasePointPos(value.toPointF()); break; + case 61: // AttrNotes + i->SetNotes(value.toString()); + break; default: qWarning()<<"Unknown property type. id = "<setValue(i->GetBasePointPos()); AddProperty(itemPosition, QLatin1String("position")); + + VPE::VTextProperty* itemNotes = new VPE::VTextProperty(tr("Notes:")); + itemNotes->setValue(i->GetNotes()); + AddProperty(itemNotes, AttrNotes); } //--------------------------------------------------------------------------------------------------------------------- @@ -2501,6 +2508,7 @@ void VToolOptionsPropertyBrowser::UpdateOptionsToolSinglePoint() VToolBasePoint *i = qgraphicsitem_cast(currentItem); idToProperty[AttrName]->setValue(i->name()); idToProperty[QLatin1String("position")]->setValue(i->GetBasePointPos()); + idToProperty[AttrNotes]->setValue(i->GetNotes()); } //--------------------------------------------------------------------------------------------------------------------- @@ -3406,6 +3414,7 @@ QStringList VToolOptionsPropertyBrowser::PropertiesList() const << AttrPoint3 /* 57 */ << AttrPoint4 /* 58 */ << AttrPenStyle /* 59 */ - << AttrAScale; /* 60 */ + << AttrAScale /* 60 */ + << AttrNotes; /* 61 */ return attr; } diff --git a/src/libs/vpropertyexplorer/plugins/vtextproperty.cpp b/src/libs/vpropertyexplorer/plugins/vtextproperty.cpp new file mode 100644 index 000000000..0e9ae1d1d --- /dev/null +++ b/src/libs/vpropertyexplorer/plugins/vtextproperty.cpp @@ -0,0 +1,134 @@ +/************************************************************************ + ** + ** @file vtextproperty.cpp + ** @author Roman Telezhynskyi + ** @date 27 10, 2020 + ** + ** @brief + ** @copyright + ** All rights reserved. This program and the accompanying materials + ** are made available under the terms of the GNU Lesser General Public License + ** (LGPL) version 2.1 which accompanies this distribution, and is available at + ** http://www.gnu.org/licenses/lgpl-2.1.html + ** + ** This library is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + ** Lesser General Public License for more details. + ** + *************************************************************************/ +#include "vtextproperty.h" +#include "../vproperty_p.h" + +#include +#include + +namespace +{ +//--------------------------------------------------------------------------------------------------------------------- +void SetTabStopDistance(QPlainTextEdit *edit, int tabWidthChar=4); +void SetTabStopDistance(QPlainTextEdit *edit, int tabWidthChar) +{ + const auto fontMetrics = edit->fontMetrics(); + + const QString testString(" "); + +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + const int singleCharWidth = fontMetrics.width(testString); + edit->setTabStopWidth(tabWidthChar * singleCharWidth); +#else + // compute the size of a char in double-precision + static constexpr int bigNumber = 1000; // arbitrary big number. + const int many_char_width = fontMetrics.width(testString.repeated(bigNumber)); + const double singleCharWidthDouble = many_char_width / double(bigNumber); + // set the tab stop with double precision + edit->setTabStopDistance(tabWidthChar * singleCharWidthDouble); +#endif +} +} + + +VPE::VTextProperty::VTextProperty(const QString &name, const QMap &settings) + : VProperty(name, QVariant::String), + readOnly(false) +{ + VProperty::setSettings(settings); + d_ptr->VariantValue.setValue(QString()); + d_ptr->VariantValue.convert(QVariant::String); +} + +VPE::VTextProperty::VTextProperty(const QString &name) + : VProperty(name), + readOnly(false) +{ + d_ptr->VariantValue.setValue(QString()); + d_ptr->VariantValue.convert(QVariant::String); +} + +QWidget *VPE::VTextProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options, + const QAbstractItemDelegate *delegate) +{ + Q_UNUSED(options) + Q_UNUSED(delegate) + + QPlainTextEdit* tmpEditor = new QPlainTextEdit(parent); + tmpEditor->setLocale(parent->locale()); + tmpEditor->setReadOnly(readOnly); + tmpEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + tmpEditor->setPlainText(d_ptr->VariantValue.toString()); + SetTabStopDistance(tmpEditor); + + d_ptr->editor = tmpEditor; + return d_ptr->editor; +} + +QVariant VPE::VTextProperty::getEditorData(const QWidget *editor) const +{ + const QPlainTextEdit* tmpEditor = qobject_cast(editor); + if (tmpEditor) + { + return tmpEditor->toPlainText(); + } + + return QVariant(QString()); +} + +void VPE::VTextProperty::setReadOnly(bool readOnly) +{ + this->readOnly = readOnly; +} + +void VPE::VTextProperty::setSetting(const QString &key, const QVariant &value) +{ + if (key == QLatin1String("ReadOnly")) + { + setReadOnly(value.toBool()); + } +} + +QVariant VPE::VTextProperty::getSetting(const QString &key) const +{ + if (key == QLatin1String("ReadOnly")) + { + return readOnly; + } + else + return VProperty::getSetting(key); +} + +QStringList VPE::VTextProperty::getSettingKeys() const +{ + QStringList settings; + settings << QStringLiteral("ReadOnly"); + return settings; +} + +QString VPE::VTextProperty::type() const +{ + return QStringLiteral("string"); +} + +VPE::VProperty *VPE::VTextProperty::clone(bool include_children, VPE::VProperty *container) const +{ + return VProperty::clone(include_children, container ? container : new VTextProperty(getName(), getSettings())); +} diff --git a/src/libs/vpropertyexplorer/plugins/vtextproperty.h b/src/libs/vpropertyexplorer/plugins/vtextproperty.h new file mode 100644 index 000000000..91ddec63d --- /dev/null +++ b/src/libs/vpropertyexplorer/plugins/vtextproperty.h @@ -0,0 +1,88 @@ +/************************************************************************ + ** + ** @file vtextproperty.h + ** @author Roman Telezhynskyi + ** @date 27 10, 2020 + ** + ** @brief + ** @copyright + ** All rights reserved. This program and the accompanying materials + ** are made available under the terms of the GNU Lesser General Public License + ** (LGPL) version 2.1 which accompanies this distribution, and is available at + ** http://www.gnu.org/licenses/lgpl-2.1.html + ** + ** This library is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + ** Lesser General Public License for more details. + ** + *************************************************************************/ +#ifndef VTEXTPROPERTY_H +#define VTEXTPROPERTY_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../vproperty.h" +#include "../vpropertyexplorer_global.h" + +namespace VPE +{ + +//! Class for holding a text property +class VPROPERTYEXPLORERSHARED_EXPORT VTextProperty : public VProperty +{ +public: + VTextProperty(const QString& name, const QMap& settings); + + explicit VTextProperty(const QString& name); + + //! Returns an editor widget, or NULL if it doesn't supply one + //! \param parent The widget to which the editor will be added as a child + //! \options Render options + //! \delegate A pointer to the QAbstractItemDelegate requesting the editor. This can be used to connect signals and + //! slots. + virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& options, + const QAbstractItemDelegate* delegate) override; + + //! Gets the data from the widget + virtual QVariant getEditorData(const QWidget* editor) const override; + + void setReadOnly(bool readOnly); + + //! Sets the settings. + virtual void setSetting(const QString& key, const QVariant& value) override; + + //! Get the settings. This function has to be implemented in a subclass in order to have an effect + virtual QVariant getSetting(const QString& key) const override; + + //! Returns the list of keys of the property's settings + virtual QStringList getSettingKeys() const override; + + //! Returns a string containing the type of the property + virtual QString type() const override; + + //! Clones this property + //! \param include_children Indicates whether to also clone the children + //! \param container If a property is being passed here, no new VProperty is being created but instead it is tried + //! to fill all the data into container. This can also be used when subclassing this function. + //! \return Returns the newly created property (or container, if it was not NULL) + Q_REQUIRED_RESULT virtual VProperty* clone(bool include_children = true, + VProperty* container = nullptr) const override; +protected: + bool readOnly; + +private: + Q_DISABLE_COPY(VTextProperty) +}; + +} + +#endif // VTEXTPROPERTY_H diff --git a/src/libs/vpropertyexplorer/vproperties.h b/src/libs/vpropertyexplorer/vproperties.h index 8b2634b33..5ac50968f 100644 --- a/src/libs/vpropertyexplorer/vproperties.h +++ b/src/libs/vpropertyexplorer/vproperties.h @@ -37,5 +37,6 @@ #include "plugins/vwidgetproperty.h" #include "plugins/vlinecolorproperty.h" #include "plugins/vlabelproperty.h" +#include "plugins/vtextproperty.h" #endif // VPROPERTIES_H diff --git a/src/libs/vpropertyexplorer/vpropertyexplorer.pri b/src/libs/vpropertyexplorer/vpropertyexplorer.pri index a647804a0..f71b6547c 100644 --- a/src/libs/vpropertyexplorer/vpropertyexplorer.pri +++ b/src/libs/vpropertyexplorer/vpropertyexplorer.pri @@ -2,6 +2,7 @@ # This need for corect working file translations.pro SOURCES += \ + $$PWD/plugins/vtextproperty.cpp \ $$PWD/vproperty.cpp \ $$PWD/vpropertydelegate.cpp \ $$PWD/vpropertyfactorymanager.cpp \ @@ -35,6 +36,7 @@ SOURCES += \ *msvc*:SOURCES += $$PWD/stable.cpp HEADERS +=\ + $$PWD/plugins/vtextproperty.h \ $$PWD/vpropertyexplorer_global.h \ $$PWD/vpropertyfactorymanager_p.h \ $$PWD/vpropertytreeview_p.h \ diff --git a/src/libs/vpropertyexplorer/vpropertyformwidget.cpp b/src/libs/vpropertyexplorer/vpropertyformwidget.cpp index 182cc5631..04b3e8ea1 100644 --- a/src/libs/vpropertyexplorer/vpropertyformwidget.cpp +++ b/src/libs/vpropertyexplorer/vpropertyformwidget.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -304,19 +305,39 @@ bool VPE::VPropertyFormWidget::eventFilter(QObject *object, QEvent *event) if (event->type() == QEvent::KeyPress) { - switch (static_cast(event)->key()) + if (QPlainTextEdit *textEdit = qobject_cast(editor)) { - case Qt::Key_Tab: - case Qt::Key_Backtab: - case Qt::Key_Enter: - case Qt::Key_Return: - case Qt::Key_Escape: - commitData(editor); - event->accept(); - return true; - default: - return false; + switch (static_cast(event)->key()) + { + case Qt::Key_Escape: + commitData(editor); + event->accept(); + return true; + case Qt::Key_Tab: + case Qt::Key_Backtab: + case Qt::Key_Enter: + case Qt::Key_Return: + default: + return false; + } } + else + { + switch (static_cast(event)->key()) + { + case Qt::Key_Tab: + case Qt::Key_Backtab: + case Qt::Key_Enter: + case Qt::Key_Return: + case Qt::Key_Escape: + commitData(editor); + event->accept(); + return true; + default: + return false; + } + } + return false; } else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) { diff --git a/src/libs/vtools/dialogs/dialogtoolbox.cpp b/src/libs/vtools/dialogs/dialogtoolbox.cpp index 8d70b7d71..ef757a014 100644 --- a/src/libs/vtools/dialogs/dialogtoolbox.cpp +++ b/src/libs/vtools/dialogs/dialogtoolbox.cpp @@ -486,3 +486,24 @@ void CurrentCurveLength(vidtype curveId, VContainer *data) data->AddVariable(length); } + +//--------------------------------------------------------------------------------------------------------------------- +void SetTabStopDistance(QPlainTextEdit *edit, int tabWidthChar) +{ + SCASSERT(edit != nullptr) + const auto fontMetrics = edit->fontMetrics(); + + const QString testString(" "); + +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + const int single_char_width = fontMetrics.width(testString); + edit->setTabStopWidth(tabWidthChar * single_char_width); +#else + // compute the size of a char in double-precision + static constexpr int bigNumber = 1000; // arbitrary big number. + const int many_char_width = fontMetrics.width(testString.repeated(bigNumber)); + const double singleCharWidthDouble = many_char_width / double(bigNumber); + // set the tab stop with double precision + edit->setTabStopDistance(tabWidthChar * singleCharWidthDouble); +#endif +} diff --git a/src/libs/vtools/dialogs/dialogtoolbox.h b/src/libs/vtools/dialogs/dialogtoolbox.h index 1db4a737e..97c99b410 100644 --- a/src/libs/vtools/dialogs/dialogtoolbox.h +++ b/src/libs/vtools/dialogs/dialogtoolbox.h @@ -86,5 +86,6 @@ bool EachPointLabelIsUnique(QListWidget *listWidget); QString DialogWarningIcon(); QFont NodeFont(QFont font, bool nodeExcluded = false); void CurrentCurveLength(vidtype curveId, VContainer *data); +void SetTabStopDistance(QPlainTextEdit *edit, int tabWidthChar=4); #endif // DIALOGTOOLBOX_H diff --git a/src/libs/vtools/dialogs/tools/dialogsinglepoint.cpp b/src/libs/vtools/dialogs/tools/dialogsinglepoint.cpp index 0a1d0abec..7a6f11dd8 100644 --- a/src/libs/vtools/dialogs/tools/dialogsinglepoint.cpp +++ b/src/libs/vtools/dialogs/tools/dialogsinglepoint.cpp @@ -64,6 +64,7 @@ DialogSinglePoint::DialogSinglePoint(const VContainer *data, quint32 toolId, QWi }); ui->tabWidget->setCurrentIndex(0); + SetTabStopDistance(ui->plainTextEditToolNotes); } //--------------------------------------------------------------------------------------------------------------------- @@ -134,11 +135,11 @@ QString DialogSinglePoint::GetPointName() const //--------------------------------------------------------------------------------------------------------------------- void DialogSinglePoint::SetNotes(const QString ¬es) { - ui->textEditToolNotes->setText(notes); + ui->plainTextEditToolNotes->setPlainText(notes); } //--------------------------------------------------------------------------------------------------------------------- QString DialogSinglePoint::GetNotes() const { - return ui->textEditToolNotes->toPlainText(); + return ui->plainTextEditToolNotes->toPlainText(); } diff --git a/src/libs/vtools/dialogs/tools/dialogsinglepoint.ui b/src/libs/vtools/dialogs/tools/dialogsinglepoint.ui index 8fdb229bd..eecf0d912 100644 --- a/src/libs/vtools/dialogs/tools/dialogsinglepoint.ui +++ b/src/libs/vtools/dialogs/tools/dialogsinglepoint.ui @@ -126,7 +126,7 @@ - + diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp index 644359102..ec7834a0f 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp @@ -372,8 +372,9 @@ void VToolBasePoint::ShowContextMenu(QGraphicsSceneContextMenuEvent *event, quin /** * @brief FullUpdateFromFile update tool data form file. */ -void VToolBasePoint::FullUpdateFromFile() +void VToolBasePoint::FullUpdateFromFile() { + ReadAttributes(); RefreshPointGeometry(*VAbstractTool::data.GeometricObject(m_id)); }