Skip to content

Commit

Permalink
Merge pull request #212 from kevinzg/add-drag-zoom-gui
Browse files Browse the repository at this point in the history
Add drag and zoom to gui
  • Loading branch information
Łukasz Hryniuk committed Oct 25, 2017
2 parents 3ab2be5 + fe933e7 commit 180bfd3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion WangscapeGui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include_directories(${Qt5Widgets_INCLUDE_DIR})
qt5_wrap_ui(UIS_HDRS MainWindow.ui OptionsEditor.ui)

# Tell CMake to create the helloworld executable
add_executable(WangscapeGui WIN32 Main.cpp MainWindow.cpp OptionsEditor.cpp ${UIS_HDRS})
add_executable(WangscapeGui WIN32 Main.cpp MainWindow.cpp OptionsEditor.cpp CustomGraphicsView.cpp ${UIS_HDRS})

# Use the Widgets module from Qt 5.
target_link_libraries(WangscapeGui wangscape-main)
Expand Down
29 changes: 29 additions & 0 deletions WangscapeGui/CustomGraphicsView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "CustomGraphicsView.h"

CustomGraphicsView::CustomGraphicsView(QWidget* parent) :
QGraphicsView(parent) {}

CustomGraphicsView::CustomGraphicsView (QGraphicsScene* scene, QWidget* parent) :
QGraphicsView(scene, parent) {}

void CustomGraphicsView::wheelEvent(QWheelEvent* event)
{
static const double zoomInFactor = 1.5;
static const double zoomOutFactor = 1.0 / zoomInFactor;

if(event->modifiers() & Qt::ControlModifier)
{
QGraphicsView::wheelEvent(event);
}
else
{
if(event->delta() > 0)
{
scale(zoomInFactor, zoomInFactor);
}
else
{
scale(zoomOutFactor, zoomOutFactor);
}
}
}
17 changes: 17 additions & 0 deletions WangscapeGui/CustomGraphicsView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CUSTOMGRAPHICSVIEW_H
#define CUSTOMGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QWheelEvent>

class CustomGraphicsView : public QGraphicsView
{
public:
CustomGraphicsView(QWidget* parent = 0);
CustomGraphicsView(QGraphicsScene* scene, QWidget* parent = 0);

protected:
virtual void wheelEvent(QWheelEvent* event);
};

#endif // CUSTOMGRAPHICSVIEW_H
2 changes: 2 additions & 0 deletions WangscapeGui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ void MainWindow::loadOptionsFromFile()
mUi->generateButton->setEnabled(true);
mUi->saveButton->setEnabled(false);

mUi->tilesetPreview->setDragMode(QGraphicsView::ScrollHandDrag);

mUi->comboBox->clear();
mPreviewImages.clear();
mScene->clear();
Expand Down
9 changes: 8 additions & 1 deletion WangscapeGui/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
</layout>
</item>
<item>
<widget class="QGraphicsView" name="tilesetPreview"/>
<widget class="CustomGraphicsView" name="tilesetPreview"/>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
Expand Down Expand Up @@ -164,6 +164,13 @@
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>CustomGraphicsView</class>
<extends>QGraphicsView</extends>
<header>CustomGraphicsView.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit 180bfd3

Please sign in to comment.