1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "mainwindow.h"
5#include "splineeditor.h"
6#include <QtQuick/QQuickView>
7#include <QtQml/QQmlContext>
8#include <QEasingCurve>
9#include <QHBoxLayout>
10#include <QVBoxLayout>
11#include <QDoubleValidator>
12#include <QDialog>
13
14MainWindow::MainWindow(QWidget *parent) :
15 QMainWindow(parent)
16{
17 setWindowTitle("QML Easing Curve Editor");
18 SplineEditor *splineEditor = new SplineEditor(this);
19
20 QWidget *mainWidget = new QWidget(this);
21
22 setCentralWidget(mainWidget);
23
24 QHBoxLayout *hboxLayout = new QHBoxLayout(mainWidget);
25 QVBoxLayout *vboxLayout = new QVBoxLayout();
26
27 mainWidget->setLayout(hboxLayout);
28 hboxLayout->addLayout(layout: vboxLayout);
29
30 QWidget *propertyWidget = new QWidget(this);
31 ui_properties.setupUi(propertyWidget);
32
33 ui_properties.spinBox->setMinimum(50);
34 ui_properties.spinBox->setMaximum(10000);
35 ui_properties.spinBox->setValue(500);
36
37 hboxLayout->addWidget(propertyWidget);
38
39 m_placeholder = new QWidget(this);
40
41 m_placeholder->setFixedSize(quickView.size());
42
43 vboxLayout->addWidget(splineEditor);
44 vboxLayout->addWidget(m_placeholder);
45
46 ui_properties.plainTextEdit->setPlainText(splineEditor->generateCode());
47 connect(splineEditor, &SplineEditor::easingCurveCodeChanged, ui_properties.plainTextEdit, &QPlainTextEdit::setPlainText);
48
49 quickView.rootContext()->setContextProperty(QLatin1String("spinBox"), ui_properties.spinBox);
50
51 const auto presetNames = splineEditor->presetNames();
52 for (const QString &name : presetNames)
53 ui_properties.comboBox->addItem(name);
54
55 connect(ui_properties.comboBox, &QComboBox::currentTextChanged, splineEditor, &SplineEditor::setPreset);
56
57 splineEditor->setPreset(ui_properties.comboBox->currentText());
58
59 QVBoxLayout *groupBoxLayout = new QVBoxLayout(ui_properties.groupBox);
60 groupBoxLayout->setContentsMargins(QMargins());
61 ui_properties.groupBox->setLayout(groupBoxLayout);
62
63 groupBoxLayout->addWidget(splineEditor->pointListWidget());
64 m_splineEditor = splineEditor;
65 connect(ui_properties.plainTextEdit, &QPlainTextEdit::textChanged, this, &MainWindow::textEditTextChanged);
66
67 QDialog* importDialog = new QDialog(this);
68 ui_import.setupUi(importDialog);
69 ui_import.inInfluenceEdit->setValidator(new QDoubleValidator(this));
70 ui_import.inSlopeEdit->setValidator(new QDoubleValidator(this));
71 ui_import.outInfluenceEdit->setValidator(new QDoubleValidator(this));
72 ui_import.outSlopeEdit->setValidator(new QDoubleValidator(this));
73 connect(ui_properties.importButton, &QPushButton::clicked, importDialog, &QDialog::show);
74 connect(importDialog, &QDialog::finished, this, &MainWindow::importData);
75
76 initQml();
77}
78
79void MainWindow::showQuickView()
80{
81 const int margin = 16;
82 quickView.setPosition(pos() + QPoint(0, frameGeometry().height() + margin));
83
84 quickView.raise();
85 quickView.show();
86}
87
88void MainWindow::textEditTextChanged()
89{
90 m_splineEditor->setEasingCurve(ui_properties.plainTextEdit->toPlainText().trimmed());
91}
92
93void MainWindow::moveEvent(QMoveEvent *event)
94{
95 QMainWindow::moveEvent(event);
96 showQuickView();
97}
98
99void MainWindow::resizeEvent(QResizeEvent *event)
100{
101 QMainWindow::resizeEvent(event);
102 showQuickView();
103}
104
105void MainWindow::initQml()
106{
107 quickView.setFlags(Qt::FramelessWindowHint);
108 quickView.rootContext()->setContextProperty(QLatin1String("editor"), m_splineEditor);
109 quickView.setSource(QUrl("qrc:/preview.qml"));
110 quickView.show();
111}
112
113void MainWindow::closeEvent(QCloseEvent *)
114{
115 quickView.close();
116}
117
118void MainWindow::importData(int result)
119{
120 if (!result)
121 return;
122 double ii = ui_import.inInfluenceEdit->text().toDouble();
123 double is = ui_import.inSlopeEdit->text().toDouble();
124 double oi = ui_import.outInfluenceEdit->text().toDouble();
125 double os = ui_import.outSlopeEdit->text().toDouble();
126 ii = qBound<double>(min: 0., val: ii, max: 100.) / 100.;
127 oi = qBound<double>(min: 0., val: oi, max: 100.) / 100.;
128 QString generatedString = QString("[%1,%2,%3,%4,1,1]").arg(a: ii, fieldWidth: 0, format: 'f', precision: 3)
129 .arg(a: ii*is,fieldWidth: 0,format: 'f',precision: 3).arg(a: 1-oi, fieldWidth: 0, format: 'f', precision: 3).arg(a: 1-(oi*os), fieldWidth: 0, format: 'f', precision: 3);
130 ui_properties.plainTextEdit->setPlainText(generatedString);
131}
132
133#include "moc_mainwindow.cpp"
134

source code of qtdeclarative/tools/qmleasing/mainwindow.cpp