Skip to content

Commit

Permalink
Derive setsquare classes from geometryTool classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo committed Oct 15, 2022
1 parent 456f9c3 commit 714e479
Show file tree
Hide file tree
Showing 23 changed files with 1,106 additions and 866 deletions.
22 changes: 12 additions & 10 deletions src/core/control/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ void Control::initWindow(MainWindow* win) {

fireActionSelected(GROUP_SNAPPING, settings->isSnapRotation() ? ACTION_ROTATION_SNAPPING : ACTION_NONE);
fireActionSelected(GROUP_GRID_SNAPPING, settings->isSnapGrid() ? ACTION_GRID_SNAPPING : ACTION_NONE);
fireActionSelected(GROUP_SETSQUARE, ACTION_NONE);
fireActionSelected(GROUP_GEOMETRY_TOOL, ACTION_NONE);
}

auto Control::autosaveCallback(Control* control) -> bool {
Expand Down Expand Up @@ -632,12 +632,14 @@ void Control::actionPerformed(ActionType type, ActionGroup group, GdkEvent* even
}
break;
case ACTION_SETSQUARE:
if (auto xournal = this->win->getXournal(); !xournal->getSetsquareController()) {
xournal->resetSetsquare();
xournal->makeSetsquare();
if (auto xournal = this->win->getXournal();
!xournal->getGeometryToolController() ||
xournal->getGeometryToolController()->getType() != "setsquare") {
xournal->resetGeometryTool();
xournal->makeGeometryTool("setsquare");
xournal->getViewFor(getCurrentPageNo())->rerenderPage();
} else {
xournal->resetSetsquare();
xournal->resetGeometryTool();
xournal->getViewFor(getCurrentPageNo())->rerenderPage();
}
break;
Expand Down Expand Up @@ -1330,11 +1332,11 @@ void Control::updateDeletePageButton() {
void Control::deletePage() {
clearSelectionEndText();

// if the current page contains the Setsquare, reset it
// if the current page contains the geometry tool, reset it
size_t pNr = getCurrentPageNo();
auto setsquareController = win->getXournal()->getSetsquareController();
if (setsquareController && doc->indexOf(setsquareController->getPage()) == pNr) {
win->getXournal()->resetSetsquare();
auto geometryToolController = win->getXournal()->getGeometryToolController();
if (geometryToolController && doc->indexOf(geometryToolController->getPage()) == pNr) {
win->getXournal()->resetGeometryTool();
}
// don't allow delete pages if we have less than 2 pages,
// so we can be (more or less) sure there is at least one page.
Expand Down Expand Up @@ -2721,7 +2723,7 @@ auto Control::close(const bool allowDestroy, const bool allowCancel) -> bool {
if (allowDestroy && discard) {
this->closeDocument();
}
win->getXournal()->resetSetsquare();
win->getXournal()->resetGeometryTool();
return true;
}

Expand Down
81 changes: 81 additions & 0 deletions src/core/control/GeometryToolController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "GeometryToolController.h"

#include "control/Control.h"
#include "control/layer/LayerController.h"
#include "gui/XournalView.h"
#include "gui/XournalppCursor.h"
#include "model/GeometryTool.h"
#include "model/Stroke.h"
#include "model/XojPage.h"
#include "undo/InsertUndoAction.h"

using xoj::util::Rectangle;
GeometryToolController::GeometryToolController(XojPageView* view, GeometryTool* s): view(view), s(s) {}

GeometryToolController::~GeometryToolController() = default;

void GeometryToolController::move(double x, double y) {
s->setTranslationX(s->getTranslationX() + x);
s->setTranslationY(s->getTranslationY() + y);
s->notify();
}

void GeometryToolController::rotate(double da, double cx, double cy) {
s->setRotation(s->getRotation() + da);
const auto tx = s->getTranslationX();
const auto ty = s->getTranslationY();
const auto offsetX = tx - cx;
const auto offsetY = ty - cy;
const auto mx = offsetX * cos(da) - offsetY * sin(da);
const auto my = offsetX * sin(da) + offsetY * cos(da);
s->setTranslationX(cx + mx);
s->setTranslationY(cy + my);
s->notify();
}

void GeometryToolController::scale(double f, double cx, double cy) {
s->setHeight(s->getHeight() * f);
const auto tx = s->getTranslationX();
const auto ty = s->getTranslationY();
const auto offsetX = tx - cx;
const auto offsetY = ty - cy;
const auto mx = offsetX * f;
const auto my = offsetY * f;
s->setTranslationX(cx + mx);
s->setTranslationY(cy + my);
s->notify();
}


void GeometryToolController::addStrokeToLayer() {
const auto xournal = view->getXournal();
const auto control = xournal->getControl();
const auto page = view->getPage();
control->getLayerController()->ensureLayerExists(page);
const auto layer = page->getSelectedLayer();

const auto undo = control->getUndoRedoHandler();
undo->addUndoAction(std::make_unique<InsertUndoAction>(page, layer, stroke));

layer->addElement(stroke);

const Rectangle<double> rect{stroke->getX(), stroke->getY(), stroke->getElementWidth(), stroke->getElementHeight()};
view->rerenderRect(rect.x, rect.y, rect.width, rect.height);
stroke = nullptr;
s->setStroke(nullptr);
xournal->getCursor()->updateCursor();
}

void GeometryToolController::initializeStroke() {
const auto h = view->getXournal()->getControl()->getToolHandler();
stroke = new Stroke();
s->setStroke(stroke);
stroke->setWidth(h->getThickness());
stroke->setColor(h->getColor());
stroke->setFill(h->getFill());
stroke->setLineStyle(h->getLineStyle());
}

auto GeometryToolController::getPage() const -> PageRef { return view->getPage(); }

auto GeometryToolController::getView() const -> XojPageView* { return view; }
95 changes: 95 additions & 0 deletions src/core/control/GeometryToolController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Xournal++
*
* A geometry tool controller
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/

#pragma once

#include <gtk/gtk.h>

#include "gui/PageView.h"
#include "util/Point.h"

class GeometryTool;
class Stroke;

/**
* @brief A class that controls a geometry tool
* The geometry tool can be moved, rotated and scaled
* There are methods for translating coordinates
* and methods to deal with the temporary stroke
*/

class GeometryToolController {
public:
GeometryToolController(XojPageView* view, GeometryTool* s);
~GeometryToolController();

public:
/**
* @brief moves the geometry tool in x- and y-direction
* @param x the translation in x-direction (in document coordinates)
* @param y the translation in y-direction (in document coordinates)
*/
void move(double x, double y);

/**
* @brief rotates the geometry tool around its rotation center
* @param da the rotation angle
* @param cx the x-coordinate of the rotation center (in document coordinates)
* @param cy the y-coordinate of the rotation center (in document coordinates)
*/
void rotate(double da, double cx, double cy);

/**
* @brief resizes the geometry tool by the factor f with respect to a given scaling center
* @param f the scaling factor
* @param cx the x-coordinate of the scaling center (in document coordinates)
* @param cy the y-coordinate of the scaling center (in document coordiantes)
*/
void scale(double f, double cx, double cy);

/**
* @brief adds the stroke to the layer and rerenders the stroke area
*/
void addStrokeToLayer();

/**
* @brief initializes the stroke by using the properties from the tool handler
*/
void initializeStroke();

/**
* @brief the page view of the page with respect to which the geometry tool is initialized
*/
XojPageView* getView() const;

/**
* @brief the page with respect to which the setsquare is initialized
*/
PageRef getPage() const;

virtual bool isInsideGeometryTool(double x, double y, double border = 0.0) const = 0;

virtual std::string getType() const = 0;

public:
XojPageView* view;

/**
* @brief the underlying geometry tool
*/
GeometryTool* s;

/**
* @brief The stroke drawn
*/
Stroke* stroke = nullptr;
};
72 changes: 4 additions & 68 deletions src/core/control/SetsquareController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,18 @@
#include "control/Control.h"
#include "control/layer/LayerController.h"
#include "gui/XournalView.h"
#include "gui/XournalppCursor.h"
#include "model/GeometryTool.h"
#include "model/Setsquare.h"
#include "model/Stroke.h"
#include "model/XojPage.h"
#include "undo/InsertUndoAction.h" // for InsertUndoAction

using xoj::util::Rectangle;
SetsquareController::SetsquareController(XojPageView* view, Setsquare* s): view(view), s(s) {}
SetsquareController::SetsquareController(XojPageView* view, Setsquare* s): GeometryToolController(view, s) {}

SetsquareController::~SetsquareController() = default;

void SetsquareController::move(double x, double y) {
s->setTranslationX(s->getTranslationX() + x);
s->setTranslationY(s->getTranslationY() + y);
s->notify();
}

void SetsquareController::rotate(double da, double cx, double cy) {
s->setRotation(s->getRotation() + da);
const auto tx = s->getTranslationX();
const auto ty = s->getTranslationY();
const auto offsetX = tx - cx;
const auto offsetY = ty - cy;
const auto mx = offsetX * cos(da) - offsetY * sin(da);
const auto my = offsetX * sin(da) + offsetY * cos(da);
s->setTranslationX(cx + mx);
s->setTranslationY(cy + my);
s->notify();
}

void SetsquareController::scale(double f, double cx, double cy) {
s->setHeight(s->getHeight() * f);
const auto tx = s->getTranslationX();
const auto ty = s->getTranslationY();
const auto offsetX = tx - cx;
const auto offsetY = ty - cy;
const auto mx = offsetX * f;
const auto my = offsetY * f;
s->setTranslationX(cx + mx);
s->setTranslationY(cy + my);
s->notify();
}


void SetsquareController::addStrokeToLayer() {
const auto xournal = view->getXournal();
const auto control = xournal->getControl();
const auto page = view->getPage();
control->getLayerController()->ensureLayerExists(page);
const auto layer = page->getSelectedLayer();

const auto undo = control->getUndoRedoHandler();
undo->addUndoAction(std::make_unique<InsertUndoAction>(page, layer, stroke));

layer->addElement(stroke);

const Rectangle<double> rect{stroke->getX(), stroke->getY(), stroke->getElementWidth(), stroke->getElementHeight()};
view->rerenderRect(rect.x, rect.y, rect.width, rect.height);
stroke = nullptr;
s->setStroke(nullptr);
xournal->getCursor()->updateCursor();
}

void SetsquareController::initializeStroke() {
const auto h = view->getXournal()->getControl()->getToolHandler();
stroke = new Stroke();
s->setStroke(stroke);
stroke->setWidth(h->getThickness());
stroke->setColor(h->getColor());
stroke->setFill(h->getFill());
stroke->setLineStyle(h->getLineStyle());
}

auto SetsquareController::getPage() const -> PageRef { return view->getPage(); }

auto SetsquareController::getView() const -> XojPageView* { return view; }
auto SetsquareController::getType() const -> std::string { return "setsquare"; }

auto SetsquareController::posRelToSide(Leg leg, double x, double y) const -> utl::Point<double> {
cairo_matrix_t matrix{};
Expand All @@ -97,7 +33,7 @@ auto SetsquareController::posRelToSide(Leg leg, double x, double y) const -> utl
}
}

auto SetsquareController::isInsideSetsquare(double x, double y, double border) const -> bool {
auto SetsquareController::isInsideGeometryTool(double x, double y, double border) const -> bool {
return posRelToSide(HYPOTENUSE, x, y).y < border && posRelToSide(LEFT_LEG, x, y).y < border &&
posRelToSide(RIGHT_LEG, x, y).y < border;
}
Expand Down

0 comments on commit 714e479

Please sign in to comment.