All Classes Namespaces Functions Variables Enumerations Properties Pages
bucketoptionswidget.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 #include "bucketoptionswidget.h"
18 #include "ui_bucketoptionswidget.h"
19 
20 #include <QDebug>
21 
22 #include "spinslider.h"
23 #include "pencilsettings.h"
24 
25 #include "layer.h"
26 #include "editor.h"
27 #include "layermanager.h"
28 #include "toolmanager.h"
29 #include "util.h"
30 
31 BucketOptionsWidget::BucketOptionsWidget(Editor* editor, QWidget* parent) :
32  QWidget(parent),
33  ui(new Ui::BucketOptionsWidget),
34  mEditor(editor)
35 {
36  ui->setupUi(this);
37 
38  ui->colorToleranceSlider->init(tr("Color tolerance"), SpinSlider::GROWTH_TYPE::LINEAR, SpinSlider::VALUE_TYPE::INTEGER, 0, MAX_COLOR_TOLERANCE);
39  ui->expandSlider->init(tr("Expand fill"), SpinSlider::GROWTH_TYPE::LINEAR, SpinSlider::VALUE_TYPE::INTEGER, 0, MAX_EXPAND);
40  ui->strokeThicknessSlider->init(tr("Stroke thickness"), SpinSlider::GROWTH_TYPE::LOG, SpinSlider::VALUE_TYPE::FLOAT, 1, MAX_STROKE_THICKNESS);
41 
42  QSettings settings(PENCIL2D, PENCIL2D);
43 
44  ui->colorToleranceCheckbox->setChecked(settings.value(SETTING_BUCKET_TOLERANCE_ON, true).toBool());
45  ui->expandCheckbox->setChecked(settings.value(SETTING_BUCKET_FILL_EXPAND_ON, true).toBool());
46 
47  ui->expandSpinBox->setMaximum(MAX_EXPAND);
48  ui->strokeThicknessSpinBox->setMaximum(MAX_STROKE_THICKNESS);
49  ui->colorToleranceSpinbox->setMaximum(MAX_COLOR_TOLERANCE);
50  ui->strokeThicknessSpinBox->setMinimum(1);
51 
52  ui->fillToLayerComboBox->addItem(tr("Current layer"), 0);
53  ui->fillToLayerComboBox->addItem(tr("Layer below"), 1);
54  ui->fillToLayerComboBox->setToolTip(tr("Fill to the current layer or the layer below"));
55 
56  ui->referenceLayerComboBox->addItem(tr("Current layer", "Reference Layer Options"), 0);
57  ui->referenceLayerComboBox->addItem(tr("All layers", "Reference Layer Options"), 1);
58  ui->referenceLayerComboBox->setToolTip(tr("Refers to the layer that used to flood fill from"));
59 
60  ui->blendModeComboBox->addItem(tr("Overlay", "Blend Mode dropdown option"), 0);
61  ui->blendModeComboBox->addItem(tr("Replace", "Blend Mode dropdown option"), 1);
62  ui->blendModeComboBox->addItem(tr("Behind", "Blend Mode dropdown option"), 2);
63  ui->blendModeComboBox->setToolTip(tr("Defines how the fill will behave when the new color is not opaque"));
64 
65  connect(ui->colorToleranceSlider, &SpinSlider::valueChanged, mEditor->tools(), &ToolManager::setTolerance);
66  connect(ui->colorToleranceSpinbox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), mEditor->tools(), &ToolManager::setTolerance);
67  connect(ui->colorToleranceCheckbox, &QCheckBox::toggled, mEditor->tools(), &ToolManager::setBucketColorToleranceEnabled);
68 
69  connect(ui->expandSlider, &SpinSlider::valueChanged, mEditor->tools(), &ToolManager::setBucketFillExpand);
70  connect(ui->expandSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), mEditor->tools(), &ToolManager::setBucketFillExpand);
71  connect(ui->expandCheckbox, &QCheckBox::toggled, mEditor->tools(), &ToolManager::setBucketFillExpandEnabled);
72 
73  connect(ui->strokeThicknessSlider, &SpinSlider::valueChanged, mEditor->tools(), &ToolManager::setWidth);
74  connect(ui->strokeThicknessSpinBox, static_cast<void (QDoubleSpinBox::*)(qreal)>(&QDoubleSpinBox::valueChanged), mEditor->tools(), &ToolManager::setWidth);
75 
76  connect(mEditor->tools(), &ToolManager::toolPropertyChanged, this, &BucketOptionsWidget::onPropertyChanged);
77  connect(mEditor->layers(), &LayerManager::currentLayerChanged, this, &BucketOptionsWidget::onLayerChanged);
78 
79  connect(ui->fillToLayerComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), mEditor->tools(), &ToolManager::setBucketFillToLayer);
80  connect(ui->referenceLayerComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), mEditor->tools(), &ToolManager::setBucketFillReferenceMode);
81  connect(ui->blendModeComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), mEditor->tools(), &ToolManager::setFillMode);
82 
83  ui->expandSlider->setValue(settings.value(SETTING_BUCKET_FILL_EXPAND, 2).toInt());
84  ui->expandSpinBox->setValue(settings.value(SETTING_BUCKET_FILL_EXPAND, 2).toInt());
85  ui->colorToleranceSlider->setValue(settings.value(SETTING_BUCKET_TOLERANCE, 50).toInt());
86  ui->colorToleranceSpinbox->setValue(settings.value(SETTING_BUCKET_TOLERANCE, 50).toInt());
87  ui->fillToLayerComboBox->setCurrentIndex(settings.value(SETTING_BUCKET_FILL_TO_LAYER_MODE, 0).toInt());
88  ui->referenceLayerComboBox->setCurrentIndex(settings.value(SETTING_BUCKET_FILL_REFERENCE_MODE, 0).toInt());
89  ui->blendModeComboBox->setCurrentIndex(settings.value(SETTING_FILL_MODE, 0).toInt());
90 
91  clearFocusOnFinished(ui->colorToleranceSpinbox);
92  clearFocusOnFinished(ui->expandSpinBox);
93 
94  updatePropertyVisibility();
95 }
96 
97 BucketOptionsWidget::~BucketOptionsWidget()
98 {
99  delete ui;
100 }
101 
102 void BucketOptionsWidget::updatePropertyVisibility()
103 {
104  Layer* layer = mEditor->layers()->currentLayer();
105 
106  Q_ASSERT(layer != nullptr);
107 
108  switch (layer->type()) {
109  case Layer::VECTOR:
110  ui->strokeThicknessSlider->show();
111  ui->strokeThicknessSpinBox->show();
112 
113  ui->fillToLayerComboBox->hide();
114  ui->fillToDescLabel->hide();
115  ui->colorToleranceCheckbox->hide();
116  ui->colorToleranceSlider->hide();
117  ui->colorToleranceSpinbox->hide();
118  ui->expandCheckbox->hide();
119  ui->expandSlider->hide();
120  ui->expandSpinBox->hide();
121  ui->referenceLayerComboBox->hide();
122  ui->referenceLayerDescLabel->hide();
123  ui->blendModeComboBox->hide();
124  ui->blendModeLabel->hide();
125  break;
126  case Layer::BITMAP:
127  ui->strokeThicknessSlider->hide();
128  ui->strokeThicknessSpinBox->hide();
129 
130  ui->fillToLayerComboBox->show();
131  ui->fillToDescLabel->show();
132  ui->colorToleranceCheckbox->show();
133  ui->colorToleranceSlider->show();
134  ui->colorToleranceSpinbox->show();
135  ui->expandCheckbox->show();
136  ui->expandSlider->show();
137  ui->expandSpinBox->show();
138  ui->referenceLayerComboBox->show();
139  ui->referenceLayerDescLabel->show();
140  ui->blendModeComboBox->show();
141  ui->blendModeLabel->show();
142  break;
143  default:
144  ui->strokeThicknessSlider->hide();
145  ui->strokeThicknessSpinBox->hide();
146  ui->fillToLayerComboBox->hide();
147  ui->fillToDescLabel->hide();
148  ui->colorToleranceCheckbox->hide();
149  ui->colorToleranceSlider->hide();
150  ui->colorToleranceSpinbox->hide();
151  ui->expandCheckbox->hide();
152  ui->expandSlider->hide();
153  ui->expandSpinBox->hide();
154  ui->referenceLayerComboBox->hide();
155  ui->referenceLayerDescLabel->hide();
156  ui->blendModeComboBox->hide();
157  ui->blendModeLabel->hide();
158  }
159 }
160 
161 void BucketOptionsWidget::onPropertyChanged(ToolType, ToolPropertyType propertyType)
162 {
163  const Properties& p = mEditor->tools()->currentTool()->properties;
164  switch (propertyType)
165  {
166  case ToolPropertyType::TOLERANCE:
167  setColorTolerance(static_cast<int>(p.tolerance)); break;
168  case ToolPropertyType::USETOLERANCE:
169  setColorToleranceEnabled(p.toleranceEnabled); break;
170  case ToolPropertyType::WIDTH:
171  setStrokeWidth(static_cast<int>(p.width)); break;
172  case ToolPropertyType::BUCKETFILLEXPAND:
173  setFillExpand(static_cast<int>(p.bucketFillExpand)); break;
174  case ToolPropertyType::USEBUCKETFILLEXPAND:
175  setFillExpandEnabled(p.bucketFillExpandEnabled); break;
176  case ToolPropertyType::BUCKETFILLLAYERMODE:
177  setFillToLayerMode(p.bucketFillToLayerMode); break;
178  case ToolPropertyType::BUCKETFILLLAYERREFERENCEMODE:
179  setFillReferenceMode(p.bucketFillReferenceMode); break;
180  case ToolPropertyType::FILL_MODE:
181  setFillMode(p.fillMode); break;
182  default:
183  break;
184  }
185 }
186 
187 void BucketOptionsWidget::onLayerChanged(int)
188 {
189  updatePropertyVisibility();
190 }
191 
192 void BucketOptionsWidget::setColorTolerance(int tolerance)
193 {
194  QSignalBlocker b(ui->colorToleranceSlider);
195  ui->colorToleranceSlider->setValue(tolerance);
196 
197  QSignalBlocker b2(ui->colorToleranceSpinbox);
198  ui->colorToleranceSpinbox->setValue(tolerance);
199 }
200 
201 void BucketOptionsWidget::setColorToleranceEnabled(bool enabled)
202 {
203  QSignalBlocker b(ui->colorToleranceCheckbox);
204  ui->colorToleranceCheckbox->setChecked(enabled);
205 }
206 
207 void BucketOptionsWidget::setFillMode(int mode)
208 {
209  QSignalBlocker b(ui->blendModeComboBox);
210  ui->blendModeComboBox->setCurrentIndex(mode);
211 }
212 
213 void BucketOptionsWidget::setFillExpandEnabled(bool enabled)
214 {
215  QSignalBlocker b(ui->expandCheckbox);
216  ui->expandCheckbox->setChecked(enabled);
217 }
218 
219 void BucketOptionsWidget::setFillExpand(int value)
220 {
221  QSignalBlocker b(ui->expandSlider);
222  ui->expandSlider->setValue(value);
223 
224  QSignalBlocker b2(ui->expandSpinBox);
225  ui->expandSpinBox->setValue(value);
226 }
227 
228 void BucketOptionsWidget::setFillToLayerMode(int layerMode)
229 {
230  QSignalBlocker b(ui->fillToLayerComboBox);
231  ui->fillToLayerComboBox->setCurrentIndex(layerMode);
232 }
233 
234 void BucketOptionsWidget::setFillReferenceMode(int referenceMode)
235 {
236  QSignalBlocker b(ui->referenceLayerComboBox);
237  ui->referenceLayerComboBox->setCurrentIndex(referenceMode);
238 }
239 
240 void BucketOptionsWidget::setStrokeWidth(qreal value)
241 {
242  QSignalBlocker b(ui->strokeThicknessSlider);
243  ui->strokeThicknessSlider->setValue(value);
244 
245  QSignalBlocker b2(ui->strokeThicknessSpinBox);
246  ui->strokeThicknessSpinBox->setValue(value);
247 }
void setupUi(QWidget *widget)
QString tr(const char *sourceText, const char *disambiguation, int n)
void valueChanged(int i)
void valueChanged(double d)
Definition: layer.h:38
void toggled(bool checked)
Definition: editor.h:55
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void currentIndexChanged(int index)