Skip to content

Commit

Permalink
Move quick size handling to the tools
Browse files Browse the repository at this point in the history
In addition to be being a more logical location for this code, it
also allows for more customization of the quick sizing to each
tools specific needs. For example, the bucket tool uses it for
adjusting the fill expansion, but that only applies to the vector
layer so it should not be active when using the bitmap layer. The
pencil tool has a feather property, but it can't be changed, so
quick sizing should not be enabled for that.

Fixes pencil2d#1186
  • Loading branch information
scribblemaniac committed May 30, 2020
1 parent 701dd95 commit bfda42f
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 53 deletions.
19 changes: 1 addition & 18 deletions core_lib/src/interface/scribblearea.cpp
Expand Up @@ -517,7 +517,7 @@ void ScribbleArea::pointerPressEvent(PointerEvent* event)
if (isPressed && mQuickSizing)
{
//qDebug() << "Start Adjusting" << event->buttons();
if (isDoingAssistedToolAdjustment(event->modifiers()))
if (currentTool()->startAdjusting(event->modifiers(), 1))
{
return;
}
Expand Down Expand Up @@ -686,23 +686,6 @@ void ScribbleArea::resizeEvent(QResizeEvent* event)
updateAllFrames();
}

bool ScribbleArea::isDoingAssistedToolAdjustment(Qt::KeyboardModifiers keyMod)
{
if ((keyMod == Qt::ShiftModifier) && (currentTool()->properties.width > -1))
{
//adjust width if not locked
currentTool()->startAdjusting(WIDTH, 1);
return true;
}
if ((keyMod == Qt::ControlModifier) && (currentTool()->properties.feather > -1))
{
//adjust feather if not locked
currentTool()->startAdjusting(FEATHER, 1);
return true;
}
return false;
}

void ScribbleArea::showLayerNotVisibleWarning()
{
QMessageBox::warning(this, tr("Warning"),
Expand Down
2 changes: 0 additions & 2 deletions core_lib/src/interface/scribblearea.h
Expand Up @@ -131,8 +131,6 @@ public slots:
void updateToolCursor();
void paletteColorChanged(QColor);

bool isDoingAssistedToolAdjustment(Qt::KeyboardModifiers keyMod);

void showLayerNotVisibleWarning();


Expand Down
65 changes: 34 additions & 31 deletions core_lib/src/tool/basetool.cpp
Expand Up @@ -287,19 +287,32 @@ QPixmap BaseTool::quickSizeCursor(qreal scalingFac)
return cursorPixmap;
}

void BaseTool::startAdjusting(ToolPropertyType propertyType, qreal step)
bool BaseTool::startAdjusting(Qt::KeyboardModifiers modifiers, qreal step)
{
msIsAdjusting = true;
mAdjustmentStep = step;
if (propertyType == WIDTH)
if (mQuickSizingProperties.contains(modifiers))
{
msOriginalPropertyValue = properties.width;
}
else if (propertyType == FEATHER)
{
msOriginalPropertyValue = properties.feather;
switch (mQuickSizingProperties.value(modifiers)) {
case WIDTH:
msOriginalPropertyValue = properties.width;
break;
case FEATHER:
msOriginalPropertyValue = properties.feather;
break;
case TOLERANCE:
msOriginalPropertyValue = properties.tolerance;
break;
default:
qDebug() << "Unhandled quick sizing property for tool" << typeName();
Q_ASSERT(false);
return false;
}

msIsAdjusting = true;
mAdjustmentStep = step;
mScribbleArea->updateCanvasCursor();
return true;
}
mScribbleArea->updateCanvasCursor();
return false;
}

void BaseTool::stopAdjusting()
Expand All @@ -310,15 +323,11 @@ void BaseTool::stopAdjusting()
mEditor->getScribbleArea()->updateCanvasCursor();
}

void BaseTool::adjustCursor(Qt::KeyboardModifiers keyMod)
void BaseTool::adjustCursor(Qt::KeyboardModifiers modifiers)
{
ToolPropertyType propertyType;
propertyType = (keyMod & Qt::ControlModifier) ? FEATHER : WIDTH;

if (mQuickSizingProperties.contains(modifiers));
qreal inc = qPow(msOriginalPropertyValue * 100, 0.5);
qreal newValue = inc + getCurrentPoint().x();
int max = (propertyType == FEATHER) ? 200 : 200;
int min = (propertyType == FEATHER) ? 2 : 1;

if (newValue < 0)
{
Expand All @@ -331,27 +340,21 @@ void BaseTool::adjustCursor(Qt::KeyboardModifiers keyMod)
int tempValue = (int)(newValue / mAdjustmentStep); // + 0.5 ?
newValue = tempValue * mAdjustmentStep;
}
if (newValue < min) // can be optimized for size: min(200,max(0.2,newValueX))
{
newValue = min;
}
else if (newValue > max)
{
newValue = max;
}

switch (propertyType)
switch (mQuickSizingProperties.value(modifiers))
{
case WIDTH:
mEditor->tools()->setWidth(qBound(1., newValue, 200.));
break;
case FEATHER:
if ((type() == BRUSH) || (type() == ERASER) || (this->type() == SMUDGE))
{
mEditor->tools()->setFeather(newValue);
}
mEditor->tools()->setFeather(qBound(2., newValue, 200.));
break;
case WIDTH:
mEditor->tools()->setWidth(newValue);
case TOLERANCE:
mEditor->tools()->setTolerance(qBound(0., newValue, 100.));
break;
default:
qDebug() << "Unhandled quick sizing property for tool" << typeName();
Q_ASSERT(false);
break;
};
}
Expand Down
6 changes: 4 additions & 2 deletions core_lib/src/tool/basetool.h
Expand Up @@ -83,9 +83,9 @@ class BaseTool : public QObject
virtual bool keyReleaseEvent(QKeyEvent*) { return false; }

// dynamic cursor adjustment
virtual void startAdjusting(ToolPropertyType argSettingType, qreal argStep);
virtual bool startAdjusting(Qt::KeyboardModifiers modifiers, qreal argStep);
virtual void stopAdjusting();
virtual void adjustCursor(Qt::KeyboardModifiers keyMod);
virtual void adjustCursor(Qt::KeyboardModifiers modifiers);

virtual void clearToolData() {}
virtual void resetToDefault() {}
Expand Down Expand Up @@ -143,6 +143,8 @@ class BaseTool : public QObject
Editor* mEditor = nullptr;
ScribbleArea* mScribbleArea = nullptr;

QMap<Qt::KeyboardModifiers,ToolPropertyType> mQuickSizingProperties;

private:
StrokeManager* mStrokeManager = nullptr;
qreal mAdjustmentStep = 0.0f;
Expand Down
3 changes: 3 additions & 0 deletions core_lib/src/tool/brushtool.cpp
Expand Up @@ -66,6 +66,9 @@ void BrushTool::loadSettings()

if (properties.width <= 0) { setWidth(15); }
if (std::isnan(properties.feather)) { setFeather(15); }

mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
mQuickSizingProperties.insert(Qt::ControlModifier, FEATHER);
}

void BrushTool::resetToDefault()
Expand Down
14 changes: 14 additions & 0 deletions core_lib/src/tool/buckettool.cpp
Expand Up @@ -147,6 +147,20 @@ void BucketTool::pointerReleaseEvent(PointerEvent* event)
endStroke();
}

bool BucketTool::startAdjusting(Qt::KeyboardModifiers modifiers, qreal argStep)
{
mQuickSizingProperties.clear();
if (mEditor->layers()->currentLayer()->type() == Layer::VECTOR)
{
mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
}
else
{
mQuickSizingProperties.insert(Qt::ControlModifier, TOLERANCE);
}
return BaseTool::startAdjusting(modifiers, argStep);
}

void BucketTool::paintBitmap(Layer* layer)
{
Layer* targetLayer = layer; // by default
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/tool/buckettool.h
Expand Up @@ -37,6 +37,8 @@ class BucketTool : public StrokeTool
void pointerMoveEvent(PointerEvent*) override;
void pointerReleaseEvent(PointerEvent*) override;

bool startAdjusting(Qt::KeyboardModifiers modifiers, qreal argStep) override;

void setTolerance(const int tolerance) override;
void setWidth(const qreal width) override;

Expand Down
3 changes: 3 additions & 0 deletions core_lib/src/tool/erasertool.cpp
Expand Up @@ -62,6 +62,9 @@ void EraserTool::loadSettings()
properties.useAA = settings.value("eraserAA", 1).toInt();

if (properties.useFeather) { properties.useAA = -1; }

mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
mQuickSizingProperties.insert(Qt::ControlModifier, FEATHER);
}

void EraserTool::resetToDefault()
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/tool/penciltool.cpp
Expand Up @@ -56,6 +56,8 @@ void PencilTool::loadSettings()
properties.useFillContour = false;
// properties.invisibility = 1;
// properties.preserveAlpha = 0;

mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
}

void PencilTool::resetToDefault()
Expand Down
2 changes: 2 additions & 0 deletions core_lib/src/tool/pentool.cpp
Expand Up @@ -51,6 +51,8 @@ void PenTool::loadSettings()
properties.preserveAlpha = OFF;
properties.useAA = settings.value("penAA", true).toBool();
properties.stabilizerLevel = settings.value("penLineStabilization", StabilizationLevel::STRONG).toInt();

mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
}

void PenTool::resetToDefault()
Expand Down
3 changes: 3 additions & 0 deletions core_lib/src/tool/smudgetool.cpp
Expand Up @@ -51,6 +51,9 @@ void SmudgeTool::loadSettings()
properties.feather = settings.value("smudgeFeather", 48.0).toDouble();
properties.pressure = false;
properties.stabilizerLevel = -1;

mQuickSizingProperties.insert(Qt::ShiftModifier, WIDTH);
mQuickSizingProperties.insert(Qt::ControlModifier, FEATHER);
}

void SmudgeTool::resetToDefault()
Expand Down

0 comments on commit bfda42f

Please sign in to comment.