Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drag and zoom to gui #212

Merged
merged 2 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>