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 #include "layermanager.h"
24 #include "selectionmanager.h"
25 
26 PegBarAlignmentDialog::PegBarAlignmentDialog(Editor *editor, QWidget *parent) :
27  QDialog(parent),
28  ui(new Ui::PegBarAlignmentDialog)
29 {
30  ui->setupUi(this);
31  mEditor = editor;
32  connect(ui->btnAlign, &QPushButton::clicked, this, &PegBarAlignmentDialog::alignPegs);
33  connect(ui->btnCancel, &QPushButton::clicked, this, &PegBarAlignmentDialog::closeClicked);
34  connect(ui->lwLayers, &QListWidget::clicked, this, &PegBarAlignmentDialog::updatePegRegDialog);
35 
36  connect(mEditor->layers(), &LayerManager::layerCountChanged, this, &PegBarAlignmentDialog::updatePegRegLayers);
37  connect(mEditor->select(), &SelectionManager::selectionChanged, this, &PegBarAlignmentDialog::updatePegRegDialog);
38  connect(mEditor, &Editor::currentFrameChanged, this, &PegBarAlignmentDialog::updatePegRegDialog);
39  connect(mEditor->layers(), &LayerManager::currentLayerChanged, this, &PegBarAlignmentDialog::updatePegRegDialog);
40 
41  ui->btnAlign->setEnabled(false);
42  mLayernames.clear();
43 }
44 
45 PegBarAlignmentDialog::~PegBarAlignmentDialog()
46 {
47  delete ui;
48 }
49 
50 void PegBarAlignmentDialog::setLayerList(QStringList layerList)
51 {
52  ui->lwLayers->clear();
53  mLayernames = layerList;
54  for (int i = 0; i < mLayernames.count(); i++)
55  {
56  ui->lwLayers->addItem(mLayernames.at(i));
57  }
58 }
59 
60 QStringList PegBarAlignmentDialog::getLayerList()
61 {
62  QStringList selectedLayers;
63  selectedLayers.clear();
64  for (int i = 0; i < ui->lwLayers->count(); i++)
65  {
66  if (ui->lwLayers->item(i)->isSelected())
67  selectedLayers.append(ui->lwLayers->item(i)->text());
68  }
69  return selectedLayers;
70 }
71 
72 void PegBarAlignmentDialog::updateRefKeyLabelText()
73 {
74  ui->labRefKey->setText(QStringLiteral("%1 - %2").arg(mRefLayer).arg(mRefkey));
75 }
76 
77 void PegBarAlignmentDialog::setAreaSelected(bool b)
78 {
79  mAreaSelected = b;
80  setBtnAlignEnabled();
81 }
82 
83 void PegBarAlignmentDialog::setReferenceSelected(bool b)
84 {
85  mReferenceSelected = b;
86  setBtnAlignEnabled();
87 }
88 
89 void PegBarAlignmentDialog::setLayerSelected(bool b)
90 {
91  mLayerSelected = b;
92  setBtnAlignEnabled();
93 }
94 
95 void PegBarAlignmentDialog::updatePegRegLayers()
96 {
97  QStringList bitmaplayers;
98  for (int i = 0; i < mEditor->layers()->count(); i++)
99  {
100  if (mEditor->layers()->getLayer(i)->type() == Layer::BITMAP)
101  {
102  bitmaplayers.append(mEditor->layers()->getLayer(i)->name());
103  }
104  }
105  setLayerList(bitmaplayers);
106 }
107 
108 void PegBarAlignmentDialog::updatePegRegDialog()
109 {
110  // is something selected in scribblearea?
111  setAreaSelected(mEditor->select()->somethingSelected());
112 
113  const Layer* currentLayer = mEditor->layers()->currentLayer();
114  // is the reference key valid?
115  setRefLayer(currentLayer->name());
116  setRefKey(mEditor->currentFrame());
117 
118  bool isReferenceSelected = (currentLayer->type() == Layer::BITMAP &&
119  currentLayer->keyExists(mEditor->currentFrame()));
120  setReferenceSelected(isReferenceSelected);
121 
122  // has minimum one layer been selected?
123  const QStringList bitmaplayers = getLayerList();
124 
125  if (bitmaplayers.isEmpty())
126  {
127  setLayerSelected(false);
128  }
129  else
130  {
131  setRefLayer(currentLayer->name());
132  setRefKey(mEditor->currentFrame());
133  setLayerSelected(true);
134  }
135 
136  setBtnAlignEnabled();
137 }
138 
139 void PegBarAlignmentDialog::alignPegs()
140 {
141  const QStringList bitmaplayers = getLayerList();
142  if (bitmaplayers.isEmpty())
143  {
144  QMessageBox::information(this, "Pencil2D",
145  tr("No layers selected!", "PegBar Dialog error message"),
147  return;
148  }
149 
150  Status result = mEditor->pegBarAlignment(bitmaplayers);
151  if (!result.ok())
152  {
153  QMessageBox::information(this, "Pencil2D",
154  result.description(),
156  return;
157  }
159 }
160 
161 void PegBarAlignmentDialog::setBtnAlignEnabled()
162 {
163  if (mAreaSelected && mReferenceSelected && mLayerSelected)
164  ui->btnAlign->setEnabled(true);
165  else
166  ui->btnAlign->setEnabled(false);
167 }
168 
169 void PegBarAlignmentDialog::setRefLayer(QString s)
170 {
171  mRefLayer = s;
172  updateRefKeyLabelText();
173 }
174 
175 void PegBarAlignmentDialog::setRefKey(int i)
176 {
177  mRefkey = i;
178  updateRefKeyLabelText();
179 }
180 
181 void PegBarAlignmentDialog::closeClicked()
182 {
184 }
void clear()
const T & at(int i) const const
virtual void done(int r)
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 count(const T &value) const const
void append(const T &value)
bool isEmpty() const const
void clicked(bool checked)
Definition: layer.h:39
int result() const const
void clicked(const QModelIndex &index)
Definition: editor.h:51
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)