All Classes Namespaces Functions Variables Enumerations Properties Pages
presetdialog.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 "presetdialog.h"
18 #include "ui_presetdialog.h"
19 #include "app_util.h"
20 
21 #include <QFile>
22 #include <QStandardPaths>
23 #include <QDir>
24 #include <QSettings>
25 
26 
27 PresetDialog::PresetDialog(PreferenceManager* preferences, QWidget* parent) :
28  QDialog(parent),
29  ui(new Ui::PresetDialog),
30  mPrefs(preferences)
31 {
32  ui->setupUi(this);
33 
34  initPresets();
35  hideQuestionMark(*this);
36 }
37 
38 PresetDialog::~PresetDialog()
39 {
40  delete ui;
41 }
42 
43 QString PresetDialog::getPreset()
44 {
45  int index = getPresetIndex();
46  return PresetDialog::getPresetPath(index);
47 }
48 
49 int PresetDialog::getPresetIndex()
50 {
51  bool ok = true;
52  int index = ui->presetComboBox->currentData().toInt(&ok);
53  Q_ASSERT(ok);
54  return index;
55 }
56 
57 bool PresetDialog::shouldAlwaysUse()
58 {
59  return ui->alwaysUse->isChecked();
60 }
61 
62 QString PresetDialog::getPresetPath(int index)
63 {
64  if (index == 0)
65  {
66  return QString();
67  }
68 
69  const QString filename = QString("%1.pclx").arg(index);
71  if (dataDir.cd("presets"))
72  {
73  if (dataDir.exists(filename))
74  {
75  return dataDir.filePath(filename);
76  }
77  }
78  return QString();
79 }
80 
81 void PresetDialog::initPresets()
82 {
83  // Make sure the presets directory in the data directory exists and navigate to it
85  QDir dataDir(dataPath);
86  dataDir.mkdir("presets");
87  if (dataDir.cd("presets") == false)
88  {
89  reject(); // the presets folder doesn't exist and cannot be created
90  return;
91  }
92 
93  // Find all presets in the preferences and add them to the combo box
94  int defaultIndex = mPrefs->getInt(SETTING::DEFAULT_PRESET);
95  ui->presetComboBox->addItem("Default", 0);
96  ui->presetComboBox->setCurrentIndex(0);
97 
98  if (!dataDir.exists("presets.ini"))
99  {
100  reject();
101  return;
102  }
103  QSettings presets(dataDir.filePath("presets.ini"), QSettings::IniFormat, this);
104 
105  bool ok = true;
106  for (const QString& key : presets.allKeys())
107  {
108  int index = key.toInt(&ok);
109  if (!ok || index == 0 || !dataDir.exists(QString("%1.pclx").arg(index))) continue;
110 
111  QString name = presets.value(key, QString()).toString();
112  if (name.isEmpty()) continue;
113  ui->presetComboBox->addItem(name, index);
114  if (index == defaultIndex)
115  {
116  ui->presetComboBox->setCurrentIndex(ui->presetComboBox->count()-1);
117  }
118  }
119 
120  ui->presetComboBox->model()->sort(0);
121 }
virtual void reject()
QString writableLocation(QStandardPaths::StandardLocation type)
QString filePath(const QString &fileName) const const
bool exists() const const
bool cd(const QString &dirName)
bool isEmpty() const const
bool mkdir(const QString &dirName) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const