1 /* GUI_LibraryPreferences.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "GUI_LibraryPreferences.h"
22 #include "LibraryListModel.h"
23 #include "Gui/Preferences/ui_GUI_LibraryPreferences.h"
24 
25 #include "Gui/Library/Utils/GUI_EditLibrary.h"
26 
27 #include "Gui/Utils/Icons.h"
28 #include "Gui/Utils/Delegates/StyledItemDelegate.h"
29 
30 #include "Utils/Library/SearchMode.h"
31 #include "Utils/Settings/Settings.h"
32 #include "Utils/Language/Language.h"
33 #include "Utils/Logger/Logger.h"
34 
35 #include <QShowEvent>
36 #include <QItemSelectionModel>
37 
38 struct GUI_LibraryPreferences::Private
39 {
40 	Library::Manager* libraryManager;
41 	LibraryListModel* model = nullptr;
42 
43 	Private(Library::Manager* libraryManager) :
44 		libraryManager {libraryManager} {}
45 };
46 
47 GUI_LibraryPreferences::GUI_LibraryPreferences(Library::Manager* libraryManager, const QString& identifier) :
48 	Preferences::Base(identifier)
49 {
50 	m = Pimpl::make<Private>(libraryManager);
51 }
52 
53 GUI_LibraryPreferences::~GUI_LibraryPreferences()
54 {
55 	if(ui)
56 	{
57 		delete ui;
58 		ui = nullptr;
59 	}
60 }
61 
62 void GUI_LibraryPreferences::initUi()
63 {
64 	setupParent(this, &ui);
65 
66 	m->model = new LibraryListModel(m->libraryManager, ui->lvLibs);
67 
68 	ui->lvLibs->setModel(m->model);
69 	ui->lvLibs->setItemDelegate(
70 		new Gui::StyledItemDelegate(ui->lvLibs)
71 	);
72 
73 	ui->tab_widget->setCurrentIndex(0);
74 
75 	QItemSelectionModel* sel_model = ui->lvLibs->selectionModel();
76 	connect(sel_model, &QItemSelectionModel::currentChanged, this, &GUI_LibraryPreferences::selectedIndexChanged);
77 
78 	connect(ui->btnNew, &QPushButton::clicked, this, &GUI_LibraryPreferences::newClicked);
79 	connect(ui->btnEdit, &QPushButton::clicked, this, &GUI_LibraryPreferences::editClicked);
80 	connect(ui->btnDelete, &QPushButton::clicked, this, &GUI_LibraryPreferences::deleteClicked);
81 	connect(ui->btnUp, &QPushButton::clicked, this, &GUI_LibraryPreferences::upClicked);
82 	connect(ui->btnDown, &QPushButton::clicked, this, &GUI_LibraryPreferences::downClicked);
83 
84 	revert();
85 
86 	selectedIndexChanged(m->model->index(currentRow()));
87 }
88 
89 QString GUI_LibraryPreferences::actionName() const
90 {
91 	return Lang::get(Lang::Library);
92 }
93 
94 bool GUI_LibraryPreferences::commit()
95 {
96 	SetSetting(Set::Lib_DC_DoNothing, ui->rbDcDoNothing->isChecked());
97 	SetSetting(Set::Lib_DC_PlayIfStopped, ui->rbDcPlayIfStopped->isChecked());
98 	SetSetting(Set::Lib_DC_PlayImmediately, ui->rbDcPlayImmediatly->isChecked());
99 	SetSetting(Set::Lib_DD_DoNothing, ui->rbDdDoNothing->isChecked());
100 	SetSetting(Set::Lib_DD_PlayIfStoppedAndEmpty, ui->rbDdStartIfStopped->isChecked());
101 	SetSetting(Set::Lib_UseViewClearButton, ui->cbShowClearSelectionButton->isChecked());
102 	SetSetting(Set::Lib_SortIgnoreArtistArticle, ui->cbIgnoreArticle->isChecked());
103 
104 	return m->model->commit();
105 }
106 
107 void GUI_LibraryPreferences::revert()
108 {
109 	ui->rbDcDoNothing->setChecked(GetSetting(Set::Lib_DC_DoNothing));
110 	ui->rbDcPlayIfStopped->setChecked(GetSetting(Set::Lib_DC_PlayIfStopped));
111 	ui->rbDcPlayImmediatly->setChecked(GetSetting(Set::Lib_DC_PlayImmediately));
112 	ui->rbDdDoNothing->setChecked(GetSetting(Set::Lib_DD_DoNothing));
113 	ui->rbDdStartIfStopped->setChecked(GetSetting(Set::Lib_DD_PlayIfStoppedAndEmpty));
114 	ui->cbShowClearSelectionButton->setChecked(GetSetting(Set::Lib_UseViewClearButton));
115 	ui->cbIgnoreArticle->setChecked(GetSetting(Set::Lib_SortIgnoreArtistArticle));
116 
117 	m->model->reset();
118 }
119 
120 void GUI_LibraryPreferences::retranslate()
121 {
122 	ui->retranslateUi(this);
123 
124 	ui->btnNew->setText(Lang::get(Lang::New));
125 	ui->btnEdit->setText(Lang::get(Lang::Edit));
126 	ui->btnDelete->setText(Lang::get(Lang::Remove));
127 	ui->btnDown->setText(Lang::get(Lang::MoveDown));
128 	ui->btnUp->setText(Lang::get(Lang::MoveUp));
129 }
130 
131 void GUI_LibraryPreferences::skinChanged()
132 {
133 	if(ui)
134 	{
135 		ui->btnNew->setIcon(Gui::Icons::icon(Gui::Icons::New));
136 		ui->btnEdit->setIcon(Gui::Icons::icon(Gui::Icons::Edit));
137 		ui->btnDelete->setIcon(Gui::Icons::icon(Gui::Icons::Remove));
138 	}
139 }
140 
141 void GUI_LibraryPreferences::showEvent(QShowEvent* e)
142 {
143 	Base::showEvent(e);
144 	this->revert();
145 }
146 
147 QString GUI_LibraryPreferences::errorString() const
148 {
149 	return tr("Cannot edit library");
150 }
151 
152 int GUI_LibraryPreferences::currentRow() const
153 {
154 	return ui->lvLibs->selectionModel()->currentIndex().row();
155 }
156 
157 void GUI_LibraryPreferences::newClicked()
158 {
159 	GUI_EditLibrary* edit_dialog = new GUI_EditLibrary(this);
160 
161 	connect(edit_dialog, &GUI_EditLibrary::sigAccepted, this, &GUI_LibraryPreferences::editDialogAccepted);
162 
163 	edit_dialog->show();
164 }
165 
166 void GUI_LibraryPreferences::editClicked()
167 {
168 	int cur_row = currentRow();
169 	if(cur_row < 0)
170 	{
171 		return;
172 	}
173 
174 	QString name = m->model->name(cur_row);
175 	QString path = m->model->path(cur_row);
176 
177 	GUI_EditLibrary* edit_dialog = new GUI_EditLibrary(name, path, this);
178 
179 	connect(edit_dialog, &GUI_EditLibrary::sigAccepted, this, &GUI_LibraryPreferences::editDialogAccepted);
180 
181 	edit_dialog->show();
182 }
183 
184 void GUI_LibraryPreferences::deleteClicked()
185 {
186 	QModelIndex idx = ui->lvLibs->currentIndex();
187 	if(!idx.isValid())
188 	{
189 		return;
190 	}
191 
192 	m->model->removeRow(idx.row());
193 }
194 
195 void GUI_LibraryPreferences::upClicked()
196 {
197 	int row = ui->lvLibs->currentIndex().row();
198 
199 	m->model->moveRow(row, row - 1);
200 	ui->lvLibs->setCurrentIndex(m->model->index(row - 1));
201 }
202 
203 void GUI_LibraryPreferences::downClicked()
204 {
205 	int row = ui->lvLibs->currentIndex().row();
206 
207 	m->model->moveRow(row, row + 1);
208 	ui->lvLibs->setCurrentIndex(m->model->index(row + 1));
209 }
210 
211 void GUI_LibraryPreferences::editDialogAccepted()
212 {
213 	auto* edit_dialog = static_cast<GUI_EditLibrary*>(sender());
214 
215 	GUI_EditLibrary::EditMode edit_mode = edit_dialog->editMode();
216 
217 	QString name = edit_dialog->name();
218 	QString path = edit_dialog->path();
219 
220 	switch(edit_mode)
221 	{
222 		case GUI_EditLibrary::EditMode::New:
223 		{
224 			if(!name.isEmpty() && !path.isEmpty())
225 			{
226 				m->model->appendRow(name, path);
227 			}
228 
229 		}
230 			break;
231 
232 		case GUI_EditLibrary::EditMode::Edit:
233 		{
234 			if(!name.isEmpty())
235 			{
236 				if(edit_dialog->hasNameChanged())
237 				{
238 					m->model->renameRow(currentRow(), name);
239 				}
240 			}
241 
242 			if(!path.isEmpty())
243 			{
244 				if(edit_dialog->hasPathChanged())
245 				{
246 					m->model->changePath(currentRow(), path);
247 				}
248 			}
249 
250 		}
251 			break;
252 
253 		default:
254 			break;
255 	}
256 
257 	edit_dialog->deleteLater();
258 }
259 
260 void GUI_LibraryPreferences::selectedIndexChanged(const QModelIndex& idx)
261 {
262 	int curentRow = idx.row();
263 	int rowCount = ui->lvLibs->model()->rowCount();
264 
265 	ui->btnUp->setDisabled(curentRow <= 0 || curentRow >= rowCount);
266 	ui->btnDown->setDisabled(curentRow < 0 || curentRow >= rowCount - 1);
267 	ui->btnDelete->setDisabled(curentRow < 0 || curentRow >= rowCount);
268 	ui->btnEdit->setDisabled(curentRow < 0 || curentRow >= rowCount);
269 
270 	ui->labCurrentPath->setVisible(curentRow >= 0 || curentRow < rowCount);
271 	if(curentRow < 0 || curentRow >= rowCount)
272 	{
273 		return;
274 	}
275 
276 	QString path = m->model->path(curentRow);
277 	ui->labCurrentPath->setText(path);
278 }
279 
280