All Classes Namespaces Functions Variables Enumerations Properties Pages
importpositiondialog.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 "importpositiondialog.h"
18 #include "ui_importpositiondialog.h"
19 
20 #include <QSettings>
21 #include "editor.h"
22 #include "viewmanager.h"
23 #include "scribblearea.h"
24 
25 ImportPositionDialog::ImportPositionDialog(QWidget *parent) :
26  QDialog(parent),
27  ui(new Ui::ImportPositionDialog)
28 {
29  ui->setupUi(this);
30 
31  ui->cbImagePosition->addItem(tr("Center of current view"));
32  ui->cbImagePosition->addItem(tr("Center of canvas (0,0)"));
33  ui->cbImagePosition->addItem(tr("Center of camera, current frame"));
34  ui->cbImagePosition->addItem(tr("Center of camera, follow camera"));
35 
36  connect(ui->cbImagePosition, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &ImportPositionDialog::didChangeComboBoxIndex);
37  connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &ImportPositionDialog::changeImportView);
38 
39  QSettings settings(PENCIL2D, PENCIL2D);
40  int value = settings.value(IMPORT_REPOSITION_TYPE).toInt();
41 
42  ui->cbImagePosition->setCurrentIndex(value);
43  didChangeComboBoxIndex(value);
44 }
45 
46 ImportPositionDialog::~ImportPositionDialog()
47 {
48  delete ui;
49 }
50 
51 void ImportPositionDialog::didChangeComboBoxIndex(const int index)
52 {
53  mImportOption = ImportPosition::getTypeFromIndex(index);
54 }
55 
56 void ImportPositionDialog::changeImportView()
57 {
58  mEditor->view()->setImportFollowsCamera(false);
59  QTransform transform;
60  if (mImportOption == ImportPosition::Type::CenterOfView)
61  {
62  QPointF centralPoint = mEditor->getScribbleArea()->getCentralPoint();
63  transform = transform.fromTranslate(centralPoint.x(), centralPoint.y());
64  mEditor->view()->setImportView(transform);
65  QSettings settings(PENCIL2D, PENCIL2D);
66  settings.setValue(IMPORT_REPOSITION_TYPE, ui->cbImagePosition->currentIndex());
67  return;
68  }
69  else if (mImportOption == ImportPosition::Type::CenterOfCanvas)
70  {
71  transform = transform.fromTranslate(0, 0);
72  mEditor->view()->setImportView(transform);
73  QSettings settings(PENCIL2D, PENCIL2D);
74  settings.setValue(IMPORT_REPOSITION_TYPE, ui->cbImagePosition->currentIndex());
75  return;
76  }
77  else if (mImportOption == ImportPosition::Type::CenterOfCamera)
78  {
79  QRectF cameraRect = mEditor->getScribbleArea()->getCameraRect(); // Must be QRectF for the precision of cameraRect.center()
80  transform = transform.fromTranslate(cameraRect.center().x(), cameraRect.center().y());
81  mEditor->view()->setImportView(transform);
82  QSettings settings(PENCIL2D, PENCIL2D);
83  settings.setValue(IMPORT_REPOSITION_TYPE, ui->cbImagePosition->currentIndex());
84  return;
85  }
86 
87  mEditor->view()->setImportFollowsCamera(true);
88  QSettings settings(PENCIL2D, PENCIL2D);
89  settings.setValue(IMPORT_REPOSITION_TYPE, ui->cbImagePosition->currentIndex());
90 }
QTransform fromTranslate(qreal dx, qreal dy)
QString tr(const char *sourceText, const char *disambiguation, int n)
qreal x() const const
qreal y() const const
QPointF center() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void currentIndexChanged(int index)