Debian Codegraph @

src/gui/settingsDialog/StickerSettings.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "StickerSettings.h"
21
22 StickerSettings::StickerSettings(const QSharedPointer<IConfig> &config, const QSharedPointer<IDirectoryPathProvider> &directoryPathProvider) :
23 mConfig(config),
24 mDirectoryPathProvider(directoryPathProvider),
25 mListWidget(new QListWidget(this)),
26 mAddButton(new QPushButton(this)),
27 mRemoveButton(new QPushButton(this)),
28 mUpButton(new QPushButton(this)),
29 mDownButton(new QPushButton(this)),
30 mUseDefaultStickerCheckBox(new QCheckBox(this)),
31 mLayout(new QGridLayout),
32 mPathDataKey(1001),
33 mIsSavedDataKey(1002),
34 mIsRemovedDataKey(1003)
35 {
36 initGui();
37 loadConfig();
38 }
39
40 StickerSettings::~StickerSettings()
41 {
42 delete mListWidget;
43 delete mAddButton;
44 delete mRemoveButton;
45 delete mUpButton;
46 delete mDownButton;
47 delete mUseDefaultStickerCheckBox;
48 delete mLayout;
49 }
50
51 void StickerSettings::saveSettings()
52 {
53 auto selectedSticker = processSticker();
54 mConfig->setUseDefaultSticker(mUseDefaultStickerCheckBox->isChecked());
55 mConfig->setStickerPaths(selectedSticker);
56 }
57
58 QStringList StickerSettings::processSticker() const
59 {
60 QStringList paths;
61 for (int i = 0; i < mListWidget->count(); i++) {
62 auto path = mListWidget->item(i)->data(mPathDataKey).toString();
63 auto isSaved = mListWidget->item(i)->data(mIsSavedDataKey).toBool();
64 auto isRemovedSaved = mListWidget->item(i)->data(mIsRemovedDataKey).toBool();
65
66 if(isSaved && isRemovedSaved) {
67 QFile::remove(path);
68 } else if (isSaved) {
69 paths.append(path);
70 } else {
71 auto directory = stickerDirectory();
72 auto filename = PathHelper::extractFilenameWithFormat(path);
73 auto newPath = directory + QLatin1String("/") + filename;
74 QFile::copy(path, newPath);
75 paths.append(newPath);
76 }
77 }
78 return paths;
79 }
80
81 QString StickerSettings::stickerDirectory() const
82 {
83 auto directory = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
84 QDir qdir;
85 qdir.mkpath(directory);
86 return directory;
87 }
88
89 void StickerSettings::initGui()
90 {
91 connect(mListWidget, &QListWidget::currentRowChanged, this, &StickerSettings::currentRowChanged);
92
93 mAddButton->setText(tr("Add"));
94 connect(mAddButton, &QPushButton::clicked, this, &StickerSettings::addTriggered);
95
96 mRemoveButton->setText(tr("Remove"));
97 connect(mRemoveButton, &QPushButton::clicked, this, &StickerSettings::removeTriggered);
98
99 mUpButton->setText(tr("Up"));
100 connect(mUpButton, &QPushButton::clicked, this, &StickerSettings::upTriggered);
101
102 mDownButton->setText(tr("Down"));
103 connect(mDownButton, &QPushButton::clicked, this, &StickerSettings::downTriggered);
104
105 mUseDefaultStickerCheckBox->setText(tr("Use Default Stickers"));
106
107 mLayout->setAlignment(Qt::AlignTop);
108 mLayout->addWidget(mListWidget, 0, 0, 5,1);
109 mLayout->addWidget(mAddButton, 0, 1, 1,1),
110 mLayout->addWidget(mRemoveButton, 1, 1, 1,1),
111 mLayout->addWidget(mUpButton, 2, 1, 1,1),
112 mLayout->addWidget(mDownButton, 3, 1, 1,1),
113 mLayout->addWidget(mUseDefaultStickerCheckBox, 6, 0, 1,1),
114 mLayout->setRowStretch(0,1);
115
116 setTitle(tr("Sticker Settings"));
117 setLayout(mLayout);
118 }
119
120 void StickerSettings::loadConfig()
121 {
122 auto list = mConfig->stickerPaths();
123 for(const auto& path : list) {
124 addSticker(path, true);
125 }
126 currentRowChanged(mListWidget->currentRow());
127
128 mUseDefaultStickerCheckBox->setChecked(mConfig->useDefaultSticker());
129 }
130
131 void StickerSettings::addTriggered()
132 {
133 auto title = tr("Add Stickers");
134 auto filter = tr("Vector Image Files (*.svg)");
135 auto paths = QFileDialog::getOpenFileNames(this, title, mDirectoryPathProvider->home(), filter);
136
137 for(const auto& path : paths) {
138 addSticker(path, false);
139 }
140 }
141
142 void StickerSettings::addSticker(const QString &path, bool isSaved) const
143 {
144 auto filename = PathHelper::extractFilename(path);
145 auto item = new QListWidgetItem(QIcon(path), filename);
146 item->setData(mPathDataKey, path);
147 item->setData(mIsSavedDataKey, isSaved);
148 item->setData(mIsRemovedDataKey, false);
149 mListWidget->addItem(item);
150 }
151
152 void StickerSettings::removeTriggered()
153 {
154 auto selectedItem = mListWidget->currentItem();
155 if(selectedItem != nullptr) {
156 selectedItem->setData(mIsRemovedDataKey, true);
157 selectedItem->setHidden(true);
158 }
159 }
160
161 void StickerSettings::upTriggered()
162 {
163 auto row = mListWidget->currentRow();
164 if(row > 0) {
165 auto item = mListWidget->takeItem(row);
166 auto newRow = row - 1;
167 mListWidget->insertItem(newRow, item);
168 mListWidget->setCurrentRow(newRow);
169 mListWidget->setFocus();
170 }
171 }
172
173 void StickerSettings::downTriggered()
174 {
175 auto row = mListWidget->currentRow();
176 if(row < mListWidget->count() - 1) {
177 auto item = mListWidget->takeItem(row);
178 auto newRow = row + 1;
179 mListWidget->insertItem(newRow, item);
180 mListWidget->setCurrentRow(newRow);
181 mListWidget->setFocus();
182 }
183 }
184
185 void StickerSettings::currentRowChanged(int row)
186 {
187 mRemoveButton->setEnabled(row != -1);
188 mUpButton->setEnabled(row > 0);
189 mDownButton->setEnabled(row >= 0 && row < mListWidget->count() - 1);
190 }