1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 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 General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "newprojectunitdialog.h"
18#include "ui_newprojectunitdialog.h"
19#include "../iconsmanager.h"
20#include "../utils.h"
21
22#include <QFileDialog>
23
24NewProjectUnitDialog::NewProjectUnitDialog(QWidget *parent) :
25 QDialog(parent),
26 ui(new Ui::NewProjectUnitDialog),
27 mSuffix("cpp")
28{
29 ui->setupUi(this);
30 onUpdateIcons();
31 connect(pIconsManager,&IconsManager::actionIconsUpdated,
32 this, &NewProjectUnitDialog::onUpdateIcons);
33}
34
35NewProjectUnitDialog::~NewProjectUnitDialog()
36{
37 delete ui;
38}
39
40QString NewProjectUnitDialog::folder() const
41{
42 return ui->txtFolder->text();
43}
44
45void NewProjectUnitDialog::setFolder(const QString &folderName)
46{
47 if (folderName!=folder()) {
48 ui->txtFolder->setText(folderName);
49 QDir dir(folder());
50 if (filename().isEmpty() || dir.exists(filename())) {
51 //todo change filename
52 QString newFileName;
53 QString ext;
54 if (filename().isEmpty()) {
55 ext = mSuffix;
56 } else {
57 ext = QFileInfo(filename()).suffix();
58 }
59 do {
60 newFileName = tr("untitled")+QString("%1").arg(getNewFileNumber());
61 if (!ext.isEmpty())
62 newFileName += "." + ext;
63 } while (dir.exists(newFileName));
64 setFilename(newFileName);
65 }
66 }
67}
68
69QString NewProjectUnitDialog::filename() const
70{
71 return ui->txtFilename->text();
72}
73
74void NewProjectUnitDialog::setFilename(const QString &filename)
75{
76 ui->txtFilename->setText(filename);
77}
78
79void NewProjectUnitDialog::onUpdateIcons()
80{
81 pIconsManager->setIcon(ui->btnBrowse, IconsManager::ACTION_FILE_OPEN_FOLDER);
82}
83
84void NewProjectUnitDialog::on_btnBrowse_clicked()
85{
86 QString dir = QFileDialog::getExistingDirectory(
87 this,
88 tr("Choose directory"),
89 folder()
90 );
91 if (!dir.isEmpty()) {
92 setFolder(dir);
93 }
94}
95
96void NewProjectUnitDialog::on_btnOk_clicked()
97{
98 accept();
99}
100
101
102void NewProjectUnitDialog::on_btnCancel_clicked()
103{
104 reject();
105}
106
107
108void NewProjectUnitDialog::on_txtFilename_textChanged(const QString &/*arg1*/)
109{
110 updateBtnOkStatus();
111}
112
113void NewProjectUnitDialog::updateBtnOkStatus()
114{
115 ui->btnOk->setEnabled(!ui->txtFilename->text().isEmpty()
116 && QFileInfo(ui->txtFolder->text()).isDir());
117}
118
119const QString &NewProjectUnitDialog::suffix() const
120{
121 return mSuffix;
122}
123
124void NewProjectUnitDialog::setSuffix(const QString &newSuffix)
125{
126 mSuffix = newSuffix;
127}
128
129void NewProjectUnitDialog::closeEvent(QCloseEvent */*event*/)
130{
131 reject();
132}
133
134void NewProjectUnitDialog::on_txtFolder_textChanged(const QString &/*arg1*/)
135{
136 updateBtnOkStatus();
137}
138
139