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
filespage.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
18#include "filespage.h"
19
20#include <QSettings>
21#include <QStandardPaths>
22
23#include "errordialog.h"
24#include "filemanager.h"
25#include "pencilerror.h"
26#include "presetdialog.h"
27
28#include "ui_filespage.h"
29
30FilesPage::FilesPage()
31 : ui(new Ui::FilesPage)
32{
33 ui->setupUi(this);
34
35 initPreset();
36
37 connect(ui->addPreset, &QPushButton::clicked, this, &FilesPage::addPreset);
38 connect(ui->removePreset, &QPushButton::clicked, this, &FilesPage::removePreset);
39 connect(ui->setDefaultPreset, &QPushButton::clicked, this, &FilesPage::setDefaultPreset);
40 connect(ui->askPresetRbtn, &QRadioButton::toggled, this, &FilesPage::askForPresetChange);
41 connect(ui->loadLastActiveRbtn, &QRadioButton::toggled, this, &FilesPage::loadMostRecentChange);
42 connect(ui->loadDefaultPresetRbtn, &QRadioButton::toggled, this, &FilesPage::loadDefaultPreset);
43 connect(ui->presetListWidget, &QListWidget::itemChanged, this, &FilesPage::presetNameChanged);
44
45 auto spinBoxValueChange = static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged);
46 connect(ui->autosaveCheckBox, &QCheckBox::stateChanged, this, &FilesPage::autoSaveChange);
47 connect(ui->autosaveNumberBox, spinBoxValueChange, this, &FilesPage::autoSaveNumberChange);
48 connect(ui->autosaveByTimeCheckBox, &QCheckBox::stateChanged, this, &FilesPage::autoSaveByTimeChange);
49 connect(ui->autosaveByTimeNumberBox, spinBoxValueChange, this, &FilesPage::autoSaveByTimeTimerChange);
50}
51
52FilesPage::~FilesPage()
53{
54 delete ui;
55}
56
57void FilesPage::initPreset()
58{
59 mPresetDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
60 mPresetDir.mkpath("presets");
61 mPresetDir.cd("presets");
62
63 mPresetSettings = new QSettings(mPresetDir.filePath("presets.ini"), QSettings::IniFormat, this);
64
65 QListWidgetItem* defaultItem = new QListWidgetItem("Blank");
66 defaultItem->setData(Qt::UserRole, 0);
67 ui->presetListWidget->addItem(defaultItem);
68
69 bool ok = true;
70 for (const QString& key : mPresetSettings->allKeys())
71 {
72 int index = key.toInt(&ok);
73 if (!ok || index == 0 || !mPresetDir.exists(QString("%1.pclx").arg(index))) continue;
74
75 mMaxPresetIndex = qMax(index, mMaxPresetIndex);
76
77 QString name = mPresetSettings->value(key).toString();
78 if (name.isEmpty())
79 continue;
80
81 QListWidgetItem* item = new QListWidgetItem(name);
82 item->setFlags(item->flags() | Qt::ItemIsEditable);
83 item->setData(Qt::UserRole, index);
84 ui->presetListWidget->addItem(item);
85 }
86}
87
88void FilesPage::addPreset()
89{
90 int newPresetIndex = mMaxPresetIndex + 1;
91
92 // 1. save the current object to the preset folder
93 FileManager fm(this);
94 Status st = fm.save(mManager->object(), PresetDialog::getPresetPath(newPresetIndex));
95 if (!st.ok())
96 {
97 ErrorDialog errorDialog(st.title(),
98 st.description().append(tr("<br><br>Error: your preset may not have saved successfully. "
99 "If you believe that this error is an issue with Pencil2D, please create a new issue at:"
100 "<br><a href='https://github.com/pencil2d/pencil/issues'>https://github.com/pencil2d/pencil/issues</a><br>"
101 "Please include the following details in your issue:")), st.details().html());
102 errorDialog.exec();
103 return;
104 }
105
106 // 2. update the preset ini
107 QString presetName = QString("Preset %1").arg(newPresetIndex);
108 mPresetSettings->setValue(QString::number(newPresetIndex), presetName);
109 mMaxPresetIndex = newPresetIndex;
110
111 // 3. update the list widget
112 QListWidgetItem* newItem = new QListWidgetItem(presetName);
113 newItem->setFlags(newItem->flags() | Qt::ItemIsEditable);
114 newItem->setData(Qt::UserRole, newPresetIndex);
115 ui->presetListWidget->addItem(newItem);
116
117 ui->presetListWidget->scrollToBottom();
118 ui->presetListWidget->editItem(newItem);
119}
120
121void FilesPage::removePreset()
122{
123 if (ui->presetListWidget->count() <= 1) { return; }
124 if (ui->presetListWidget->selectedItems().empty()) { return; }
125
126 // 1. Remove the items from list widget
127 QList<QListWidgetItem*> itemsToRemove = ui->presetListWidget->selectedItems();
128 for (QListWidgetItem* item : itemsToRemove)
129 {
130 ui->presetListWidget->removeItemWidget(item);
131 }
132
133 // 2. Delete preset pclx files
134 for (QListWidgetItem* item : itemsToRemove)
135 {
136 int index = item->data(Qt::UserRole).toInt();
137 QFile presetFile(PresetDialog::getPresetPath(index));
138 presetFile.remove();
139 }
140
141 // 3. Delete items from the ini settings
142 for (QListWidgetItem* item : itemsToRemove)
143 {
144 int index = item->data(Qt::UserRole).toInt();
145 mPresetSettings->remove(QString::number(index));
146 }
147
148 // 4. check if the default preset has been deleted
149 int prevDefaultIndex = mManager->getInt(SETTING::DEFAULT_PRESET);
150 for (QListWidgetItem* item : itemsToRemove)
151 {
152 int index = item->data(Qt::UserRole).toInt();
153 if (index == prevDefaultIndex)
154 {
155 mManager->set(SETTING::DEFAULT_PRESET, 0);
156 }
157 }
158
159 // 5. delete items
160 for (QListWidgetItem* item : itemsToRemove)
161 {
162 delete item;
163 }
164 updateValues();
165}
166
167void FilesPage::setDefaultPreset()
168{
169 bool ok = true;
170
171 QListWidgetItem* newDefaultPresetItem = ui->presetListWidget->currentItem();
172 if (newDefaultPresetItem)
173 {
174 int newDefaultIndex = newDefaultPresetItem->data(Qt::UserRole).toInt(&ok);
175 Q_ASSERT(ok);
176
177 mManager->set(SETTING::DEFAULT_PRESET, newDefaultIndex);
178 updateValues();
179 }
180}
181
182void FilesPage::presetNameChanged(QListWidgetItem* item)
183{
184 // Remove characters that may be problematic for ini files
185 item->setText(item->text().remove(QChar('@')).remove(QChar('/')).remove(QChar('\\')));
186
187 bool ok = true;
188 int index = item->data(Qt::UserRole).toInt(&ok);
189 Q_ASSERT(ok);
190 mPresetSettings->setValue(QString::number(index), item->text());
191}
192
193void FilesPage::updateValues()
194{
195 bool ok = true;
196 int defaultPresetIndex = mManager->getInt(SETTING::DEFAULT_PRESET);
197
198 for (int i = 0; i < ui->presetListWidget->count(); i++)
199 {
200 QListWidgetItem* item = ui->presetListWidget->item(i);
201 int presetIndex = item->data(Qt::UserRole).toInt(&ok);
202
203 bool isDefault = presetIndex == defaultPresetIndex;
204
205 QFont font = item->font();
206 font.setBold(isDefault); // Bold text for the default item
207 item->setFont(font);
208
209 QBrush backgroundBrush = (isDefault) ? palette().light() : palette().window();
210 item->setBackground(backgroundBrush);
211 }
212 ui->autosaveCheckBox->setChecked(mManager->isOn(SETTING::AUTO_SAVE));
213 ui->autosaveNumberBox->setValue(mManager->getInt(SETTING::AUTO_SAVE_NUMBER));
214 ui->autosaveByTimeCheckBox->setChecked(mManager->isOn(SETTING::AUTO_SAVE_BY_TIME));
215 ui->autosaveByTimeNumberBox->setValue(mManager->getInt(SETTING::AUTO_SAVE_BY_TIME_TIMER));
216 ui->askPresetRbtn->setChecked(mManager->isOn(SETTING::ASK_FOR_PRESET));
217 ui->loadDefaultPresetRbtn->setChecked(mManager->isOn(SETTING::LOAD_DEFAULT_PRESET));
218 ui->loadLastActiveRbtn->setChecked(mManager->isOn(SETTING::LOAD_MOST_RECENT));
219}
220
221void FilesPage::askForPresetChange(int b)
222{
223 mManager->set(SETTING::ASK_FOR_PRESET, b != Qt::Unchecked);
224}
225
226void FilesPage::loadMostRecentChange(int b)
227{
228 mManager->set(SETTING::LOAD_MOST_RECENT, b != Qt::Unchecked);
229}
230
231void FilesPage::loadDefaultPreset(int b)
232{
233 mManager->set(SETTING::LOAD_DEFAULT_PRESET, b != Qt::Unchecked);
234}
235
236void FilesPage::autoSaveChange(int b)
237{
238 mManager->set(SETTING::AUTO_SAVE, b != Qt::Unchecked);
239}
240
241void FilesPage::autoSaveNumberChange(int number)
242{
243 mManager->set(SETTING::AUTO_SAVE_NUMBER, number);
244}
245
246void FilesPage::autoSaveByTimeChange(int b)
247{
248 mManager->set(SETTING::AUTO_SAVE_BY_TIME, b != Qt::Unchecked);
249}
250
251void FilesPage::autoSaveByTimeTimerChange(int number)
252{
253 mManager->set(SETTING::AUTO_SAVE_BY_TIME_TIMER, number);
254}
ErrorDialog
Definition: errordialog.h:28
FileManager
Definition: filemanager.h:37
FilesPage
Definition: filespage.h:32
Status
Definition: pencilerror.h:40
QAbstractButton::clicked
void clicked(bool checked)
QAbstractButton::toggled
void toggled(bool checked)
QBrush
QChar
QCheckBox::stateChanged
void stateChanged(int state)
QDir
QDir::cd
bool cd(const QString &dirName)
QDir::exists
bool exists() const const
QDir::filePath
QString filePath(const QString &fileName) const const
QDir::mkpath
bool mkpath(const QString &dirPath) const const
QFile
QFont
QList
QListWidget::itemChanged
void itemChanged(QListWidgetItem *item)
QListWidgetItem
QListWidgetItem::data
virtual QVariant data(int role) const const
QListWidgetItem::flags
Qt::ItemFlags flags() const const
QListWidgetItem::font
QFont font() const const
QListWidgetItem::setBackground
void setBackground(const QBrush &brush)
QListWidgetItem::setData
virtual void setData(int role, const QVariant &value)
QListWidgetItem::setFlags
void setFlags(Qt::ItemFlags flags)
QListWidgetItem::setFont
void setFont(const QFont &font)
QListWidgetItem::setText
void setText(const QString &text)
QListWidgetItem::text
QString text() const const
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QSettings
QSettings::IniFormat
IniFormat
QSettings::allKeys
QStringList allKeys() const const
QSettings::remove
void remove(const QString &key)
QSettings::setValue
void setValue(const QString &key, const QVariant &value)
QSettings::value
QVariant value(const QString &key, const QVariant &defaultValue) const const
QSpinBox
QSpinBox::valueChanged
void valueChanged(int i)
QStandardPaths::AppDataLocation
AppDataLocation
QStandardPaths::writableLocation
QString writableLocation(QStandardPaths::StandardLocation type)
QString
QString::append
QString & append(QChar ch)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString::isEmpty
bool isEmpty() const const
QString::number
QString number(int n, int base)
QString::remove
QString & remove(int position, int n)
Qt::Unchecked
Unchecked
Qt::UserRole
UserRole
Qt::ItemIsEditable
ItemIsEditable
QVariant::toInt
int toInt(bool *ok) const const
QVariant::toString
QString toString() const const
QWidget::font
font
QWidget::palette
palette
Generated on Mon May 4 2026 07:50:47 for Pencil2D by doxygen 1.9.6 based on revision 3ed50cdd696e72315cedf30508d3572536c3876e