workspacedialog.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include "workspacedialog.h"
  36. #include "dockmanager.h"
  37. #include <utils/algorithm.h>
  38. #include <QInputDialog>
  39. #include <QValidator>
  40. namespace ADS {
  41. class WorkspaceValidator : public QValidator
  42. {
  43. public:
  44. WorkspaceValidator(QObject *parent, const QStringList &workspaces);
  45. void fixup(QString &input) const override;
  46. QValidator::State validate(QString &input, int &pos) const override;
  47. private:
  48. QStringList m_workspaces;
  49. };
  50. WorkspaceValidator::WorkspaceValidator(QObject *parent, const QStringList &workspaces)
  51. : QValidator(parent)
  52. , m_workspaces(workspaces)
  53. {}
  54. QValidator::State WorkspaceValidator::validate(QString &input, int &pos) const
  55. {
  56. Q_UNUSED(pos)
  57. if (input.contains(QLatin1Char('/')) || input.contains(QLatin1Char(':'))
  58. || input.contains(QLatin1Char('\\')) || input.contains(QLatin1Char('?'))
  59. || input.contains(QLatin1Char('*')) || input.contains(QLatin1Char('_')))
  60. return QValidator::Invalid;
  61. if (m_workspaces.contains(input))
  62. return QValidator::Intermediate;
  63. else
  64. return QValidator::Acceptable;
  65. }
  66. void WorkspaceValidator::fixup(QString &input) const
  67. {
  68. int i = 2;
  69. QString copy;
  70. do {
  71. copy = input + QLatin1String(" (") + QString::number(i) + QLatin1Char(')');
  72. ++i;
  73. } while (m_workspaces.contains(copy));
  74. input = copy;
  75. }
  76. WorkspaceNameInputDialog::WorkspaceNameInputDialog(DockManager *manager, QWidget *parent)
  77. : QDialog(parent)
  78. , m_manager(manager)
  79. {
  80. auto hlayout = new QVBoxLayout(this);
  81. auto label = new QLabel(tr("Enter the name of the workspace:"), this);
  82. hlayout->addWidget(label);
  83. m_newWorkspaceLineEdit = new QLineEdit(this);
  84. m_newWorkspaceLineEdit->setValidator(new WorkspaceValidator(this, m_manager->workspaces()));
  85. hlayout->addWidget(m_newWorkspaceLineEdit);
  86. auto buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
  87. Qt::Horizontal,
  88. this);
  89. m_okButton = buttons->button(QDialogButtonBox::Ok);
  90. m_switchToButton = new QPushButton;
  91. buttons->addButton(m_switchToButton, QDialogButtonBox::AcceptRole);
  92. connect(m_switchToButton, &QPushButton::clicked, [this]() { m_usedSwitchTo = true; });
  93. connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
  94. connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
  95. hlayout->addWidget(buttons);
  96. setLayout(hlayout);
  97. }
  98. void WorkspaceNameInputDialog::setActionText(const QString &actionText,
  99. const QString &openActionText)
  100. {
  101. m_okButton->setText(actionText);
  102. m_switchToButton->setText(openActionText);
  103. }
  104. void WorkspaceNameInputDialog::setValue(const QString &value)
  105. {
  106. m_newWorkspaceLineEdit->setText(value);
  107. }
  108. QString WorkspaceNameInputDialog::value() const
  109. {
  110. return m_newWorkspaceLineEdit->text();
  111. }
  112. bool WorkspaceNameInputDialog::isSwitchToRequested() const
  113. {
  114. return m_usedSwitchTo;
  115. }
  116. WorkspaceDialog::WorkspaceDialog(DockManager *manager, QWidget *parent)
  117. : QDialog(parent)
  118. , m_manager(manager)
  119. {
  120. m_ui.setupUi(this);
  121. m_ui.workspaceView->setActivationMode(Utils::DoubleClickActivation);
  122. connect(m_ui.btCreateNew,
  123. &QAbstractButton::clicked,
  124. m_ui.workspaceView,
  125. &WorkspaceView::createNewWorkspace);
  126. connect(m_ui.btClone,
  127. &QAbstractButton::clicked,
  128. m_ui.workspaceView,
  129. &WorkspaceView::cloneCurrentWorkspace);
  130. connect(m_ui.btDelete,
  131. &QAbstractButton::clicked,
  132. m_ui.workspaceView,
  133. &WorkspaceView::deleteSelectedWorkspaces);
  134. connect(m_ui.btSwitch,
  135. &QAbstractButton::clicked,
  136. m_ui.workspaceView,
  137. &WorkspaceView::switchToCurrentWorkspace);
  138. connect(m_ui.btRename,
  139. &QAbstractButton::clicked,
  140. m_ui.workspaceView,
  141. &WorkspaceView::renameCurrentWorkspace);
  142. connect(m_ui.workspaceView,
  143. &WorkspaceView::activated,
  144. m_ui.workspaceView,
  145. &WorkspaceView::switchToCurrentWorkspace);
  146. connect(m_ui.workspaceView, &WorkspaceView::selected, this, &WorkspaceDialog::updateActions);
  147. connect(m_ui.workspaceView, &WorkspaceView::workspaceSwitched, this, &QDialog::reject);
  148. m_ui.whatsAWorkspaceLabel->setOpenExternalLinks(true);
  149. }
  150. void WorkspaceDialog::setAutoLoadWorkspace(bool check)
  151. {
  152. m_ui.autoLoadCheckBox->setChecked(check);
  153. }
  154. bool WorkspaceDialog::autoLoadWorkspace() const
  155. {
  156. return m_ui.autoLoadCheckBox->checkState() == Qt::Checked;
  157. }
  158. DockManager *WorkspaceDialog::dockManager() const
  159. {
  160. return m_manager;
  161. }
  162. void WorkspaceDialog::updateActions(const QStringList &workspaces)
  163. {
  164. if (workspaces.isEmpty()) {
  165. m_ui.btDelete->setEnabled(false);
  166. m_ui.btRename->setEnabled(false);
  167. m_ui.btClone->setEnabled(false);
  168. m_ui.btSwitch->setEnabled(false);
  169. return;
  170. }
  171. const bool defaultIsSelected = workspaces.contains("default"); // TODO use const var
  172. const bool activeIsSelected = Utils::anyOf(workspaces, [this](const QString &workspace) {
  173. return workspace == m_manager->activeWorkspace();
  174. });
  175. m_ui.btDelete->setEnabled(!defaultIsSelected && !activeIsSelected);
  176. m_ui.btRename->setEnabled(workspaces.size() == 1 && !defaultIsSelected);
  177. m_ui.btClone->setEnabled(workspaces.size() == 1);
  178. m_ui.btSwitch->setEnabled(workspaces.size() == 1);
  179. }
  180. } // namespace ADS