1 | #include "newtemplatedialog.h" |
2 | #include "ui_newtemplatedialog.h" |
3 | #include "../settings.h" |
4 | #include "../projecttemplate.h" |
5 | #include "../systemconsts.h" |
6 | |
7 | #include <QFile> |
8 | #include <QDir> |
9 | #include <QFileInfo> |
10 | |
11 | NewTemplateDialog::NewTemplateDialog(QWidget *parent) : |
12 | QDialog(parent), |
13 | ui(new Ui::NewTemplateDialog) |
14 | { |
15 | ui->setupUi(this); |
16 | QStringList categories = findCategories(); |
17 | ui->cbCategory->addItems(categories); |
18 | updateCreateState(); |
19 | } |
20 | |
21 | NewTemplateDialog::~NewTemplateDialog() |
22 | { |
23 | delete ui; |
24 | } |
25 | |
26 | QString NewTemplateDialog::getName() const |
27 | { |
28 | return ui->txtName->text(); |
29 | } |
30 | |
31 | QString NewTemplateDialog::getDescription() const |
32 | { |
33 | return ui->txtDescription->toPlainText(); |
34 | } |
35 | |
36 | QString NewTemplateDialog::getCategory() const |
37 | { |
38 | return ui->cbCategory->currentText(); |
39 | } |
40 | |
41 | QStringList NewTemplateDialog::findCategories() |
42 | { |
43 | QSet<QString> categories; |
44 | readTemplateCategory(":/templates/empty.template" ,categories); |
45 | readTemplateCategoriesInDir(pSettings->dirs().data(Settings::Dirs::DataType::Template),categories); |
46 | readTemplateCategoriesInDir(pSettings->dirs().config(Settings::Dirs::DataType::Template),categories); |
47 | QStringList result; |
48 | foreach(const QString& s, categories) |
49 | result.append(s); |
50 | result.sort(); |
51 | return result; |
52 | } |
53 | |
54 | void NewTemplateDialog::readTemplateCategory(const QString &filename, QSet<QString> &categories) |
55 | { |
56 | if (!QFile(filename).exists()) |
57 | return; |
58 | PProjectTemplate t = std::make_shared<ProjectTemplate>(); |
59 | t->readTemplateFile(filename); |
60 | if (!t->category().isEmpty()) |
61 | categories.insert(t->category()); |
62 | } |
63 | |
64 | void NewTemplateDialog::readTemplateCategoriesInDir(const QString &folderPath, QSet<QString> &categories) |
65 | { |
66 | QString templateExt("." ); |
67 | templateExt += TEMPLATE_EXT; |
68 | QDir dir(folderPath); |
69 | if (!dir.exists()) |
70 | return; |
71 | foreach (const QFileInfo& fileInfo,dir.entryInfoList()) { |
72 | if (fileInfo.isFile() |
73 | && fileInfo.fileName().endsWith(templateExt)) { |
74 | readTemplateCategory(fileInfo.absoluteFilePath(),categories); |
75 | } else if (fileInfo.isDir()) { |
76 | QDir subDir(fileInfo.absoluteFilePath()); |
77 | readTemplateCategory(subDir.absoluteFilePath(TEMPLATE_INFO_FILE),categories); |
78 | } |
79 | } |
80 | |
81 | } |
82 | |
83 | void NewTemplateDialog::updateCreateState() |
84 | { |
85 | ui->btnCreate->setEnabled( |
86 | !ui->txtName->text().isEmpty() |
87 | && !ui->cbCategory->currentText().isEmpty() |
88 | ); |
89 | } |
90 | |
91 | void NewTemplateDialog::closeEvent(QCloseEvent */*event*/) |
92 | { |
93 | reject(); |
94 | } |
95 | |
96 | void NewTemplateDialog::on_btnCreate_clicked() |
97 | { |
98 | accept(); |
99 | } |
100 | |
101 | |
102 | void NewTemplateDialog::on_btnCancel_clicked() |
103 | { |
104 | reject(); |
105 | } |
106 | |
107 | |
108 | void NewTemplateDialog::on_txtName_textChanged(const QString &/*arg1*/) |
109 | { |
110 | updateCreateState(); |
111 | } |
112 | |
113 | |
114 | void NewTemplateDialog::on_cbCategory_currentTextChanged(const QString &/*arg1*/) |
115 | { |
116 | updateCreateState(); |
117 | } |
118 | |
119 | |