mainwindow.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the Qt Solutions component.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** You may use this file under the terms of the BSD license as follows:
10**
11** "Redistribution and use in source and binary forms, with or without
12** modification, are permitted provided that the following conditions are
13** met:
14** * Redistributions of source code must retain the above copyright
15** notice, this list of conditions and the following disclaimer.
16** * Redistributions in binary form must reproduce the above copyright
17** notice, this list of conditions and the following disclaimer in
18** the documentation and/or other materials provided with the
19** distribution.
20** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21** of its contributors may be used to endorse or promote products derived
22** from this software without specific prior written permission.
23**
24**
25** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "mainwindow.h"
42
43#include <QAction>
44#include <QDockWidget>
45#include <QMatrix>
46#include <QMenu>
47#include <QMenuBar>
48#include <QMouseEvent>
49
50#include "qteditorfactory.h"
51#include "qtpropertymanager.h"
53
54void
56{
57 handleMouseClickEvent(event);
58}
59
60void
62{
63 handleMouseClickEvent(event);
64}
65
66void
67CanvasView::handleMouseClickEvent(QMouseEvent* event)
68{
69 QPoint p = inverseWorldMatrix().map(event->pos());
71 moving = 0;
72
73 if (!l.isEmpty())
74 {
75 moving = l.first();
76 }
77
78 moving_start = p;
79 emit itemClicked(moving);
80}
81
82void
84{
85 if (moving)
86 {
87 QPoint p = inverseWorldMatrix().map(event->pos());
88 moving->moveBy(p.x() - moving_start.x(), p.y() - moving_start.y());
89 moving_start = p;
90 canvas()->update();
91 emit itemMoved(moving);
92 }
93}
94
95MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
96{
97 QMenu* editMenu = menuBar()->addMenu(tr("Edit"));
98 QMenu* newObjectMenu = editMenu->addMenu(tr("New Object"));
99
100 QAction* newRectangleAction = new QAction(tr("Rectangle"), this);
101 connect(newRectangleAction, SIGNAL(triggered(bool)), this, SLOT(newRectangle()));
102 newObjectMenu->addAction(newRectangleAction);
103
104 QAction* newLineAction = new QAction(tr("Line"), this);
105 connect(newLineAction, SIGNAL(triggered(bool)), this, SLOT(newLine()));
106 newObjectMenu->addAction(newLineAction);
107
108 QAction* newEllipseAction = new QAction(tr("Ellipse"), this);
109 connect(newEllipseAction, SIGNAL(triggered(bool)), this, SLOT(newEllipse()));
110 newObjectMenu->addAction(newEllipseAction);
111
112 QAction* newTextAction = new QAction(tr("Text"), this);
113 connect(newTextAction, SIGNAL(triggered(bool)), this, SLOT(newText()));
114 newObjectMenu->addAction(newTextAction);
115
116 deleteAction = new QAction(tr("Delete Object"), this);
117 connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteObject()));
118 editMenu->addAction(deleteAction);
119
120 QAction* clearAction = new QAction(tr("Clear All"), this);
121 connect(clearAction, SIGNAL(triggered(bool)), this, SLOT(clearAll()));
122 editMenu->addAction(clearAction);
123
124 QAction* fillAction = new QAction(tr("Fill View"), this);
125 connect(fillAction, SIGNAL(triggered(bool)), this, SLOT(fillView()));
126 editMenu->addAction(fillAction);
127
128 doubleManager = new QtDoublePropertyManager(this);
129 stringManager = new QtStringPropertyManager(this);
130 colorManager = new QtColorPropertyManager(this);
131 fontManager = new QtFontPropertyManager(this);
132 pointManager = new QtPointPropertyManager(this);
133 sizeManager = new QtSizePropertyManager(this);
134
135 connect(doubleManager,
136 SIGNAL(valueChanged(QtProperty*, double)),
137 this,
138 SLOT(valueChanged(QtProperty*, double)));
139 connect(stringManager,
140 SIGNAL(valueChanged(QtProperty*, const QString&)),
141 this,
142 SLOT(valueChanged(QtProperty*, const QString&)));
143 connect(colorManager,
144 SIGNAL(valueChanged(QtProperty*, const QColor&)),
145 this,
146 SLOT(valueChanged(QtProperty*, const QColor&)));
147 connect(fontManager,
148 SIGNAL(valueChanged(QtProperty*, const QFont&)),
149 this,
150 SLOT(valueChanged(QtProperty*, const QFont&)));
151 connect(pointManager,
152 SIGNAL(valueChanged(QtProperty*, const QPoint&)),
153 this,
154 SLOT(valueChanged(QtProperty*, const QPoint&)));
155 connect(sizeManager,
156 SIGNAL(valueChanged(QtProperty*, const QSize&)),
157 this,
158 SLOT(valueChanged(QtProperty*, const QSize&)));
159
160 QtDoubleSpinBoxFactory* doubleSpinBoxFactory = new QtDoubleSpinBoxFactory(this);
161 QtCheckBoxFactory* checkBoxFactory = new QtCheckBoxFactory(this);
162 QtSpinBoxFactory* spinBoxFactory = new QtSpinBoxFactory(this);
163 QtLineEditFactory* lineEditFactory = new QtLineEditFactory(this);
164 QtEnumEditorFactory* comboBoxFactory = new QtEnumEditorFactory(this);
165
166 canvas = new QtCanvas(800, 600);
167 canvasView = new CanvasView(canvas, this);
168 setCentralWidget(canvasView);
169
170 QDockWidget* dock = new QDockWidget(this);
171 addDockWidget(Qt::RightDockWidgetArea, dock);
172
173 propertyEditor = new QtTreePropertyBrowser(dock);
174 propertyEditor->setFactoryForManager(doubleManager, doubleSpinBoxFactory);
175 propertyEditor->setFactoryForManager(stringManager, lineEditFactory);
176 propertyEditor->setFactoryForManager(colorManager->subIntPropertyManager(), spinBoxFactory);
177 propertyEditor->setFactoryForManager(fontManager->subIntPropertyManager(), spinBoxFactory);
178 propertyEditor->setFactoryForManager(fontManager->subBoolPropertyManager(), checkBoxFactory);
179 propertyEditor->setFactoryForManager(fontManager->subEnumPropertyManager(), comboBoxFactory);
180 propertyEditor->setFactoryForManager(pointManager->subIntPropertyManager(), spinBoxFactory);
181 propertyEditor->setFactoryForManager(sizeManager->subIntPropertyManager(), spinBoxFactory);
182 dock->setWidget(propertyEditor);
183
184 currentItem = 0;
185
186 connect(canvasView, SIGNAL(itemClicked(QtCanvasItem*)), this, SLOT(itemClicked(QtCanvasItem*)));
187 connect(canvasView, SIGNAL(itemMoved(QtCanvasItem*)), this, SLOT(itemMoved(QtCanvasItem*)));
188
189 fillView();
190 itemClicked(0);
191}
192
193void
194MainWindow::newRectangle()
195{
196 QtCanvasItem* item = addRectangle();
197 canvas->update();
198 itemClicked(item);
199}
200
201void
202MainWindow::newEllipse()
203{
204 QtCanvasItem* item = addEllipse();
205 canvas->update();
206 itemClicked(item);
207}
208
209void
210MainWindow::newLine()
211{
212 QtCanvasItem* item = addLine();
213 canvas->update();
214 itemClicked(item);
215}
216
217void
218MainWindow::newText()
219{
220 QtCanvasItem* item = addText();
221 canvas->update();
222 itemClicked(item);
223}
224
225void
226MainWindow::deleteObject()
227{
228 if (!currentItem)
229 {
230 return;
231 }
232
233 delete currentItem;
234 itemClicked(0);
235 canvas->update();
236}
237
238void
239MainWindow::clearAll()
240{
241 QtCanvasItemList list = canvas->allItems();
242 qDeleteAll(list);
243 itemClicked(0);
244 canvas->update();
245}
246
247void
248MainWindow::fillView()
249{
250 for (int i = 0; i < 10; i++)
251 {
252 addRectangle();
253 addEllipse();
254 addLine();
255 addText();
256 }
257
258 canvas->update();
259}
260
262MainWindow::addRectangle()
263{
264 QtCanvasPolygonalItem* item =
265 new QtCanvasRectangle(rand() % canvas->width(), rand() % canvas->height(), 50, 50, canvas);
266 int z = rand() % 256;
267 item->setBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
268 item->setPen(QPen(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8), 4));
269 item->setZ(z);
270 item->show();
271 return item;
272}
273
275MainWindow::addEllipse()
276{
277 QtCanvasPolygonalItem* item = new QtCanvasEllipse(50, 50, canvas);
278 item->setBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
279 item->move(rand() % canvas->width(), rand() % canvas->height());
280 item->setZ(rand() % 256);
281 item->show();
282 return item;
283}
284
286MainWindow::addLine()
287{
288 QtCanvasLine* item = new QtCanvasLine(canvas);
289 item->setPoints(0,
290 0,
291 rand() % canvas->width() - canvas->width() / 2,
292 rand() % canvas->height() - canvas->height() / 2);
293 item->move(rand() % canvas->width(), rand() % canvas->height());
294 item->setPen(QPen(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8), 6));
295 item->setZ(rand() % 256);
296 item->show();
297 return item;
298}
299
301MainWindow::addText()
302{
303 QtCanvasText* item = new QtCanvasText(canvas);
304 item->setText(tr("Text"));
305 item->setColor(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
306 item->move(rand() % canvas->width(), rand() % canvas->height());
307 item->setZ(rand() % 256);
308 item->show();
309 return item;
310}
311
312void
313MainWindow::itemMoved(QtCanvasItem* item)
314{
315 if (item != currentItem)
316 {
317 return;
318 }
319
320 doubleManager->setValue(idToProperty[QLatin1String("xpos")], item->x());
321 doubleManager->setValue(idToProperty[QLatin1String("ypos")], item->y());
322 doubleManager->setValue(idToProperty[QLatin1String("zpos")], item->z());
323}
324
325void
326MainWindow::updateExpandState()
327{
328 QList<QtBrowserItem*> list = propertyEditor->topLevelItems();
329 QListIterator<QtBrowserItem*> it(list);
330
331 while (it.hasNext())
332 {
333 QtBrowserItem* item = it.next();
334 QtProperty* prop = item->property();
335 idToExpanded[propertyToId[prop]] = propertyEditor->isExpanded(item);
336 }
337}
338
339void
340MainWindow::itemClicked(QtCanvasItem* item)
341{
342 updateExpandState();
343
344 QMap<QtProperty*, QString>::ConstIterator itProp = propertyToId.constBegin();
345
346 while (itProp != propertyToId.constEnd())
347 {
348 delete itProp.key();
349 itProp++;
350 }
351
352 propertyToId.clear();
353 idToProperty.clear();
354
355 currentItem = item;
356
357 if (!currentItem)
358 {
359 deleteAction->setEnabled(false);
360 return;
361 }
362
363 deleteAction->setEnabled(true);
364
365 QtProperty* property;
366
367 property = doubleManager->addProperty(tr("Position X"));
368 doubleManager->setRange(property, 0, canvas->width());
369 doubleManager->setValue(property, item->x());
370 addProperty(property, QLatin1String("xpos"));
371
372 property = doubleManager->addProperty(tr("Position Y"));
373 doubleManager->setRange(property, 0, canvas->height());
374 doubleManager->setValue(property, item->y());
375 addProperty(property, QLatin1String("ypos"));
376
377 property = doubleManager->addProperty(tr("Position Z"));
378 doubleManager->setRange(property, 0, 256);
379 doubleManager->setValue(property, item->z());
380 addProperty(property, QLatin1String("zpos"));
381
382 if (item->rtti() == QtCanvasItem::Rtti_Rectangle)
383 {
384 QtCanvasRectangle* i = (QtCanvasRectangle*)item;
385
386 property = colorManager->addProperty(tr("Brush Color"));
387 colorManager->setValue(property, i->brush().color());
388 addProperty(property, QLatin1String("brush"));
389
390 property = colorManager->addProperty(tr("Pen Color"));
391 colorManager->setValue(property, i->pen().color());
392 addProperty(property, QLatin1String("pen"));
393
394 property = sizeManager->addProperty(tr("Size"));
395 sizeManager->setValue(property, i->size());
396 addProperty(property, QLatin1String("size"));
397 }
398 else if (item->rtti() == QtCanvasItem::Rtti_Line)
399 {
400 QtCanvasLine* i = (QtCanvasLine*)item;
401
402 property = colorManager->addProperty(tr("Pen Color"));
403 colorManager->setValue(property, i->pen().color());
404 addProperty(property, QLatin1String("pen"));
405
406 property = pointManager->addProperty(tr("Vector"));
407 pointManager->setValue(property, i->endPoint());
408 addProperty(property, QLatin1String("endpoint"));
409 }
410 else if (item->rtti() == QtCanvasItem::Rtti_Ellipse)
411 {
412 QtCanvasEllipse* i = (QtCanvasEllipse*)item;
413
414 property = colorManager->addProperty(tr("Brush Color"));
415 colorManager->setValue(property, i->brush().color());
416 addProperty(property, QLatin1String("brush"));
417
418 property = sizeManager->addProperty(tr("Size"));
419 sizeManager->setValue(property, QSize(i->width(), i->height()));
420 sizeManager->setRange(property, QSize(0, 0), QSize(1000, 1000));
421 addProperty(property, QLatin1String("size"));
422 }
423 else if (item->rtti() == QtCanvasItem::Rtti_Text)
424 {
425 QtCanvasText* i = (QtCanvasText*)item;
426
427 property = colorManager->addProperty(tr("Color"));
428 colorManager->setValue(property, i->color());
429 addProperty(property, QLatin1String("color"));
430
431 property = stringManager->addProperty(tr("Text"));
432 stringManager->setValue(property, i->text());
433 addProperty(property, QLatin1String("text"));
434
435 property = fontManager->addProperty(tr("Font"));
436 fontManager->setValue(property, i->font());
437 addProperty(property, QLatin1String("font"));
438 }
439}
440
441void
442MainWindow::addProperty(QtProperty* property, const QString& id)
443{
444 propertyToId[property] = id;
445 idToProperty[id] = property;
446 QtBrowserItem* item = propertyEditor->addProperty(property);
447
448 if (idToExpanded.contains(id))
449 {
450 propertyEditor->setExpanded(item, idToExpanded[id]);
451 }
452}
453
454void
455MainWindow::valueChanged(QtProperty* property, double value)
456{
457 if (!propertyToId.contains(property))
458 {
459 return;
460 }
461
462 if (!currentItem)
463 {
464 return;
465 }
466
467 QString id = propertyToId[property];
468
469 if (id == QLatin1String("xpos"))
470 {
471 currentItem->setX(value);
472 }
473 else if (id == QLatin1String("ypos"))
474 {
475 currentItem->setY(value);
476 }
477 else if (id == QLatin1String("zpos"))
478 {
479 currentItem->setZ(value);
480 }
481
482 canvas->update();
483}
484
485void
486MainWindow::valueChanged(QtProperty* property, const QString& value)
487{
488 if (!propertyToId.contains(property))
489 {
490 return;
491 }
492
493 if (!currentItem)
494 {
495 return;
496 }
497
498 QString id = propertyToId[property];
499
500 if (id == QLatin1String("text"))
501 {
502 if (currentItem->rtti() == QtCanvasItem::Rtti_Text)
503 {
504 QtCanvasText* i = (QtCanvasText*)currentItem;
505 i->setText(value);
506 }
507 }
508
509 canvas->update();
510}
511
512void
513MainWindow::valueChanged(QtProperty* property, const QColor& value)
514{
515 if (!propertyToId.contains(property))
516 {
517 return;
518 }
519
520 if (!currentItem)
521 {
522 return;
523 }
524
525 QString id = propertyToId[property];
526
527 if (id == QLatin1String("color"))
528 {
529 if (currentItem->rtti() == QtCanvasItem::Rtti_Text)
530 {
531 QtCanvasText* i = (QtCanvasText*)currentItem;
532 i->setColor(value);
533 }
534 }
535 else if (id == QLatin1String("brush"))
536 {
537 if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle ||
538 currentItem->rtti() == QtCanvasItem::Rtti_Ellipse)
539 {
540 QtCanvasPolygonalItem* i = (QtCanvasPolygonalItem*)currentItem;
541 QBrush b = i->brush();
542 b.setColor(value);
543 i->setBrush(b);
544 }
545 }
546 else if (id == QLatin1String("pen"))
547 {
548 if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle ||
549 currentItem->rtti() == QtCanvasItem::Rtti_Line)
550 {
551 QtCanvasPolygonalItem* i = (QtCanvasPolygonalItem*)currentItem;
552 QPen p = i->pen();
553 p.setColor(value);
554 i->setPen(p);
555 }
556 }
557
558 canvas->update();
559}
560
561void
562MainWindow::valueChanged(QtProperty* property, const QFont& value)
563{
564 if (!propertyToId.contains(property))
565 {
566 return;
567 }
568
569 if (!currentItem)
570 {
571 return;
572 }
573
574 QString id = propertyToId[property];
575
576 if (id == QLatin1String("font"))
577 {
578 if (currentItem->rtti() == QtCanvasItem::Rtti_Text)
579 {
580 QtCanvasText* i = (QtCanvasText*)currentItem;
581 i->setFont(value);
582 }
583 }
584
585 canvas->update();
586}
587
588void
589MainWindow::valueChanged(QtProperty* property, const QPoint& value)
590{
591 if (!propertyToId.contains(property))
592 {
593 return;
594 }
595
596 if (!currentItem)
597 {
598 return;
599 }
600
601 QString id = propertyToId[property];
602
603 if (currentItem->rtti() == QtCanvasItem::Rtti_Line)
604 {
605 QtCanvasLine* i = (QtCanvasLine*)currentItem;
606
607 if (id == QLatin1String("endpoint"))
608 {
609 i->setPoints(i->startPoint().x(), i->startPoint().y(), value.x(), value.y());
610 }
611 }
612
613 canvas->update();
614}
615
616void
617MainWindow::valueChanged(QtProperty* property, const QSize& value)
618{
619 if (!propertyToId.contains(property))
620 {
621 return;
622 }
623
624 if (!currentItem)
625 {
626 return;
627 }
628
629 QString id = propertyToId[property];
630
631 if (id == QLatin1String("size"))
632 {
633 if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle)
634 {
635 QtCanvasRectangle* i = (QtCanvasRectangle*)currentItem;
636 i->setSize(value.width(), value.height());
637 }
638 else if (currentItem->rtti() == QtCanvasItem::Rtti_Ellipse)
639 {
640 QtCanvasEllipse* i = (QtCanvasEllipse*)currentItem;
641 i->setSize(value.width(), value.height());
642 }
643 }
644
645 canvas->update();
646}
QList< QtCanvasItem * > QtCanvasItemList
Definition qtcanvas.h:62
void contentsMouseMoveEvent(QMouseEvent *event)
void itemClicked(QtCanvasItem *item)
void itemMoved(QtCanvasItem *item)
void contentsMouseDoubleClickEvent(QMouseEvent *event)
void contentsMousePressEvent(QMouseEvent *event)
MainWindow(QWidget *parent=0)
QtProperty * property() const
void setSize(int w, int h)
int height() const
int width() const
virtual void setEnabled(bool yes)
double x() const
Definition qtcanvas.h:74
virtual int rtti() const
void setZ(double a)
Definition qtcanvas.h:107
void update()
Definition qtcanvas.h:227
double y() const
Definition qtcanvas.h:80
double z() const
Definition qtcanvas.h:86
void move(double x, double y)
void setPoints(int x1, int y1, int x2, int y2)
QPoint endPoint() const
Definition qtcanvas.h:865
QPoint startPoint() const
Definition qtcanvas.h:859
void setPen(QPen p)
virtual void setBrush(QBrush b)
QBrush brush() const
Definition qtcanvas.h:725
QPen pen() const
Definition qtcanvas.h:719
virtual void setPen(QPen p)
void setSize(int w, int h)
QSize size() const
Definition qtcanvas.h:781
QColor color() const
void setText(const QString &)
QString text() const
QFont font() const
void setFont(const QFont &)
void setColor(const QColor &)
QtCanvas * canvas() const
Definition qtcanvas.h:474
const QMatrix & inverseWorldMatrix() const
QtCanvasItemList collisions(const QPoint &) const
virtual void update()
The QtCheckBoxFactory class provides QCheckBox widgets for properties created by QtBoolPropertyManage...
The QtColorPropertyManager provides and manages QColor properties.
The QtDoublePropertyManager provides and manages double properties.
The QtDoubleSpinBoxFactory class provides QDoubleSpinBox widgets for properties created by QtDoublePr...
The QtEnumEditorFactory class provides QComboBox widgets for properties created by QtEnumPropertyMana...
The QtFontPropertyManager provides and manages QFont properties.
The QtLineEditFactory class provides QLineEdit widgets for properties created by QtStringPropertyMana...
The QtPointPropertyManager provides and manages QPoint properties.
The QtProperty class encapsulates an instance of a property.
The QtSizePropertyManager provides and manages QSize properties.
The QtSpinBoxFactory class provides QSpinBox widgets for properties created by QtIntPropertyManager o...
The QtStringPropertyManager provides and manages QString properties.
The QtTreePropertyBrowser class provides QTreeWidget based property browser.
std::shared_ptr< Value > value()
Definition cxxopts.hpp:855