1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Designer of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "templateoptionspage.h" |
30 | #include "ui_templateoptionspage.h" |
31 | |
32 | #include <shared_settings_p.h> |
33 | #include <iconloader_p.h> |
34 | |
35 | #include <QtDesigner/abstractformeditor.h> |
36 | #include <abstractdialoggui_p.h> |
37 | |
38 | QT_BEGIN_NAMESPACE |
39 | |
40 | namespace qdesigner_internal { |
41 | |
42 | // ----------------- TemplateOptionsWidget |
43 | TemplateOptionsWidget::TemplateOptionsWidget(QDesignerFormEditorInterface *core, QWidget *parent) : |
44 | QWidget(parent), |
45 | m_core(core), |
46 | m_ui(new Ui::TemplateOptionsWidget) |
47 | { |
48 | m_ui->setupUi(this); |
49 | |
50 | m_ui->m_addTemplatePathButton->setIcon( |
51 | qdesigner_internal::createIconSet(name: QString::fromUtf8(str: "plus.png" ))); |
52 | m_ui->m_removeTemplatePathButton->setIcon( |
53 | qdesigner_internal::createIconSet(name: QString::fromUtf8(str: "minus.png" ))); |
54 | |
55 | connect(sender: m_ui->m_templatePathListWidget, signal: &QListWidget::itemSelectionChanged, |
56 | receiver: this, slot: &TemplateOptionsWidget::templatePathSelectionChanged); |
57 | connect(sender: m_ui->m_addTemplatePathButton, signal: &QAbstractButton::clicked, |
58 | receiver: this, slot: &TemplateOptionsWidget::addTemplatePath); |
59 | connect(sender: m_ui->m_removeTemplatePathButton, signal: &QAbstractButton::clicked, |
60 | receiver: this, slot: &TemplateOptionsWidget::removeTemplatePath); |
61 | } |
62 | |
63 | TemplateOptionsWidget::~TemplateOptionsWidget() |
64 | { |
65 | delete m_ui; |
66 | } |
67 | |
68 | QStringList TemplateOptionsWidget::templatePaths() const |
69 | { |
70 | QStringList rc; |
71 | const int count = m_ui->m_templatePathListWidget->count(); |
72 | for (int i = 0; i < count; i++) { |
73 | rc += m_ui->m_templatePathListWidget->item(row: i)->text(); |
74 | } |
75 | return rc; |
76 | } |
77 | |
78 | void TemplateOptionsWidget::setTemplatePaths(const QStringList &l) |
79 | { |
80 | // add paths and select 0 |
81 | m_ui->m_templatePathListWidget->clear(); |
82 | if (l.isEmpty()) { |
83 | // disable button |
84 | templatePathSelectionChanged(); |
85 | } else { |
86 | const QStringList::const_iterator cend = l.constEnd(); |
87 | for (QStringList::const_iterator it = l.constBegin(); it != cend; ++it) |
88 | m_ui->m_templatePathListWidget->addItem(label: *it); |
89 | m_ui->m_templatePathListWidget->setCurrentItem(m_ui->m_templatePathListWidget->item(row: 0)); |
90 | } |
91 | } |
92 | |
93 | void TemplateOptionsWidget::addTemplatePath() |
94 | { |
95 | const QString templatePath = chooseTemplatePath(core: m_core, parent: this); |
96 | if (templatePath.isEmpty()) |
97 | return; |
98 | |
99 | const auto existing |
100 | = m_ui->m_templatePathListWidget->findItems(text: templatePath, flags: Qt::MatchExactly); |
101 | if (!existing.isEmpty()) |
102 | return; |
103 | |
104 | QListWidgetItem *newItem = new QListWidgetItem(templatePath); |
105 | m_ui->m_templatePathListWidget->addItem(aitem: newItem); |
106 | m_ui->m_templatePathListWidget->setCurrentItem(newItem); |
107 | } |
108 | |
109 | void TemplateOptionsWidget::removeTemplatePath() |
110 | { |
111 | const auto selectedPaths = m_ui->m_templatePathListWidget->selectedItems(); |
112 | if (selectedPaths.isEmpty()) |
113 | return; |
114 | delete selectedPaths.constFirst(); |
115 | } |
116 | |
117 | void TemplateOptionsWidget::templatePathSelectionChanged() |
118 | { |
119 | const auto selectedPaths = m_ui->m_templatePathListWidget->selectedItems(); |
120 | m_ui->m_removeTemplatePathButton->setEnabled(!selectedPaths.isEmpty()); |
121 | } |
122 | |
123 | QString TemplateOptionsWidget::chooseTemplatePath(QDesignerFormEditorInterface *core, QWidget *parent) |
124 | { |
125 | QString rc = core->dialogGui()->getExistingDirectory(parent, |
126 | caption: tr(s: "Pick a directory to save templates in" )); |
127 | if (rc.isEmpty()) |
128 | return rc; |
129 | |
130 | if (rc.endsWith(c: QDir::separator())) |
131 | rc.remove(i: rc.size() - 1, len: 1); |
132 | return rc; |
133 | } |
134 | |
135 | // ----------------- TemplateOptionsPage |
136 | TemplateOptionsPage::TemplateOptionsPage(QDesignerFormEditorInterface *core) : |
137 | m_core(core) |
138 | { |
139 | } |
140 | |
141 | QString TemplateOptionsPage::name() const |
142 | { |
143 | //: Tab in preferences dialog |
144 | return QCoreApplication::translate(context: "TemplateOptionsPage" , key: "Template Paths" ); |
145 | } |
146 | |
147 | QWidget *TemplateOptionsPage::createPage(QWidget *parent) |
148 | { |
149 | m_widget = new TemplateOptionsWidget(m_core, parent); |
150 | m_initialTemplatePaths = QDesignerSharedSettings(m_core).additionalFormTemplatePaths(); |
151 | m_widget->setTemplatePaths(m_initialTemplatePaths); |
152 | return m_widget; |
153 | } |
154 | |
155 | void TemplateOptionsPage::apply() |
156 | { |
157 | if (m_widget) { |
158 | const QStringList newTemplatePaths = m_widget->templatePaths(); |
159 | if (newTemplatePaths != m_initialTemplatePaths) { |
160 | QDesignerSharedSettings settings(m_core); |
161 | settings.setAdditionalFormTemplatePaths(newTemplatePaths); |
162 | m_initialTemplatePaths = newTemplatePaths; |
163 | } |
164 | } |
165 | } |
166 | |
167 | void TemplateOptionsPage::finish() |
168 | { |
169 | } |
170 | } |
171 | QT_END_NAMESPACE |
172 | |