Debian Codegraph @

tests/manual/widgets/qgraphicsview/rubberband/rubberbandtest.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <QtWidgets>
30
31 class MyGraphicsItem : public QGraphicsRectItem
32 {
33 public:
34 MyGraphicsItem() : QGraphicsRectItem()
35 {
36 setFlags(QGraphicsItem::ItemIsSelectable);
37 }
38
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem * /* option*/, QWidget * /*widget*/) override
40 {
41 if (isSelected())
42 painter->fillRect(rect(), QColor(255, 0, 0));
43 else
44 painter->fillRect(rect(), QColor(0, 255, 0));
45 }
46 };
47
48 class MyGraphicsView : public QGraphicsView
49 {
50 Q_OBJECT
51 public:
52 MyGraphicsView(QWidget *w, QLabel *l) : QGraphicsView(w), rubberbandLabel(l)
53 {
54 setDragMode(QGraphicsView::RubberBandDrag);
55 connect(this, SIGNAL(rubberBandChanged(QRect, QPointF, QPointF)), this, SLOT(updateRubberbandInfo(QRect, QPointF, QPointF)));
56 }
57 protected:
58 void mouseMoveEvent(QMouseEvent *event) override
59 {
60 QGraphicsView::mouseMoveEvent(event);
61
62 int rightmostInView = viewport()->mapToGlobal(viewport()->geometry().topRight()).x();
63 int xglobal = event->globalX();
64 if (xglobal > rightmostInView)
65 horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 10);
66
67 int bottomPos = viewport()->mapToGlobal(viewport()->geometry().bottomRight()).y();
68 int yglobal = event->globalY();
69 if (yglobal > bottomPos)
70 verticalScrollBar()->setValue(verticalScrollBar()->value() + 10);
71 }
72
73 protected slots:
74 void updateRubberbandInfo(QRect r, QPointF from, QPointF to)
75 {
76 QString textToShow;
77 QDebug s(&textToShow);
78 s << r << from << to;
79 rubberbandLabel->setText(textToShow);
80 }
81 protected:
82 QLabel *rubberbandLabel;
83 };
84
85 int main(int argc, char *argv[])
86 {
87 QApplication app(argc, argv);
88
89 QWidget w;
90 w.setLayout(new QVBoxLayout);
91 QLabel *l = new QLabel(&w);
92 MyGraphicsView *v = new MyGraphicsView(&w, l);
93
94 w.layout()->addWidget(v);
95 w.layout()->addWidget(l);
96
97 QGraphicsScene s(0.0, 0.0, 5000.0, 5000.0);
98 v->setScene(&s);
99 v->setInteractive(true);
100 v->setRubberBandSelectionMode(Qt::IntersectsItemBoundingRect);
101
102 for (int u = 0; u < 100; ++u)
103 for (int n = 0; n < 100; ++n) {
104 MyGraphicsItem *item = new MyGraphicsItem();
105 item->setRect(QRectF(n * 80.0, u * 80.0, 50.0, 20.0));
106 s.addItem(item);
107 }
108
109 w.show();
110 app.exec();
111 return 0;
112 }
113
114 #include "rubberbandtest.moc"