/
dialogboxes.cpp
388 lines (330 loc) · 12.9 KB
/
dialogboxes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "dialogboxes.h"
//============================================================================//
// ColorEdit //
//============================================================================//
ColorEdit::ColorEdit(QRgb initialColor, int id)
: m_color(initialColor), m_id(id)
{
QHBoxLayout *layout = new QHBoxLayout;
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
m_lineEdit = new QLineEdit(QString::number(m_color, 16));
layout->addWidget(m_lineEdit);
m_button = new QFrame;
QPalette palette = m_button->palette();
palette.setColor(QPalette::Window, QColor(m_color));
m_button->setPalette(palette);
m_button->setAutoFillBackground(true);
m_button->setMinimumSize(32, 0);
m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
m_button->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
layout->addWidget(m_button);
connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(editDone()));
}
void ColorEdit::editDone()
{
bool ok;
QRgb newColor = m_lineEdit->text().toUInt(&ok, 16);
if (ok)
setColor(newColor);
}
void ColorEdit::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
QColor color(m_color);
QColorDialog dialog(color, 0);
dialog.setOption(QColorDialog::ShowAlphaChannel, true);
// The ifdef block is a workaround for the beta, TODO: remove when bug 238525 is fixed
#ifdef Q_DEAD_CODE_FROM_QT4_MAC
dialog.setOption(QColorDialog::DontUseNativeDialog, true);
#endif
dialog.move(280, 120);
if (dialog.exec() == QDialog::Rejected)
return;
QRgb newColor = dialog.selectedColor().rgba();
if (newColor == m_color)
return;
setColor(newColor);
}
}
void ColorEdit::setColor(QRgb color)
{
m_color = color;
m_lineEdit->setText(QString::number(m_color, 16)); // "Clean up" text
QPalette palette = m_button->palette();
palette.setColor(QPalette::Window, QColor(m_color));
m_button->setPalette(palette);
emit colorChanged(m_color, m_id);
}
//============================================================================//
// FloatEdit //
//============================================================================//
FloatEdit::FloatEdit(float initialValue, int id)
: m_value(initialValue), m_id(id)
{
QHBoxLayout *layout = new QHBoxLayout;
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
m_lineEdit = new QLineEdit(QString::number(m_value));
layout->addWidget(m_lineEdit);
connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(editDone()));
}
void FloatEdit::editDone()
{
bool ok;
float newValue = m_lineEdit->text().toFloat(&ok);
if (ok) {
m_value = newValue;
m_lineEdit->setText(QString::number(m_value)); // "Clean up" text
emit valueChanged(m_value, m_id);
}
}
//============================================================================//
// TwoSidedGraphicsWidget //
//============================================================================//
TwoSidedGraphicsWidget::TwoSidedGraphicsWidget(QGraphicsScene *scene)
: QObject(scene)
, m_current(0)
, m_angle(0)
, m_delta(0)
{
for (int i = 0; i < 2; ++i)
m_proxyWidgets[i] = 0;
}
void TwoSidedGraphicsWidget::setWidget(int index, QWidget *widget)
{
if (index < 0 || index >= 2)
{
qWarning("TwoSidedGraphicsWidget::setWidget: Index out of bounds, index == %d", index);
return;
}
GraphicsWidget *proxy = new GraphicsWidget;
proxy->setWidget(widget);
if (m_proxyWidgets[index])
delete m_proxyWidgets[index];
m_proxyWidgets[index] = proxy;
proxy->setCacheMode(QGraphicsItem::ItemCoordinateCache);
proxy->setZValue(1e30); // Make sure the dialog is drawn on top of all other (OpenGL) items
if (index != m_current)
proxy->setVisible(false);
qobject_cast<QGraphicsScene *>(parent())->addItem(proxy);
}
QWidget *TwoSidedGraphicsWidget::widget(int index)
{
if (index < 0 || index >= 2)
{
qWarning("TwoSidedGraphicsWidget::widget: Index out of bounds, index == %d", index);
return 0;
}
return m_proxyWidgets[index]->widget();
}
void TwoSidedGraphicsWidget::flip()
{
m_delta = (m_current == 0 ? 9 : -9);
animateFlip();
}
void TwoSidedGraphicsWidget::animateFlip()
{
m_angle += m_delta;
if (m_angle == 90) {
int old = m_current;
m_current ^= 1;
m_proxyWidgets[old]->setVisible(false);
m_proxyWidgets[m_current]->setVisible(true);
m_proxyWidgets[m_current]->setGeometry(m_proxyWidgets[old]->geometry());
}
QRectF r = m_proxyWidgets[m_current]->boundingRect();
m_proxyWidgets[m_current]->setTransform(QTransform()
.translate(r.width() / 2, r.height() / 2)
.rotate(m_angle - 180 * m_current, Qt::YAxis)
.translate(-r.width() / 2, -r.height() / 2));
if ((m_current == 0 && m_angle > 0) || (m_current == 1 && m_angle < 180))
QTimer::singleShot(25, this, SLOT(animateFlip()));
}
QVariant GraphicsWidget::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QRectF rect = boundingRect();
QPointF pos = value.toPointF();
QRectF sceneRect = scene()->sceneRect();
if (pos.x() + rect.left() < sceneRect.left())
pos.setX(sceneRect.left() - rect.left());
else if (pos.x() + rect.right() >= sceneRect.right())
pos.setX(sceneRect.right() - rect.right());
if (pos.y() + rect.top() < sceneRect.top())
pos.setY(sceneRect.top() - rect.top());
else if (pos.y() + rect.bottom() >= sceneRect.bottom())
pos.setY(sceneRect.bottom() - rect.bottom());
return pos;
}
return QGraphicsProxyWidget::itemChange(change, value);
}
void GraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
setCacheMode(QGraphicsItem::NoCache);
setCacheMode(QGraphicsItem::ItemCoordinateCache);
QGraphicsProxyWidget::resizeEvent(event);
}
void GraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing, false);
QGraphicsProxyWidget::paint(painter, option, widget);
//painter->setRenderHint(QPainter::Antialiasing, true);
}
//============================================================================//
// RenderOptionsDialog //
//============================================================================//
RenderOptionsDialog::RenderOptionsDialog()
: QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint)
{
setWindowOpacity(0.75);
setWindowTitle(tr("Options (double click to flip)"));
QGridLayout *layout = new QGridLayout;
setLayout(layout);
layout->setColumnStretch(1, 1);
int row = 0;
QCheckBox *check = new QCheckBox(tr("Dynamic cube map"));
check->setCheckState(Qt::Unchecked);
// Dynamic cube maps are only enabled when multi-texturing and render to texture are available.
check->setEnabled(glActiveTexture && glGenFramebuffersEXT);
connect(check, SIGNAL(stateChanged(int)), this, SIGNAL(dynamicCubemapToggled(int)));
layout->addWidget(check, 0, 0, 1, 2);
++row;
QPalette palette;
// Load all .par files
// .par files have a simple syntax for specifying user adjustable uniform variables.
QSet<QByteArray> uniforms;
QList<QString> filter = QStringList("*.par");
QList<QFileInfo> files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
foreach (QFileInfo fileInfo, files) {
QFile file(fileInfo.absoluteFilePath());
if (file.open(QIODevice::ReadOnly)) {
while (!file.atEnd()) {
QList<QByteArray> tokens = file.readLine().simplified().split(' ');
QList<QByteArray>::const_iterator it = tokens.begin();
if (it == tokens.end())
continue;
QByteArray type = *it;
if (++it == tokens.end())
continue;
QByteArray name = *it;
bool singleElement = (tokens.size() == 3); // type, name and one value
char counter[10] = "000000000";
int counterPos = 8; // position of last digit
while (++it != tokens.end()) {
m_parameterNames << name;
if (!singleElement) {
m_parameterNames.back() += "[";
m_parameterNames.back() += counter + counterPos;
m_parameterNames.back() += "]";
int j = 8; // position of last digit
++counter[j];
while (j > 0 && counter[j] > '9') {
counter[j] = '0';
++counter[--j];
}
if (j < counterPos)
counterPos = j;
}
if (type == "color") {
layout->addWidget(new QLabel(m_parameterNames.back()));
bool ok;
ColorEdit *colorEdit = new ColorEdit(it->toUInt(&ok, 16), m_parameterNames.size() - 1);
m_parameterEdits << colorEdit;
layout->addWidget(colorEdit);
connect(colorEdit, SIGNAL(colorChanged(QRgb,int)), this, SLOT(setColorParameter(QRgb,int)));
++row;
} else if (type == "float") {
layout->addWidget(new QLabel(m_parameterNames.back()));
bool ok;
FloatEdit *floatEdit = new FloatEdit(it->toFloat(&ok), m_parameterNames.size() - 1);
m_parameterEdits << floatEdit;
layout->addWidget(floatEdit);
connect(floatEdit, SIGNAL(valueChanged(float,int)), this, SLOT(setFloatParameter(float,int)));
++row;
}
}
}
file.close();
}
}
layout->addWidget(new QLabel(tr("Texture:")));
m_textureCombo = new QComboBox;
connect(m_textureCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(textureChanged(int)));
layout->addWidget(m_textureCombo);
++row;
layout->addWidget(new QLabel(tr("Shader:")));
m_shaderCombo = new QComboBox;
connect(m_shaderCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(shaderChanged(int)));
layout->addWidget(m_shaderCombo);
++row;
layout->setRowStretch(row, 1);
}
int RenderOptionsDialog::addTexture(const QString &name)
{
m_textureCombo->addItem(name);
return m_textureCombo->count() - 1;
}
int RenderOptionsDialog::addShader(const QString &name)
{
m_shaderCombo->addItem(name);
return m_shaderCombo->count() - 1;
}
void RenderOptionsDialog::emitParameterChanged()
{
foreach (ParameterEdit *edit, m_parameterEdits)
edit->emitChange();
}
void RenderOptionsDialog::setColorParameter(QRgb color, int id)
{
emit colorParameterChanged(m_parameterNames[id], color);
}
void RenderOptionsDialog::setFloatParameter(float value, int id)
{
emit floatParameterChanged(m_parameterNames[id], value);
}
void RenderOptionsDialog::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
emit doubleClicked();
}
//============================================================================//
// ItemDialog //
//============================================================================//
ItemDialog::ItemDialog()
: QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint)
{
setWindowTitle(tr("Items (double click to flip)"));
setWindowOpacity(0.75);
resize(160, 100);
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
QPushButton *button;
button = new QPushButton(tr("Add Qt box"));
layout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(triggerNewQtBox()));
button = new QPushButton(tr("Add circle"));
layout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(triggerNewCircleItem()));
button = new QPushButton(tr("Add square"));
layout->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(triggerNewSquareItem()));
layout->addStretch(1);
}
void ItemDialog::triggerNewQtBox()
{
emit newItemTriggered(QtBoxItem);
}
void ItemDialog::triggerNewCircleItem()
{
emit newItemTriggered(CircleItem);
}
void ItemDialog::triggerNewSquareItem()
{
emit newItemTriggered(SquareItem);
}
void ItemDialog::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
emit doubleClicked();
}