From 9610698c66a170e04ad49c6d8c85ed3788df5afc Mon Sep 17 00:00:00 2001 From: Knud Dollereder Date: Thu, 11 Aug 2022 11:23:13 +0200 Subject: [PATCH] Allow editing keyframe values for other types than double Currently, the setKeyframeValue dialog only supports a QDoubleSpinbox as value field. When using this dialog to edit keyframe values of different type, they need to be converted which fails for some types (QColor). With this patch the dialog creates a dedicated control for each value type. In case the type is not caught it falls back to the old behavior. Fixes: QDS-6949 Change-Id: Icbaa2af24f06418fa60648d23269f1a12c08f9de Reviewed-by: Reviewed-by: Thomas Hartmann --- src/plugins/qmldesigner/CMakeLists.txt | 2 +- .../timelineeditor/setframevaluedialog.cpp | 142 +++++++++++++++--- .../timelineeditor/setframevaluedialog.h | 13 +- .../timelineeditor/setframevaluedialog.ui | 84 ----------- 4 files changed, 129 insertions(+), 112 deletions(-) delete mode 100644 src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.ui diff --git a/src/plugins/qmldesigner/CMakeLists.txt b/src/plugins/qmldesigner/CMakeLists.txt index ec0eaa65378..8b0081b1d89 100644 --- a/src/plugins/qmldesigner/CMakeLists.txt +++ b/src/plugins/qmldesigner/CMakeLists.txt @@ -497,7 +497,7 @@ extend_qtc_plugin(QmlDesigner easingcurve.cpp easingcurve.h easingcurvedialog.cpp easingcurvedialog.h preseteditor.cpp preseteditor.h - setframevaluedialog.cpp setframevaluedialog.h setframevaluedialog.ui + setframevaluedialog.cpp setframevaluedialog.h splineeditor.cpp splineeditor.h timeline.qrc timelineabstracttool.cpp timelineabstracttool.h diff --git a/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.cpp b/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.cpp index ad4d02df891..1bf437e802a 100644 --- a/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.cpp +++ b/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.cpp @@ -24,47 +24,147 @@ ****************************************************************************/ #include "setframevaluedialog.h" -#include "ui_setframevaluedialog.h" +#include "curveeditor/detail/colorcontrol.h" -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include namespace QmlDesigner { SetFrameValueDialog::SetFrameValueDialog(qreal frame, const QVariant &value, const QString &propertyName, QWidget *parent) - : QDialog(parent) - , ui(new Ui::SetFrameValueDialog) + : QDialog(parent, Qt::Tool) + , m_valueGetter() + , m_valueType(value.metaType()) + , m_frameControl(new QSpinBox) { - ui->setupUi(this); setWindowTitle(tr("Edit Keyframe")); - setFixedSize(size()); - ui->lineEditFrame->setValidator(new QIntValidator(0, 99999, this)); - auto dv = new QDoubleValidator(this); - dv->setDecimals(2); - ui->lineEditValue->setValidator(dv); + auto frameLabelString = QString(tr("Frame")); + auto labelWidth = fontMetrics().boundingRect(frameLabelString).width(); + if (auto tmp = fontMetrics().boundingRect(propertyName).width(); tmp > labelWidth) + labelWidth = tmp; - QLocale l; - ui->lineEditFrame->setText(l.toString(qRound(frame))); - ui->lineEditValue->setText(l.toString(value.toDouble(), 'f', 2)); - ui->labelValue->setText(propertyName); + auto *frameLabel = new QLabel(frameLabelString); + frameLabel->setAlignment(Qt::AlignRight); + frameLabel->setFixedWidth(labelWidth); + + auto *valueLabel = new QLabel(propertyName); + valueLabel->setAlignment(Qt::AlignRight); + valueLabel->setFixedWidth(labelWidth); + + m_frameControl->setRange(std::numeric_limits::min(), std::numeric_limits::max()); + m_frameControl->setValue(static_cast(frame)); + m_frameControl->setAlignment(Qt::AlignRight); + + auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); + + auto* frameRow = new QHBoxLayout; + frameRow->addWidget(frameLabel); + frameRow->addWidget(m_frameControl); + + auto* valueRow = new QHBoxLayout; + valueRow->addWidget(valueLabel); + valueRow->addWidget(createValueControl(value)); + + auto* hbox = new QVBoxLayout; + hbox->addLayout(frameRow); + hbox->addLayout(valueRow); + hbox->addStretch(); + hbox->addWidget(buttons); + + setLayout(hbox); } SetFrameValueDialog::~SetFrameValueDialog() -{ - delete ui; -} +{ } qreal SetFrameValueDialog::frame() const { - QLocale l; - return l.toDouble(ui->lineEditFrame->text()); + return static_cast(m_frameControl->value()); } QVariant SetFrameValueDialog::value() const { - QLocale l; - return QVariant(l.toDouble(ui->lineEditValue->text())); + if (m_valueGetter) + return m_valueGetter(); + return QVariant(m_valueType); +} + +QWidget* SetFrameValueDialog::createValueControl(const QVariant& value) +{ + m_valueType = value.metaType(); + + switch (value.metaType().id()) + { + + case QMetaType::QColor: { + auto* widget = new StyleEditor::ColorControl(value.value()); + m_valueGetter = [widget]() { return widget->value(); }; + return widget; + } + + case QMetaType::Bool: { + auto* widget = new QCheckBox; + widget->setChecked(value.toBool()); + m_valueGetter = [widget]() { return widget->isChecked(); }; + return widget; + } + + case QMetaType::Int: { + auto* widget = new QSpinBox; + widget->setRange(std::numeric_limits::min(), std::numeric_limits::max()); + widget->setAlignment(Qt::AlignRight); + widget->setValue(value.toInt()); + m_valueGetter = [widget]() { return widget->value(); }; + return widget; + } + + case QMetaType::UInt: { + auto* widget = new QSpinBox; + widget->setRange(0, std::numeric_limits::max()); + widget->setAlignment(Qt::AlignRight); + widget->setValue(value.toUInt()); + m_valueGetter = [widget]() { return static_cast(widget->value()); }; + return widget; + } + + case QMetaType::Float: { + auto* widget = new QDoubleSpinBox; + widget->setRange(std::numeric_limits::min(), std::numeric_limits::max()); + widget->setAlignment(Qt::AlignRight); + widget->setValue(value.toFloat()); + m_valueGetter = [widget]() { return static_cast(widget->value()); }; + return widget; + } + + case QMetaType::Double: + [[fallthrough]]; + + default: { + auto* widget = new QDoubleSpinBox; + widget->setRange(std::numeric_limits::min(), std::numeric_limits::max()); + widget->setAlignment(Qt::AlignRight); + widget->setValue(value.toDouble()); + m_valueGetter = [widget]() { return widget->value(); }; + return widget; + } + + } + + m_valueGetter = nullptr; + return nullptr; } } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.h b/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.h index 799e3fadc52..e2ca6e39010 100644 --- a/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.h +++ b/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.h @@ -27,14 +27,10 @@ #include -QT_FORWARD_DECLARE_CLASS(QLineEdit) +QT_FORWARD_DECLARE_CLASS(QSpinBox) namespace QmlDesigner { -namespace Ui { -class SetFrameValueDialog; -} - class SetFrameValueDialog : public QDialog { Q_OBJECT @@ -48,7 +44,12 @@ public: QVariant value() const; private: - Ui::SetFrameValueDialog *ui; + QWidget* createValueControl(const QVariant& value); + + std::function m_valueGetter; + + QMetaType m_valueType; + QSpinBox *m_frameControl; }; } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.ui b/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.ui deleted file mode 100644 index 8a3ba3c99cb..00000000000 --- a/src/plugins/qmldesigner/components/timelineeditor/setframevaluedialog.ui +++ /dev/null @@ -1,84 +0,0 @@ - - - QmlDesigner::SetFrameValueDialog - - - - 0 - 0 - 212 - 148 - - - - Dialog - - - - - - Frame - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - - - - - - Value - - - - - - - - - buttonBox - accepted() - QmlDesigner::SetFrameValueDialog - accept() - - - 248 - 254 - - - 157 - 274 - - - - - buttonBox - rejected() - QmlDesigner::SetFrameValueDialog - reject() - - - 316 - 260 - - - 286 - 274 - - - - -