17#include "selecttool.h"
19#include "pointerevent.h"
20#include "vectorimage.h"
22#include "layervector.h"
23#include "scribblearea.h"
24#include "layermanager.h"
25#include "toolmanager.h"
26#include "selectionmanager.h"
27#include "undoredomanager.h"
33void SelectTool::loadSettings()
35 QSettings pencilSettings(PENCIL2D, PENCIL2D);
39 mPropertyUsed[TransformToolProperties::SHOWSELECTIONINFO_ENABLED] = { Layer::BITMAP, Layer::VECTOR };
41 info[TransformToolProperties::SHOWSELECTIONINFO_ENABLED] =
false;
42 toolProperties().insertProperties(info);
43 toolProperties().loadFrom(typeName(), pencilSettings);
45 if (toolProperties().requireMigration(pencilSettings, ToolProperties::VERSION_1)) {
46 toolProperties().setBaseValue(TransformToolProperties::SHOWSELECTIONINFO_ENABLED, pencilSettings.
value(
"ShowSelectionInfo",
false).
toBool());
53 if (mScribbleArea->isPointerInUse()) {
return QCursor(mCursorPixmap); }
55 MoveMode mode = mEditor->select()->getMoveMode();
58 QPainter cursorPainter(&mCursorPixmap);
63 case MoveMode::TOPLEFT:
64 case MoveMode::BOTTOMRIGHT:
66 cursorPainter.drawPixmap(
QPoint(6,6),
QPixmap(
"://icons/general/cursor-diagonal-left.svg"));
69 case MoveMode::TOPRIGHT:
70 case MoveMode::BOTTOMLEFT:
72 cursorPainter.drawPixmap(
QPoint(6,6),
QPixmap(
"://icons/general/cursor-diagonal-right.svg"));
75 case MoveMode::MIDDLE:
77 cursorPainter.drawPixmap(
QPoint(6,6),
QPixmap(
"://icons/general/cursor-move.svg"));
81 cursorPainter.drawPixmap(
QPoint(3,3),
QPixmap(
":icons/general/cross.png"));
90void SelectTool::beginSelection(
Layer* currentLayer,
const QPointF& pos)
92 auto selectMan = mEditor->select();
94 if (selectMan->somethingSelected() && mMoveMode != MoveMode::NONE)
96 if (currentLayer->type() == Layer::VECTOR)
98 VectorImage* vectorImage =
static_cast<LayerVector*
>(currentLayer)->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
99 if (vectorImage !=
nullptr) {
103 mSelectionRect = mEditor->select()->
mapToSelection(mEditor->select()->mySelectionRect()).boundingRect();
107 selectMan->setSelection(
QRectF(pos.
x(), pos.
y(), 0, 0), mEditor->layers()->currentLayer()->type() == Layer::BITMAP);
108 mAnchorOriginPoint = pos;
116 Layer* currentLayer = mEditor->layers()->currentLayer();
117 if (currentLayer ==
nullptr)
return;
118 if (!currentLayer->isPaintable()) {
return; }
120 auto selectMan = mEditor->select();
122 mUndoState = mEditor->undoRedo()->
state(UndoRedoRecordType::KEYFRAME_MODIFY);
124 mPressPoint =
event->canvasPos();
126 if (currentLayer->type() == Layer::BITMAP) {
127 mPressPoint = mPressPoint.
toPoint();
130 selectMan->setMoveModeForAnchorInRange(mPressPoint);
131 mMoveMode = selectMan->getMoveMode();
132 mStartMoveMode = mMoveMode;
134 beginSelection(currentLayer, mPressPoint);
139 Layer* currentLayer = mEditor->layers()->currentLayer();
140 if (currentLayer ==
nullptr) {
return; }
141 if (!currentLayer->isPaintable()) {
return; }
142 auto selectMan = mEditor->select();
144 QPointF canvasPos =
event->canvasPos();
146 if (currentLayer->type() == Layer::BITMAP) {
147 canvasPos = canvasPos.
toPoint();
150 selectMan->setMoveModeForAnchorInRange(canvasPos);
151 mMoveMode = selectMan->getMoveMode();
152 mScribbleArea->updateToolCursor();
154 if (mScribbleArea->isPointerInUse())
156 controlOffsetOrigin(canvasPos, mAnchorOriginPoint, currentLayer->type());
158 if (currentLayer->type() == Layer::VECTOR)
160 VectorImage* vectorImage =
static_cast<LayerVector*
>(currentLayer)->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
161 if (vectorImage !=
nullptr) {
162 vectorImage->select(selectMan->mapToSelection(
QPolygonF(selectMan->mySelectionRect())).boundingRect());
170void SelectTool::pointerReleaseEvent(
PointerEvent* event)
172 Layer* currentLayer = mEditor->layers()->currentLayer();
173 if (currentLayer ==
nullptr)
return;
176 QPointF canvasPos =
event->canvasPos();
177 if (currentLayer->type() == Layer::BITMAP) {
178 canvasPos = canvasPos.
toPoint();
184 if (
QLineF(mAnchorOriginPoint, canvasPos).length() < 1.0)
186 mEditor->deselectAll();
188 else if (maybeDeselect(canvasPos))
190 mEditor->deselectAll();
194 mSelectionRect = mEditor->select()->
mapToSelection(mEditor->select()->mySelectionRect()).boundingRect();
198 mEditor->undoRedo()->
record(mUndoState, typeName());
200 mStartMoveMode = MoveMode::NONE;
201 mMoveMode = MoveMode::NONE;
203 mScribbleArea->updateToolCursor();
207bool SelectTool::maybeDeselect(
const QPointF& pos)
209 return ((!isSelectionPointValid(pos) && mEditor->select()->getMoveMode() == MoveMode::NONE)
210 || !mEditor->select()->mySelectionRect().
isValid());
219 if (currentLayer->type() == Layer::VECTOR)
221 VectorImage* vectorImage =
static_cast<LayerVector*
>(currentLayer)->getLastVectorImageAtFrame(mEditor->currentFrame(), 0);
222 if (vectorImage ==
nullptr) {
return; }
223 auto selectMan = mEditor->select();
224 selectMan->
setSelection(vectorImage->getSelectionRect(),
false);
228void SelectTool::controlOffsetOrigin(
QPointF currentPoint,
QPointF anchorPoint, Layer::LAYER_TYPE layerType)
231 if (mStartMoveMode == MoveMode::NONE) {
233 newSelection =
QRectF(currentPoint, anchorPoint);
235 newSelection = mSelectionRect;
237 QPointF offset = offsetFromPressPos(currentPoint);
238 if (mStartMoveMode == MoveMode::TOPLEFT) {
239 newSelection.
adjust(offset.
x(), offset.
y(), 0, 0);
240 }
else if (mStartMoveMode == MoveMode::TOPRIGHT) {
241 newSelection.
adjust(0, offset.
y(), offset.
x(), 0);
242 }
else if (mStartMoveMode == MoveMode::BOTTOMRIGHT) {
243 newSelection.
adjust(0, 0, offset.
x(), offset.
y());
244 }
else if (mStartMoveMode == MoveMode::BOTTOMLEFT) {
245 newSelection.
adjust(offset.
x(), 0, 0, offset.
y());
251 mEditor->select()->
setSelection(newSelection, layerType == Layer::BITMAP);
254bool SelectTool::keyPressEvent(
QKeyEvent* event)
256 switch (
event->key())
269 return TransformTool::keyPressEvent(
event);
274 return pos - mPressPoint;
void updateFrame()
Update frame.
QPointF mapToSelection(const QPointF &point) const
Maps a point from Canvas/Layer space INTO the Selection's transformed space.
void setSelection(QRectF rect, bool roundPixels=false)
Defines the selection area.
void record(const UndoSaveState *&undoState, const QString &description)
Records the given save state.
const UndoSaveState * state(UndoRedoRecordType recordType) const
Prepares and returns a save state with the given scope.
void deselectAll()
VectorImage::deselectAll.
virtual bool event(QEvent *e)
void fill(const QColor &color)
QPoint toPoint() const const
void adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
bool isValid() const const
QRectF normalized() const const
void translate(qreal dx, qreal dy)
QVariant value(const QString &key, const QVariant &defaultValue) const const
bool toBool() const const