summaryrefslogtreecommitdiff
path: root/examples/graphicsview/anchorlayout
diff options
context:
space:
mode:
authorJesus Sanchez-Palencia <jesus.palencia@openbossa.org>2009-05-28 18:22:27 -0300
committerEduardo M. Fleury <eduardo.fleury@openbossa.org>2009-07-22 15:04:02 -0300
commitd61d477d2487bdc5c1fc854106a82450b613673f (patch)
tree9349011bced5e2e02d80c710f78d32ef04c83299 /examples/graphicsview/anchorlayout
parent2f002ef720412dc834daac46843d1439dc49680f (diff)
downloadqt4-tools-d61d477d2487bdc5c1fc854106a82450b613673f.tar.gz
QGraphicsAnchorLayout: Adding an usage example
Signed-off-by: Jesus Sanchez-Palencia <jesus.palencia@openbossa.org>
Diffstat (limited to 'examples/graphicsview/anchorlayout')
-rw-r--r--examples/graphicsview/anchorlayout/anchorlayout.pro14
-rw-r--r--examples/graphicsview/anchorlayout/main.cpp144
-rw-r--r--examples/graphicsview/anchorlayout/noGUI.cpp83
3 files changed, 241 insertions, 0 deletions
diff --git a/examples/graphicsview/anchorlayout/anchorlayout.pro b/examples/graphicsview/anchorlayout/anchorlayout.pro
new file mode 100644
index 0000000000..c969c8b68f
--- /dev/null
+++ b/examples/graphicsview/anchorlayout/anchorlayout.pro
@@ -0,0 +1,14 @@
+######################################################################
+# Automatically generated by qmake (2.01a) Tue May 12 15:22:25 2009
+######################################################################
+
+# Input
+SOURCES += main.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/anchorlayout
+sources.files = $$SOURCES $$HEADERS $$RESOURCES anchorlayout.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/graphicsview/anchorlayout
+INSTALLS += target sources
+
+TARGET = anchorlayout_example
diff --git a/examples/graphicsview/anchorlayout/main.cpp b/examples/graphicsview/anchorlayout/main.cpp
new file mode 100644
index 0000000000..d7563beb67
--- /dev/null
+++ b/examples/graphicsview/anchorlayout/main.cpp
@@ -0,0 +1,144 @@
+#include <QGraphicsWidget>
+#include <QGraphicsProxyWidget>
+#include <QGraphicsAnchorLayout>
+#include <QtGui>
+
+//#define BENCHMARK_RUN 1
+#define CALLGRIND_DEBUG 1
+
+#if defined(BENCHMARK_RUN)
+
+// Callgrind command line:
+// valgrind --tool=callgrind --instr-atstart=no ./anchorlayout >/dev/null 2>&1
+
+#if defined(CALLGRIND_DEBUG)
+#include <valgrind/callgrind.h>
+#else
+#define CALLGRIND_START_INSTRUMENTATION do { } while (0)
+#define CALLGRIND_STOP_INSTRUMENTATION do { } while (0)
+#endif
+
+#endif
+
+
+static QGraphicsProxyWidget *createItem(const QSizeF &minimum = QSizeF(100.0, 100.0),
+ const QSizeF &preferred = QSize(150.0, 100.0),
+ const QSizeF &maximum = QSizeF(200.0, 100.0),
+ const QString &name = "0")
+{
+ QGraphicsProxyWidget *w = new QGraphicsProxyWidget;
+ w->setWidget(new QPushButton(name));
+ w->setData(0, name);
+ w->setMinimumSize(minimum);
+ w->setPreferredSize(preferred);
+ w->setMaximumSize(maximum);
+
+ return w;
+}
+
+class Test : public QObject
+{
+ Q_OBJECT;
+public:
+ Test(QGraphicsWidget *wi, QGraphicsLayout* la)
+ : QObject(), w(wi), l(la), first(true) {}
+
+ QGraphicsWidget *w;
+ QGraphicsLayout *l;
+ bool first;
+
+public slots:
+ void onTimer() {
+ if (first) {
+ first = false;
+ w->setContentsMargins(10, 10, 10, 10);
+ } else {
+ l->setContentsMargins(10, 10, 10, 10);
+ }
+ }
+};
+
+int main(int argc, char **argv)
+{
+ QApplication app(argc, argv);
+
+ QGraphicsScene *scene = new QGraphicsScene();
+ scene->setSceneRect(0, 0, 800, 480);
+
+ QSizeF min(30, 100);
+ QSizeF pref(210, 100);
+ QSizeF max(300, 100);
+
+ QGraphicsProxyWidget *a = createItem(min, pref, max, "A");
+ QGraphicsProxyWidget *b = createItem(min, pref, max, "B");
+ QGraphicsProxyWidget *c = createItem(min, pref, max, "C");
+ QGraphicsProxyWidget *d = createItem(min, pref, max, "D");
+ QGraphicsProxyWidget *e = createItem(min, pref, max, "E");
+ QGraphicsProxyWidget *f = createItem(QSizeF(30, 50), QSizeF(150, 50), max, "F");
+ QGraphicsProxyWidget *g = createItem(QSizeF(30, 50), QSizeF(30, 100), max, "G");
+
+ QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
+
+ QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window);
+ w->setPos(20, 20);
+ w->setLayout(l);
+
+ // vertical
+ l->anchor(a, QGraphicsAnchorLayout::Top, l, QGraphicsAnchorLayout::Top);
+ l->anchor(b, QGraphicsAnchorLayout::Top, l, QGraphicsAnchorLayout::Top);
+
+ l->anchor(c, QGraphicsAnchorLayout::Top, a, QGraphicsAnchorLayout::Bottom);
+ l->anchor(c, QGraphicsAnchorLayout::Top, b, QGraphicsAnchorLayout::Bottom);
+ l->anchor(c, QGraphicsAnchorLayout::Bottom, d, QGraphicsAnchorLayout::Top);
+ l->anchor(c, QGraphicsAnchorLayout::Bottom, e, QGraphicsAnchorLayout::Top);
+
+ l->anchor(d, QGraphicsAnchorLayout::Bottom, l, QGraphicsAnchorLayout::Bottom);
+ l->anchor(e, QGraphicsAnchorLayout::Bottom, l, QGraphicsAnchorLayout::Bottom);
+
+ l->anchor(c, QGraphicsAnchorLayout::Top, f, QGraphicsAnchorLayout::Top);
+ l->anchor(c, QGraphicsAnchorLayout::VCenter, f, QGraphicsAnchorLayout::Bottom);
+ l->anchor(f, QGraphicsAnchorLayout::Bottom, g, QGraphicsAnchorLayout::Top);
+ l->anchor(c, QGraphicsAnchorLayout::Bottom, g, QGraphicsAnchorLayout::Bottom);
+
+ // horizontal
+ l->anchor(l, QGraphicsAnchorLayout::Left, a, QGraphicsAnchorLayout::Left);
+ l->anchor(l, QGraphicsAnchorLayout::Left, d, QGraphicsAnchorLayout::Left);
+ l->anchor(a, QGraphicsAnchorLayout::Right, b, QGraphicsAnchorLayout::Left);
+
+ l->anchor(a, QGraphicsAnchorLayout::Right, c, QGraphicsAnchorLayout::Left);
+ l->anchor(c, QGraphicsAnchorLayout::Right, e, QGraphicsAnchorLayout::Left);
+
+ l->anchor(b, QGraphicsAnchorLayout::Right, l, QGraphicsAnchorLayout::Right);
+ l->anchor(e, QGraphicsAnchorLayout::Right, l, QGraphicsAnchorLayout::Right);
+ l->anchor(d, QGraphicsAnchorLayout::Right, e, QGraphicsAnchorLayout::Left);
+
+ l->anchor(l, QGraphicsAnchorLayout::Left, f, QGraphicsAnchorLayout::Left);
+ l->anchor(l, QGraphicsAnchorLayout::Left, g, QGraphicsAnchorLayout::Left);
+ l->anchor(f, QGraphicsAnchorLayout::Right, g, QGraphicsAnchorLayout::Right);
+
+#ifdef BENCHMARK_RUN
+ CALLGRIND_START_INSTRUMENTATION;
+ for (int i = 0; i < 100; i++) {
+ l->invalidate();
+ l->d_ptr->calculateGraphs();
+ }
+ CALLGRIND_STOP_INSTRUMENTATION;
+ return 0;
+#endif
+
+ l->dumpGraph();
+
+ scene->addItem(w);
+ scene->setBackgroundBrush(Qt::darkGreen);
+ QGraphicsView *view = new QGraphicsView(scene);
+ view->show();
+
+ Test test(w, l);
+ QTimer timer;
+ test.connect(&timer, SIGNAL(timeout()), SLOT(onTimer()));
+ timer.start(5000);
+
+ return app.exec();
+}
+
+#include "main.moc"
diff --git a/examples/graphicsview/anchorlayout/noGUI.cpp b/examples/graphicsview/anchorlayout/noGUI.cpp
new file mode 100644
index 0000000000..3f04f9a295
--- /dev/null
+++ b/examples/graphicsview/anchorlayout/noGUI.cpp
@@ -0,0 +1,83 @@
+#include <QGraphicsWidget>
+#include "qgraphicsanchorlayout.h"
+
+// DEBUG
+#include "qgraphicsanchorlayout_p.h"
+
+static QGraphicsWidget *createItem(const QSizeF &minimum = QSizeF(100.0, 100.0),
+ const QSizeF &preferred = QSize(150.0, 100.0),
+ const QSizeF &maximum = QSizeF(200.0, 100.0))
+{
+ QGraphicsWidget *w = new QGraphicsWidget;
+ w->setMinimumSize(minimum);
+ w->setPreferredSize(preferred);
+ w->setMaximumSize(maximum);
+ return w;
+}
+
+
+int main(int argc, char **argv)
+{
+ Q_UNUSED(argc);
+ Q_UNUSED(argv);
+
+
+ QGraphicsWidget *a = createItem(QSizeF(50.0, 100.0),
+ QSizeF(70.0, 100.0),
+ QSizeF(100.0, 200.0));
+ a->setData(0, "A");
+
+ QGraphicsWidget *b = createItem(QSizeF(10.0, 100.0),
+ QSizeF(20.0, 100.0),
+ QSizeF(40.0, 200.0));
+ b->setData(0, "B");
+
+ QGraphicsWidget *c = createItem(QSizeF(50.0, 100.0),
+ QSizeF(70.0, 100.0),
+ QSizeF(100.0, 200.0));
+ c->setData(0, "C");
+
+ QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
+
+ l->anchor(l, QGraphicsAnchorLayout::Top, a, QGraphicsAnchorLayout::Top);
+ l->anchor(a, QGraphicsAnchorLayout::Bottom, b, QGraphicsAnchorLayout::Top);
+ l->anchor(b, QGraphicsAnchorLayout::Bottom, c, QGraphicsAnchorLayout::Top);
+ l->anchor(c, QGraphicsAnchorLayout::Bottom, l, QGraphicsAnchorLayout::Bottom);
+
+ l->anchor(l, QGraphicsAnchorLayout::Left, a, QGraphicsAnchorLayout::Left);
+ l->anchor(a, QGraphicsAnchorLayout::Right, b, QGraphicsAnchorLayout::Right);
+ l->anchor(b, QGraphicsAnchorLayout::Left, c, QGraphicsAnchorLayout::Left);
+ l->anchor(c, QGraphicsAnchorLayout::Right, l, QGraphicsAnchorLayout::Right);
+
+ l->dumpGraph();
+
+ QSizeF layoutMinimumSize = l->effectiveSizeHint(Qt::MinimumSize);
+ QSizeF layoutMaximumSize = l->effectiveSizeHint(Qt::MaximumSize);
+ QSizeF layoutPreferredSize = l->effectiveSizeHint(Qt::PreferredSize);
+
+
+ qWarning() << "Layout Min/Pref/Max: " << layoutMinimumSize
+ << " " << layoutPreferredSize
+ << " " << layoutMaximumSize;
+
+
+ qWarning() << "\nSetting minimum size";
+ l->setGeometry(QRectF(QPointF(0,0), layoutMinimumSize));
+ qWarning() << "A: " << a->geometry();
+ qWarning() << "B: " << b->geometry();
+ qWarning() << "C: " << c->geometry();
+
+ qWarning() << "\nSetting maximum size";
+ l->setGeometry(QRectF(QPointF(0,0), layoutMaximumSize));
+ qWarning() << "A: " << a->geometry();
+ qWarning() << "B: " << b->geometry();
+ qWarning() << "C: " << c->geometry();
+
+ qWarning() << "\nSetting size 85.0";
+ l->setGeometry(QRectF(QPointF(0,0), QSizeF(85.0, 200.0)));
+ qWarning() << "A: " << a->geometry();
+ qWarning() << "B: " << b->geometry();
+ qWarning() << "C: " << c->geometry();
+
+ return 0;
+}