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());
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 mUndoStateId = mEditor->undoRedo()->
createState(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());
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(mUndoStateId, 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());
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)
230 if (mStartMoveMode != MoveMode::NONE) {
231 QRectF newSelection = mSelectionRect;
233 QPointF offset = offsetFromPressPos(currentPoint);
234 if (mStartMoveMode == MoveMode::TOPLEFT) {
235 newSelection.
adjust(offset.
x(), offset.
y(), 0, 0);
236 }
else if (mStartMoveMode == MoveMode::TOPRIGHT) {
237 newSelection.
adjust(0, offset.
y(), offset.
x(), 0);
238 }
else if (mStartMoveMode == MoveMode::BOTTOMRIGHT) {
239 newSelection.
adjust(0, 0, offset.
x(), offset.
y());
240 }
else if (mStartMoveMode == MoveMode::BOTTOMLEFT) {
241 newSelection.
adjust(offset.
x(), 0, 0, offset.
y());
248 mEditor->select()->
setSelection(newSelection, layerType == Layer::BITMAP);
261 qreal mouseX = currentPoint.
x();
262 qreal mouseY = currentPoint.
y();
264 QRectF selectRect = mSelectionRect;
266 if (mouseX <= originPoint.
x())
273 selectRect.
setLeft(originPoint.
x());
277 if (mouseY <= originPoint.
y())
279 selectRect.
setTop(mouseY);
284 selectRect.
setTop(originPoint.
y());
288 if (selectRect.
width() <= 0) {
291 if (selectRect.
height() <= 0) {
294 editor()->select()->
setSelection(selectRect, layerType == Layer::BITMAP);
297bool SelectTool::keyPressEvent(
QKeyEvent* event)
299 switch (
event->key())
312 return TransformTool::keyPressEvent(
event);
317 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(SAVESTATE_ID SaveStateId, const QString &description)
Records the given save state.
SAVESTATE_ID createState(UndoRedoRecordType recordType)
Prepares and returns an save state with common data.
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)
qreal height() const const
bool isValid() const const
QRectF normalized() const const
void setHeight(qreal height)
void setWidth(qreal width)
void translate(qreal dx, qreal dy)
qreal width() const const
QVariant value(const QString &key, const QVariant &defaultValue) const const
bool toBool() const const