All Classes Namespaces Functions Variables Enumerations Properties Pages
repositionframesdialog.cpp
1 #include "repositionframesdialog.h"
2 #include "ui_repositionframesdialog.h"
3 
4 #include <QList>
5 #include <QListWidgetItem>
6 #include <QMessageBox>
7 
8 #include "selectionmanager.h"
9 #include "selectionpainter.h"
10 #include "viewmanager.h"
11 #include "toolmanager.h"
12 #include "layer.h"
13 #include "layermanager.h"
14 #include "layerbitmap.h"
15 #include "scribblearea.h"
16 
17 
18 RepositionFramesDialog::RepositionFramesDialog(QWidget* parent) :
19  QDialog(parent),
20  ui(new Ui::RepositionFramesDialog)
21 {
22  ui->setupUi(this);
23 }
24 
25 RepositionFramesDialog::~RepositionFramesDialog()
26 {
27  delete ui;
28 }
29 
30 void RepositionFramesDialog::setCore(Editor *editor)
31 {
32  mEditor = editor;
33 }
34 
35 void RepositionFramesDialog::initUI()
36 {
37  if (mEditor->layers()->currentLayer()->keyExists(mEditor->currentFrame()))
38  {
39  mRepositionFrame = mEditor->currentFrame();
40  }
41  else
42  {
43  mRepositionFrame = mEditor->layers()->currentLayer()->getSelectedFramesByPos().at(0);
44  }
45 
46  prepareRepositionSelectedImages(mRepositionFrame);
47 
48  updateRadioButtons();
49 
50  ui->rbAllKeyframes->setChecked(true);
51  connect(ui->cbOtherLayers, &QCheckBox::stateChanged, this, &RepositionFramesDialog::checkboxStateChanged);
52  connect(ui->rbAllKeyframes, &QRadioButton::clicked, this, &RepositionFramesDialog::updateLayersBox);
53  connect(ui->rbSameKeyframes, &QRadioButton::clicked, this, &RepositionFramesDialog::updateLayersBox);
54  connect(ui->btnReposition, &QPushButton::clicked, this, &RepositionFramesDialog::repositionFrames);
55  connect(ui->btnCancel, &QPushButton::clicked, this, &RepositionFramesDialog::closeClicked);
56  connect(mEditor->getScribbleArea(), &ScribbleArea::selectionUpdated, this, &RepositionFramesDialog::updateDialogText);
57  connect(mEditor->select(), &SelectionManager::selectionReset, this, &RepositionFramesDialog::closeClicked);
58  mEndPoint = mStartPoint = QPoint(0,0);
59  mOriginalPolygonF = mEditor->select()->currentSelectionPolygonF();
60  updateDialogSelectedFrames();
61  updateDialogText();
62 }
63 
64 void RepositionFramesDialog::updateDialogText()
65 {
66  if (mOriginalPolygonF.boundingRect().isEmpty())
67  {
68  mOriginalPolygonF = mEditor->select()->currentSelectionPolygonF();
69  }
70  mCurrentPolygonF = mEditor->select()->currentSelectionPolygonF();
71  QPoint point = getRepositionPoint();
72  ui->labRepositioned->setText(tr("Repositioned: ( %1, %2 )").arg(point.x()).arg(point.y()));
73 }
74 
75 void RepositionFramesDialog::updateDialogSelectedFrames()
76 {
77  QList<int> framelist = mEditor->layers()->currentLayer()->getSelectedFramesByPos();
78  ui->labRepositionFrames->setText(tr("Selected on Layer: %1").arg(mEditor->layers()->currentLayer()->name()));
79  ui->listSelectedFrames->clear();
80  for (int i = 0; i < framelist.count(); i++)
81  {
82  ui->listSelectedFrames->addItem(QString::number(framelist.at(i)));
83  }
84 }
85 
86 void RepositionFramesDialog::repositionFrames()
87 {
88  if (mStartPoint == mEndPoint)
89  {
90  QMessageBox::information(this, nullptr,
91  tr("Please move selection to desired destination\n"
92  "or cancel"),
94  return;
95  }
96 
97  mEditor->getScribbleArea()->updateOriginalPolygonF();
98  QList<int> frames = mEditor->layers()->currentLayer()->getSelectedFramesByPos();
99  for (int i = 0; i < frames.size(); i++)
100  {
101  mEditor->repositionImage(mEndPoint, frames.at(i));
102  }
103 
104  if (!ui->listSelectedLayers->selectedItems().isEmpty())
105  {
106  auto layerManager = mEditor->layers();
107 
108  // if only selected keyframe-numbers should be repositioned
109  if (ui->rbSameKeyframes->isChecked())
110  {
111  int currLayer = mEditor->currentLayerIndex();
112  for (int j = 0; j < mLayerIndexes.size(); j++)
113  {
114  QListWidgetItem* item = ui->listSelectedLayers->item(j);
115  if (item->isSelected())
116  {
117  layerManager->setCurrentLayer(mLayerIndexes.at(j));
118  for (int i = 0; i < frames.size(); i++)
119  { // only move frame if it exists
120  if (layerManager->currentLayer()->keyExists(frames.at(i)))
121  {
122  mEditor->repositionImage(mEndPoint, frames.at(i));
123  }
124  }
125  }
126  }
127  layerManager->setCurrentLayer(currLayer);
128  }
129  // if all keyframes on layer should be repositioned
130  else
131  {
132  int currLayer = mEditor->currentLayerIndex();
133  for (int i= 0; i < mLayerIndexes.size(); i++)
134  {
135  QListWidgetItem* item = ui->listSelectedLayers->item(i);
136  if (item->isSelected())
137  {
138  layerManager->setCurrentLayer(mLayerIndexes.at(i));
139  int keyframe = layerManager->currentLayer()->firstKeyFramePosition();
140  do {
141  mEditor->repositionImage(mEndPoint, keyframe);
142  keyframe = layerManager->currentLayer()->getNextKeyFramePosition(keyframe);
143  }
144  while (mEditor->currentFrame() != layerManager->currentLayer()->getMaxKeyFramePosition());
145  }
146  }
147  layerManager->setCurrentLayer(currLayer);
148  }
149  }
150  mEditor->getScribbleArea()->applySelectionChanges();
151  mEditor->select()->resetSelectionProperties();
152  mEditor->scrubTo(mRepositionFrame);
153 
154  accept();
155 }
156 
157 void RepositionFramesDialog::updateRadioButtons()
158 {
159  if (ui->cbOtherLayers->isChecked())
160  {
161  ui->rbAllKeyframes->setEnabled(true);
162  ui->rbSameKeyframes->setEnabled(true);
163  ui->listSelectedLayers->setEnabled(true);
164  }
165  else
166  {
167  ui->rbAllKeyframes->setEnabled(false);
168  ui->rbSameKeyframes->setEnabled(false);
169  ui->listSelectedLayers->setEnabled(false);
170  }
171 }
172 
173 void RepositionFramesDialog::checkboxStateChanged(int i)
174 {
175  switch (i)
176  {
177  case Qt::Checked:
178  updateLayersToSelect();
179  break;
180  default:
181  ui->listSelectedLayers->clear();
182  break;
183  }
184  updateRadioButtons();
185 }
186 
187 void RepositionFramesDialog::updateLayersBox()
188 {
189  updateLayersToSelect();
190 }
191 
192 void RepositionFramesDialog::closeClicked()
193 {
194  rejected();
195 }
196 
197 void RepositionFramesDialog::updateLayersToSelect()
198 {
199  ui->listSelectedLayers->clear();
200 
201  mLayerIndexes.clear();
202  auto layerManager = mEditor->layers();
203  if (ui->rbAllKeyframes->isChecked())
204  {
205  for (int i = layerManager->count() - 1; i >= 0; i--)
206  {
207  if (layerManager->getLayer(i)->type() == Layer::BITMAP &&
208  i != layerManager->currentLayerIndex())
209  {
210  ui->listSelectedLayers->addItem(layerManager->getLayer(i)->name());
211  mLayerIndexes.append(i);
212  }
213  }
214  }
215  else
216  {
217  QList<int> frames = layerManager->currentLayer()->getSelectedFramesByPos();
218  bool ok = false;
219  for (int i = layerManager->count() - 1; i >= 0; i--)
220  {
221  if (layerManager->getLayer(i)->type() == Layer::BITMAP &&
222  i != layerManager->currentLayerIndex())
223  {
224  Layer* layer = layerManager->getLayer(i);
225  for (int j = 0; j < frames.size(); j++)
226  {
227  if (layer->keyExists(frames.at(j)))
228  ok = true;
229  }
230  if (ok)
231  {
232  ui->listSelectedLayers->addItem(layer->name());
233  mLayerIndexes.append(i);
234  }
235  ok = false;
236  }
237 
238  }
239  }
240 }
241 
242 QPoint RepositionFramesDialog::getRepositionPoint()
243 {
244  int x = static_cast<int>(mCurrentPolygonF.boundingRect().x() - mOriginalPolygonF.boundingRect().x());
245  int y = static_cast<int>(mCurrentPolygonF.boundingRect().y() - mOriginalPolygonF.boundingRect().y());
246  mEndPoint = QPoint(x, y);
247  return mEndPoint;
248 }
249 
250 void RepositionFramesDialog::prepareRepositionSelectedImages(int repositionFrame)
251 {
252  auto select = mEditor->select();
253  auto layers = mEditor->layers();
254 
255  if (select->somethingSelected()) { return; }
256 
257  if (layers->currentLayer()->type() == Layer::BITMAP)
258  {
259  mEditor->scrubTo(repositionFrame);
260  LayerBitmap* layer = static_cast<LayerBitmap*>(layers->currentLayer());
261  QRect reposRect = layer->getFrameBounds(repositionFrame);
262  select->setSelection(reposRect);
263  }
264 }
265 
void clear()
void setupUi(QWidget *widget)
bool isSelected() const const
qreal x() const const
qreal y() const const
void rejected()
const T & at(int i) const const
int y() const const
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
int size() const const
QString number(int n, int base)
int count(const T &value) const const
void append(const T &value)
int x() const const
void clicked(bool checked)
Definition: layer.h:38
virtual void accept()
bool isEmpty() const const
QRectF boundingRect() const const
void stateChanged(int state)
Definition: editor.h:55
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)