Skip to content

Commit

Permalink
Revert back to using QGraphicsView but with a custom QGraphicsItem.
Browse files Browse the repository at this point in the history
This change keeps the previous performance improvements from commit
8de96dc... but changes back to using QGraphicsView with the custom
QWidget modified to a custom QGraphicsItem.

The reason for this change is for much better dragging and scrolling
support. QGraphicsView supports dragging natively, and supports the
centerOn() function, which are not naturally supported in QScrollArea.
  • Loading branch information
damaki committed Feb 8, 2016
1 parent 0c4eeea commit 8be239a
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 135 deletions.
40 changes: 6 additions & 34 deletions forms/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,19 @@
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="opaqueResize">
<bool>true</bool>
</property>
<widget class="QScrollArea" name="scrollArea">
<widget class="QGraphicsView" name="graphicsView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>782</width>
<height>78</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContentsOnFirstShow</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
<property name="dragMode">
<enum>QGraphicsView::ScrollHandDrag</enum>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<property name="resizeAnchor">
<enum>QGraphicsView::AnchorViewCenter</enum>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>780</width>
<height>76</height>
</rect>
</property>
</widget>
</widget>
<widget class="QWidget" name="">
<layout class="QHBoxLayout" name="horizontalLayout">
Expand Down
4 changes: 2 additions & 2 deletions src/commandrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <QMutexLocker>
#include <cassert>

CommandRunner::CommandRunner(TurtleGraphicsWidget* const graphicsWidget) :
CommandRunner::CommandRunner(TurtleGraphicsItem* const graphicsWidget) :
m_state(luaL_newstate()),
m_graphicsWidget(graphicsWidget),
m_scriptDataSema(),
Expand Down Expand Up @@ -52,7 +52,7 @@ CommandRunner::CommandRunner(TurtleGraphicsWidget* const graphicsWidget) :
setupCommands(m_state, this);
}

TurtleGraphicsWidget* CommandRunner::graphicsWidget() const
TurtleGraphicsItem* CommandRunner::graphicsWidget() const
{
return m_graphicsWidget;
}
Expand Down
6 changes: 3 additions & 3 deletions src/commandrunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class CommandRunner : public QThread
Q_OBJECT

public:
CommandRunner(TurtleGraphicsWidget* graphicsWidget);
CommandRunner(TurtleGraphicsItem* graphicsWidget);

TurtleGraphicsWidget* graphicsWidget() const;
TurtleGraphicsItem* graphicsWidget() const;

void requestThreadStop();

Expand All @@ -64,7 +64,7 @@ class CommandRunner : public QThread

private:
lua_State* m_state;
TurtleGraphicsWidget* m_graphicsWidget;
TurtleGraphicsItem* m_graphicsWidget;

mutable QMutex m_luaMutex; // locked while a script is running

Expand Down
40 changes: 31 additions & 9 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,28 @@
#include <cmath>
#include <iostream>

static const int VIEW_UPDATE_DELAY = 33; // update every 33ms (~30 fps)

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_turtleGraphics(new TurtleGraphicsWidget),
m_scene(new QGraphicsScene),
m_turtleGraphics(new TurtleGraphicsItem),
m_cmds(m_turtleGraphics),
m_prefsDialog(new PreferencesDialog(m_turtleGraphics, this)),
m_helpDialog(new HelpDialog(this))
{
ui->setupUi(this);

m_turtleGraphics->show();
ui->scrollArea->setWidget(m_turtleGraphics);
ui->graphicsView->setScene(m_scene);
m_scene->addItem(m_turtleGraphics);

// Make the scroll area occupy as much as possible.
// (the size will be constrained to the maximum possible according to the
// layout, which will be less than this MainWindow's size).
ui->scrollArea->resize(width(), height());
ui->graphicsView->centerOn(0.0, 0.0);

ui->haltButton->setEnabled(false);
ui->pauseButton->setEnabled(false);
ui->resumeButton->setEnabled(false);

connect(m_turtleGraphics, SIGNAL(canvasResized()), this, SLOT(resizeGraphicsScene()));

connect(ui->runButton, SIGNAL(clicked()), this, SLOT(runCommand()));
connect(ui->haltButton, SIGNAL(clicked()), this, SLOT(haltCommand()));
connect(ui->pauseButton, SIGNAL(clicked()), this, SLOT(pauseCommand()));
Expand Down Expand Up @@ -98,6 +96,11 @@ void MainWindow::handleError(const QString& message)
std::cerr << message.toStdString() << std::endl;
}

/**
* @brief Called when a command finishes executing.
*
* This method updates the UI's buttons to allow another command to be executed.
*/
void MainWindow::commandFinished()
{
ui->runButton->setEnabled(true);
Expand All @@ -106,6 +109,9 @@ void MainWindow::commandFinished()
ui->resumeButton->setEnabled(false);
}

/**
* @brief Pauses the currently executing command(s)
*/
void MainWindow::pauseCommand()
{
ui->pauseButton->setEnabled(false);
Expand All @@ -114,6 +120,9 @@ void MainWindow::pauseCommand()
m_cmds.requestCommandPause();
}

/**
* @brief Resumes a previously paused command.
*/
void MainWindow::resumeCommand()
{
ui->pauseButton->setEnabled(true);
Expand All @@ -122,6 +131,9 @@ void MainWindow::resumeCommand()
m_cmds.requestCommandResume();
}

/**
* @brief Halts/stops/aborts the currently running command.
*/
void MainWindow::haltCommand()
{
ui->runButton->setEnabled(false);
Expand All @@ -131,3 +143,13 @@ void MainWindow::haltCommand()

m_cmds.requestCommandHalt();
}

/**
* @brief Resets the QGraphicsScene to perfectly fit the turtle graphic's bounding box.
*
* This avoids unused space around the drawing canvas when the canvas size is reduced.
*/
void MainWindow::resizeGraphicsScene()
{
m_scene->setSceneRect(m_turtleGraphics->boundingRect().translated(m_turtleGraphics->pos()));
}
5 changes: 4 additions & 1 deletion src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ private slots:
void resumeCommand();
void haltCommand();

void resizeGraphicsScene();

private:
Ui::MainWindow *ui;

TurtleGraphicsWidget* m_turtleGraphics;
QGraphicsScene* m_scene;
TurtleGraphicsItem* m_turtleGraphics;
CommandRunner m_cmds;

PreferencesDialog* m_prefsDialog;
Expand Down
6 changes: 3 additions & 3 deletions src/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "preferencesdialog.h"
#include "ui_preferencesdialog.h"

PreferencesDialog::PreferencesDialog(TurtleGraphicsWidget* const graphicsWidget,
PreferencesDialog::PreferencesDialog(TurtleGraphicsItem* const graphicsWidget,
QWidget* const parent) :
QDialog(parent),
ui(new Ui::PreferencesDialog),
Expand All @@ -41,7 +41,7 @@ void PreferencesDialog::loadPreferences()
if (NULL != m_graphicsWidget)
{
ui->antialiasingCheckBox->setChecked(m_graphicsWidget->antialiased());
ui->canvasSizeSpinBox->setValue(m_graphicsWidget->width());
ui->canvasSizeSpinBox->setValue(m_graphicsWidget->boundingRect().width());
}
}

Expand All @@ -52,6 +52,6 @@ void PreferencesDialog::applyPreferences()
m_graphicsWidget->setAntialiased(ui->antialiasingCheckBox->checkState() == Qt::Checked);

const int newSize = ui->canvasSizeSpinBox->value();
m_graphicsWidget->setFixedSize(newSize, newSize);
m_graphicsWidget->resize(newSize);
}
}
4 changes: 2 additions & 2 deletions src/preferencesdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PreferencesDialog : public QDialog
Q_OBJECT

public:
PreferencesDialog(TurtleGraphicsWidget* graphicsWidget, QWidget* parent = 0);
PreferencesDialog(TurtleGraphicsItem* graphicsWidget, QWidget* parent = 0);
~PreferencesDialog();

public slots:
Expand All @@ -45,7 +45,7 @@ public slots:
private:
Ui::PreferencesDialog* ui;

TurtleGraphicsWidget* m_graphicsWidget;
TurtleGraphicsItem* m_graphicsWidget;
};

#endif // PREFERENCESDIALOG_H
Loading

0 comments on commit 8be239a

Please sign in to comment.