1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 or (at your option) any later version.
19 ** The licenses are as published by the Free Software Foundation
20 ** and appearing in the file LICENSE.LGPLv21 included in the packaging
21 ** of this file. Please review the following information to ensure
22 ** the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3 or (at your option) any later version
28 ** approved by the KDE Free Qt Foundation. The licenses are as published by
29 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
30 ** included in the packaging of this file. Please review the following
31 ** information to ensure the GNU General Public License requirements will
32 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
33 **
34 ****************************************************************************/
35
36 #include "workspacedialog.h"
37
38 #include "dockmanager.h"
39
40 #include <utils/algorithm.h>
41
42 #include <QInputDialog>
43 #include <QRegularExpression>
44 #include <QValidator>
45
46 namespace ADS {
47
48 class WorkspaceValidator : public QValidator
49 {
50 public:
51 WorkspaceValidator(QObject *parent, const QStringList &workspaces);
52 void fixup(QString &input) const override;
53 QValidator::State validate(QString &input, int &pos) const override;
54
55 private:
56 QStringList m_workspaces;
57 };
58
WorkspaceValidator(QObject * parent,const QStringList & workspaces)59 WorkspaceValidator::WorkspaceValidator(QObject *parent, const QStringList &workspaces)
60 : QValidator(parent)
61 , m_workspaces(workspaces)
62 {}
63
validate(QString & input,int & pos) const64 QValidator::State WorkspaceValidator::validate(QString &input, int &pos) const
65 {
66 Q_UNUSED(pos)
67
68 static const QRegularExpression rx("^[a-zA-Z0-9 ()\\-]*$");
69
70 if (!rx.match(input).hasMatch())
71 return QValidator::Invalid;
72
73 if (m_workspaces.contains(input))
74 return QValidator::Intermediate;
75 else
76 return QValidator::Acceptable;
77 }
78
fixup(QString & input) const79 void WorkspaceValidator::fixup(QString &input) const
80 {
81 int i = 2;
82 QString copy;
83 do {
84 copy = input + QLatin1String(" (") + QString::number(i) + QLatin1Char(')');
85 ++i;
86 } while (m_workspaces.contains(copy));
87 input = copy;
88 }
89
WorkspaceNameInputDialog(DockManager * manager,QWidget * parent)90 WorkspaceNameInputDialog::WorkspaceNameInputDialog(DockManager *manager, QWidget *parent)
91 : QDialog(parent)
92 , m_manager(manager)
93 {
94 auto hlayout = new QVBoxLayout(this);
95 auto label = new QLabel(tr("Enter the name of the workspace:"), this);
96 hlayout->addWidget(label);
97 m_newWorkspaceLineEdit = new QLineEdit(this);
98 m_newWorkspaceLineEdit->setValidator(new WorkspaceValidator(this, m_manager->workspaces()));
99 hlayout->addWidget(m_newWorkspaceLineEdit);
100 auto buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
101 Qt::Horizontal,
102 this);
103 m_okButton = buttons->button(QDialogButtonBox::Ok);
104 m_switchToButton = new QPushButton;
105 buttons->addButton(m_switchToButton, QDialogButtonBox::AcceptRole);
106 connect(m_switchToButton, &QPushButton::clicked, [this]() { m_usedSwitchTo = true; });
107 connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
108 connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
109 hlayout->addWidget(buttons);
110 setLayout(hlayout);
111 }
112
setActionText(const QString & actionText,const QString & openActionText)113 void WorkspaceNameInputDialog::setActionText(const QString &actionText,
114 const QString &openActionText)
115 {
116 m_okButton->setText(actionText);
117 m_switchToButton->setText(openActionText);
118 }
119
setValue(const QString & value)120 void WorkspaceNameInputDialog::setValue(const QString &value)
121 {
122 m_newWorkspaceLineEdit->setText(value);
123 }
124
value() const125 QString WorkspaceNameInputDialog::value() const
126 {
127 return m_newWorkspaceLineEdit->text();
128 }
129
isSwitchToRequested() const130 bool WorkspaceNameInputDialog::isSwitchToRequested() const
131 {
132 return m_usedSwitchTo;
133 }
134
WorkspaceDialog(DockManager * manager,QWidget * parent)135 WorkspaceDialog::WorkspaceDialog(DockManager *manager, QWidget *parent)
136 : QDialog(parent)
137 , m_manager(manager)
138 {
139 m_ui.setupUi(this);
140 m_ui.workspaceView->setActivationMode(Utils::DoubleClickActivation);
141
142 connect(m_ui.btCreateNew,
143 &QAbstractButton::clicked,
144 m_ui.workspaceView,
145 &WorkspaceView::createNewWorkspace);
146 connect(m_ui.btClone,
147 &QAbstractButton::clicked,
148 m_ui.workspaceView,
149 &WorkspaceView::cloneCurrentWorkspace);
150 connect(m_ui.btDelete,
151 &QAbstractButton::clicked,
152 m_ui.workspaceView,
153 &WorkspaceView::deleteSelectedWorkspaces);
154 connect(m_ui.btSwitch,
155 &QAbstractButton::clicked,
156 m_ui.workspaceView,
157 &WorkspaceView::switchToCurrentWorkspace);
158 connect(m_ui.btRename,
159 &QAbstractButton::clicked,
160 m_ui.workspaceView,
161 &WorkspaceView::renameCurrentWorkspace);
162 connect(m_ui.btReset,
163 &QAbstractButton::clicked,
164 m_ui.workspaceView,
165 &WorkspaceView::resetCurrentWorkspace);
166 connect(m_ui.workspaceView,
167 &WorkspaceView::workspaceActivated,
168 m_ui.workspaceView,
169 &WorkspaceView::switchToCurrentWorkspace);
170 connect(m_ui.workspaceView,
171 &WorkspaceView::workspacesSelected,
172 this,
173 &WorkspaceDialog::updateActions);
174 connect(m_ui.btImport,
175 &QAbstractButton::clicked,
176 m_ui.workspaceView,
177 &WorkspaceView::importWorkspace);
178 connect(m_ui.btExport,
179 &QAbstractButton::clicked,
180 m_ui.workspaceView,
181 &WorkspaceView::exportCurrentWorkspace);
182
183 m_ui.whatsAWorkspaceLabel->setOpenExternalLinks(true);
184
185 updateActions(m_ui.workspaceView->selectedWorkspaces());
186 }
187
setAutoLoadWorkspace(bool check)188 void WorkspaceDialog::setAutoLoadWorkspace(bool check)
189 {
190 m_ui.autoLoadCheckBox->setChecked(check);
191 }
192
autoLoadWorkspace() const193 bool WorkspaceDialog::autoLoadWorkspace() const
194 {
195 return m_ui.autoLoadCheckBox->checkState() == Qt::Checked;
196 }
197
dockManager() const198 DockManager *WorkspaceDialog::dockManager() const
199 {
200 return m_manager;
201 }
202
updateActions(const QStringList & workspaces)203 void WorkspaceDialog::updateActions(const QStringList &workspaces)
204 {
205 if (workspaces.isEmpty()) {
206 m_ui.btDelete->setEnabled(false);
207 m_ui.btRename->setEnabled(false);
208 m_ui.btClone->setEnabled(false);
209 m_ui.btReset->setEnabled(false);
210 m_ui.btSwitch->setEnabled(false);
211 m_ui.btExport->setEnabled(false);
212 return;
213 }
214 const bool presetIsSelected = Utils::anyOf(workspaces, [this](const QString &workspace) {
215 return m_manager->isWorkspacePreset(workspace);
216 });
217 const bool activeIsSelected = Utils::anyOf(workspaces, [this](const QString &workspace) {
218 return workspace == m_manager->activeWorkspace();
219 });
220 m_ui.btDelete->setEnabled(!activeIsSelected && !presetIsSelected);
221 m_ui.btRename->setEnabled(workspaces.size() == 1 && !presetIsSelected);
222 m_ui.btClone->setEnabled(workspaces.size() == 1);
223 m_ui.btReset->setEnabled(presetIsSelected);
224 m_ui.btSwitch->setEnabled(workspaces.size() == 1);
225 m_ui.btExport->setEnabled(workspaces.size() == 1);
226 }
227
228 } // namespace ADS
229