-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derive setsquare classes from geometryTool classes
- Loading branch information
Showing
23 changed files
with
1,106 additions
and
866 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.