2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-09-16 12:03:10 +08:00
|
|
|
#include "newprojectdialog.h"
|
|
|
|
#include "ui_newprojectdialog.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "systemconsts.h"
|
2021-12-23 09:11:58 +08:00
|
|
|
#include "../iconsmanager.h"
|
2021-09-16 12:03:10 +08:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2021-09-16 23:51:05 +08:00
|
|
|
#include <QFileDialog>
|
2022-01-08 21:23:20 +08:00
|
|
|
#include <QPainter>
|
2021-09-16 23:51:05 +08:00
|
|
|
#include <QPushButton>
|
2021-09-16 12:03:10 +08:00
|
|
|
|
|
|
|
NewProjectDialog::NewProjectDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::NewProjectDialog)
|
|
|
|
{
|
2022-02-02 17:34:37 +08:00
|
|
|
setWindowFlag(Qt::WindowContextHelpButtonHint,false);
|
2021-09-16 12:03:10 +08:00
|
|
|
ui->setupUi(this);
|
2022-12-19 09:35:48 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
|
|
|
|
ui->lstTemplates->setItemAlignment(Qt::AlignCenter);
|
|
|
|
#endif
|
2021-09-16 12:03:10 +08:00
|
|
|
mTemplatesTabBar = new QTabBar(this);
|
2022-01-08 21:49:09 +08:00
|
|
|
mTemplatesTabBar->setExpanding(false);
|
2021-09-16 12:03:10 +08:00
|
|
|
ui->verticalLayout->insertWidget(0,mTemplatesTabBar);
|
2021-09-16 23:51:05 +08:00
|
|
|
|
2022-08-07 12:09:22 +08:00
|
|
|
readTemplateDirs();
|
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
int i=0;
|
|
|
|
QString projectName;
|
|
|
|
QString location;
|
2021-11-18 21:25:28 +08:00
|
|
|
location = excludeTrailingPathDelimiter(pSettings->dirs().projectDir());
|
2021-09-16 23:51:05 +08:00
|
|
|
while (true) {
|
|
|
|
i++;
|
2022-10-30 11:58:42 +08:00
|
|
|
projectName = QString("Project%1").arg(i);
|
2021-11-18 21:25:28 +08:00
|
|
|
QString tempLocation = includeTrailingPathDelimiter(location)+projectName;
|
|
|
|
if (!QDir(tempLocation).exists())
|
2021-09-16 23:51:05 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ui->txtProjectName->setText(projectName);
|
|
|
|
ui->txtLocation->setText(location);
|
2022-01-08 21:23:20 +08:00
|
|
|
resize(pSettings->ui().newProjectDialogWidth(),pSettings->ui().newProjectDialogHeight());
|
2021-09-16 23:51:05 +08:00
|
|
|
|
|
|
|
connect(mTemplatesTabBar,
|
|
|
|
&QTabBar::currentChanged,
|
|
|
|
this,
|
|
|
|
&NewProjectDialog::updateView
|
|
|
|
);
|
|
|
|
connect(ui->txtProjectName,
|
|
|
|
&QLineEdit::textChanged,
|
|
|
|
this,
|
|
|
|
&NewProjectDialog::updateProjectLocation);
|
2022-03-15 20:17:47 +08:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
2022-04-08 17:22:24 +08:00
|
|
|
|
|
|
|
onUpdateIcons();
|
|
|
|
connect(pIconsManager,&IconsManager::actionIconsUpdated,
|
|
|
|
this, &NewProjectDialog::onUpdateIcons);
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NewProjectDialog::~NewProjectDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
PProjectTemplate NewProjectDialog::getTemplate()
|
|
|
|
{
|
|
|
|
QListWidgetItem * item = ui->lstTemplates->currentItem();
|
|
|
|
if (!item)
|
|
|
|
return PProjectTemplate();
|
|
|
|
int index = item->data(Qt::UserRole).toInt();
|
|
|
|
return mTemplates[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
QString NewProjectDialog::getLocation()
|
|
|
|
{
|
|
|
|
return ui->txtLocation->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString NewProjectDialog::getProjectName()
|
|
|
|
{
|
|
|
|
return ui->txtProjectName->text();
|
|
|
|
}
|
|
|
|
|
2021-11-18 21:25:28 +08:00
|
|
|
bool NewProjectDialog::useAsDefaultProjectDir()
|
|
|
|
{
|
|
|
|
return ui->chkAsDefaultLocation->isChecked();
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
bool NewProjectDialog::isCProject()
|
|
|
|
{
|
|
|
|
return ui->rdCProject->isChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NewProjectDialog::isCppProject()
|
|
|
|
{
|
|
|
|
return ui->rdCppProject->isChecked();
|
|
|
|
}
|
|
|
|
|
2021-12-03 21:36:12 +08:00
|
|
|
bool NewProjectDialog::makeDefaultLanguage()
|
2021-09-16 23:51:05 +08:00
|
|
|
{
|
2021-12-03 21:36:12 +08:00
|
|
|
return ui->chkMakeDefaultLanguage->isChecked();
|
2021-09-16 23:51:05 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 12:03:10 +08:00
|
|
|
void NewProjectDialog::addTemplate(const QString &filename)
|
|
|
|
{
|
|
|
|
if (!QFile(filename).exists())
|
|
|
|
return;
|
|
|
|
PProjectTemplate t = std::make_shared<ProjectTemplate>();
|
|
|
|
t->readTemplateFile(filename);
|
2023-08-17 19:24:49 +08:00
|
|
|
Settings::PCompilerSet pSet=pSettings->compilerSets().defaultSet();
|
|
|
|
if (pSet) {
|
2023-12-17 13:53:22 +08:00
|
|
|
#ifdef ENABLE_SDCC
|
2023-08-17 19:24:49 +08:00
|
|
|
if (pSet->compilerType()==CompilerType::SDCC) {
|
|
|
|
if (t->options().type==ProjectType::MicroController)
|
|
|
|
mTemplates.append(t);
|
2023-12-17 13:53:22 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2023-08-17 19:24:49 +08:00
|
|
|
if (t->options().type!=ProjectType::MicroController)
|
|
|
|
mTemplates.append(t);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
mTemplates.append(t);
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:09:22 +08:00
|
|
|
void NewProjectDialog::readTemplateDirs()
|
2021-09-16 12:03:10 +08:00
|
|
|
{
|
2022-03-15 20:17:47 +08:00
|
|
|
addTemplate(":/templates/empty.template");
|
2022-08-07 12:09:22 +08:00
|
|
|
readTemplateDir(pSettings->dirs().data(Settings::Dirs::DataType::Template));
|
|
|
|
readTemplateDir(pSettings->dirs().config(Settings::Dirs::DataType::Template));
|
|
|
|
rebuildTabs();
|
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewProjectDialog::readTemplateDir(const QString& folderPath)
|
|
|
|
{
|
|
|
|
|
2021-09-16 12:03:10 +08:00
|
|
|
QString templateExt(".");
|
|
|
|
templateExt += TEMPLATE_EXT;
|
2022-08-07 12:09:22 +08:00
|
|
|
QDir dir(folderPath);
|
|
|
|
if (!dir.exists())
|
|
|
|
return;
|
2021-09-16 12:03:10 +08:00
|
|
|
foreach (const QFileInfo& fileInfo,dir.entryInfoList()) {
|
|
|
|
if (fileInfo.isFile()
|
|
|
|
&& fileInfo.fileName().endsWith(templateExt)) {
|
|
|
|
addTemplate(fileInfo.absoluteFilePath());
|
2022-08-07 12:09:22 +08:00
|
|
|
} else if (fileInfo.isDir()) {
|
|
|
|
QDir subDir(fileInfo.absoluteFilePath());
|
|
|
|
if (subDir.exists(TEMPLATE_INFO_FILE)) {
|
2022-10-24 17:23:37 +08:00
|
|
|
addTemplate(cleanPath(subDir.absoluteFilePath(TEMPLATE_INFO_FILE)));
|
2022-08-07 12:09:22 +08:00
|
|
|
}
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
void NewProjectDialog::rebuildTabs()
|
2021-09-16 12:03:10 +08:00
|
|
|
{
|
|
|
|
while (mTemplatesTabBar->count()>0) {
|
|
|
|
mTemplatesTabBar->removeTab(0);
|
|
|
|
}
|
2021-09-16 23:51:05 +08:00
|
|
|
|
|
|
|
mCategories.clear();
|
2021-09-16 12:03:10 +08:00
|
|
|
foreach (const PProjectTemplate& t, mTemplates) {
|
|
|
|
QString category = t->category();
|
|
|
|
if (category.isEmpty())
|
|
|
|
category = tr("Default");
|
|
|
|
// Add a page for each unique category
|
2021-09-16 23:51:05 +08:00
|
|
|
int tabIndex = mCategories.value(category,-1);
|
2021-09-16 12:03:10 +08:00
|
|
|
if (tabIndex<0) {
|
|
|
|
tabIndex = mTemplatesTabBar->addTab(category);
|
2021-09-16 23:51:05 +08:00
|
|
|
mCategories.insert(category,tabIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mTemplatesTabBar->setCurrentIndex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewProjectDialog::updateView()
|
|
|
|
{
|
|
|
|
int index = std::max(0,mTemplatesTabBar->currentIndex());
|
|
|
|
if (index>=mTemplatesTabBar->count())
|
|
|
|
return;
|
|
|
|
ui->lstTemplates->clear();
|
|
|
|
for (int i=0;i<mTemplates.count();i++) {
|
|
|
|
const PProjectTemplate& t = mTemplates[i];
|
|
|
|
QString category = t->category();
|
|
|
|
if (category.isEmpty())
|
|
|
|
category = tr("Default");
|
|
|
|
QString tabText = mTemplatesTabBar->tabText(index);
|
|
|
|
if (category == tabText) {
|
|
|
|
QListWidgetItem * item;
|
2022-10-24 17:23:37 +08:00
|
|
|
QString iconFilename = cleanPath(QFileInfo(t->fileName()).absoluteDir().absoluteFilePath(t->icon()));
|
2022-01-08 21:23:20 +08:00
|
|
|
QIcon icon=QIcon(iconFilename);
|
2021-09-16 23:51:05 +08:00
|
|
|
if (icon.isNull()) {
|
|
|
|
//todo : use an default icon
|
|
|
|
item = new QListWidgetItem(
|
|
|
|
QIcon(":/icons/images/associations/template.ico"),
|
|
|
|
t->name());
|
|
|
|
} else {
|
|
|
|
item = new QListWidgetItem(
|
|
|
|
icon,
|
|
|
|
t->name());
|
|
|
|
}
|
2022-01-08 21:23:20 +08:00
|
|
|
item->setSizeHint(QSize(font().pixelSize()*6,font().pixelSize()*2+64));
|
|
|
|
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignTop);
|
2022-01-08 11:46:39 +08:00
|
|
|
item->setData(Qt::ToolTipRole,t->name());
|
2021-09-16 23:51:05 +08:00
|
|
|
item->setData(Qt::UserRole,i);
|
|
|
|
ui->lstTemplates->addItem(item);
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-16 23:51:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewProjectDialog::updateProjectLocation()
|
|
|
|
{
|
2021-11-18 21:25:28 +08:00
|
|
|
QString newLocation = ui->txtLocation->text();
|
2021-09-16 12:03:10 +08:00
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
QListWidgetItem * current = ui->lstTemplates->currentItem();
|
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
|
|
|
|
current && !ui->txtProjectName->text().isEmpty()
|
|
|
|
);
|
|
|
|
}
|
2021-09-16 12:03:10 +08:00
|
|
|
|
2021-09-16 23:51:05 +08:00
|
|
|
void NewProjectDialog::on_lstTemplates_itemDoubleClicked(QListWidgetItem *item)
|
|
|
|
{
|
|
|
|
if (item)
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NewProjectDialog::on_lstTemplates_currentItemChanged(QListWidgetItem *current, QListWidgetItem *)
|
|
|
|
{
|
|
|
|
if (current) {
|
|
|
|
int index = current->data(Qt::UserRole).toInt();
|
|
|
|
PProjectTemplate t = mTemplates[index];
|
|
|
|
ui->lblDescription->setText(t->description());
|
2022-02-01 16:17:28 +08:00
|
|
|
if (t->options().isCpp) {
|
2021-12-03 21:36:12 +08:00
|
|
|
ui->rdCProject->setEnabled(false);
|
2021-09-16 23:51:05 +08:00
|
|
|
ui->rdCppProject->setChecked(true);
|
|
|
|
} else {
|
2021-12-03 21:36:12 +08:00
|
|
|
ui->rdCProject->setEnabled(true);
|
2022-04-25 21:48:04 +08:00
|
|
|
ui->rdCProject->setChecked(true);
|
2021-12-03 21:36:12 +08:00
|
|
|
if (pSettings->editor().defaultFileCpp()) {
|
|
|
|
ui->rdCppProject->setChecked(true);
|
|
|
|
} else {
|
|
|
|
ui->rdCProject->setChecked(true);
|
|
|
|
}
|
2021-09-16 23:51:05 +08:00
|
|
|
}
|
2023-08-18 10:04:02 +08:00
|
|
|
if (t->iconInfo().isEmpty()) {
|
|
|
|
ui->panelIconInfo->setVisible(false);
|
|
|
|
} else {
|
|
|
|
ui->panelIconInfo->setVisible(true);
|
|
|
|
ui->lblIconInfo->setText(t->iconInfo());
|
|
|
|
}
|
2021-09-16 23:51:05 +08:00
|
|
|
} else {
|
|
|
|
ui->lblDescription->setText("");
|
|
|
|
ui->rdCProject->setChecked(false);
|
|
|
|
ui->rdCppProject->setChecked(false);
|
2021-12-03 21:36:12 +08:00
|
|
|
ui->chkMakeDefaultLanguage->setChecked(false);
|
2023-08-18 10:04:02 +08:00
|
|
|
ui->panelIconInfo->setVisible(false);
|
2021-09-16 23:51:05 +08:00
|
|
|
}
|
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
|
|
|
|
current && !ui->txtProjectName->text().isEmpty()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-18 22:06:33 +08:00
|
|
|
void NewProjectDialog::on_btnBrowse_clicked()
|
2021-09-16 23:51:05 +08:00
|
|
|
{
|
|
|
|
QString dirPath = ui->txtLocation->text();
|
|
|
|
if (!QDir(dirPath).exists()) {
|
|
|
|
dirPath = pSettings->dirs().projectDir();
|
|
|
|
}
|
|
|
|
QString dir = QFileDialog::getExistingDirectory(
|
|
|
|
this,
|
2022-04-08 17:22:24 +08:00
|
|
|
tr("Choose directory"),
|
2021-09-16 23:51:05 +08:00
|
|
|
dirPath
|
|
|
|
);
|
|
|
|
if (!dir.isEmpty()) {
|
|
|
|
ui->txtLocation->setText(dir);
|
|
|
|
QListWidgetItem * current = ui->lstTemplates->currentItem();
|
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(
|
|
|
|
current && !ui->txtProjectName->text().isEmpty()
|
|
|
|
);
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-16 23:51:05 +08:00
|
|
|
|
2022-04-08 17:22:24 +08:00
|
|
|
void NewProjectDialog::onUpdateIcons()
|
2021-12-23 09:11:58 +08:00
|
|
|
{
|
|
|
|
pIconsManager->setIcon(ui->btnBrowse, IconsManager::ACTION_FILE_OPEN_FOLDER);
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:17:47 +08:00
|
|
|
|
|
|
|
void NewProjectDialog::on_btnOk_clicked()
|
|
|
|
{
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NewProjectDialog::on_btnCancel_clicked()
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|