Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
templateoptionspage.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5#include "ui_templateoptionspage.h"
6
7#include <shared_settings_p.h>
8#include <iconloader_p.h>
9
10#include <QtDesigner/abstractformeditor.h>
11#include <abstractdialoggui_p.h>
12
14
15using namespace Qt::StringLiterals;
16
17namespace qdesigner_internal {
18
19// ----------------- TemplateOptionsWidget
20TemplateOptionsWidget::TemplateOptionsWidget(QDesignerFormEditorInterface *core, QWidget *parent) :
21 QWidget(parent),
22 m_core(core),
23 m_ui(new QT_PREPEND_NAMESPACE(qdesigner_internal)::Ui::TemplateOptionsWidget)
24{
25 m_ui->setupUi(this);
26
27 m_ui->m_addTemplatePathButton->setIcon(
28 qdesigner_internal::createIconSet("plus.png"_L1));
29 m_ui->m_removeTemplatePathButton->setIcon(
30 qdesigner_internal::createIconSet("minus.png"_L1));
31
32 connect(m_ui->m_templatePathListWidget, &QListWidget::itemSelectionChanged,
33 this, &TemplateOptionsWidget::templatePathSelectionChanged);
34 connect(m_ui->m_addTemplatePathButton, &QAbstractButton::clicked,
35 this, &TemplateOptionsWidget::addTemplatePath);
36 connect(m_ui->m_removeTemplatePathButton, &QAbstractButton::clicked,
37 this, &TemplateOptionsWidget::removeTemplatePath);
38}
39
41{
42 delete m_ui;
43}
44
46{
47 QStringList rc;
48 const int count = m_ui->m_templatePathListWidget->count();
49 for (int i = 0; i < count; i++) {
50 rc += m_ui->m_templatePathListWidget->item(i)->text();
51 }
52 return rc;
53}
54
55void TemplateOptionsWidget::setTemplatePaths(const QStringList &l)
56{
57 // add paths and select 0
58 m_ui->m_templatePathListWidget->clear();
59 if (l.isEmpty()) {
60 // disable button
61 templatePathSelectionChanged();
62 } else {
63 for (const auto &s : l)
64 m_ui->m_templatePathListWidget->addItem(s);
65 m_ui->m_templatePathListWidget->setCurrentItem(m_ui->m_templatePathListWidget->item(0));
66 }
67}
68
69void TemplateOptionsWidget::addTemplatePath()
70{
71 const QString templatePath = chooseTemplatePath(m_core, this);
72 if (templatePath.isEmpty())
73 return;
74
75 const auto existing
76 = m_ui->m_templatePathListWidget->findItems(templatePath, Qt::MatchExactly);
77 if (!existing.isEmpty())
78 return;
79
80 QListWidgetItem *newItem = new QListWidgetItem(templatePath);
81 m_ui->m_templatePathListWidget->addItem(newItem);
82 m_ui->m_templatePathListWidget->setCurrentItem(newItem);
83}
84
85void TemplateOptionsWidget::removeTemplatePath()
86{
87 const auto selectedPaths = m_ui->m_templatePathListWidget->selectedItems();
88 if (selectedPaths.isEmpty())
89 return;
90 delete selectedPaths.constFirst();
91}
92
93void TemplateOptionsWidget::templatePathSelectionChanged()
94{
95 const auto selectedPaths = m_ui->m_templatePathListWidget->selectedItems();
96 m_ui->m_removeTemplatePathButton->setEnabled(!selectedPaths.isEmpty());
97}
98
99QString TemplateOptionsWidget::chooseTemplatePath(QDesignerFormEditorInterface *core, QWidget *parent)
100{
101 QString rc = core->dialogGui()->getExistingDirectory(parent,
102 tr("Pick a directory to save templates in"));
103 if (rc.isEmpty())
104 return rc;
105
106 if (rc.endsWith(QDir::separator()))
107 rc.remove(rc.size() - 1, 1);
108 return rc;
109}
110
111// ----------------- TemplateOptionsPage
112TemplateOptionsPage::TemplateOptionsPage(QDesignerFormEditorInterface *core) :
113 m_core(core)
114{
115}
116
117QString TemplateOptionsPage::name() const
118{
119 //: Tab in preferences dialog
120 return QCoreApplication::translate("TemplateOptionsPage", "Template Paths");
121}
122
124{
125 m_widget = new TemplateOptionsWidget(m_core, parent);
126 m_initialTemplatePaths = QDesignerSharedSettings(m_core).additionalFormTemplatePaths();
127 m_widget->setTemplatePaths(m_initialTemplatePaths);
128 return m_widget;
129}
130
132{
133 if (m_widget) {
134 const QStringList newTemplatePaths = m_widget->templatePaths();
135 if (newTemplatePaths != m_initialTemplatePaths) {
136 QDesignerSharedSettings settings(m_core);
137 settings.setAdditionalFormTemplatePaths(newTemplatePaths);
138 m_initialTemplatePaths = newTemplatePaths;
139 }
140 }
141}
142
144{
145}
146}
147QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:421
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.