Debian Codegraph @

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

raw
1 // Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtWidgets>
5
6 class MyGraphicsItem : public QGraphicsRectItem
7 {
8 public:
9 MyGraphicsItem() : QGraphicsRectItem()
10 {
11 setFlags(QGraphicsItem::ItemIsSelectable);
12 }
13
14 void paint(QPainter *painter, const QStyleOptionGraphicsItem * /* option*/, QWidget * /*widget*/) override
15 {
16 if (isSelected())
17 painter->fillRect(rect(), QColor(255, 0, 0));
18 else
19 painter->fillRect(rect(), QColor(0, 255, 0));
20 }
21 };
22
23 class MyGraphicsView : public QGraphicsView
24 {
25 Q_OBJECT
26 public:
27 MyGraphicsView(QWidget *w, QLabel *l) : QGraphicsView(w), rubberbandLabel(l)
28 {
29 setDragMode(QGraphicsView::RubberBandDrag);
30 connect(this, SIGNAL(rubberBandChanged(QRect, QPointF, QPointF)), this, SLOT(updateRubberbandInfo(QRect, QPointF, QPointF)));
31 }
32 protected:
33 void mouseMoveEvent(QMouseEvent *event) override
34 {
35 QGraphicsView::mouseMoveEvent(event);
36
37 int rightmostInView = viewport()->mapToGlobal(viewport()->geometry().topRight()).x();
38 int xglobal = event->globalX();
39 if (xglobal > rightmostInView)
40 horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 10);
41
42 int bottomPos = viewport()->mapToGlobal(viewport()->geometry().bottomRight()).y();
43 int yglobal = event->globalY();
44 if (yglobal > bottomPos)
45 verticalScrollBar()->setValue(verticalScrollBar()->value() + 10);
46 }
47
48 protected slots:
49 void updateRubberbandInfo(QRect r, QPointF from, QPointF to)
50 {
51 QString textToShow;
52 QDebug s(&textToShow);
53 s << r << from << to;
54 rubberbandLabel->setText(textToShow);
55 }
56 protected:
57 QLabel *rubberbandLabel;
58 };
59
60 int main(int argc, char *argv[])
61 {
62 QApplication app(argc, argv);
63
64 QWidget w;
65 w.setLayout(new QVBoxLayout);
66 QLabel *l = new QLabel(&w);
67 MyGraphicsView *v = new MyGraphicsView(&w, l);
68
69 w.layout()->addWidget(v);
70 w.layout()->addWidget(l);
71
72 QGraphicsScene s(0.0, 0.0, 5000.0, 5000.0);
73 v->setScene(&s);
74 v->setInteractive(true);
75 v->setRubberBandSelectionMode(Qt::IntersectsItemBoundingRect);
76
77 for (int u = 0; u < 100; ++u)
78 for (int n = 0; n < 100; ++n) {
79 MyGraphicsItem *item = new MyGraphicsItem();
80 item->setRect(QRectF(n * 80.0, u * 80.0, 50.0, 20.0));
81 s.addItem(item);
82 }
83
84 w.show();
85 app.exec();
86 return 0;
87 }
88
89 #include "rubberbandtest.moc"