All Classes Namespaces Functions Variables Enumerations Properties Pages
tooloptionwidget.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 "tooloptionwidget.h"
18 #include "ui_tooloptions.h"
19 
20 #include <QSettings>
21 #include <QDebug>
22 
23 #include "bucketoptionswidget.h"
24 #include "spinslider.h"
25 #include "editor.h"
26 #include "util.h"
27 #include "layer.h"
28 #include "layermanager.h"
29 #include "toolmanager.h"
30 
31 ToolOptionWidget::ToolOptionWidget(QWidget* parent) : BaseDockWidget(parent)
32 {
33  setWindowTitle(tr("Options", "Window title of tool option panel like pen width, feather etc.."));
34 
35  QWidget* innerWidget = new QWidget;
36  setWidget(innerWidget);
37  ui = new Ui::ToolOptions;
38  ui->setupUi(innerWidget);
39 }
40 
41 ToolOptionWidget::~ToolOptionWidget()
42 {
43  delete ui;
44 }
45 
46 void ToolOptionWidget::initUI()
47 {
48  mBucketOptionsWidget = new BucketOptionsWidget(editor(), this);
49  ui->horizontalLayout_2->addWidget(mBucketOptionsWidget);
50 
51  QSettings settings(PENCIL2D, PENCIL2D);
52 
53  ui->sizeSlider->init(tr("Width"), SpinSlider::EXPONENT, SpinSlider::INTEGER, 1, 200);
54  ui->sizeSlider->setValue(settings.value("brushWidth", "3").toDouble());
55  ui->brushSpinBox->setValue(settings.value("brushWidth", "3").toDouble());
56 
57  ui->featherSlider->init(tr("Feather"), SpinSlider::LOG, SpinSlider::INTEGER, 1, 99);
58  ui->featherSlider->setValue(settings.value("brushFeather", "5").toDouble());
59  ui->featherSpinBox->setValue(settings.value("brushFeather", "5").toDouble());
60 }
61 
62 void ToolOptionWidget::updateUI()
63 {
64  BaseTool* currentTool = editor()->tools()->currentTool();
65  Q_ASSERT(currentTool);
66 
67  disableAllOptions();
68 
69  setVisibility(currentTool);
70 
71  const Properties& p = currentTool->properties;
72 
73  setPenWidth(p.width);
74  setPenFeather(p.feather);
75  setUseFeather(p.useFeather);
76  setPressure(p.pressure);
77  setPenInvisibility(p.invisibility);
78  setPreserveAlpha(p.preserveAlpha);
79  setVectorMergeEnabled(p.vectorMergeEnabled);
80  setAA(p.useAA);
81  setStabilizerLevel(p.stabilizerLevel);
82  setFillContour(p.useFillContour);
83  setShowSelectionInfo(p.showSelectionInfo);
84 }
85 
86 void ToolOptionWidget::createUI()
87 {}
88 
89 void ToolOptionWidget::makeConnectionToEditor(Editor* editor)
90 {
91  auto toolManager = editor->tools();
92 
93  connect(ui->useBezierBox, &QCheckBox::clicked, toolManager, &ToolManager::setBezier);
94  connect(ui->usePressureBox, &QCheckBox::clicked, toolManager, &ToolManager::setPressure);
95  connect(ui->makeInvisibleBox, &QCheckBox::clicked, toolManager, &ToolManager::setInvisibility);
96  connect(ui->preserveAlphaBox, &QCheckBox::clicked, toolManager, &ToolManager::setPreserveAlpha);
97 
98  connect(ui->sizeSlider, &SpinSlider::valueChanged, toolManager, &ToolManager::setWidth);
99  connect(ui->featherSlider, &SpinSlider::valueChanged, toolManager, &ToolManager::setFeather);
100 
101  auto spinboxValueChanged = static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged);
102  connect(ui->brushSpinBox, spinboxValueChanged, toolManager, &ToolManager::setWidth);
103  clearFocusOnFinished(ui->brushSpinBox);
104  connect(ui->featherSpinBox, spinboxValueChanged, toolManager, &ToolManager::setFeather);
105  clearFocusOnFinished(ui->featherSpinBox);
106 
107  connect(ui->useFeatherBox, &QCheckBox::clicked, toolManager, &ToolManager::setUseFeather);
108 
109  connect(ui->vectorMergeBox, &QCheckBox::clicked, toolManager, &ToolManager::setVectorMergeEnabled);
110  connect(ui->useAABox, &QCheckBox::clicked, toolManager, &ToolManager::setAA);
111 
112  connect(ui->fillMode, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), toolManager, &ToolManager::setFillMode);
113 
114  connect(ui->inpolLevelsCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), toolManager, &ToolManager::setStabilizerLevel);
115 
116  connect(ui->fillContourBox, &QCheckBox::clicked, toolManager, &ToolManager::setUseFillContour);
117 
118  connect(ui->showInfoBox, &QCheckBox::clicked, toolManager, &ToolManager::setShowSelectionInfo);
119 
120  connect(toolManager, &ToolManager::toolChanged, this, &ToolOptionWidget::onToolChanged);
121  connect(toolManager, &ToolManager::toolPropertyChanged, this, &ToolOptionWidget::onToolPropertyChanged);
122 }
123 
124 void ToolOptionWidget::onToolPropertyChanged(ToolType, ToolPropertyType ePropertyType)
125 {
126  const Properties& p = editor()->tools()->currentTool()->properties;
127 
128  switch (ePropertyType)
129  {
130  case WIDTH: setPenWidth(p.width); break;
131  case FEATHER: setPenFeather(p.feather); break;
132  case USEFEATHER: setUseFeather(p.useFeather); break;
133  case PRESSURE: setPressure(p.pressure); break;
134  case INVISIBILITY: setPenInvisibility(p.invisibility); break;
135  case PRESERVEALPHA: setPreserveAlpha(p.preserveAlpha); break;
136  case VECTORMERGE: setVectorMergeEnabled(p.vectorMergeEnabled); break;
137  case ANTI_ALIASING: setAA(p.useAA); break;
138  case STABILIZATION: setStabilizerLevel(p.stabilizerLevel); break;
139  case FILLCONTOUR: setFillContour(p.useFillContour); break;
140  case SHOWSELECTIONINFO: setShowSelectionInfo(p.showSelectionInfo); break;
141  case BEZIER: setBezier(p.bezier_state); break;
142  case TOLERANCE: break;
143  case USETOLERANCE: break;
144  case BUCKETFILLEXPAND: break;
145  case USEBUCKETFILLEXPAND: break;
146  case BUCKETFILLLAYERMODE: break;
147  case BUCKETFILLLAYERREFERENCEMODE: break;
148  case FILL_MODE: break;
149  default:
150  Q_ASSERT(false);
151  break;
152  }
153 }
154 
155 void ToolOptionWidget::setVisibility(BaseTool* tool)
156 {
157  Q_ASSERT(mBucketOptionsWidget);
158  if (tool->type() == BUCKET)
159  {
160  disableAllOptions();
161  mBucketOptionsWidget->setHidden(false);
162  return;
163  }
164  else
165  {
166  mBucketOptionsWidget->setHidden(true);
167  }
168 
169  ui->sizeSlider->setVisible(tool->isPropertyEnabled(WIDTH));
170  ui->brushSpinBox->setVisible(tool->isPropertyEnabled(WIDTH));
171  ui->featherSlider->setVisible(tool->isPropertyEnabled(FEATHER));
172  ui->featherSpinBox->setVisible(tool->isPropertyEnabled(FEATHER));
173  ui->useFeatherBox->setVisible(tool->isPropertyEnabled(USEFEATHER));
174  ui->useBezierBox->setVisible(tool->isPropertyEnabled(BEZIER));
175  ui->usePressureBox->setVisible(tool->isPropertyEnabled(PRESSURE));
176  ui->makeInvisibleBox->setVisible(tool->isPropertyEnabled(INVISIBILITY));
177  ui->preserveAlphaBox->setVisible(tool->isPropertyEnabled(PRESERVEALPHA));
178  ui->useAABox->setVisible(tool->isPropertyEnabled(ANTI_ALIASING));
179  ui->stabilizerLabel->setVisible(tool->isPropertyEnabled(STABILIZATION));
180  ui->inpolLevelsCombo->setVisible(tool->isPropertyEnabled(STABILIZATION));
181  ui->fillContourBox->setVisible(tool->isPropertyEnabled(FILLCONTOUR));
182  ui->showInfoBox->setVisible(tool->isPropertyEnabled(SHOWSELECTIONINFO));
183 
184  auto currentLayerType = editor()->layers()->currentLayer()->type();
185  auto propertyType = editor()->tools()->currentTool()->type();
186 
187  if (currentLayerType == Layer::VECTOR)
188  {
189  switch (propertyType)
190  {
191  case SMUDGE:
192  ui->sizeSlider->setVisible(false);
193  ui->brushSpinBox->setVisible(false);
194  ui->usePressureBox->setVisible(false);
195  ui->featherSlider->setVisible(false);
196  ui->featherSpinBox->setVisible(false);
197  ui->useFeatherBox->setVisible(false);
198  break;
199  case PENCIL:
200  ui->sizeSlider->setVisible(false);
201  ui->brushSpinBox->setVisible(false);
202  ui->usePressureBox->setVisible(false);
203  break;
204  default:
205  ui->sizeSlider->setLabel(tr("Width"));
206  ui->useAABox->setVisible(false);
207  break;
208  }
209  }
210  else
211  {
212  switch (propertyType)
213  {
214  case PENCIL:
215  ui->fillContourBox->setVisible(false);
216  break;
217  case BUCKET:
218  ui->brushSpinBox->setVisible(false);
219  ui->sizeSlider->setVisible(false);
220  break;
221  case SELECT:
222  case MOVE:
223  ui->sizeSlider->setVisible(false);
224  ui->brushSpinBox->setVisible(false);
225  ui->usePressureBox->setVisible(false);
226  ui->featherSlider->setVisible(false);
227  ui->featherSpinBox->setVisible(false);
228  ui->useFeatherBox->setVisible(false);
229  break;
230  default:
231  ui->makeInvisibleBox->setVisible(false);
232  break;
233  }
234  }
235 }
236 
237 void ToolOptionWidget::onToolChanged(ToolType)
238 {
239  updateUI();
240 }
241 
242 void ToolOptionWidget::setPenWidth(qreal width)
243 {
244  QSignalBlocker b(ui->sizeSlider);
245  ui->sizeSlider->setEnabled(true);
246  ui->sizeSlider->setValue(width);
247 
248  QSignalBlocker b2(ui->brushSpinBox);
249  ui->brushSpinBox->setEnabled(true);
250  ui->brushSpinBox->setValue(width);
251 }
252 
253 void ToolOptionWidget::setPenFeather(qreal featherValue)
254 {
255  QSignalBlocker b(ui->featherSlider);
256  ui->featherSlider->setEnabled(true);
257  ui->featherSlider->setValue(featherValue);
258 
259  QSignalBlocker b2(ui->featherSpinBox);
260  ui->featherSpinBox->setEnabled(true);
261  ui->featherSpinBox->setValue(featherValue);
262 }
263 
264 void ToolOptionWidget::setUseFeather(bool useFeather)
265 {
266  QSignalBlocker b(ui->useFeatherBox);
267  ui->useFeatherBox->setEnabled(true);
268  ui->useFeatherBox->setChecked(useFeather);
269 }
270 
271 void ToolOptionWidget::setPenInvisibility(int x)
272 {
273  QSignalBlocker b(ui->makeInvisibleBox);
274  ui->makeInvisibleBox->setEnabled(true);
275  ui->makeInvisibleBox->setChecked(x > 0);
276 }
277 
278 void ToolOptionWidget::setPressure(int x)
279 {
280  QSignalBlocker b(ui->usePressureBox);
281  ui->usePressureBox->setEnabled(true);
282  ui->usePressureBox->setChecked(x > 0);
283 }
284 
285 void ToolOptionWidget::setPreserveAlpha(int x)
286 {
287  QSignalBlocker b(ui->preserveAlphaBox);
288  ui->preserveAlphaBox->setEnabled(true);
289  ui->preserveAlphaBox->setChecked(x > 0);
290 }
291 
292 void ToolOptionWidget::setVectorMergeEnabled(int x)
293 {
294  QSignalBlocker b(ui->vectorMergeBox);
295  ui->vectorMergeBox->setEnabled(true);
296  ui->vectorMergeBox->setChecked(x > 0);
297 }
298 
299 void ToolOptionWidget::setAA(int x)
300 {
301  QSignalBlocker b(ui->useAABox);
302  ui->useAABox->setEnabled(true);
303  ui->useAABox->setVisible(false);
304 
305  auto layerType = editor()->layers()->currentLayer()->type();
306 
307  if (layerType == Layer::BITMAP)
308  {
309  if (x == -1)
310  {
311  ui->useAABox->setEnabled(false);
312  ui->useAABox->setVisible(false);
313  }
314  else
315  {
316  ui->useAABox->setVisible(true);
317  }
318  ui->useAABox->setChecked(x > 0);
319  }
320 }
321 
322 void ToolOptionWidget::setStabilizerLevel(int x)
323 {
324  ui->inpolLevelsCombo->setCurrentIndex(qBound(0, x, ui->inpolLevelsCombo->count() - 1));
325 }
326 
327 void ToolOptionWidget::setFillContour(int useFill)
328 {
329  QSignalBlocker b(ui->fillContourBox);
330  ui->fillContourBox->setEnabled(true);
331  ui->fillContourBox->setChecked(useFill > 0);
332 }
333 
334 void ToolOptionWidget::setBezier(bool useBezier)
335 {
336  QSignalBlocker b(ui->useBezierBox);
337  ui->useBezierBox->setChecked(useBezier);
338 }
339 
340 void ToolOptionWidget::setShowSelectionInfo(bool showSelectionInfo)
341 {
342  QSignalBlocker b(ui->showInfoBox);
343  ui->showInfoBox->setChecked(showSelectionInfo);
344 }
345 
346 void ToolOptionWidget::disableAllOptions()
347 {
348  ui->sizeSlider->hide();
349  ui->brushSpinBox->hide();
350  ui->featherSlider->hide();
351  ui->featherSpinBox->hide();
352  ui->useFeatherBox->hide();
353  ui->useBezierBox->hide();
354  ui->usePressureBox->hide();
355  ui->makeInvisibleBox->hide();
356  ui->preserveAlphaBox->hide();
357  ui->vectorMergeBox->hide();
358  ui->useAABox->hide();
359  ui->fillModeGroup->hide();
360  ui->inpolLevelsCombo->hide();
361  ui->fillContourBox->hide();
362  ui->showInfoBox->hide();
363  ui->stabilizerLabel->hide();
364 }
QString tr(const char *sourceText, const char *disambiguation, int n)
void valueChanged(double d)
void clicked(bool checked)
void activated(int index)
void setWidget(QWidget *widget)
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
Definition: editor.h:55
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setHidden(bool hidden)