diff options
author | Yoann Lopes <yoann.lopes@nokia.com> | 2010-11-15 13:42:49 (GMT) |
---|---|---|
committer | Yoann Lopes <yoann.lopes@nokia.com> | 2010-11-15 13:47:21 (GMT) |
commit | 0f905e860804dbb4182663f1628a359e3c0795b1 (patch) | |
tree | e24f22eaa8d7c8abd77ddfbefc6faf3144f3cd9f /demos | |
parent | e084fbbe18d771702312d7db65030aaaf18b596b (diff) | |
download | Qt-0f905e860804dbb4182663f1628a359e3c0795b1.zip Qt-0f905e860804dbb4182663f1628a359e3c0795b1.tar.gz Qt-0f905e860804dbb4182663f1628a359e3c0795b1.tar.bz2 |
Chip demo: makes scene interaction easier.
It is now possible to zoom in or out using the mouse wheel (while
pressing the CTRL key). It is also possible to switch between two pointer
modes: select and drag. The first one allows to select items and move
them, the second allows to drag the scene.
Reviewed-by: bnilsen
Diffstat (limited to 'demos')
-rw-r--r-- | demos/chip/view.cpp | 54 | ||||
-rw-r--r-- | demos/chip/view.h | 32 |
2 files changed, 74 insertions, 12 deletions
diff --git a/demos/chip/view.cpp b/demos/chip/view.cpp index 7af3074..d6a641e 100644 --- a/demos/chip/view.cpp +++ b/demos/chip/view.cpp @@ -48,15 +48,29 @@ #include <qmath.h> +void GraphicsView::wheelEvent(QWheelEvent *e) +{ + if (e->modifiers() & Qt::ControlModifier) { + if (e->delta() > 0) + view->zoomIn(6); + else + view->zoomOut(6); + e->accept(); + } else { + QGraphicsView::wheelEvent(e); + } +} + View::View(const QString &name, QWidget *parent) : QFrame(parent) { setFrameStyle(Sunken | StyledPanel); - graphicsView = new QGraphicsView; + graphicsView = new GraphicsView(this); graphicsView->setRenderHint(QPainter::Antialiasing, false); graphicsView->setDragMode(QGraphicsView::RubberBandDrag); graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState); graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate); + graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse); int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize); QSize iconSize(size, size); @@ -111,6 +125,15 @@ View::View(const QString &name, QWidget *parent) // Label layout QHBoxLayout *labelLayout = new QHBoxLayout; label = new QLabel(name); + label2 = new QLabel(tr("Pointer Mode")); + selectModeButton = new QToolButton; + selectModeButton->setText(tr("Select")); + selectModeButton->setCheckable(true); + selectModeButton->setChecked(true); + dragModeButton = new QToolButton; + dragModeButton->setText(tr("Drag")); + dragModeButton->setCheckable(true); + dragModeButton->setChecked(false); antialiasButton = new QToolButton; antialiasButton->setText(tr("Antialiasing")); antialiasButton->setCheckable(true); @@ -126,8 +149,17 @@ View::View(const QString &name, QWidget *parent) printButton = new QToolButton; printButton->setIcon(QIcon(QPixmap(":/fileprint.png"))); + QButtonGroup *pointerModeGroup = new QButtonGroup; + pointerModeGroup->setExclusive(true); + pointerModeGroup->addButton(selectModeButton); + pointerModeGroup->addButton(dragModeButton); + labelLayout->addWidget(label); labelLayout->addStretch(); + labelLayout->addWidget(label2); + labelLayout->addWidget(selectModeButton); + labelLayout->addWidget(dragModeButton); + labelLayout->addStretch(); labelLayout->addWidget(antialiasButton); labelLayout->addWidget(openGlButton); labelLayout->addWidget(printButton); @@ -145,6 +177,8 @@ View::View(const QString &name, QWidget *parent) connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix())); connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled())); connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setResetButtonEnabled())); + connect(selectModeButton, SIGNAL(toggled(bool)), this, SLOT(togglePointerMode())); + connect(dragModeButton, SIGNAL(toggled(bool)), this, SLOT(togglePointerMode())); connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing())); connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL())); connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft())); @@ -158,7 +192,7 @@ View::View(const QString &name, QWidget *parent) QGraphicsView *View::view() const { - return graphicsView; + return static_cast<QGraphicsView *>(graphicsView); } void View::resetView() @@ -188,6 +222,14 @@ void View::setupMatrix() setResetButtonEnabled(); } +void View::togglePointerMode() +{ + graphicsView->setDragMode(selectModeButton->isChecked() + ? QGraphicsView::RubberBandDrag + : QGraphicsView::ScrollHandDrag); + graphicsView->setInteractive(selectModeButton->isChecked()); +} + void View::toggleOpenGL() { #ifndef QT_NO_OPENGL @@ -212,14 +254,14 @@ void View::print() #endif } -void View::zoomIn() +void View::zoomIn(int level) { - zoomSlider->setValue(zoomSlider->value() + 1); + zoomSlider->setValue(zoomSlider->value() + level); } -void View::zoomOut() +void View::zoomOut(int level) { - zoomSlider->setValue(zoomSlider->value() - 1); + zoomSlider->setValue(zoomSlider->value() - level); } void View::rotateLeft() diff --git a/demos/chip/view.h b/demos/chip/view.h index 8047b8b..67c7675 100644 --- a/demos/chip/view.h +++ b/demos/chip/view.h @@ -43,12 +43,27 @@ #define VIEW_H #include <QFrame> +#include <QGraphicsView> -QT_FORWARD_DECLARE_CLASS(QGraphicsView) QT_FORWARD_DECLARE_CLASS(QLabel) QT_FORWARD_DECLARE_CLASS(QSlider) QT_FORWARD_DECLARE_CLASS(QToolButton) +class View; + +class GraphicsView : public QGraphicsView +{ + Q_OBJECT +public: + GraphicsView(View *v) : QGraphicsView(), view(v) { } + +protected: + void wheelEvent(QWheelEvent *); + +private: + View *view; +}; + class View : public QFrame { Q_OBJECT @@ -57,22 +72,27 @@ public: QGraphicsView *view() const; +public slots: + void zoomIn(int level = 1); + void zoomOut(int level = 1); + private slots: void resetView(); void setResetButtonEnabled(); void setupMatrix(); + void togglePointerMode(); void toggleOpenGL(); void toggleAntialiasing(); void print(); - - void zoomIn(); - void zoomOut(); void rotateLeft(); void rotateRight(); - + private: - QGraphicsView *graphicsView; + GraphicsView *graphicsView; QLabel *label; + QLabel *label2; + QToolButton *selectModeButton; + QToolButton *dragModeButton; QToolButton *openGlButton; QToolButton *antialiasButton; QToolButton *printButton; |