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
51#include "qtvariantproperty.h"
52
53void
55{
56 handleMouseClickEvent(event);
57}
58
59void
61{
62 handleMouseClickEvent(event);
63}
64
65void
66CanvasView::handleMouseClickEvent(QMouseEvent* event)
67{
68 QPoint p = inverseWorldMatrix().map(event->pos());
70 moving = 0;
71
72 if (!l.isEmpty())
73 {
74 moving = l.first();
75 }
76
77 moving_start = p;
78 emit itemClicked(moving);
79}
80
81void
82CanvasView::contentsMouseMoveEvent(QMouseEvent* event)
83{
84 if (moving)
85 {
86 QPoint p = inverseWorldMatrix().map(event->pos());
87 moving->moveBy(p.x() - moving_start.x(), p.y() - moving_start.y());
88 moving_start = p;
89 canvas()->update();
90 emit itemMoved(moving);
91 }
92}
93
94MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
95{
96 QMenu* editMenu = menuBar()->addMenu(tr("Edit"));
97 QMenu* newObjectMenu = editMenu->addMenu(tr("New Object"));
98
99 QAction* newRectangleAction = new QAction(tr("Rectangle"), this);
100 connect(newRectangleAction, SIGNAL(triggered(bool)), this, SLOT(newRectangle()));
101 newObjectMenu->addAction(newRectangleAction);
102
103 QAction* newLineAction = new QAction(tr("Line"), this);
104 connect(newLineAction, SIGNAL(triggered(bool)), this, SLOT(newLine()));
105 newObjectMenu->addAction(newLineAction);
106
107 QAction* newEllipseAction = new QAction(tr("Ellipse"), this);
108 connect(newEllipseAction, SIGNAL(triggered(bool)), this, SLOT(newEllipse()));
109 newObjectMenu->addAction(newEllipseAction);
110
111 QAction* newTextAction = new QAction(tr("Text"), this);
112 connect(newTextAction, SIGNAL(triggered(bool)), this, SLOT(newText()));
113 newObjectMenu->addAction(newTextAction);
114
115 deleteAction = new QAction(tr("Delete Object"), this);
116 connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(deleteObject()));
117 editMenu->addAction(deleteAction);
118
119 QAction* clearAction = new QAction(tr("Clear All"), this);
120 connect(clearAction, SIGNAL(triggered(bool)), this, SLOT(clearAll()));
121 editMenu->addAction(clearAction);
122
123 QAction* fillAction = new QAction(tr("Fill View"), this);
124 connect(fillAction, SIGNAL(triggered(bool)), this, SLOT(fillView()));
125 editMenu->addAction(fillAction);
126
127 variantManager = new QtVariantPropertyManager(this);
128
129 connect(variantManager,
130 SIGNAL(valueChanged(QtProperty*, const QVariant&)),
131 this,
132 SLOT(valueChanged(QtProperty*, const QVariant&)));
133
134 QtVariantEditorFactory* variantFactory = new QtVariantEditorFactory(this);
135
136 canvas = new QtCanvas(800, 600);
137 canvasView = new CanvasView(canvas, this);
138 setCentralWidget(canvasView);
139
140 QDockWidget* dock = new QDockWidget(this);
141 addDockWidget(Qt::RightDockWidgetArea, dock);
142
143 propertyEditor = new QtTreePropertyBrowser(dock);
144 propertyEditor->setFactoryForManager(variantManager, variantFactory);
145 dock->setWidget(propertyEditor);
146
147 currentItem = 0;
148
149 connect(canvasView, SIGNAL(itemClicked(QtCanvasItem*)), this, SLOT(itemClicked(QtCanvasItem*)));
150 connect(canvasView, SIGNAL(itemMoved(QtCanvasItem*)), this, SLOT(itemMoved(QtCanvasItem*)));
151
152 fillView();
153 itemClicked(0);
154}
155
156void
157MainWindow::newRectangle()
158{
159 QtCanvasItem* item = addRectangle();
160 canvas->update();
161 itemClicked(item);
162}
163
164void
165MainWindow::newEllipse()
166{
167 QtCanvasItem* item = addEllipse();
168 canvas->update();
169 itemClicked(item);
170}
171
172void
173MainWindow::newLine()
174{
175 QtCanvasItem* item = addLine();
176 canvas->update();
177 itemClicked(item);
178}
179
180void
181MainWindow::newText()
182{
183 QtCanvasItem* item = addText();
184 canvas->update();
185 itemClicked(item);
186}
187
188void
189MainWindow::deleteObject()
190{
191 if (!currentItem)
192 {
193 return;
194 }
195
196 delete currentItem;
197 itemClicked(0);
198 canvas->update();
199}
200
201void
202MainWindow::clearAll()
203{
204 QtCanvasItemList list = canvas->allItems();
205 qDeleteAll(list);
206 itemClicked(0);
207 canvas->update();
208}
209
210void
211MainWindow::fillView()
212{
213 for (int i = 0; i < 10; i++)
214 {
215 addRectangle();
216 addEllipse();
217 addLine();
218 addText();
219 }
220
221 canvas->update();
222}
223
225MainWindow::addRectangle()
226{
227 QtCanvasPolygonalItem* item =
228 new QtCanvasRectangle(rand() % canvas->width(), rand() % canvas->height(), 50, 50, canvas);
229 int z = rand() % 256;
230 item->setBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
231 item->setPen(QPen(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8), 4));
232 item->setZ(z);
233 item->show();
234 return item;
235}
236
238MainWindow::addEllipse()
239{
240 QtCanvasPolygonalItem* item = new QtCanvasEllipse(50, 50, canvas);
241 item->setBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
242 item->move(rand() % canvas->width(), rand() % canvas->height());
243 item->setZ(rand() % 256);
244 item->show();
245 return item;
246}
247
249MainWindow::addLine()
250{
251 QtCanvasLine* item = new QtCanvasLine(canvas);
252 item->setPoints(0,
253 0,
254 rand() % canvas->width() - canvas->width() / 2,
255 rand() % canvas->height() - canvas->height() / 2);
256 item->move(rand() % canvas->width(), rand() % canvas->height());
257 item->setPen(QPen(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8), 6));
258 item->setZ(rand() % 256);
259 item->show();
260 return item;
261}
262
264MainWindow::addText()
265{
266 QtCanvasText* item = new QtCanvasText(canvas);
267 item->setText(tr("Text"));
268 item->setColor(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8));
269 item->move(rand() % canvas->width(), rand() % canvas->height());
270 item->setZ(rand() % 256);
271 item->show();
272 return item;
273}
274
275void
276MainWindow::itemMoved(QtCanvasItem* item)
277{
278 if (item != currentItem)
279 {
280 return;
281 }
282
283 variantManager->setValue(idToProperty[QLatin1String("xpos")], item->x());
284 variantManager->setValue(idToProperty[QLatin1String("ypos")], item->y());
285 variantManager->setValue(idToProperty[QLatin1String("zpos")], item->z());
286}
287
288void
289MainWindow::updateExpandState()
290{
291 QList<QtBrowserItem*> list = propertyEditor->topLevelItems();
292 QListIterator<QtBrowserItem*> it(list);
293
294 while (it.hasNext())
295 {
296 QtBrowserItem* item = it.next();
297 QtProperty* prop = item->property();
298 idToExpanded[propertyToId[prop]] = propertyEditor->isExpanded(item);
299 }
300}
301
302void
303MainWindow::itemClicked(QtCanvasItem* item)
304{
305 updateExpandState();
306
307 QMap<QtProperty*, QString>::ConstIterator itProp = propertyToId.constBegin();
308
309 while (itProp != propertyToId.constEnd())
310 {
311 delete itProp.key();
312 itProp++;
313 }
314
315 propertyToId.clear();
316 idToProperty.clear();
317
318 currentItem = item;
319
320 if (!currentItem)
321 {
322 deleteAction->setEnabled(false);
323 return;
324 }
325
326 deleteAction->setEnabled(true);
327
328 QtVariantProperty* property;
329
330 property = variantManager->addProperty(QVariant::Double, tr("Position X"));
331 property->setAttribute(QLatin1String("minimum"), 0);
332 property->setAttribute(QLatin1String("maximum"), canvas->width());
333 property->setValue(item->x());
334 addProperty(property, QLatin1String("xpos"));
335
336 property = variantManager->addProperty(QVariant::Double, tr("Position Y"));
337 property->setAttribute(QLatin1String("minimum"), 0);
338 property->setAttribute(QLatin1String("maximum"), canvas->height());
339 property->setValue(item->y());
340 addProperty(property, QLatin1String("ypos"));
341
342 property = variantManager->addProperty(QVariant::Double, tr("Position Z"));
343 property->setAttribute(QLatin1String("minimum"), 0);
344 property->setAttribute(QLatin1String("maximum"), 256);
345 property->setValue(item->z());
346 addProperty(property, QLatin1String("zpos"));
347
348 if (item->rtti() == QtCanvasItem::Rtti_Rectangle)
349 {
350 QtCanvasRectangle* i = (QtCanvasRectangle*)item;
351
352 property = variantManager->addProperty(QVariant::Color, tr("Brush Color"));
353 property->setValue(i->brush().color());
354 addProperty(property, QLatin1String("brush"));
355
356 property = variantManager->addProperty(QVariant::Color, tr("Pen Color"));
357 property->setValue(i->pen().color());
358 addProperty(property, QLatin1String("pen"));
359
360 property = variantManager->addProperty(QVariant::Size, tr("Size"));
361 property->setValue(i->size());
362 addProperty(property, QLatin1String("size"));
363 }
364 else if (item->rtti() == QtCanvasItem::Rtti_Line)
365 {
366 QtCanvasLine* i = (QtCanvasLine*)item;
367
368 property = variantManager->addProperty(QVariant::Color, tr("Pen Color"));
369 property->setValue(i->pen().color());
370 addProperty(property, QLatin1String("pen"));
371
372 property = variantManager->addProperty(QVariant::Point, tr("Vector"));
373 property->setValue(i->endPoint());
374 addProperty(property, QLatin1String("endpoint"));
375 }
376 else if (item->rtti() == QtCanvasItem::Rtti_Ellipse)
377 {
378 QtCanvasEllipse* i = (QtCanvasEllipse*)item;
379
380 property = variantManager->addProperty(QVariant::Color, tr("Brush Color"));
381 property->setValue(i->brush().color());
382 addProperty(property, QLatin1String("brush"));
383
384 property = variantManager->addProperty(QVariant::Size, tr("Size"));
385 property->setValue(QSize(i->width(), i->height()));
386 addProperty(property, QLatin1String("size"));
387 }
388 else if (item->rtti() == QtCanvasItem::Rtti_Text)
389 {
390 QtCanvasText* i = (QtCanvasText*)item;
391
392 property = variantManager->addProperty(QVariant::Color, tr("Color"));
393 property->setValue(i->color());
394 addProperty(property, QLatin1String("color"));
395
396 property = variantManager->addProperty(QVariant::String, tr("Text"));
397 property->setValue(i->text());
398 addProperty(property, QLatin1String("text"));
399
400 property = variantManager->addProperty(QVariant::Font, tr("Font"));
401 property->setValue(i->font());
402 addProperty(property, QLatin1String("font"));
403 }
404}
405
406void
407MainWindow::addProperty(QtVariantProperty* property, const QString& id)
408{
409 propertyToId[property] = id;
410 idToProperty[id] = property;
411 QtBrowserItem* item = propertyEditor->addProperty(property);
412
413 if (idToExpanded.contains(id))
414 {
415 propertyEditor->setExpanded(item, idToExpanded[id]);
416 }
417}
418
419void
420MainWindow::valueChanged(QtProperty* property, const QVariant& value)
421{
422 if (!propertyToId.contains(property))
423 {
424 return;
425 }
426
427 if (!currentItem)
428 {
429 return;
430 }
431
432 QString id = propertyToId[property];
433
434 if (id == QLatin1String("xpos"))
435 {
436 currentItem->setX(value.toDouble());
437 }
438 else if (id == QLatin1String("ypos"))
439 {
440 currentItem->setY(value.toDouble());
441 }
442 else if (id == QLatin1String("zpos"))
443 {
444 currentItem->setZ(value.toDouble());
445 }
446 else if (id == QLatin1String("text"))
447 {
448 if (currentItem->rtti() == QtCanvasItem::Rtti_Text)
449 {
450 QtCanvasText* i = (QtCanvasText*)currentItem;
451 i->setText(value.value<QString>());
452 }
453 }
454 else if (id == QLatin1String("color"))
455 {
456 if (currentItem->rtti() == QtCanvasItem::Rtti_Text)
457 {
458 QtCanvasText* i = (QtCanvasText*)currentItem;
459 i->setColor(value.value<QColor>());
460 }
461 }
462 else if (id == QLatin1String("brush"))
463 {
464 if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle ||
465 currentItem->rtti() == QtCanvasItem::Rtti_Ellipse)
466 {
467 QtCanvasPolygonalItem* i = (QtCanvasPolygonalItem*)currentItem;
468 QBrush b = i->brush();
469 b.setColor(value.value<QColor>());
470 i->setBrush(b);
471 }
472 }
473 else if (id == QLatin1String("pen"))
474 {
475 if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle ||
476 currentItem->rtti() == QtCanvasItem::Rtti_Line)
477 {
478 QtCanvasPolygonalItem* i = (QtCanvasPolygonalItem*)currentItem;
479 QPen p = i->pen();
480 p.setColor(value.value<QColor>());
481 i->setPen(p);
482 }
483 }
484 else if (id == QLatin1String("font"))
485 {
486 if (currentItem->rtti() == QtCanvasItem::Rtti_Text)
487 {
488 QtCanvasText* i = (QtCanvasText*)currentItem;
489 i->setFont(value.value<QFont>());
490 }
491 }
492 else if (id == QLatin1String("endpoint"))
493 {
494 if (currentItem->rtti() == QtCanvasItem::Rtti_Line)
495 {
496 QtCanvasLine* i = (QtCanvasLine*)currentItem;
497 QPoint p = value.value<QPoint>();
498 i->setPoints(i->startPoint().x(), i->startPoint().y(), p.x(), p.y());
499 }
500 }
501 else if (id == QLatin1String("size"))
502 {
503 if (currentItem->rtti() == QtCanvasItem::Rtti_Rectangle)
504 {
505 QtCanvasRectangle* i = (QtCanvasRectangle*)currentItem;
506 QSize s = value.value<QSize>();
507 i->setSize(s.width(), s.height());
508 }
509 else if (currentItem->rtti() == QtCanvasItem::Rtti_Ellipse)
510 {
511 QtCanvasEllipse* i = (QtCanvasEllipse*)currentItem;
512 QSize s = value.value<QSize>();
513 i->setSize(s.width(), s.height());
514 }
515 }
516
517 canvas->update();
518}
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 QtProperty class encapsulates an instance of a property.
The QtTreePropertyBrowser class provides QTreeWidget based property browser.
The QtVariantEditorFactory class provides widgets for properties created by QtVariantPropertyManager ...
The QtVariantPropertyManager class provides and manages QVariant based properties.
The QtVariantProperty class is a convenience class handling QVariant based properties.
double s(double t, double s0, double v0, double a0, double j)
Definition CtrlUtil.h:33
std::shared_ptr< Value > value()
Definition cxxopts.hpp:855