Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • app
  • src
presetdialog.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU 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
26PresetDialog::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
37PresetDialog::~PresetDialog()
38{
39 delete ui;
40}
41
42int PresetDialog::getPresetIndex()
43{
44 bool ok = true;
45 int index = ui->presetComboBox->currentData().toInt(&ok);
46 Q_ASSERT(ok);
47 return index;
48}
49
50bool PresetDialog::shouldAlwaysUse()
51{
52 return ui->alwaysUse->isChecked();
53}
54
55QString PresetDialog::getPresetPath(int index)
56{
57 if (index == 0)
58 {
59 return QString();
60 }
61
62 const QString filename = QString("%1.pclx").arg(index);
63 QDir dataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
64 if (dataDir.cd("presets"))
65 {
66 return dataDir.filePath(filename);
67 }
68 return QString();
69}
70
71void PresetDialog::initPresets()
72{
73 // Make sure the presets directory in the data directory exists and navigate to it
74 QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
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}
PreferenceManager
Definition: preferencemanager.h:28
PresetDialog
Definition: presetdialog.h:29
QDialog
QDialog::reject
virtual void reject()
QDir
QDir::cd
bool cd(const QString &dirName)
QDir::exists
bool exists() const const
QDir::filePath
QString filePath(const QString &fileName) const const
QDir::mkdir
bool mkdir(const QString &dirName) const const
QSettings
QSettings::IniFormat
IniFormat
QStandardPaths::AppDataLocation
AppDataLocation
QStandardPaths::writableLocation
QString writableLocation(QStandardPaths::StandardLocation type)
QString
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString::isEmpty
bool isEmpty() const const
QWidget
QWidget::setupUi
void setupUi(QWidget *widget)
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39