1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the tools applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "mainwindow.h" |
30 | #include "splineeditor.h" |
31 | #include <QtQuick/QQuickView> |
32 | #include <QtQml/QQmlContext> |
33 | #include <QEasingCurve> |
34 | #include <QHBoxLayout> |
35 | #include <QVBoxLayout> |
36 | #include <QDoubleValidator> |
37 | #include <QDialog> |
38 | |
39 | MainWindow::MainWindow(QWidget *parent) : |
40 | QMainWindow(parent) |
41 | { |
42 | setWindowTitle("QML Easing Curve Editor" ); |
43 | SplineEditor *splineEditor = new SplineEditor(this); |
44 | |
45 | QWidget *mainWidget = new QWidget(this); |
46 | |
47 | setCentralWidget(mainWidget); |
48 | |
49 | QHBoxLayout *hboxLayout = new QHBoxLayout(mainWidget); |
50 | QVBoxLayout *vboxLayout = new QVBoxLayout(); |
51 | |
52 | mainWidget->setLayout(hboxLayout); |
53 | hboxLayout->addLayout(layout: vboxLayout); |
54 | |
55 | QWidget *propertyWidget = new QWidget(this); |
56 | ui_properties.setupUi(propertyWidget); |
57 | |
58 | ui_properties.spinBox->setMinimum(50); |
59 | ui_properties.spinBox->setMaximum(10000); |
60 | ui_properties.spinBox->setValue(500); |
61 | |
62 | hboxLayout->addWidget(propertyWidget); |
63 | |
64 | m_placeholder = new QWidget(this); |
65 | |
66 | m_placeholder->setFixedSize(quickView.size()); |
67 | |
68 | vboxLayout->addWidget(splineEditor); |
69 | vboxLayout->addWidget(m_placeholder); |
70 | |
71 | ui_properties.plainTextEdit->setPlainText(splineEditor->generateCode()); |
72 | connect(sender: splineEditor, SIGNAL(easingCurveCodeChanged(QString)), receiver: ui_properties.plainTextEdit, SLOT(setPlainText(QString))); |
73 | |
74 | quickView.rootContext()->setContextProperty(QLatin1String("spinBox" ), ui_properties.spinBox); |
75 | |
76 | const auto presetNames = splineEditor->presetNames(); |
77 | for (const QString &name : presetNames) |
78 | ui_properties.comboBox->addItem(atext: name); |
79 | |
80 | connect(sender: ui_properties.comboBox, SIGNAL(currentIndexChanged(QString)), receiver: splineEditor, SLOT(setPreset(QString))); |
81 | |
82 | splineEditor->setPreset(ui_properties.comboBox->currentText()); |
83 | |
84 | QVBoxLayout *groupBoxLayout = new QVBoxLayout(ui_properties.groupBox); |
85 | groupBoxLayout->setContentsMargins(QMargins()); |
86 | ui_properties.groupBox->setLayout(groupBoxLayout); |
87 | |
88 | groupBoxLayout->addWidget(splineEditor->pointListWidget()); |
89 | m_splineEditor = splineEditor; |
90 | connect(sender: ui_properties.plainTextEdit, SIGNAL(textChanged()), receiver: this, SLOT(textEditTextChanged())); |
91 | |
92 | QDialog* importDialog = new QDialog(this); |
93 | ui_import.setupUi(importDialog); |
94 | ui_import.inInfluenceEdit->setValidator(new QDoubleValidator(this)); |
95 | ui_import.inSlopeEdit->setValidator(new QDoubleValidator(this)); |
96 | ui_import.outInfluenceEdit->setValidator(new QDoubleValidator(this)); |
97 | ui_import.outSlopeEdit->setValidator(new QDoubleValidator(this)); |
98 | connect(sender: ui_properties.importButton, SIGNAL(clicked()), receiver: importDialog, SLOT(show())); |
99 | connect(sender: importDialog, SIGNAL(finished(int)), receiver: this, SLOT(importData(int))); |
100 | |
101 | initQml(); |
102 | } |
103 | |
104 | void MainWindow::showQuickView() |
105 | { |
106 | const int margin = 16; |
107 | quickView.setPosition(pos() + QPoint(0, frameGeometry().height() + margin)); |
108 | |
109 | quickView.raise(); |
110 | quickView.show(); |
111 | } |
112 | |
113 | void MainWindow::textEditTextChanged() |
114 | { |
115 | m_splineEditor->setEasingCurve(ui_properties.plainTextEdit->toPlainText().trimmed()); |
116 | } |
117 | |
118 | void MainWindow::moveEvent(QMoveEvent *event) |
119 | { |
120 | QMainWindow::moveEvent(event); |
121 | showQuickView(); |
122 | } |
123 | |
124 | void MainWindow::resizeEvent(QResizeEvent *event) |
125 | { |
126 | QMainWindow::resizeEvent(event); |
127 | showQuickView(); |
128 | } |
129 | |
130 | void MainWindow::initQml() |
131 | { |
132 | quickView.setFlags(Qt::FramelessWindowHint); |
133 | quickView.rootContext()->setContextProperty(QLatin1String("editor" ), m_splineEditor); |
134 | quickView.setSource(QUrl("qrc:/preview.qml" )); |
135 | quickView.show(); |
136 | } |
137 | |
138 | void MainWindow::closeEvent(QCloseEvent *) |
139 | { |
140 | quickView.close(); |
141 | } |
142 | |
143 | void MainWindow::importData(int result) |
144 | { |
145 | if (!result) |
146 | return; |
147 | double ii = ui_import.inInfluenceEdit->text().toDouble(); |
148 | double is = ui_import.inSlopeEdit->text().toDouble(); |
149 | double oi = ui_import.outInfluenceEdit->text().toDouble(); |
150 | double os = ui_import.outSlopeEdit->text().toDouble(); |
151 | ii = qBound<double>(min: 0., val: ii, max: 100.) / 100.; |
152 | oi = qBound<double>(min: 0., val: oi, max: 100.) / 100.; |
153 | QString generatedString = QString("[%1,%2,%3,%4,1,1]" ).arg(a: ii, fieldWidth: 0, fmt: 'f', prec: 3) |
154 | .arg(a: ii*is,fieldWidth: 0,fmt: 'f',prec: 3).arg(a: 1-oi, fieldWidth: 0, fmt: 'f', prec: 3).arg(a: 1-(oi*os), fieldWidth: 0, fmt: 'f', prec: 3); |
155 | ui_properties.plainTextEdit->setPlainText(generatedString); |
156 | } |
157 | |
158 | #include "moc_mainwindow.cpp" |
159 | |