Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • app
  • src
addtransparencytopaperdialog.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2020 David Lamhauge
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License
8as published by the Free Software Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15*/
16
17#include "addtransparencytopaperdialog.h"
18#include "ui_addtransparencytopaperdialog.h"
19
20#include <QGraphicsPixmapItem>
21#include <QProgressDialog>
22#include <QPushButton>
23
24#include "editor.h"
25#include "layermanager.h"
26#include "selectionmanager.h"
27#include "layerbitmap.h"
28#include "bitmapimage.h"
29
30
31AddTransparencyToPaperDialog::AddTransparencyToPaperDialog(QWidget *parent) :
32 QDialog(parent),
33 ui(new Ui::AddTransparencyToPaperDialog)
34{
35 ui->setupUi(this);
36
37 connect(ui->sb_threshold, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &AddTransparencyToPaperDialog::thresholdSpinboxChanged);
38 connect(ui->sliderThreshold, &QSlider::valueChanged, this, &AddTransparencyToPaperDialog::thresholdSliderChanged);
39 connect(ui->cb_Red, &QCheckBox::stateChanged, this, &AddTransparencyToPaperDialog::updateDrawing);
40 connect(ui->cb_Green, &QCheckBox::stateChanged, this, &AddTransparencyToPaperDialog::updateDrawing);
41 connect(ui->cb_Blue, &QCheckBox::stateChanged, this, &AddTransparencyToPaperDialog::updateDrawing);
42 connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &AddTransparencyToPaperDialog::buttonClicked);
43 connect(ui->testTransparencyCheckbox, &QCheckBox::stateChanged, this, &AddTransparencyToPaperDialog::checkerStateChanged);
44 connect(ui->zoomSlider, &QSlider::valueChanged, this, &AddTransparencyToPaperDialog::zoomChanged);
45}
46
47AddTransparencyToPaperDialog::~AddTransparencyToPaperDialog()
48{
49 delete ui;
50}
51
52void AddTransparencyToPaperDialog::setCore(Editor *editor)
53{
54 mEditor = editor;
55}
56
57void AddTransparencyToPaperDialog::initUI()
58{
59 if (mEditor->layers()->currentLayer()->type() != Layer::BITMAP)
60 this->setEnabled(false);
61 loadDrawing(mEditor->currentFrame());
62 connect(mEditor->layers(), &LayerManager::currentLayerChanged, this, &AddTransparencyToPaperDialog::layerChanged);
63 connect(mEditor, &Editor::scrubbed, this, &AddTransparencyToPaperDialog::updateDrawing);
64
65 scene.setBackgroundBrush(Qt::white);
66 ui->preview->setScene(&scene);
67 ui->preview->show();
68
69 if (!mBitmap.bounds().isValid()) {
70 ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
71 }
72}
73
74void AddTransparencyToPaperDialog::thresholdSpinboxChanged(int value)
75{
76 mThreshold = value;
77 ui->sliderThreshold->setValue(value);
78 updateDrawing();
79}
80
81void AddTransparencyToPaperDialog::thresholdSliderChanged(int value)
82{
83 mThreshold = value;
84 ui->sb_threshold->setValue(value);
85 updateDrawing();
86}
87
88void AddTransparencyToPaperDialog::checkerStateChanged(bool state)
89{
90 if (state) {
91 scene.setBackgroundBrush(QBrush(QImage(":/background/checkerboard.png")));
92 } else {
93 scene.setBackgroundBrush(Qt::white);
94 }
95}
96
97void AddTransparencyToPaperDialog::zoomChanged(int zoomLevel)
98{
99 mZoomLevel = zoomLevel;
100 updatePreview();
101}
102
103void AddTransparencyToPaperDialog::buttonClicked(QAbstractButton* button)
104{
105 switch (ui->buttonBox->buttonRole(button)) {
106 case QDialogButtonBox::ApplyRole:
107 accept();
108 return;
109 case QDialogButtonBox::RejectRole:
110 reject();
111 return;
112 default:
113 Q_UNREACHABLE();
114 }
115}
116
117void AddTransparencyToPaperDialog::resizeEvent(QResizeEvent*)
118{
119 updatePreview();
120}
121
122void AddTransparencyToPaperDialog::updatePreview()
123{
124 QImage loadedImage = *mBitmap.image();
125
126 QSize previewSize = ui->preview->size()*mZoomLevel;
127 QSize size = mBitmap.size().scaled(previewSize, Qt::KeepAspectRatioByExpanding);
128 mPixmapFromImage = QPixmap(size);
129 mPixmapFromImage.fill(Qt::transparent);
130
131 QPainter painter(&mPixmapFromImage);
132
133 painter.drawImage(QRect(QPoint(0,0),QSize(size)), loadedImage, loadedImage.rect());
134 mPreviewImageItem->setPixmap(mPixmapFromImage);
135
136 scene.setSceneRect(QRect(QPoint(), previewSize));
137}
138
139void AddTransparencyToPaperDialog::loadDrawing(int frame)
140{
141 if (mEditor->layers()->currentLayer()->type() != Layer::BITMAP) { return; }
142
143 LayerBitmap* layer = static_cast<LayerBitmap*>(mEditor->layers()->currentLayer());
144
145 if (!layer->keyExists(frame))
146 {
147 if (!layer->keyExistsWhichCovers(frame))
148 frame = layer->getPreviousKeyFramePosition(frame);
149 else
150 frame = layer->getNextKeyFramePosition(frame);
151 }
152
153 ui->labShowingFrame->setText(tr("Previewing Frame %1").arg(QString::number(frame)));
154
155 BitmapImage* currentImage = layer->getBitmapImageAtFrame(frame);
156
157 if (!currentImage) { return; }
158
159 mBitmap = currentImage->copy();
160
161 mBitmap = *mBitmap.scanToTransparent(&mBitmap,
162 mThreshold,
163 ui->cb_Red->isChecked(),
164 ui->cb_Green->isChecked(),
165 ui->cb_Blue->isChecked());
166
167 if (mPreviewImageItem == nullptr) {
168 mPreviewImageItem = scene.addPixmap(mPixmapFromImage);
169 } else {
170 mPreviewImageItem->setPixmap(mPixmapFromImage);
171 }
172
173 ui->buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
174
175 updatePreview();
176}
177
178void AddTransparencyToPaperDialog::updateDrawing()
179{
180 loadDrawing(mEditor->currentFrame());
181}
182
183void AddTransparencyToPaperDialog::layerChanged(int index)
184{
185 if (mEditor->layers()->getLayer(index)->type() == Layer::BITMAP)
186 {
187 this->setEnabled(true);
188 updateDrawing();
189 }
190 else
191 {
192 this->setEnabled(false);
193 }
194}
195
196void AddTransparencyToPaperDialog::traceScannedDrawings()
197{
198 if (mEditor->layers()->currentLayer()->type() != Layer::BITMAP) { return; }
199
200 LayerBitmap* layer = static_cast<LayerBitmap*>(mEditor->layers()->currentLayer());
201 BitmapImage* img = new BitmapImage();
202 const bool somethingSelected = mEditor->select()->somethingSelected();
203 const QRect selectionRect = mEditor->select()->mySelectionRect().toRect();
204
205 if (ui->rbCurrentKeyframe->isChecked())
206 {
207 int frame = mEditor->currentFrame();
208 if (!layer->keyExists(frame))
209 {
210 if (!layer->keyExistsWhichCovers(frame))
211 frame = layer->getPreviousKeyFramePosition(frame);
212 else
213 frame = layer->getNextKeyFramePosition(frame);
214 }
215 mEditor->scrubTo(frame);
216
217 if (somethingSelected)
218 {
219 BitmapImage selection = layer->getBitmapImageAtFrame(frame)->copy(selectionRect);
220 layer->removeKeyFrame(frame);
221 layer->addNewKeyFrameAt(frame);
222 layer->getBitmapImageAtFrame(frame)->paste(&selection);
223 }
224 img = layer->getBitmapImageAtFrame(frame);
225 img = img->scanToTransparent(img,
226 mThreshold,
227 ui->cb_Red->isChecked(),
228 ui->cb_Green->isChecked(),
229 ui->cb_Blue->isChecked());
230 img->modification();
231 }
232 else
233 {
234 QProgressDialog* mProgress = new QProgressDialog(tr("Tracing scanned drawings..."), tr("Abort"), 0, 100, this);
235 mProgress->setWindowModality(Qt::WindowModal);
236 mProgress->show();
237 mProgress->setMaximum(layer->keyFrameCount());
238 mProgress->setValue(0);
239 int keysThinned = 0;
240 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
241 for (int i = layer->firstKeyFramePosition(); i <= layer->getMaxKeyFramePosition(); i++)
242 {
243 if (layer->keyExists(i))
244 {
245 mProgress->setValue(keysThinned++);
246 mEditor->scrubTo(i);
247 if (mProgress->wasCanceled())
248 {
249 break;
250 }
251 if (somethingSelected)
252 {
253 BitmapImage selection = layer->getBitmapImageAtFrame(i)->copy(selectionRect);
254 layer->removeKeyFrame(i);
255 layer->addNewKeyFrameAt(i);
256 layer->getBitmapImageAtFrame(i)->paste(&selection);
257 }
258 img = layer->getBitmapImageAtFrame(i);
259 img = img->scanToTransparent(img,
260 mThreshold,
261 ui->cb_Red->isChecked(),
262 ui->cb_Green->isChecked(),
263 ui->cb_Blue->isChecked());
264 img->modification();
265 }
266 }
267 mProgress->close();
268 }
269}
AddTransparencyToPaperDialog
Definition: addtransparencytopaperdialog.h:34
BitmapImage
Definition: bitmapimage.h:28
Editor
Definition: editor.h:71
Editor::scrubbed
void scrubbed(int frameNumber)
This should be emitted after scrubbing.
LayerBitmap
Definition: layerbitmap.h:26
Layer::addNewKeyFrameAt
bool addNewKeyFrameAt(int position)
Creates a new keyframe at the given position, unless one already exists.
Definition: layer.cpp:170
QAbstractButton
QAbstractSlider::valueChanged
void valueChanged(int value)
QBrush
QCheckBox::stateChanged
void stateChanged(int state)
QCoreApplication::processEvents
void processEvents(QEventLoop::ProcessEventsFlags flags)
QDialog
QDialog::accept
virtual void accept()
QDialog::reject
virtual void reject()
QDialogButtonBox::ApplyRole
ApplyRole
QDialogButtonBox::Apply
Apply
QDialogButtonBox::clicked
void clicked(QAbstractButton *button)
QEventLoop::ExcludeUserInputEvents
ExcludeUserInputEvents
QGraphicsPixmapItem::setPixmap
void setPixmap(const QPixmap &pixmap)
QGraphicsScene::addPixmap
QGraphicsPixmapItem * addPixmap(const QPixmap &pixmap)
QGraphicsScene::setBackgroundBrush
void setBackgroundBrush(const QBrush &brush)
QGraphicsScene::setSceneRect
void setSceneRect(const QRectF &rect)
QImage
QImage::rect
QRect rect() const const
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QPainter
QPixmap
QPixmap::fill
void fill(const QColor &color)
QPoint
QProgressDialog
QProgressDialog::setMaximum
void setMaximum(int maximum)
QProgressDialog::setValue
void setValue(int progress)
QProgressDialog::wasCanceled
wasCanceled
QRect
QRect::isValid
bool isValid() const const
QRectF::toRect
QRect toRect() const const
QResizeEvent
QSize
QSize::scaled
QSize scaled(int width, int height, Qt::AspectRatioMode mode) const const
QSpinBox
QSpinBox::valueChanged
void valueChanged(int i)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString::number
QString number(int n, int base)
Qt::KeepAspectRatioByExpanding
KeepAspectRatioByExpanding
Qt::white
white
Qt::WindowModal
WindowModal
QWidget
QWidget::close
bool close()
QWidget::setEnabled
void setEnabled(bool)
QWidget::setupUi
void setupUi(QWidget *widget)
QWidget::show
void show()
QWidget::size
size
QWidget::setWindowModality
void setWindowModality(Qt::WindowModality windowModality)
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39