1/* Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
2 http://woboq.com/blog/property-bindings-in-cpp.html
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
7and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
8subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or substantial
11portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
16OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18*/
19
20#include "property.h"
21#include "qobject_wrappers.h"
22
23#include <QtWidgets/QtWidgets>
24
25typedef QtWrapper::Widget<QWidget> Widget;
26typedef QtWrapper::AbstractSlider<QSlider> Slider;
27typedef QtWrapper::Widget<QGraphicsView> GraphicsView;
28typedef QtWrapper::Label<QLabel> Label;
29typedef QtWrapper::LineEdit<QLineEdit> LineEdit;
30
31struct GraphicsRectObject : QGraphicsWidget {
32 // bind the QObject properties.
33 property_qobject<QRectF> geometry { this, "geometry" };
34 property_qobject<qreal> opacity { this, "opacity" };
35 property_qobject<qreal> rotation { this, "rotation" };
36
37 // add a color property, with a hook to update when it changes
38 property_hook<QColor> color { [this]{ this->update(); } };
39private:
40 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget*) override {
41 painter->setBrush(color());
42 painter->drawRect(boundingRect());
43 }
44};
45
46
47struct MyWindow : Widget {
48 LineEdit colorEdit {this};
49
50 Slider rotationSlider {Qt::Horizontal, this};
51 Slider opacitySlider {Qt::Horizontal, this};
52
53 QGraphicsScene scene;
54 GraphicsView view {&scene, this};
55 GraphicsRectObject rectangle;
56
57 ::property<int> margin {10};
58
59 MyWindow() {
60 // Layout the items. Not really as good as real layouts, but it demonstrates bindings
61 colorEdit.geometry = [&]{ return QRect(margin, margin,
62 geometry().width() - 2*margin,
63 colorEdit.sizeHint().height()); };
64 rotationSlider.geometry = [&]{ return QRect(margin,
65 colorEdit.geometry().bottom() + margin,
66 geometry().width() - 2*margin,
67 rotationSlider.sizeHint().height()); };
68 opacitySlider.geometry = [&]{ return QRect(margin,
69 rotationSlider.geometry().bottom() + margin,
70 geometry().width() - 2*margin,
71 opacitySlider.sizeHint().height()); };
72 view.geometry = [&]{
73 int x = opacitySlider.geometry().bottom() + margin;
74 return QRect(margin, x, width() - 2*margin, geometry().height() - x - margin);
75 };
76
77 // Some proper default value
78 colorEdit.text = QString("blue");
79 rotationSlider.minimum = -180;
80 rotationSlider.maximum = 180;
81 opacitySlider.minimum = 0;
82 opacitySlider.maximum = 100;
83 opacitySlider.value = 100;
84
85 scene.addItem(&rectangle);
86
87 // now the 'cool' bindings
88 rectangle.color = [&]{ return QColor(colorEdit.text); };
89 rectangle.opacity = [&]{ return qreal(opacitySlider.value/100.); };
90 rectangle.rotation = [&]{ return rotationSlider.value(); };
91 }
92};
93
94int main(int argc, char **argv)
95{
96 QApplication app(argc,argv);
97 MyWindow window;
98 window.show();
99 return app.exec();
100}
101
102// c++ -std=c++11 -I../../src -lQt5Core -lQt5Gui -lQt5Widgets -fPIC -I/usr/include/qt ./graphicsview.cc
103