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 "projectgeneralwidget.h"
18#include "ui_projectgeneralwidget.h"
19#include "../project.h"
20#include "../mainwindow.h"
21#include "settings.h"
22#include "../systemconsts.h"
23#include "../iconsmanager.h"
24
25#include <QFileDialog>
26#include <QIcon>
27#include <QImageWriter>
28#include <QImageWriter>
29#include <QMessageBox>
30#include <QTextCodec>
31
32ProjectGeneralWidget::ProjectGeneralWidget(const QString &name, const QString &group, QWidget *parent) :
33 SettingsWidget(name,group,parent),
34 ui(new Ui::ProjectGeneralWidget)
35{
36 ui->setupUi(this);
37}
38
39ProjectGeneralWidget::~ProjectGeneralWidget()
40{
41 delete ui;
42}
43
44void ProjectGeneralWidget::refreshIcon()
45{
46 QPixmap icon(mIconPath);
47 ui->lbIcon->setPixmap(icon);
48}
49
50void ProjectGeneralWidget::doLoad()
51{
52 std::shared_ptr<Project> project = pMainWindow->project();
53 if (!project)
54 return;
55 ui->txtName->setText(project->name());
56 ui->txtFileName->setText(project->filename());
57 ui->txtOutputFile->setText(project->executable());
58
59 int srcCount=0,headerCount=0,resCount=0,otherCount=0, totalCount=0;
60 foreach (const PProjectUnit& unit, project->units()) {
61 switch(getFileType(unit->fileName())) {
62 case FileType::CSource:
63 case FileType::CppSource:
64 srcCount++;
65 break;
66 case FileType::CppHeader:
67 case FileType::CHeader:
68 headerCount++;
69 break;
70 case FileType::WindowsResourceSource:
71 resCount++;
72 break;
73 default:
74 otherCount++;
75 }
76 totalCount++;
77 }
78 ui->lblFiles->setText(tr("%1 files [ %2 sources, %3 headers, %4 resources, %5 other files ]")
79 .arg(totalCount).arg(srcCount).arg(headerCount)
80 .arg(resCount).arg(otherCount));
81
82 ui->cbDefaultEncoding->setCurrentText(project->options().encoding);
83
84 ui->lstType->setCurrentRow( static_cast<int>(project->options().type));
85
86 ui->cbDefaultCpp->setChecked(project->options().isCpp);
87 ui->cbSupportXPTheme->setChecked(project->options().supportXPThemes);
88 mIconPath = project->options().icon;
89 QPixmap icon(mIconPath);
90 refreshIcon();
91}
92
93void ProjectGeneralWidget::doSave()
94{
95 std::shared_ptr<Project> project = pMainWindow->project();
96 if (!project)
97 return;
98 project->setName(ui->txtName->text().trimmed());
99
100 project->options().encoding = ui->cbDefaultEncoding->currentText();
101
102 int row = std::max(0,ui->lstType->currentRow());
103 project->options().type = static_cast<ProjectType>(row);
104
105 project->options().isCpp = ui->cbDefaultCpp->isChecked();
106 project->options().supportXPThemes = ui->cbSupportXPTheme->isChecked();
107 qDebug()<<"iconpath"<<mIconPath;
108 if (mIconPath.isEmpty()
109#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
110 || ui->lbIcon->pixmap(Qt::ReturnByValue).isNull()) {
111#else
112 || !ui->lbIcon->pixmap() || ui->lbIcon->pixmap()->isNull()) {
113#endif
114 project->options().icon = "";
115 } else {
116 QString iconPath = QFileInfo(project->filename()).absoluteDir().absoluteFilePath("app.ico");
117 if (iconPath!=mIconPath) {
118 if (QFile(iconPath).exists()) {
119 if (!QFile::remove(iconPath)) {
120 QMessageBox::critical(this,
121 tr("Can't remove old icon file"),
122 tr("Can't remove old icon file '%1'")
123 .arg(iconPath),
124 QMessageBox::Ok);
125 }
126 }
127 if (!mIconPath.endsWith(".ico",PATH_SENSITIVITY) && QImageWriter::supportedImageFormats().contains("ico")) {
128#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
129 ui->lbIcon->pixmap(Qt::ReturnByValue).save(iconPath,"ico");
130#else
131 ui->lbIcon->pixmap()->save(iconPath,"ico");
132#endif
133 } else
134 copyFile(mIconPath, iconPath,true);
135 project->options().icon = iconPath;
136 mIconPath = iconPath;
137 refreshIcon();
138 }
139 }
140
141 project->saveOptions();
142}
143
144void ProjectGeneralWidget::on_btnBrowse_clicked()
145{
146 QString fileName = QFileDialog::getOpenFileName(this,
147 tr("Select icon file"),
148 pSettings->dirs().appDir(),
149 tr("Image Files (*.ico *.png *.jpg)"));
150 if (!fileName.isEmpty()) {
151 mIconPath = fileName;
152 refreshIcon();
153 setSettingsChanged();
154 }
155 ui->btnRemove->setEnabled(!mIconPath.isEmpty());
156}
157
158
159void ProjectGeneralWidget::on_btnRemove_clicked()
160{
161 mIconPath = "";
162 ui->lbIcon->setPixmap(QPixmap());
163 ui->btnRemove->setEnabled(!mIconPath.isEmpty());
164 setSettingsChanged();
165}
166
167void ProjectGeneralWidget::init()
168{
169 ui->cbDefaultEncoding->clear();
170 ui->cbDefaultEncoding->addItems(pSystemConsts->codecNames());
171 SettingsWidget::init();
172}
173
174void ProjectGeneralWidget::updateIcons(const QSize &)
175{
176 pIconsManager->setIcon(ui->btnBrowse,IconsManager::ACTION_FILE_OPEN_FOLDER);
177 pIconsManager->setIcon(ui->btnRemove, IconsManager::ACTION_MISC_CROSS);
178}
179
180