All Classes Namespaces Functions Variables Enumerations Properties Pages
onionskinwidget.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2012-2020 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 
17 #include "onionskinwidget.h"
18 #include "ui_onionskin.h"
19 
20 #include "preferencemanager.h"
21 #include "editor.h"
22 #include "flowlayout.h"
23 #include "util.h"
24 
25 OnionSkinWidget::OnionSkinWidget(QWidget *parent) :
26  BaseDockWidget(parent),
27  ui(new Ui::OnionSkin)
28 {
29  ui->setupUi(this);
30 
31  clearFocusOnFinished(ui->onionPrevFramesNumBox);
32  clearFocusOnFinished(ui->onionNextFramesNumBox);
33  clearFocusOnFinished(ui->onionMinOpacityBox);
34  clearFocusOnFinished(ui->onionMaxOpacityBox);
35 }
36 
37 OnionSkinWidget::~OnionSkinWidget()
38 {
39  delete ui;
40 }
41 
42 void OnionSkinWidget::initUI()
43 {
44  updateUI();
45  makeConnections();
46 
47  // Change the horizontal layout in the Distributed Opacity group box to a
48  // flow layout to reduce the minimum width
49  FlowLayout *opacityLayout = new FlowLayout;
50  opacityLayout->setAlignment(Qt::AlignHCenter);
51  opacityLayout->setContentsMargins(0, 6, 0, 6);
52  ui->opacityGroup->layout()->removeWidget(ui->minOpacityGroup);
53  ui->opacityGroup->layout()->removeWidget(ui->maxOpacityGroup);
54  opacityLayout->addWidget(ui->minOpacityGroup);
55  opacityLayout->addWidget(ui->maxOpacityGroup);
56  delete ui->opacityGroup->layout();
57  ui->opacityGroup->setLayout(opacityLayout);
58 
59 #ifdef __APPLE__
60  // Mac only style. ToolButtons are naturally borderless on Win/Linux.
61  QString stylesheet =
62  "QToolButton { border: 0px; } "
63  "QToolButton:pressed{ border: 1px solid #FFADAD; border-radius: 2px; background-color: #D5D5D5; }"
64  "QToolButton:checked{ border: 1px solid #ADADAD; border-radius: 2px; background-color: #D5D5D5; }";
65  setStyleSheet(this->styleSheet().append(stylesheet));
66 #endif
67 }
68 
69 void OnionSkinWidget::makeConnections()
70 {
71  auto spinBoxChanged = static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged);
72  connect(ui->onionMaxOpacityBox, spinBoxChanged, this, &OnionSkinWidget::onionMaxOpacityChange);
73  connect(ui->onionMinOpacityBox, spinBoxChanged, this, &OnionSkinWidget::onionMinOpacityChange);
74  connect(ui->onionPrevFramesNumBox, spinBoxChanged, this, &OnionSkinWidget::onionPrevFramesNumChange);
75  connect(ui->onionNextFramesNumBox, spinBoxChanged, this, &OnionSkinWidget::onionNextFramesNumChange);
76 
77  connect(ui->onionPrevButton, &QToolButton::clicked, this, &OnionSkinWidget::onionPrevButtonClicked);
78  connect(ui->onionNextButton, &QToolButton::clicked, this, &OnionSkinWidget::onionNextButtonClicked);
79  connect(ui->onionBlueButton, &QToolButton::clicked, this, &OnionSkinWidget::onionBlueButtonClicked);
80  connect(ui->onionRedButton, &QToolButton::clicked, this, &OnionSkinWidget::onionRedButtonClicked);
81 
82  connect(ui->onionSkinMode, &QCheckBox::stateChanged, this, &OnionSkinWidget::onionSkinModeChange);
83  connect(ui->onionWhilePlayback, &QCheckBox::stateChanged, this, &OnionSkinWidget::playbackStateChanged);
84 
85  PreferenceManager* prefs = editor()->preference();
86  connect(prefs, &PreferenceManager::optionChanged, this, &OnionSkinWidget::updateUI);
87 
88 }
89 
90 void OnionSkinWidget::updateUI()
91 {
92  PreferenceManager* prefs = editor()->preference();
93 
94  QSignalBlocker b1(ui->onionPrevButton);
95  ui->onionPrevButton->setChecked(prefs->isOn(SETTING::PREV_ONION));
96 
97  QSignalBlocker b2(ui->onionNextButton);
98  ui->onionNextButton->setChecked(prefs->isOn(SETTING::NEXT_ONION));
99 
100  QSignalBlocker b3(ui->onionBlueButton);
101  ui->onionBlueButton->setChecked(prefs->isOn(SETTING::ONION_BLUE));
102 
103  ui->onionRedButton->setEnabled(ui->onionPrevButton->isChecked());
104  ui->onionBlueButton->setEnabled(ui->onionNextButton->isChecked());
105 
106  QSignalBlocker b4(ui->onionRedButton);
107  ui->onionRedButton->setChecked(prefs->isOn(SETTING::ONION_RED));
108 
109  ui->onionMaxOpacityBox->setValue(prefs->getInt(SETTING::ONION_MAX_OPACITY));
110  ui->onionMinOpacityBox->setValue(prefs->getInt(SETTING::ONION_MIN_OPACITY));
111  ui->onionPrevFramesNumBox->setValue(prefs->getInt(SETTING::ONION_PREV_FRAMES_NUM));
112  ui->onionNextFramesNumBox->setValue(prefs->getInt(SETTING::ONION_NEXT_FRAMES_NUM));
113 
114  QSignalBlocker b5(ui->onionSkinMode);
115  ui->onionSkinMode->setChecked(prefs->getString(SETTING::ONION_TYPE) == "absolute");
116 
117  QSignalBlocker b6(ui->onionWhilePlayback);
118  ui->onionWhilePlayback->setChecked(prefs->getInt(SETTING::ONION_WHILE_PLAYBACK));
119 
120 }
121 
122 void OnionSkinWidget::onionPrevButtonClicked(bool isOn)
123 {
124  PreferenceManager* prefs = editor()->preference();
125  prefs->set(SETTING::PREV_ONION, isOn);
126 }
127 
128 void OnionSkinWidget::onionNextButtonClicked(bool isOn)
129 {
130  PreferenceManager* prefs = editor()->preference();
131  prefs->set(SETTING::NEXT_ONION, isOn);
132 }
133 
134 void OnionSkinWidget::onionRedButtonClicked(bool isOn)
135 {
136  PreferenceManager* prefs = editor()->preference();
137  prefs->set(SETTING::ONION_RED, isOn);
138 }
139 
140 void OnionSkinWidget::onionBlueButtonClicked(bool isOn)
141 {
142  PreferenceManager* prefs = editor()->preference();
143  prefs->set(SETTING::ONION_BLUE, isOn);
144 }
145 
146 void OnionSkinWidget::onionMaxOpacityChange(int value)
147 {
148  PreferenceManager* prefs = editor()->preference();
149  prefs->set(SETTING::ONION_MAX_OPACITY, value);
150 }
151 
152 void OnionSkinWidget::onionMinOpacityChange(int value)
153 {
154  PreferenceManager* prefs = editor()->preference();
155  prefs->set(SETTING::ONION_MIN_OPACITY, value);
156 }
157 
158 void OnionSkinWidget::onionPrevFramesNumChange(int value)
159 {
160  PreferenceManager* prefs = editor()->preference();
161  prefs->set(SETTING::ONION_PREV_FRAMES_NUM, value);
162 }
163 
164 void OnionSkinWidget::onionNextFramesNumChange(int value)
165 {
166  PreferenceManager* prefs = editor()->preference();
167  prefs->set(SETTING::ONION_NEXT_FRAMES_NUM, value);
168 }
169 
170 void OnionSkinWidget::onionSkinModeChange(int value)
171 {
172  PreferenceManager* prefs = editor()->preference();
173  if (value == Qt::Checked)
174  {
175  prefs->set(SETTING::ONION_TYPE, QString("absolute"));
176  }
177  else
178  {
179  prefs->set(SETTING::ONION_TYPE, QString("relative"));
180  }
181 }
182 
183 void OnionSkinWidget::playbackStateChanged(int value)
184 {
185  PreferenceManager* prefs = editor()->preference();
186  prefs->set(SETTING::ONION_WHILE_PLAYBACK, value);
187 }
void setStyleSheet(const QString &styleSheet)
void setContentsMargins(int left, int top, int right, int bottom)
AlignHCenter
void valueChanged(int i)
void clicked(bool checked)
void addWidget(QWidget *w)
bool setAlignment(QWidget *w, Qt::Alignment alignment)
void stateChanged(int state)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)