1 /* GUI_EditLibrary.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_EditLibrary.h"
22 #include "Gui/Utils/ui_GUI_EditLibrary.h"
23 #include "Utils/Language/Language.h"
24 #include "Utils/FileUtils.h"
25 
26 #include <QFileDialog>
27 #include <QSizePolicy>
28 #include <QStringList>
29 
30 struct GUI_EditLibrary::Private
31 {
32 	QString oldName;
33 	QString oldPath;
34 
35 	EditMode editMode;
36 	bool nameEdited;
37 
PrivateGUI_EditLibrary::Private38 	Private() :
39 		editMode(EditMode::New),
40 		nameEdited(false) {}
41 };
42 
GUI_EditLibrary(QWidget * parent)43 GUI_EditLibrary::GUI_EditLibrary(QWidget* parent) :
44 	Dialog(parent),
45 	ui(new Ui::GUI_EditLibrary)
46 {
47 	ui->setupUi(this);
48 
49 	m = Pimpl::make<Private>();
50 
51 	ui->btnChooseDir->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
52 	ui->lePath->setFocus();
53 
54 	connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &GUI_EditLibrary::okClicked);
55 	connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &GUI_EditLibrary::cancelClicked);
56 	connect(ui->btnChooseDir, &QPushButton::clicked, this, &GUI_EditLibrary::chooseDirClicked);
57 	connect(ui->leName, &QLineEdit::textEdited, this, &GUI_EditLibrary::nameEdited);
58 }
59 
GUI_EditLibrary(const QString & name,const QString & path,QWidget * parent)60 GUI_EditLibrary::GUI_EditLibrary(const QString& name, const QString& path, QWidget* parent) :
61 	GUI_EditLibrary(parent)
62 {
63 	m->editMode = EditMode::Edit;
64 	m->nameEdited = true;
65 
66 	m->oldName = name;
67 	m->oldPath = path;
68 
69 	ui->leName->setText(name);
70 	ui->lePath->setText(path);
71 	ui->labTitle->setText(Lang::get(Lang::Edit));
72 
73 	this->setWindowTitle(ui->labTitle->text());
74 	this->setAttribute(Qt::WA_DeleteOnClose);
75 }
76 
~GUI_EditLibrary()77 GUI_EditLibrary::~GUI_EditLibrary()
78 {
79 	delete ui;
80 	ui = nullptr;
81 }
82 
okClicked()83 void GUI_EditLibrary::okClicked()
84 {
85 	close();
86 	emit sigAccepted();
87 }
88 
cancelClicked()89 void GUI_EditLibrary::cancelClicked()
90 {
91 	ui->lePath->clear();
92 	ui->leName->clear();
93 	close();
94 
95 	emit sigRejected();
96 }
97 
chooseDirClicked()98 void GUI_EditLibrary::chooseDirClicked()
99 {
100 	QString oldDir = m->oldPath;
101 	if(oldDir.isEmpty())
102 	{
103 		oldDir = QDir::homePath();
104 	}
105 
106 	QString newDir = QFileDialog::getExistingDirectory
107 		(
108 			this,
109 			Lang::get(Lang::OpenDir),
110 			oldDir,
111 			QFileDialog::ShowDirsOnly
112 		);
113 
114 	if(newDir.isEmpty())
115 	{
116 		newDir = m->oldPath;
117 	}
118 
119 	if(m->editMode == EditMode::New)
120 	{
121 		if(!m->nameEdited)
122 		{
123 			QString pureFilename = Util::File::getFilenameOfPath(newDir);
124 			ui->leName->setText(pureFilename);
125 		}
126 	}
127 
128 	ui->lePath->setText(newDir);
129 }
130 
nameEdited(const QString & text)131 void GUI_EditLibrary::nameEdited(const QString& text)
132 {
133 	m->nameEdited = (text.size() > 0);
134 }
135 
name() const136 QString GUI_EditLibrary::name() const
137 {
138 	return ui->leName->text();
139 }
140 
path() const141 QString GUI_EditLibrary::path() const
142 {
143 	return ui->lePath->text();
144 }
145 
hasNameChanged() const146 bool GUI_EditLibrary::hasNameChanged() const
147 {
148 	return (name() != m->oldName);
149 }
150 
hasPathChanged() const151 bool GUI_EditLibrary::hasPathChanged() const
152 {
153 	return (path() != m->oldPath);
154 }
155 
editMode() const156 GUI_EditLibrary::EditMode GUI_EditLibrary::editMode() const
157 {
158 	return m->editMode;
159 }
160 
reset()161 void GUI_EditLibrary::reset()
162 {
163 	ui->leName->setText(QString());
164 	ui->lePath->setText(QString());
165 
166 	m->oldName = QString();
167 	m->oldPath = QString();
168 	m->editMode = EditMode::New;
169 	m->nameEdited = false;
170 }
171 
languageChanged()172 void GUI_EditLibrary::languageChanged()
173 {
174 	Dialog::languageChanged();
175 
176 	ui->labPath->setText(Lang::get(Lang::Directory));
177 	ui->labName->setText(Lang::get(Lang::Name));
178 
179 	if(m->editMode == EditMode::New)
180 	{
181 		ui->labTitle->setText(Lang::get(Lang::New));
182 	}
183 	else
184 	{
185 		ui->labTitle->setText(Lang::get(Lang::Edit));
186 	}
187 
188 	this->setWindowTitle(ui->labTitle->text());
189 }
190 
skinChanged()191 void GUI_EditLibrary::skinChanged()
192 {
193 	Dialog::skinChanged();
194 }
195