kis_tool_transform_config_widget.cpp 43.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  Copyright (c) 2013 Dmitry Kazakov <dimula73@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_tool_transform_config_widget.h"

21
#include <kis_icon.h>
22
#include "rotation_icons.h"
23
#include "kis_canvas2.h"
24
#include <KisSignalMapper.h>
25
#include "kis_liquify_properties.h"
26

27 28
#include "KisMainWindow.h"
#include "KisViewManager.h"
29 30
#include "kis_transform_utils.h"

31

32 33 34 35
template<typename T> inline T sign(T x) {
    return x > 0 ? 1 : x == (T)0 ? 0 : -1;
}

36 37 38
const int KisToolTransformConfigWidget::DEFAULT_POINTS_PER_LINE = 3;


39
KisToolTransformConfigWidget::KisToolTransformConfigWidget(TransformTransactionProperties *transaction, KisCanvas2 *canvas, bool workRecursively, QWidget *parent)
40
    : QWidget(parent),
41 42
      m_transaction(transaction),
      m_notificationsBlocked(0),
43 44
      m_uiSlotsBlocked(0),
      m_configChanged(false)
45 46
{
    setupUi(this);
47
    chkWorkRecursively->setIcon(KisIconUtils::loadIcon("krita_tool_transform_recursive"));
48 49 50 51
    flipXButton->setIcon(KisIconUtils::loadIcon("transform_icons_mirror_x"));
    flipYButton->setIcon(KisIconUtils::loadIcon("transform_icons_mirror_y"));
    rotateCWButton->setIcon(KisIconUtils::loadIcon("transform_icons_rotate_cw"));
    rotateCCWButton->setIcon(KisIconUtils::loadIcon("transform_icons_rotate_ccw"));
52

53 54
    chkWorkRecursively->setChecked(workRecursively);
    connect(chkWorkRecursively, SIGNAL(toggled(bool)), this, SIGNAL(sigRestartTransform()));
55 56 57 58 59 60 61 62 63 64 65 66

    // Granularity can only be specified in the power of 2's
    QStringList granularityValues{"4","8","16","32"};
    changeGranularity->addItems(granularityValues);
    changeGranularity->setCurrentIndex(1);
    granularityPreview->addItems(granularityValues);
    granularityPreview->setCurrentIndex(2);

    connect(changeGranularity,SIGNAL(currentIndexChanged(QString)),
            this,SLOT(slotGranularityChanged(QString)));
    connect(granularityPreview, SIGNAL(currentIndexChanged(QString)),
            this,SLOT(slotPreviewGranularityChanged(QString)));
67

68 69 70
    // Init Filter  combo
    cmbFilter->setIDList(KisFilterStrategyRegistry::instance()->listKeys());
    cmbFilter->setCurrent("Bicubic");
71
    cmbFilter->setToolTip(i18nc("@info:tooltip",
Boudewijn Rempt's avatar
Boudewijn Rempt committed
72 73
                                "<p>Select filtering mode:\n"
                                "<ul>"
74 75
                                "<li><b>Bilinear</b> for areas with uniform color to avoid artifacts</li>"
                                "<li><b>Bicubic</b> for smoother results</li>"
76
                                "<li><b>Lanczos3</b> for sharp results. May produce aerials.</li>"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
77
                                "</ul></p>"));
78 79
    connect(cmbFilter, SIGNAL(activated(KoID)),
            this, SLOT(slotFilterChanged(KoID)));
80 81

    // Init Warp Type combo
Scott Petrovic's avatar
Scott Petrovic committed
82 83 84 85
    cmbWarpType->insertItem(KisWarpTransformWorker::AFFINE_TRANSFORM,i18n("Default (Affine)"));
    cmbWarpType->insertItem(KisWarpTransformWorker::RIGID_TRANSFORM,i18n("Strong (Rigid)"));
    cmbWarpType->insertItem(KisWarpTransformWorker::SIMILITUDE_TRANSFORM,i18n("Strongest (Similitude)"));
    cmbWarpType->setCurrentIndex(KisWarpTransformWorker::AFFINE_TRANSFORM);
86
    connect(cmbWarpType, SIGNAL(currentIndexChanged(int)), this, SLOT(slotWarpTypeChanged(int)));
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    // Init Rotation Center buttons
    m_handleDir[0] = QPointF(1, 0);
    m_handleDir[1] = QPointF(1, -1);
    m_handleDir[2] = QPointF(0, -1);
    m_handleDir[3] = QPointF(-1, -1);
    m_handleDir[4] = QPointF(-1, 0);
    m_handleDir[5] = QPointF(-1, 1);
    m_handleDir[6] = QPointF(0, 1);
    m_handleDir[7] = QPointF(1, 1);
    m_handleDir[8] = QPointF(0, 0); // also add the center

    m_rotationCenterButtons = new QButtonGroup(0);
    // we set the ids to match m_handleDir
    m_rotationCenterButtons->addButton(middleRightButton, 0);
    m_rotationCenterButtons->addButton(topRightButton, 1);
    m_rotationCenterButtons->addButton(middleTopButton, 2);
    m_rotationCenterButtons->addButton(topLeftButton, 3);
    m_rotationCenterButtons->addButton(middleLeftButton, 4);
    m_rotationCenterButtons->addButton(bottomLeftButton, 5);
    m_rotationCenterButtons->addButton(middleBottomButton, 6);
    m_rotationCenterButtons->addButton(bottomRightButton, 7);
    m_rotationCenterButtons->addButton(centerButton, 8);

    QToolButton *nothingSelected = new QToolButton(0);
    nothingSelected->setCheckable(true);
    nothingSelected->setAutoExclusive(true);
    nothingSelected->hide(); // a convenient button for when no button is checked in the group
    m_rotationCenterButtons->addButton(nothingSelected, 9);

117 118

    // initialize values for free transform sliders
119 120 121 122 123 124
    shearXBox->setSuffix(QChar(Qt::Key_Percent));
    shearYBox->setSuffix(QChar(Qt::Key_Percent));
    shearXBox->setRange(-500, 500, 2);
    shearYBox->setRange(-500, 500, 2);
    shearXBox->setSingleStep(1);
    shearYBox->setSingleStep(1);
125 126 127 128
    shearXBox->setValue(0.0);
    shearYBox->setValue(0.0);


129 130
    translateXBox->setSuffix(i18n(" px"));
    translateYBox->setSuffix(i18n(" px"));
131 132 133
    translateXBox->setRange(-10000, 10000);
    translateYBox->setRange(-10000, 10000);

134

135 136
    scaleXBox->setRange(-10000, 10000);
    scaleYBox->setRange(-10000, 10000);
137 138 139
    scaleXBox->setValue(100.0);
    scaleYBox->setValue(100.0);

140
    m_scaleRatio = 1.0;
141 142 143 144

    aXBox->setSuffix(QChar(Qt::Key_degree));
    aYBox->setSuffix(QChar(Qt::Key_degree));
    aZBox->setSuffix(QChar(Qt::Key_degree));
145 146 147
    aXBox->setRange(0.0, 360.0, 2);
    aYBox->setRange(0.0, 360.0, 2);
    aZBox->setRange(0.0, 360.0, 2);
148 149 150
    aXBox->setValue(0.0);
    aYBox->setValue(0.0);
    aZBox->setValue(0.0);
151 152 153
    aXBox->setSingleStep(1.0);
    aYBox->setSingleStep(1.0);
    aZBox->setSingleStep(1.0);
154 155 156 157




158
    connect(m_rotationCenterButtons, SIGNAL(buttonPressed(int)), this, SLOT(slotRotationCenterChanged(int)));
159
    connect(btnTransformAroundPivotPoint, SIGNAL(clicked(bool)), this, SLOT(slotTransformAroundRotationCenter(bool)));
160

161
    // Init Free Transform Values
162 163
    connect(scaleXBox, SIGNAL(valueChanged(int)), this, SLOT(slotSetScaleX(int)));
    connect(scaleYBox, SIGNAL(valueChanged(int)), this, SLOT(slotSetScaleY(int)));
164 165
    connect(shearXBox, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetShearX(qreal)));
    connect(shearYBox, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetShearY(qreal)));
166 167
    connect(translateXBox, SIGNAL(valueChanged(int)), this, SLOT(slotSetTranslateX(int)));
    connect(translateYBox, SIGNAL(valueChanged(int)), this, SLOT(slotSetTranslateY(int)));
168 169 170
    connect(aXBox, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetAX(qreal)));
    connect(aYBox, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetAY(qreal)));
    connect(aZBox, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetAZ(qreal)));
171
    connect(aspectButton, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(slotSetKeepAspectRatio(bool)));
172 173 174 175
    connect(flipXButton, SIGNAL(clicked(bool)), this, SLOT(slotFlipX()));
    connect(flipYButton, SIGNAL(clicked(bool)), this, SLOT(slotFlipY()));
    connect(rotateCWButton, SIGNAL(clicked(bool)), this, SLOT(slotRotateCW()));
    connect(rotateCCWButton, SIGNAL(clicked(bool)), this, SLOT(slotRotateCCW()));
176

177 178 179 180 181 182 183 184
    // toggle visibility of different free buttons
    connect(freeMoveRadioButton, SIGNAL(clicked(bool)), SLOT(slotTransformAreaVisible(bool)));
    connect(freeRotationRadioButton, SIGNAL(clicked(bool)), SLOT(slotTransformAreaVisible(bool)));
    connect(freeScaleRadioButton, SIGNAL(clicked(bool)), SLOT(slotTransformAreaVisible(bool)));
    connect(freeShearRadioButton, SIGNAL(clicked(bool)), SLOT(slotTransformAreaVisible(bool)));

    // only first group for free transform
    rotationGroup->hide();
185
    moveGroup->show();
186 187 188 189 190
    scaleGroup->hide();
    shearGroup->hide();



Boudewijn Rempt's avatar
Boudewijn Rempt committed
191
    // Init Warp Transform Values
Scott Petrovic's avatar
Scott Petrovic committed
192
    alphaBox->setSingleStep(0.1);
193
    alphaBox->setRange(0, 10, 1);
Scott Petrovic's avatar
Scott Petrovic committed
194 195

    connect(alphaBox, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetWarpAlpha(qreal)));
Boudewijn Rempt's avatar
Boudewijn Rempt committed
196
    connect(densityBox, SIGNAL(valueChanged(int)), this, SLOT(slotSetWarpDensity(int)));
197 198 199 200 201 202

    connect(defaultRadioButton, SIGNAL(clicked(bool)), this, SLOT(slotWarpDefaultPointsButtonClicked(bool)));
    connect(customRadioButton, SIGNAL(clicked(bool)), this, SLOT(slotWarpCustomPointsButtonClicked(bool)));
    connect(lockUnlockPointsButton, SIGNAL(clicked()), this, SLOT(slotWarpLockPointsButtonClicked()));
    connect(resetPointsButton, SIGNAL(clicked()), this, SLOT(slotWarpResetPointsButtonClicked()));

203
    // Init Cage Transform Values
204 205 206
    cageTransformButtonGroup->setId(cageAddEditRadio, 0); // we need to set manually since Qt Designer generates negative by default
    cageTransformButtonGroup->setId(cageDeformRadio, 1);
    connect(cageTransformButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(slotCageOptionsChanged(int)));
207

208
    // Init Liquify Transform Values
209 210
    liquifySizeSlider->setRange(KisLiquifyProperties::minSize(),
                                KisLiquifyProperties::maxSize(), 2);
211
    liquifySizeSlider->setExponentRatio(4);
212
    liquifySizeSlider->setValue(60.0);
213 214 215 216
    connect(liquifySizeSlider, SIGNAL(valueChanged(qreal)), this, SLOT(liquifySizeChanged(qreal)));
    liquifySizeSlider->setToolTip(i18nc("@info:tooltip", "Size of the deformation brush"));

    liquifyAmountSlider->setRange(0.0, 1.0, 2);
217
    liquifyAmountSlider->setValue(0.05);
218 219 220
    connect(liquifyAmountSlider, SIGNAL(valueChanged(qreal)), this, SLOT(liquifyAmountChanged(qreal)));
    liquifyAmountSlider->setToolTip(i18nc("@info:tooltip", "Amount of the deformation you get"));

221 222 223 224 225
    liquifyFlowSlider->setRange(0.0, 1.0, 2);
    liquifyFlowSlider->setValue(1.0);
    connect(liquifyFlowSlider, SIGNAL(valueChanged(qreal)), this, SLOT(liquifyFlowChanged(qreal)));
    liquifyFlowSlider->setToolTip(i18nc("@info:tooltip", "When in non-buildup mode, shows how fast the deformation limit is reached."));

226 227
    buidupModeComboBox->setCurrentIndex(0); // set to build-up mode by default
    connect(buidupModeComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(liquifyBuildUpChanged(int)));
228
    buidupModeComboBox->setToolTip("<p>" + i18nc("@info:tooltip", "Switch between Build Up and Wash mode of painting. Build Up mode adds deformations one on top of the other without any limits. Wash mode gradually deforms the piece to the selected deformation level.") + "</p>");
229

230 231 232 233 234 235 236 237 238
    liquifySpacingSlider->setRange(0.0, 3.0, 2);
    liquifySizeSlider->setExponentRatio(3);
    liquifySpacingSlider->setSingleStep(0.01);
    liquifySpacingSlider->setValue(0.2);
    connect(liquifySpacingSlider, SIGNAL(valueChanged(qreal)), this, SLOT(liquifySpacingChanged(qreal)));
    liquifySpacingSlider->setToolTip(i18nc("@info:tooltip", "Space between two sequential applications of the deformation"));

    liquifySizePressureBox->setChecked(true);
    connect(liquifySizePressureBox, SIGNAL(toggled(bool)), this, SLOT(liquifySizePressureChanged(bool)));
239
    liquifySizePressureBox->setToolTip(i18nc("@info:tooltip", "Scale <b>Size</b> value according to current stylus pressure"));
240 241 242

    liquifyAmountPressureBox->setChecked(true);
    connect(liquifyAmountPressureBox, SIGNAL(toggled(bool)), this, SLOT(liquifyAmountPressureChanged(bool)));
243
    liquifyAmountPressureBox->setToolTip(i18nc("@info:tooltip", "Scale <b>Amount</b> value according to current stylus pressure"));
244 245 246

    liquifyReverseDirectionChk->setChecked(false);
    connect(liquifyReverseDirectionChk, SIGNAL(toggled(bool)), this, SLOT(liquifyReverseDirectionChanged(bool)));
Yuri Chornoivan's avatar
Yuri Chornoivan committed
247
    liquifyReverseDirectionChk->setToolTip(i18nc("@info:tooltip", "Reverse direction of the current deformation tool"));
248

249
    KisSignalMapper *liquifyModeMapper = new KisSignalMapper(this);
250 251 252 253 254
    connect(liquifyMove, SIGNAL(toggled(bool)), liquifyModeMapper, SLOT(map()));
    connect(liquifyScale, SIGNAL(toggled(bool)), liquifyModeMapper, SLOT(map()));
    connect(liquifyRotate, SIGNAL(toggled(bool)), liquifyModeMapper, SLOT(map()));
    connect(liquifyOffset, SIGNAL(toggled(bool)), liquifyModeMapper, SLOT(map()));
    connect(liquifyUndo, SIGNAL(toggled(bool)), liquifyModeMapper, SLOT(map()));
255 256 257 258 259
    liquifyModeMapper->setMapping(liquifyMove, (int)KisLiquifyProperties::MOVE);
    liquifyModeMapper->setMapping(liquifyScale, (int)KisLiquifyProperties::SCALE);
    liquifyModeMapper->setMapping(liquifyRotate, (int)KisLiquifyProperties::ROTATE);
    liquifyModeMapper->setMapping(liquifyOffset, (int)KisLiquifyProperties::OFFSET);
    liquifyModeMapper->setMapping(liquifyUndo, (int)KisLiquifyProperties::UNDO);
260 261
    connect(liquifyModeMapper, SIGNAL(mapped(int)), SLOT(slotLiquifyModeChanged(int)));

262 263 264 265 266
    liquifyMove->setToolTip(i18nc("@info:tooltip", "Move: drag the image along the brush stroke"));
    liquifyScale->setToolTip(i18nc("@info:tooltip", "Scale: grow/shrink image under cursor"));
    liquifyRotate->setToolTip(i18nc("@info:tooltip", "Rotate: twirl image under cursor"));
    liquifyOffset->setToolTip(i18nc("@info:tooltip", "Offset: shift the image to the right of the stroke direction"));
    liquifyUndo->setToolTip(i18nc("@info:tooltip", "Undo: erase actions of other tools"));
267

268
    // Connect all edit boxes to the Editing Finished signal
269 270
    connect(densityBox, SIGNAL(editingFinished()), this, SLOT(notifyEditingFinished()));

271

Scott Petrovic's avatar
Scott Petrovic committed
272

273 274 275
    // Connect other widget (not having editingFinished signal) to
    // the same slot. From Qt 4.6 onwards the sequence of the signal
    // delivery is definite.
276
    connect(cmbFilter, SIGNAL(activated(KoID)), this, SLOT(notifyEditingFinished()));
277 278 279
    connect(cmbWarpType, SIGNAL(currentIndexChanged(int)), this, SLOT(notifyEditingFinished()));
    connect(m_rotationCenterButtons, SIGNAL(buttonPressed(int)), this, SLOT(notifyEditingFinished()));
    connect(aspectButton, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(notifyEditingFinished()));
280

281 282 283
    connect(lockUnlockPointsButton, SIGNAL(clicked()), this, SLOT(notifyEditingFinished()));
    connect(resetPointsButton, SIGNAL(clicked()), this, SLOT(notifyEditingFinished()));

284 285 286
    connect(defaultRadioButton, SIGNAL(clicked(bool)), this, SLOT(notifyEditingFinished()));
    connect(customRadioButton, SIGNAL(clicked(bool)), this, SLOT(notifyEditingFinished()));

287 288 289 290
    // Liquify
    //
    // liquify brush options do not affect the image directly and are not
    // saved to undo, so we don't emit notifyEditingFinished() for them
291 292

    // Connect Apply/Reset buttons
293
    connect(buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(slotButtonBoxClicked(QAbstractButton*)));
294 295 296

    // Mode switch buttons
    connect(freeTransformButton, SIGNAL(clicked(bool)), this, SLOT(slotSetFreeTransformModeButtonClicked(bool)));
297 298
    connect(warpButton, SIGNAL(clicked(bool)), this, SLOT(slotSetWarpModeButtonClicked(bool)));
    connect(cageButton, SIGNAL(clicked(bool)), this, SLOT(slotSetCageModeButtonClicked(bool)));
299
    connect(perspectiveTransformButton, SIGNAL(clicked(bool)), this, SLOT(slotSetPerspectiveModeButtonClicked(bool)));
300
    connect(liquifyButton, SIGNAL(clicked(bool)), this, SLOT(slotSetLiquifyModeButtonClicked(bool)));
301 302

    tooBigLabelWidget->hide();
303

304
    connect(canvas->viewManager()->mainWindow(), SIGNAL(themeChanged()), SLOT(slotUpdateIcons()), Qt::UniqueConnection);
305 306 307 308 309
    slotUpdateIcons();
}

void KisToolTransformConfigWidget::slotUpdateIcons()
{
310 311 312 313 314 315 316 317 318 319 320
    freeTransformButton->setIcon(KisIconUtils::loadIcon("transform_icons_main"));
    warpButton->setIcon(KisIconUtils::loadIcon("transform_icons_warp"));
    cageButton->setIcon(KisIconUtils::loadIcon("transform_icons_cage"));
    perspectiveTransformButton->setIcon(KisIconUtils::loadIcon("transform_icons_perspective"));
    liquifyButton->setIcon(KisIconUtils::loadIcon("transform_icons_liquify_main"));

    liquifyMove->setIcon(KisIconUtils::loadIcon("transform_icons_liquify_move"));
    liquifyScale->setIcon(KisIconUtils::loadIcon("transform_icons_liquify_resize"));
    liquifyRotate->setIcon(KisIconUtils::loadIcon("transform_icons_liquify_rotate"));
    liquifyOffset->setIcon(KisIconUtils::loadIcon("transform_icons_liquify_offset"));
    liquifyUndo->setIcon(KisIconUtils::loadIcon("transform_icons_liquify_erase"));
321

322 323 324 325 326 327 328 329 330 331 332


    middleRightButton->setIcon(KisIconUtils::loadIcon("arrow-right"));
    topRightButton->setIcon(KisIconUtils::loadIcon("arrow-topright"));
    middleTopButton->setIcon(KisIconUtils::loadIcon("arrow-up"));
    topLeftButton->setIcon(KisIconUtils::loadIcon("arrow-topleft"));
    middleLeftButton->setIcon(KisIconUtils::loadIcon("arrow-left"));
    bottomLeftButton->setIcon(KisIconUtils::loadIcon("arrow-downleft"));
    middleBottomButton->setIcon(KisIconUtils::loadIcon("arrow-down"));
    bottomRightButton->setIcon(KisIconUtils::loadIcon("arrow-downright"));

333
    btnTransformAroundPivotPoint->setIcon(KisIconUtils::loadIcon("pivot-point"));
334

335

336
    // pressure icons
337 338
    liquifySizePressureBox->setIcon(KisIconUtils::loadIcon("transform_icons_penPressure"));
    liquifyAmountPressureBox->setIcon(KisIconUtils::loadIcon("transform_icons_penPressure"));
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

double KisToolTransformConfigWidget::radianToDegree(double rad)
{
    double piX2 = 2 * M_PI;

    if (rad < 0 || rad >= piX2) {
        rad = fmod(rad, piX2);
        if (rad < 0) {
            rad += piX2;
        }
    }

    return (rad * 360. / piX2);
}

355 356 357 358 359 360 361 362 363 364 365
double KisToolTransformConfigWidget::degreeToRadian(double degree)
{
    if (degree < 0. || degree >= 360.) {
        degree = fmod(degree, 360.);
        if (degree < 0)
            degree += 360.;
    }

    return (degree * M_PI / 180.);
}

366 367 368 369 370
void KisToolTransformConfigWidget::updateLiquifyControls()
{
    blockUiSlots();

    ToolTransformArgs *config = m_transaction->currentConfig();
371
    KisLiquifyProperties *props =
372 373
        config->liquifyProperties();

374 375
    const bool useWashMode = props->useWashMode();

376 377
    liquifySizeSlider->setValue(props->size());
    liquifyAmountSlider->setValue(props->amount());
378
    liquifyFlowSlider->setValue(props->flow());
379
    buidupModeComboBox->setCurrentIndex(useWashMode);
380

381 382 383 384 385 386
    liquifySpacingSlider->setValue(props->spacing());
    liquifySizePressureBox->setChecked(props->sizeHasPressure());
    liquifyAmountPressureBox->setChecked(props->amountHasPressure());
    liquifyReverseDirectionChk->setChecked(props->reverseDirection());


387 388
    KisLiquifyProperties::LiquifyMode mode =
        static_cast<KisLiquifyProperties::LiquifyMode>(props->mode());
389 390

    bool canInverseDirection =
391
        mode != KisLiquifyProperties::UNDO;
392

393 394
    bool canUseWashMode = mode != KisLiquifyProperties::UNDO;

395
    liquifyReverseDirectionChk->setEnabled(canInverseDirection);
396
    liquifyFlowSlider->setEnabled(canUseWashMode && useWashMode);
397
    buidupModeComboBox->setEnabled(canUseWashMode);
398 399 400

    const qreal maxAmount = canUseWashMode ? 5.0 : 1.0;
    liquifyAmountSlider->setRange(0.0, maxAmount, 2);
401 402 403 404 405 406 407 408 409 410

    unblockUiSlots();
}

void KisToolTransformConfigWidget::slotLiquifyModeChanged(int value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();

411
    KisLiquifyProperties *props =
412 413
        config->liquifyProperties();

414 415
    KisLiquifyProperties::LiquifyMode mode =
        static_cast<KisLiquifyProperties::LiquifyMode>(value);
416

417 418 419 420
    if (mode == props->mode()) return;

    props->setMode(mode);
    props->loadMode();
421 422 423 424 425 426 427 428 429 430 431

    updateLiquifyControls();

    notifyConfigChanged();
}

void KisToolTransformConfigWidget::liquifySizeChanged(qreal value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
432
    KisLiquifyProperties *props =
433 434 435 436 437 438 439 440 441 442 443
        config->liquifyProperties();

    props->setSize(value);
    notifyConfigChanged();
}

void KisToolTransformConfigWidget::liquifyAmountChanged(qreal value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
444
    KisLiquifyProperties *props =
445 446 447 448 449 450
        config->liquifyProperties();

    props->setAmount(value);
    notifyConfigChanged();
}

451 452 453 454 455 456 457 458 459 460 461 462
void KisToolTransformConfigWidget::liquifyFlowChanged(qreal value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
    KisLiquifyProperties *props =
        config->liquifyProperties();

    props->setFlow(value);
    notifyConfigChanged();
}

463
void KisToolTransformConfigWidget::liquifyBuildUpChanged(int value)
464 465 466 467 468 469 470
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
    KisLiquifyProperties *props =
        config->liquifyProperties();

471
    props->setUseWashMode(value); // 0 == build up mode / 1 == wash mode
472

473 474 475 476 477 478
    notifyConfigChanged();

    // we need to enable/disable flow slider
    updateLiquifyControls();
}

479 480 481 482 483
void KisToolTransformConfigWidget::liquifySpacingChanged(qreal value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
484
    KisLiquifyProperties *props =
485 486 487 488 489 490 491 492 493 494 495
        config->liquifyProperties();

    props->setSpacing(value);
    notifyConfigChanged();
}

void KisToolTransformConfigWidget::liquifySizePressureChanged(bool value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
496
    KisLiquifyProperties *props =
497 498 499 500 501 502 503 504 505 506 507
        config->liquifyProperties();

    props->setSizeHasPressure(value);
    notifyConfigChanged();
}

void KisToolTransformConfigWidget::liquifyAmountPressureChanged(bool value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
508
    KisLiquifyProperties *props =
509 510 511 512 513 514 515 516 517 518 519
        config->liquifyProperties();

    props->setAmountHasPressure(value);
    notifyConfigChanged();
}

void KisToolTransformConfigWidget::liquifyReverseDirectionChanged(bool value)
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
520
    KisLiquifyProperties *props =
521 522 523 524 525 526
        config->liquifyProperties();

    props->setReverseDirection(value);
    notifyConfigChanged();
}

527 528
void KisToolTransformConfigWidget::updateConfig(const ToolTransformArgs &config)
{
529 530
    blockUiSlots();

531 532 533
    if (config.mode() == ToolTransformArgs::FREE_TRANSFORM ||
        config.mode() == ToolTransformArgs::PERSPECTIVE_4POINT) {

534 535
        quickTransformGroup->setEnabled(config.mode() == ToolTransformArgs::FREE_TRANSFORM);

536
        stackedWidget->setCurrentIndex(0);
537 538 539

        bool freeTransformIsActive = config.mode() == ToolTransformArgs::FREE_TRANSFORM;

540 541
        if (freeTransformIsActive)
        {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
542
            freeTransformButton->setChecked(true);
543 544 545
        }
        else
        {
546 547
            perspectiveTransformButton->setChecked(true);
        }
548 549 550 551

        aXBox->setEnabled(freeTransformIsActive);
        aYBox->setEnabled(freeTransformIsActive);
        aZBox->setEnabled(freeTransformIsActive);
552
        freeRotationRadioButton->setEnabled(freeTransformIsActive);
553

554 555
        scaleXBox->setValue(config.scaleX() * 100.);
        scaleYBox->setValue(config.scaleY() * 100.);
556 557
        shearXBox->setValue(config.shearX() * 100.);
        shearYBox->setValue(config.shearY() * 100.);
558

559 560 561
        const QPointF anchorPoint = config.originalCenter() + config.rotationCenterOffset();
        const KisTransformUtils::MatricesPack m(config);
        const QPointF anchorPointView = m.finalTransform().map(anchorPoint);
562

563 564
        translateXBox->setValue(anchorPointView.x());
        translateYBox->setValue(anchorPointView.y());
565

566 567 568
        aXBox->setValue(radianToDegree(config.aX()));
        aYBox->setValue(radianToDegree(config.aY()));
        aZBox->setValue(radianToDegree(config.aZ()));
569
        aspectButton->setKeepAspectRatio(config.keepAspectRatio());
570
        cmbFilter->setCurrent(config.filterId());
571 572 573 574 575 576 577 578 579 580 581 582 583

        QPointF pt = m_transaction->currentConfig()->rotationCenterOffset();
        pt.rx() /= m_transaction->originalHalfWidth();
        pt.ry() /= m_transaction->originalHalfHeight();

        for (int i = 0; i < 9; i++) {
            if (qFuzzyCompare(m_handleDir[i].x(), pt.x()) &&
                qFuzzyCompare(m_handleDir[i].y(), pt.y())) {

                m_rotationCenterButtons->button(i)->setChecked(true);
                break;
            }
        }
584

585
        btnTransformAroundPivotPoint->setChecked(config.transformAroundRotationCenter());
586

587 588
    } else if (config.mode() == ToolTransformArgs::WARP) {

589 590 591 592
        stackedWidget->setCurrentIndex(1);
        warpButton->setChecked(true);

        if (config.defaultPoints()) {
593
            densityBox->setValue(std::sqrt(config.numPoints()));
594 595 596 597 598
        }

        cmbWarpType->setCurrentIndex((int)config.warpType());
        defaultRadioButton->setChecked(config.defaultPoints());
        customRadioButton->setChecked(!config.defaultPoints());
Scott Petrovic's avatar
Scott Petrovic committed
599
        densityBox->setEnabled(config.defaultPoints());
600 601
        customWarpWidget->setEnabled(!config.defaultPoints());

602
        updateLockPointsButtonCaption();
603 604 605

    } else if (config.mode() == ToolTransformArgs::CAGE) {

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
        // default UI options
        resetUIOptions();

        // we need at least 3 point before we can start actively deforming
        if (config.origPoints().size() >= 3)
        {
            cageTransformDirections->setText(i18n("Switch between editing and deforming cage"));
            cageAddEditRadio->setVisible(true);
            cageDeformRadio->setVisible(true);

            // update to correct radio button
            if (config.isEditingTransformPoints())
                cageAddEditRadio->setChecked(true);
            else
                 cageDeformRadio->setChecked(true);

622 623 624
            changeGranularity->setCurrentIndex(log2(config.pixelPrecision()) - 2);
            granularityPreview->setCurrentIndex(log2(config.previewPixelPrecision()) - 2);

625
        }
626

627 628 629 630 631
    } else if (config.mode() == ToolTransformArgs::LIQUIFY) {

        stackedWidget->setCurrentIndex(3);
        liquifyButton->setChecked(true);

632
        const KisLiquifyProperties *props =
633 634
            config.liquifyProperties();

635 636
        switch (props->mode()) {
        case KisLiquifyProperties::MOVE:
637 638
            liquifyMove->setChecked(true);
            break;
639
        case KisLiquifyProperties::SCALE:
640 641
            liquifyScale->setChecked(true);
            break;
642
        case KisLiquifyProperties::ROTATE:
643 644
            liquifyRotate->setChecked(true);
            break;
645
        case KisLiquifyProperties::OFFSET:
646 647
            liquifyOffset->setChecked(true);
            break;
648
        case KisLiquifyProperties::UNDO:
649 650
            liquifyUndo->setChecked(true);
            break;
651 652
        case KisLiquifyProperties::N_MODES:
            qFatal("Unsupported mode");
653 654 655
        }

        updateLiquifyControls();
656
    }
657 658

    unblockUiSlots();
659 660
}

661 662
void KisToolTransformConfigWidget::updateLockPointsButtonCaption()
{
663 664 665
    ToolTransformArgs *config = m_transaction->currentConfig();

    if (config->isEditingTransformPoints()) {
666 667 668 669 670 671
        lockUnlockPointsButton->setText(i18n("Lock Points"));
    } else {
        lockUnlockPointsButton->setText(i18n("Unlock Points"));
    }
}

672 673 674 675 676 677 678 679 680 681 682 683
void KisToolTransformConfigWidget::setApplyResetDisabled(bool disabled)
{
    QAbstractButton *applyButton = buttonBox->button(QDialogButtonBox::Apply);
    QAbstractButton *resetButton = buttonBox->button(QDialogButtonBox::Reset);

    Q_ASSERT(applyButton);
    Q_ASSERT(resetButton);

    applyButton->setDisabled(disabled);
    resetButton->setDisabled(disabled);
}

684 685 686 687 688 689 690 691 692 693
void KisToolTransformConfigWidget::resetRotationCenterButtons()
{
    int checkedId = m_rotationCenterButtons->checkedId();

    if (checkedId >= 0 && checkedId <= 8) {
        // uncheck the current checked button
        m_rotationCenterButtons->button(9)->setChecked(true);
    }
}

694 695
bool KisToolTransformConfigWidget::workRecursively() const
{
696
    return chkWorkRecursively->isChecked();
697 698
}

699 700 701 702 703
void KisToolTransformConfigWidget::setTooBigLabelVisible(bool value)
{
    tooBigLabelWidget->setVisible(value);
}

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
void KisToolTransformConfigWidget::blockNotifications()
{
    m_notificationsBlocked++;
}

void KisToolTransformConfigWidget::unblockNotifications()
{
    m_notificationsBlocked--;
}

void KisToolTransformConfigWidget::notifyConfigChanged()
{
    if (!m_notificationsBlocked) {
        emit sigConfigChanged();
    }
719
    m_configChanged = true;
720 721 722 723 724 725 726 727 728 729 730 731
}

void KisToolTransformConfigWidget::blockUiSlots()
{
    m_uiSlotsBlocked++;
}

void KisToolTransformConfigWidget::unblockUiSlots()
{
    m_uiSlotsBlocked--;
}

732 733 734 735 736 737 738 739
void KisToolTransformConfigWidget::notifyEditingFinished()
{
    if (m_uiSlotsBlocked || m_notificationsBlocked || !m_configChanged) return;

    emit sigEditingFinished();
    m_configChanged = false;
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

void KisToolTransformConfigWidget::resetUIOptions()
{
    // reset tool states since we are done (probably can encapsulate this later)
    ToolTransformArgs *config = m_transaction->currentConfig();
    if (config->mode() == ToolTransformArgs::CAGE)
    {
        cageAddEditRadio->setVisible(false);
        cageAddEditRadio->setChecked(true);
        cageDeformRadio->setVisible(false);
        cageTransformDirections->setText(i18n("Create 3 points on the canvas to begin"));

        // ensure we are on the right options view
        stackedWidget->setCurrentIndex(2);
    }



}

760 761 762 763 764
void KisToolTransformConfigWidget::slotButtonBoxClicked(QAbstractButton *button)
{
    QAbstractButton *applyButton = buttonBox->button(QDialogButtonBox::Apply);
    QAbstractButton *resetButton = buttonBox->button(QDialogButtonBox::Reset);

765 766
    resetUIOptions();

767 768 769 770 771 772
    if (button == applyButton) {
        emit sigApplyTransform();
    }
    else if (button == resetButton) {
        emit sigResetTransform();
    }
773

774 775
}

776
void KisToolTransformConfigWidget::slotSetFreeTransformModeButtonClicked(bool value)
777
{
778 779
    if (!value) return;

780 781
    lblTransformType->setText(freeTransformButton->toolTip());

782 783 784 785 786
    ToolTransformArgs *config = m_transaction->currentConfig();
    config->setMode(ToolTransformArgs::FREE_TRANSFORM);
    emit sigResetTransform();
}

787
void KisToolTransformConfigWidget::slotSetWarpModeButtonClicked(bool value)
788
{
789 790
    if (!value) return;

791 792
    lblTransformType->setText(warpButton->toolTip());

793 794
    ToolTransformArgs *config = m_transaction->currentConfig();
    config->setMode(ToolTransformArgs::WARP);
795
    config->setWarpCalculation(KisWarpTransformWorker::WarpCalculation::GRID);
796 797 798
    emit sigResetTransform();
}

799
void KisToolTransformConfigWidget::slotSetCageModeButtonClicked(bool value)
800
{
801 802
    if (!value) return;

803 804
    lblTransformType->setText(cageButton->toolTip());

805 806 807 808 809
    ToolTransformArgs *config = m_transaction->currentConfig();
    config->setMode(ToolTransformArgs::CAGE);
    emit sigResetTransform();
}

810
void KisToolTransformConfigWidget::slotSetLiquifyModeButtonClicked(bool value)
811
{
812 813
    if (!value) return;

814 815
    lblTransformType->setText(liquifyButton->toolTip());

816 817 818 819 820 821 822 823 824
    ToolTransformArgs *config = m_transaction->currentConfig();
    config->setMode(ToolTransformArgs::LIQUIFY);
    emit sigResetTransform();
}

void KisToolTransformConfigWidget::slotSetPerspectiveModeButtonClicked(bool value)
{
    if (!value) return;

825 826
    lblTransformType->setText(perspectiveTransformButton->toolTip());

827 828 829 830 831
    ToolTransformArgs *config = m_transaction->currentConfig();
    config->setMode(ToolTransformArgs::PERSPECTIVE_4POINT);
    emit sigResetTransform();
}

832 833 834 835 836 837 838
void KisToolTransformConfigWidget::slotFilterChanged(const KoID &filterId)
{
    ToolTransformArgs *config = m_transaction->currentConfig();
    config->setFilterId(filterId.id());
    notifyConfigChanged();
}

839 840
void KisToolTransformConfigWidget::slotRotationCenterChanged(int index)
{
841 842
    if (m_uiSlotsBlocked) return;

843
    if (index >= 0 && index <= 8) {
844
        ToolTransformArgs *config = m_transaction->currentConfig();
845 846 847 848

        double i = m_handleDir[index].x();
        double j = m_handleDir[index].y();

849 850 851 852
        config->setRotationCenterOffset(QPointF(i * m_transaction->originalHalfWidth(),
                                                j * m_transaction->originalHalfHeight()));

        notifyConfigChanged();
853
        updateConfig(*config);
854 855
    }
}
856

857
void KisToolTransformConfigWidget::slotTransformAroundRotationCenter(bool value)
858 859
{
    if (m_uiSlotsBlocked) return;
860

861
    ToolTransformArgs *config = m_transaction->currentConfig();
862
    config->setTransformAroundRotationCenter(value);
863 864
    notifyConfigChanged();
    notifyEditingFinished();
865 866
}

867
void KisToolTransformConfigWidget::slotSetScaleX(int value)
868 869 870 871
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
872 873

    {
874
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
875 876
        config->setScaleX(value / 100.);
    }
877

878
    if (config->keepAspectRatio()) {
879 880

        blockNotifications();
881 882 883 884
         int calculatedValue = int( value/ m_scaleRatio );

        scaleYBox->blockSignals(true);
        scaleYBox->setValue(calculatedValue);
885 886 887 888
        {
            KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
            config->setScaleY(calculatedValue / 100.);
        }
889 890
        scaleYBox->blockSignals(false);

891 892 893 894
        unblockNotifications();
    }

    notifyConfigChanged();
895
    notifyEditingFinished();
896 897
}

898
void KisToolTransformConfigWidget::slotSetScaleY(int value)
899 900 901 902
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
903 904

    {
905
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
906 907
        config->setScaleY(value / 100.);
    }
908

909
    if (config->keepAspectRatio()) {
910
        blockNotifications();
911 912 913
        int calculatedValue = int(m_scaleRatio * value);
        scaleXBox->blockSignals(true);
        scaleXBox->setValue(calculatedValue);
914 915 916 917
        {
            KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
            config->setScaleX(calculatedValue / 100.);
        }
918
        scaleXBox->blockSignals(false);
919 920 921 922
        unblockNotifications();
    }

    notifyConfigChanged();
923
    notifyEditingFinished();
924 925
}

926
void KisToolTransformConfigWidget::slotSetShearX(qreal value)
927 928 929 930
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
931 932

    {
933
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
934
        config->setShearX((double)value / 100.);
935 936
    }

937
    notifyConfigChanged();
938
    notifyEditingFinished();
939 940
}

941
void KisToolTransformConfigWidget::slotSetShearY(qreal value)
942 943 944 945
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
946 947

    {
948
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
949
        config->setShearY((double)value / 100.);
950 951 952
    }


953
    notifyConfigChanged();
954
    notifyEditingFinished();
955 956
}

957
void KisToolTransformConfigWidget::slotSetTranslateX(int value)
958 959 960 961
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
962 963 964 965 966 967 968

    const QPointF anchorPoint = config->originalCenter() + config->rotationCenterOffset();
    const KisTransformUtils::MatricesPack m(*config);

    const QPointF anchorPointView = m.finalTransform().map(anchorPoint);
    const QPointF newAnchorPointView(value, anchorPointView.y());
    config->setTransformedCenter(config->transformedCenter() + newAnchorPointView - anchorPointView);
969
    translateXBox->setValue(value);
970 971 972
    notifyConfigChanged();
}

973
void KisToolTransformConfigWidget::slotSetTranslateY(int value)
974 975 976 977
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
978 979 980 981 982 983 984

    const QPointF anchorPoint = config->originalCenter() + config->rotationCenterOffset();
    const KisTransformUtils::MatricesPack m(*config);

    const QPointF anchorPointView = m.finalTransform().map(anchorPoint);
    const QPointF newAnchorPointView(anchorPointView.x(), value);
    config->setTransformedCenter(config->transformedCenter() + newAnchorPointView - anchorPointView);
985
    translateYBox->setValue(value);
986 987 988
    notifyConfigChanged();
}

989
void KisToolTransformConfigWidget::slotSetAX(qreal value)
990 991 992 993
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
994
    {
995
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
996 997
        config->setAX(degreeToRadian((double)value));
    }
998
    notifyConfigChanged();
999
    notifyEditingFinished();
1000 1001
}

1002
void KisToolTransformConfigWidget::slotSetAY(qreal value)
1003 1004 1005 1006
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
1007 1008

    {
1009
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
1010 1011 1012
        config->setAY(degreeToRadian((double)value));
    }

1013
    notifyConfigChanged();
1014
    notifyEditingFinished();
1015 1016
}

1017
void KisToolTransformConfigWidget::slotSetAZ(qreal value)
1018 1019 1020 1021
{
    if (m_uiSlotsBlocked) return;

    ToolTransformArgs *config = m_transaction->currentConfig();
1022 1023

    {
1024
        KisTransformUtils::AnchorHolder keeper(config->transformAroundRotationCenter(), config);
1025 1026 1027
        config->setAZ(degreeToRadian((double)value));
    }

1028
    notifyConfigChanged();
1029
    notifyEditingFinished();