Skip to content

Commit

Permalink
Add pan tool
Browse files Browse the repository at this point in the history
To allow moving the canvas around without having to hold down a key or
needing a middle click button on the pen.

Implements #1217.
  • Loading branch information
askmeaboutlo0m committed Apr 11, 2024
1 parent 22f1040 commit 4988174
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -35,6 +35,7 @@ Unreleased Version 2.2.2-pre
* Feature: Don't require restarting the application when changing kinetic scrolling settings.
* Fix: Change default canvas size to 2000x2000, because 800x600 is a bit outdated. Thanks MorrowShore for for contributing.
* Feature: Don't require restarting the application to switch between desktop and small screen mode. There's also a "dynamic" option now, which will change the mode based on the size of the window, enabled by default on Android and in the browser.
* Feature: Pan or "hand" tool. Lets you move the canvas around by clicking. Thanks DeeJii for suggesting.

2024-02-25 Version 2.2.2-beta.1
* Server Feature: Allow adding a message when kicking someone through the admin API.
Expand Down
2 changes: 2 additions & 0 deletions src/desktop/CMakeLists.txt
Expand Up @@ -252,6 +252,8 @@ target_sources(drawpile PRIVATE
toolwidgets/inspectorsettings.h
toolwidgets/lasersettings.cpp
toolwidgets/lasersettings.h
toolwidgets/pansettings.cpp
toolwidgets/pansettings.h
toolwidgets/selectionsettings.cpp
toolwidgets/selectionsettings.h
toolwidgets/toolsettings.cpp
Expand Down
14 changes: 14 additions & 0 deletions src/desktop/assets/theme/dark/hand.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/desktop/assets/theme/light/hand.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/desktop/docks/toolsettingsdock.cpp
Expand Up @@ -9,6 +9,7 @@
#include "desktop/toolwidgets/fillsettings.h"
#include "desktop/toolwidgets/inspectorsettings.h"
#include "desktop/toolwidgets/lasersettings.h"
#include "desktop/toolwidgets/pansettings.h"
#include "desktop/toolwidgets/selectionsettings.h"
#include "desktop/toolwidgets/zoomsettings.h"
#include "libclient/tools/toolcontroller.h"
Expand Down Expand Up @@ -112,6 +113,9 @@ struct ToolSettings::Private {
pages[tools::Tool::POLYGONSELECTION] = {
sel, "selection", QIcon::fromTheme("edit-select-lasso"),
QApplication::tr("Selection (Free-Form)")};
pages[tools::Tool::PAN] = {
QSharedPointer<tools::ToolSettings>(new tools::PanSettings(ctrl)),
"pan", QIcon::fromTheme("hand"), QApplication::tr("Pan")};
pages[tools::Tool::ZOOM] = {
QSharedPointer<tools::ToolSettings>(new tools::ZoomSettings(ctrl)),
"zoom", QIcon::fromTheme("edit-find"), QApplication::tr("Zoom")};
Expand Down Expand Up @@ -310,6 +314,12 @@ tools::LaserPointerSettings *ToolSettings::laserPointerSettings()
getToolSettingsPage(tools::Tool::LASERPOINTER));
}

tools::PanSettings *ToolSettings::panSettings()
{
return static_cast<tools::PanSettings *>(
getToolSettingsPage(tools::Tool::PAN));
}

tools::SelectionSettings *ToolSettings::selectionSettings()
{
return static_cast<tools::SelectionSettings *>(
Expand Down
2 changes: 2 additions & 0 deletions src/desktop/docks/toolsettingsdock.h
Expand Up @@ -13,6 +13,7 @@ class ColorPickerSettings;
class FillSettings;
class InspectorSettings;
class LaserPointerSettings;
class PanSettings;
class SelectionSettings;
class ToolController;
class ToolSettings;
Expand Down Expand Up @@ -52,6 +53,7 @@ class ToolSettings final : public DockBase {
tools::FillSettings *fillSettings();
tools::InspectorSettings *inspectorSettings();
tools::LaserPointerSettings *laserPointerSettings();
tools::PanSettings *panSettings();
tools::SelectionSettings *selectionSettings();
tools::ZoomSettings *zoomSettings();

Expand Down
2 changes: 2 additions & 0 deletions src/desktop/mainwindow.cpp
Expand Up @@ -4370,6 +4370,7 @@ void MainWindow::setupActions()
QAction *lasertool = makeAction("toollaser", tr("&Laser Pointer")).icon("cursor-arrow").statusTip(tr("Point out things on the canvas")).shortcut("L").checkable();
QAction *selectiontool = makeAction("toolselectrect", tr("&Select (Rectangular)")).icon("select-rectangular").statusTip(tr("Select area for copying")).shortcut("S").checkable();
QAction *lassotool = makeAction("toolselectpolygon", tr("&Select (Free-Form)")).icon("edit-select-lasso").statusTip(tr("Select a free-form area for copying")).shortcut("D").checkable();
QAction *pantool = makeAction("toolpan", tr("Pan")).icon("hand").statusTip(tr("Pan canvas view")).shortcut("P").checkable();
QAction *zoomtool = makeAction("toolzoom", tr("Zoom")).icon("edit-find").statusTip(tr("Zoom the canvas view")).shortcut("Z").checkable();
QAction *inspectortool = makeAction("toolinspector", tr("Inspector")).icon("help-whatsthis").statusTip(tr("Find out who did it")).shortcut("Ctrl+I").checkable();

Expand All @@ -4385,6 +4386,7 @@ void MainWindow::setupActions()
m_drawingtools->addAction(lasertool);
m_drawingtools->addAction(selectiontool);
m_drawingtools->addAction(lassotool);
m_drawingtools->addAction(pantool);
m_drawingtools->addAction(zoomtool);
m_drawingtools->addAction(inspectortool);

Expand Down
3 changes: 3 additions & 0 deletions src/desktop/scene/scenewrapper.cpp
Expand Up @@ -250,6 +250,9 @@ void SceneWrapper::connectDocument(Document *doc)
connect(
toolCtrl, &tools::ToolController::busyStateChanged, m_view,
&CanvasView::setBusy);
connect(
toolCtrl, &tools::ToolController::panRequested, m_view,
&CanvasView::scrollBy);
connect(
toolCtrl, &tools::ToolController::zoomRequested, m_view,
&CanvasView::zoomTo);
Expand Down
23 changes: 23 additions & 0 deletions src/desktop/toolwidgets/pansettings.cpp
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "desktop/toolwidgets/pansettings.h"
#include "desktop/widgets/groupedtoolbutton.h"
#include <QButtonGroup>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>

namespace tools {

PanSettings::PanSettings(ToolController *ctrl, QObject *parent)
: ToolSettings(ctrl, parent)
{
}

QWidget *PanSettings::createUiWidget(QWidget *parent)
{
QWidget *widget = new QWidget(parent);
// The pan tool has no settings.
return widget;
}

}
32 changes: 32 additions & 0 deletions src/desktop/toolwidgets/pansettings.h
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef DESKTOP_TOOLWIDGETS_PANSETTINGS_H
#define DESKTOP_TOOLWIDGETS_PANSETTINGS_H
#include "desktop/toolwidgets/toolsettings.h"

class QAction;

namespace widgets {
class GroupedToolButton;
}

namespace tools {

class PanSettings final : public ToolSettings {
Q_OBJECT
public:
PanSettings(ToolController *ctrl, QObject *parent = nullptr);

QString toolType() const override { return QStringLiteral("pan"); }

void setForeground(const QColor &color) override { Q_UNUSED(color); }

int getSize() const override { return 0; }
bool getSubpixelMode() const override { return false; }

protected:
QWidget *createUiWidget(QWidget *parent) override;
};

}

#endif
4 changes: 4 additions & 0 deletions src/desktop/view/viewwrapper.cpp
Expand Up @@ -8,6 +8,7 @@
#include "desktop/toolwidgets/annotationsettings.h"
#include "desktop/toolwidgets/brushsettings.h"
#include "desktop/toolwidgets/lasersettings.h"
#include "desktop/toolwidgets/pansettings.h"
#include "desktop/toolwidgets/selectionsettings.h"
#include "desktop/toolwidgets/zoomsettings.h"
#include "desktop/view/canvascontroller.h"
Expand Down Expand Up @@ -248,6 +249,9 @@ void ViewWrapper::connectDocument(Document *doc)
connect(
toolCtrl, &tools::ToolController::busyStateChanged, m_controller,
&CanvasController::setBusy);
connect(
toolCtrl, &tools::ToolController::panRequested, m_controller,
&CanvasController::scrollBy);
connect(
toolCtrl, &tools::ToolController::zoomRequested, m_controller,
&CanvasController::zoomTo);
Expand Down
2 changes: 2 additions & 0 deletions src/libclient/CMakeLists.txt
Expand Up @@ -131,6 +131,8 @@ target_sources(dpclient PRIVATE
tools/inspector.h
tools/laser.cpp
tools/laser.h
tools/pan.cpp
tools/pan.h
tools/selection.cpp
tools/selection.h
tools/shapetools.cpp
Expand Down
48 changes: 48 additions & 0 deletions src/libclient/tools/pan.cpp
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "libclient/tools/pan.h"
#include "libclient/tools/toolcontroller.h"
#include <QCursor>
#include <QPixmap>

namespace tools {

PanTool::PanTool(ToolController &owner)
: Tool(owner, PAN, QCursor(Qt::OpenHandCursor), true, false, true)
{
}

void PanTool::begin(
const canvas::Point &point, bool right, float zoom, const QPointF &viewPos)
{
Q_UNUSED(point);
Q_UNUSED(zoom);
if(!right) {
setCursor(Qt::ClosedHandCursor);
m_lastViewPos = viewPos;
m_panning = true;
}
}

void PanTool::motion(
const canvas::Point &point, bool constrain, bool center,
const QPointF &viewPos)
{
Q_UNUSED(point);
Q_UNUSED(constrain);
Q_UNUSED(center);
if(m_panning) {
QPointF delta = m_lastViewPos - viewPos;
emit m_owner.panRequested(qRound(delta.x()), qRound(delta.y()));
m_lastViewPos = viewPos;
}
}

void PanTool::end()
{
if(m_panning) {
setCursor(Qt::OpenHandCursor);
m_panning = false;
}
}

}
30 changes: 30 additions & 0 deletions src/libclient/tools/pan.h
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef LIBCLIENT_TOOLS_PAN_H
#define LIBCLIENT_TOOLS_PAN_H
#include "libclient/tools/tool.h"
#include <QPointF>

namespace tools {

class PanTool final : public Tool {
public:
PanTool(ToolController &owner);

void begin(
const canvas::Point &point, bool right, float zoom,
const QPointF &viewPos) override;

void motion(
const canvas::Point &point, bool constrain, bool center,
const QPointF &viewPos) override;

void end() override;

private:
QPointF m_lastViewPos;
bool m_panning = false;
};

}

#endif
1 change: 1 addition & 0 deletions src/libclient/tools/tool.h
Expand Up @@ -35,6 +35,7 @@ class Tool {
LASERPOINTER,
SELECTION,
POLYGONSELECTION,
PAN,
ZOOM,
INSPECTOR,
_LASTTOOL,
Expand Down
2 changes: 2 additions & 0 deletions src/libclient/tools/toolcontroller.cpp
Expand Up @@ -11,6 +11,7 @@
#include "libclient/tools/freehand.h"
#include "libclient/tools/inspector.h"
#include "libclient/tools/laser.h"
#include "libclient/tools/pan.h"
#include "libclient/tools/selection.h"
#include "libclient/tools/shapetools.h"
#include "libclient/tools/zoom.h"
Expand Down Expand Up @@ -52,6 +53,7 @@ ToolController::ToolController(net::Client *client, QObject *parent)
registerTool(new LaserPointer(*this));
registerTool(new RectangleSelection(*this));
registerTool(new PolygonSelection(*this));
registerTool(new PanTool(*this));
registerTool(new ZoomTool(*this));
registerTool(new Inspector(*this));

Expand Down
1 change: 1 addition & 0 deletions src/libclient/tools/toolcontroller.h
Expand Up @@ -182,6 +182,7 @@ public slots:
void actionCancelled();

void colorUsed(const QColor &color);
void panRequested(int x, int y);
void zoomRequested(const QRect &rect, int steps);

void busyStateChanged(bool busy);
Expand Down

0 comments on commit 4988174

Please sign in to comment.