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
pegbaralignmentdialog.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "pegbaralignmentdialog.h"
18#include "ui_pegbaralignmentdialog.h"
19
20#include <QListWidget>
21#include <QListWidgetItem>
22#include <QMessageBox>
23
24#include "keyframe.h"
25#include "layermanager.h"
26#include "selectionmanager.h"
27#include "toolmanager.h"
28#include "scribblearea.h"
29
30#include <pegbaraligner.h>
31
32PegBarAlignmentDialog::PegBarAlignmentDialog(Editor *editor, QWidget *parent) :
33 QDialog(parent),
34 ui(new Ui::PegBarAlignmentDialog), mEditor(editor)
35{
36 ui->setupUi(this);
37 connect(ui->btnAlign, &QPushButton::clicked, this, &PegBarAlignmentDialog::alignPegs);
38 connect(ui->btnCancel, &QPushButton::clicked, this, &PegBarAlignmentDialog::closeClicked);
39 connect(ui->lwLayers, &QListWidget::clicked, this, &PegBarAlignmentDialog::updatePegRegDialog);
40
41 connect(mEditor->layers(), &LayerManager::layerCountChanged, this, &PegBarAlignmentDialog::updatePegRegLayers);
42 connect(mEditor->select(), &SelectionManager::selectionChanged, this, &PegBarAlignmentDialog::updatePegRegDialog);
43 connect(mEditor, &Editor::scrubbed, this, &PegBarAlignmentDialog::updatePegRegDialog);
44 connect(mEditor, &Editor::framesModified, this, &PegBarAlignmentDialog::updatePegRegDialog);
45 connect(mEditor->layers(), &LayerManager::currentLayerChanged, this, &PegBarAlignmentDialog::updatePegRegDialog);
46
47 ui->btnAlign->setEnabled(false);
48 mLayernames.clear();
49
50 mEditor->tools()->setCurrentTool(SELECT);
51
52 if (!mEditor->select()->somethingSelected()) {
53 QPoint centralPoint = mEditor->getScribbleArea()->getCentralPoint().toPoint();
54 mEditor->select()->setSelection(QRect(centralPoint.x()-100,centralPoint.y()-50,200,100));
55 }
56 updatePegRegLayers();
57}
58
59PegBarAlignmentDialog::~PegBarAlignmentDialog()
60{
61 delete ui;
62}
63
64void PegBarAlignmentDialog::setLayerList(QStringList layerList)
65{
66 ui->lwLayers->clear();
67 mLayernames = layerList;
68 for (int i = 0; i < mLayernames.count(); i++)
69 {
70 ui->lwLayers->addItem(mLayernames.at(i));
71 }
72
73 // Select the first layer.
74 if (ui->lwLayers->count() > 0) {
75 ui->lwLayers->item(0)->setSelected(true);
76 }
77}
78
79QStringList PegBarAlignmentDialog::getLayerList()
80{
81 QStringList selectedLayers;
82 selectedLayers.clear();
83 for (int i = 0; i < ui->lwLayers->count(); i++)
84 {
85 if (!ui->lwLayers->item(i)->isSelected()) { continue; }
86
87 selectedLayers.append(ui->lwLayers->item(i)->text());
88 }
89 return selectedLayers;
90}
91
92void PegBarAlignmentDialog::updateRefKeyLabel(QString text)
93{
94 ui->labRefKey->setText(text);
95}
96
97void PegBarAlignmentDialog::setAreaSelected(bool b)
98{
99 mAreaSelected = b;
100 updateAlignButton();
101}
102
103void PegBarAlignmentDialog::setReferenceSelected(bool b)
104{
105 mReferenceSelected = b;
106 updateAlignButton();
107}
108
109void PegBarAlignmentDialog::setLayerSelected(bool b)
110{
111 mLayerSelected = b;
112 updateAlignButton();
113}
114
115void PegBarAlignmentDialog::updatePegRegLayers()
116{
117 QStringList bitmaplayers;
118 auto layerMan = mEditor->layers();
119 for (int i = 0; i < layerMan->count(); i++)
120 {
121 const Layer* layer = layerMan->getLayer(i);
122 if (layer->type() != Layer::BITMAP) { continue; }
123
124 bitmaplayers.append(layer->name());
125 }
126 setLayerList(bitmaplayers);
127 updatePegRegDialog();
128}
129
130void PegBarAlignmentDialog::updatePegRegDialog()
131{
132 // is something selected in scribblearea?
133 setAreaSelected(mEditor->select()->somethingSelected());
134
135 const Layer* currentLayer = mEditor->layers()->currentLayer();
136 // is the reference key valid?
137 KeyFrame* key = currentLayer->getLastKeyFrameAtPosition(mEditor->currentFrame());
138
139 if (key == nullptr) {
140 updateRefKeyLabel("-");
141 setReferenceSelected(false);
142 return;
143 }
144
145 bool isReferenceSelected = (currentLayer->type() == Layer::BITMAP &&
146 key->pos());
147 setReferenceSelected(isReferenceSelected);
148
149 // has minimum one layer been selected?
150 const QStringList bitmaplayers = getLayerList();
151
152 setLayerSelected(!bitmaplayers.isEmpty());
153 updateRefKeyLabel(QString::number(key->pos()));
154 updateAlignButton();
155}
156
157void PegBarAlignmentDialog::alignPegs()
158{
159 const QStringList bitmaplayers = getLayerList();
160 if (bitmaplayers.isEmpty())
161 {
162 QMessageBox::information(this, "Pencil2D",
163 tr("No layers selected!", "PegBar Dialog error message"),
164 QMessageBox::Ok);
165 return;
166 }
167
168
169 Status result = PegBarAligner(mEditor, mEditor->select()->mySelectionRect().toAlignedRect()).align(bitmaplayers);
170 if (!result.ok())
171 {
172 QMessageBox::information(this, "Pencil2D",
173 result.description(),
174 QMessageBox::Ok);
175 return;
176 }
177
178 mEditor->deselectAll();
179 done(QDialog::Accepted);
180}
181
182void PegBarAlignmentDialog::updateAlignButton()
183{
184 if (mAreaSelected && mReferenceSelected && mLayerSelected)
185 ui->btnAlign->setEnabled(true);
186 else
187 ui->btnAlign->setEnabled(false);
188}
189
190void PegBarAlignmentDialog::closeClicked()
191{
192 done(QDialog::Accepted);
193}
Editor
Definition: editor.h:71
Editor::framesModified
void framesModified()
This should be emitted after modifying multiple frames.
Editor::scrubbed
void scrubbed(int frameNumber)
This should be emitted after scrubbing.
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
PegBarAligner
Definition: pegbaraligner.h:36
PegBarAlignmentDialog
Definition: pegbaralignmentdialog.h:29
Status
Definition: pencilerror.h:40
QAbstractButton::clicked
void clicked(bool checked)
QAbstractItemView::clicked
void clicked(const QModelIndex &index)
QDialog
QDialog::Accepted
Accepted
QDialog::done
virtual void done(int r)
QDialog::result
int result() const const
QList::append
void append(const T &value)
QList::at
const T & at(int i) const const
QList::clear
void clear()
QList::count
int count(const T &value) const const
QList::isEmpty
bool isEmpty() const const
QMessageBox::Ok
Ok
QMessageBox::information
QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QPoint
QPoint::x
int x() const const
QPoint::y
int y() const const
QRect
QRectF::toAlignedRect
QRect toAlignedRect() const const
QString
QString::number
QString number(int n, int base)
QStringList
QWidget
QWidget::setupUi
void setupUi(QWidget *widget)
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39