All Classes Namespaces Functions Variables Enumerations Properties Pages
importexportdialog.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 
18 #include "importexportdialog.h"
19 #include "ui_importexportdialog.h"
20 #include <QFileInfo>
21 #include <QDialogButtonBox>
22 #include "filedialog.h"
23 
24 ImportExportDialog::ImportExportDialog(QWidget* parent, Mode eMode, FileType eFileType) : QDialog(parent)
25 {
26  mMode = eMode;
27  mFileType = eFileType;
28 
29  ui = new Ui::ImportExportDialog;
30  ui->setupUi(this);
31  hidePreviewGroupBox(true);
32  connect(ui->browseButton, &QPushButton::clicked, this, &ImportExportDialog::browse);
33 
35  setWindowFlags(eFlags);
36 }
37 
38 ImportExportDialog::~ImportExportDialog()
39 {
40  delete ui;
41 }
42 
43 QDialogButtonBox* ImportExportDialog::getDialogButtonBox()
44 {
45  return ui->buttonBox;
46 }
47 
48 
49 QString ImportExportDialog::getFilePath() const
50 {
51  return m_filePaths.isEmpty() ? QString() : m_filePaths.first();
52 }
53 
54 QStringList ImportExportDialog::getFilePaths()
55 {
56  return m_filePaths;
57 }
58 
59 QString ImportExportDialog::getAbsolutePath()
60 {
61  QFileInfo info(m_filePaths.first());
62  return info.absolutePath();
63 }
64 
65 void ImportExportDialog::hideInstructionsLabel(bool hide)
66 {
67  ui->instructionsLabel->setHidden(hide);
68 }
69 
70 void ImportExportDialog::setInstructionsLabel(const QString& text)
71 {
72  ui->instructionsLabel->setText(text);
73 }
74 
75 void ImportExportDialog::init()
76 {
77  switch (mMode)
78  {
79  case Import:
80  m_filePaths = QStringList(FileDialog::getLastOpenPath(mFileType));
81  break;
82  case Export:
83  m_filePaths = QStringList(FileDialog::getLastSavePath(mFileType));
84  break;
85  default:
86  Q_ASSERT(false);
87  }
88  ui->fileEdit->setText("\"" + m_filePaths.first() + "\"");
89 
90  emit filePathsChanged(m_filePaths);
91 }
92 
93 QGroupBox* ImportExportDialog::getOptionsGroupBox()
94 {
95  return ui->optionsGroupBox;
96 }
97 
98 void ImportExportDialog::hideOptionsGroupBox(bool hide)
99 {
100  ui->optionsGroupBox->setHidden(hide);
101 }
102 
103 void ImportExportDialog::hidePreviewGroupBox(bool hide)
104 {
105  ui->previewGroupBox->setHidden(hide);
106 }
107 
108 QGroupBox* ImportExportDialog::getPreviewGroupBox()
109 {
110  return ui->previewGroupBox;
111 }
112 
113 void ImportExportDialog::setFileExtension(const QString& extension)
114 {
115  for (int i = 0; i < m_filePaths.size(); i++)
116  {
117  QFileInfo info(m_filePaths.at(i));
118  m_filePaths.replace(i, info.path() + "/" + info.completeBaseName() + "." + extension);
119  }
120  ui->fileEdit->setText("\"" + m_filePaths.join("\" \"") + "\"");
121 
122  emit filePathsChanged(m_filePaths);
123 }
124 
125 void ImportExportDialog::browse()
126 {
127  QStringList filePaths;
128  switch (mMode)
129  {
130  case Import:
131  if (mFileType == FileType::IMAGE_SEQUENCE)
132  {
133  filePaths = FileDialog::getOpenFileNames(this, FileType::IMAGE_SEQUENCE);
134  break;
135  }
136  filePaths = QStringList(FileDialog::getOpenFileName(this, mFileType));
137  break;
138  case Export:
139  filePaths = QStringList(FileDialog::getSaveFileName(this, mFileType));
140  break;
141  default:
142  Q_ASSERT(false);
143  }
144 
145  if (filePaths.isEmpty() || filePaths.first().isEmpty())
146  {
147  return;
148  }
149 
150  m_filePaths = filePaths;
151  ui->fileEdit->setText("\"" + filePaths.join("\" \"") + "\"");
152 
153  emit filePathsChanged(m_filePaths);
154 }
static QString getLastSavePath(FileType fileType)
Retrieves the last used save path for a given file type.
Definition: filedialog.cpp:120
const T & at(int i) const const
QString join(const QString &separator) const const
QWidget * extension() const const
int size() const const
static QStringList getOpenFileNames(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select multiple files to open.
Definition: filedialog.cpp:46
bool isEmpty() const const
void clicked(bool checked)
T & first()
static QString getLastOpenPath(FileType fileType)
Retrieves the last opened file of a given type.
Definition: filedialog.cpp:104
static QString getOpenFileName(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select a file to open.
Definition: filedialog.cpp:27
void setWindowFlags(Qt::WindowFlags type)
static QString getSaveFileName(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select a file save path.
Definition: filedialog.cpp:65
QString absolutePath() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
typedef WindowFlags
void replace(int i, const T &value)