All Classes Namespaces Functions Variables Enumerations Properties Pages
pegbaralignmentdialog.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 "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 
32 PegBarAlignmentDialog::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 
59 PegBarAlignmentDialog::~PegBarAlignmentDialog()
60 {
61  delete ui;
62 }
63 
64 void 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 
79 QStringList 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 
92 void PegBarAlignmentDialog::updateRefKeyLabel(QString text)
93 {
94  ui->labRefKey->setText(text);
95 }
96 
97 void PegBarAlignmentDialog::setAreaSelected(bool b)
98 {
99  mAreaSelected = b;
100  updateAlignButton();
101 }
102 
103 void PegBarAlignmentDialog::setReferenceSelected(bool b)
104 {
105  mReferenceSelected = b;
106  updateAlignButton();
107 }
108 
109 void PegBarAlignmentDialog::setLayerSelected(bool b)
110 {
111  mLayerSelected = b;
112  updateAlignButton();
113 }
114 
115 void 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 
130 void 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 
157 void 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"),
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(),
175  return;
176  }
177 
178  mEditor->deselectAll();
180 }
181 
182 void PegBarAlignmentDialog::updateAlignButton()
183 {
184  if (mAreaSelected && mReferenceSelected && mLayerSelected)
185  ui->btnAlign->setEnabled(true);
186  else
187  ui->btnAlign->setEnabled(false);
188 }
189 
190 void PegBarAlignmentDialog::closeClicked()
191 {
193 }
void clear()
const T & at(int i) const const
virtual void done(int r)
void scrubbed(int frameNumber)
This should be emitted after scrubbing.
QString tr(const char *sourceText, const char *disambiguation, int n)
QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
int x() const const
int y() const const
QString number(int n, int base)
int count(const T &value) const const
void append(const T &value)
bool isEmpty() const const
void clicked(bool checked)
Definition: layer.h:38
int result() const const
QRect toAlignedRect() const const
void clicked(const QModelIndex &index)
Definition: editor.h:55
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void framesModified()
This should be emitted after modifying multiple frames.