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