workspaceview.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "workspaceview.h"
  36. #include "dockmanager.h"
  37. #include "workspacedialog.h"
  38. #include <utils/algorithm.h>
  39. #include <QHeaderView>
  40. #include <QItemSelection>
  41. #include <QStringList>
  42. #include <QStyledItemDelegate>
  43. namespace ADS {
  44. // custom item delegate class
  45. class RemoveItemFocusDelegate : public QStyledItemDelegate
  46. {
  47. public:
  48. RemoveItemFocusDelegate(QObject *parent = nullptr)
  49. : QStyledItemDelegate(parent)
  50. {}
  51. protected:
  52. void paint(QPainter *painter,
  53. const QStyleOptionViewItem &option,
  54. const QModelIndex &index) const override;
  55. };
  56. void RemoveItemFocusDelegate::paint(QPainter *painter,
  57. const QStyleOptionViewItem &option,
  58. const QModelIndex &index) const
  59. {
  60. QStyleOptionViewItem opt = option;
  61. opt.state &= ~QStyle::State_HasFocus;
  62. QStyledItemDelegate::paint(painter, opt, index);
  63. }
  64. WorkspaceDialog *WorkspaceView::castToWorkspaceDialog(QWidget *widget)
  65. {
  66. auto dialog = qobject_cast<WorkspaceDialog *>(widget);
  67. Q_ASSERT(dialog);
  68. return dialog;
  69. }
  70. WorkspaceView::WorkspaceView(QWidget *parent)
  71. : Utils::TreeView(parent)
  72. , m_manager(WorkspaceView::castToWorkspaceDialog(parent)->dockManager())
  73. , m_workspaceModel(m_manager)
  74. {
  75. setItemDelegate(new RemoveItemFocusDelegate(this));
  76. setSelectionBehavior(QAbstractItemView::SelectRows);
  77. setSelectionMode(QAbstractItemView::ExtendedSelection);
  78. setWordWrap(false);
  79. setRootIsDecorated(false);
  80. setSortingEnabled(true);
  81. setModel(&m_workspaceModel);
  82. sortByColumn(0, Qt::AscendingOrder);
  83. // Ensure that the full workspace name is visible.
  84. header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
  85. QItemSelection firstRow(m_workspaceModel.index(0, 0),
  86. m_workspaceModel.index(0, m_workspaceModel.columnCount() - 1));
  87. selectionModel()->select(firstRow, QItemSelectionModel::QItemSelectionModel::SelectCurrent);
  88. connect(this, &Utils::TreeView::activated, [this](const QModelIndex &index) {
  89. emit activated(m_workspaceModel.workspaceAt(index.row()));
  90. });
  91. connect(selectionModel(), &QItemSelectionModel::selectionChanged, [this] {
  92. emit selected(selectedWorkspaces());
  93. });
  94. connect(&m_workspaceModel,
  95. &WorkspaceModel::workspaceSwitched,
  96. this,
  97. &WorkspaceView::workspaceSwitched);
  98. connect(&m_workspaceModel,
  99. &WorkspaceModel::modelReset,
  100. this,
  101. &WorkspaceView::selectActiveWorkspace);
  102. connect(&m_workspaceModel,
  103. &WorkspaceModel::workspaceCreated,
  104. this,
  105. &WorkspaceView::selectWorkspace);
  106. }
  107. void WorkspaceView::createNewWorkspace()
  108. {
  109. m_workspaceModel.newWorkspace(this);
  110. }
  111. void WorkspaceView::deleteSelectedWorkspaces()
  112. {
  113. deleteWorkspaces(selectedWorkspaces());
  114. }
  115. void WorkspaceView::deleteWorkspaces(const QStringList &workspaces)
  116. {
  117. m_workspaceModel.deleteWorkspaces(workspaces);
  118. }
  119. void WorkspaceView::cloneCurrentWorkspace()
  120. {
  121. m_workspaceModel.cloneWorkspace(this, currentWorkspace());
  122. }
  123. void WorkspaceView::renameCurrentWorkspace()
  124. {
  125. m_workspaceModel.renameWorkspace(this, currentWorkspace());
  126. }
  127. void WorkspaceView::switchToCurrentWorkspace()
  128. {
  129. m_workspaceModel.switchToWorkspace(currentWorkspace());
  130. }
  131. QString WorkspaceView::currentWorkspace()
  132. {
  133. return m_workspaceModel.workspaceAt(selectionModel()->currentIndex().row());
  134. }
  135. WorkspaceModel *WorkspaceView::workspaceModel()
  136. {
  137. return &m_workspaceModel;
  138. }
  139. void WorkspaceView::selectActiveWorkspace()
  140. {
  141. selectWorkspace(m_manager->activeWorkspace());
  142. }
  143. void WorkspaceView::selectWorkspace(const QString &workspaceName)
  144. {
  145. int row = m_workspaceModel.indexOfWorkspace(workspaceName);
  146. selectionModel()->setCurrentIndex(model()->index(row, 0),
  147. QItemSelectionModel::ClearAndSelect
  148. | QItemSelectionModel::Rows);
  149. }
  150. void WorkspaceView::showEvent(QShowEvent *event)
  151. {
  152. Utils::TreeView::showEvent(event);
  153. selectActiveWorkspace();
  154. setFocus();
  155. }
  156. void WorkspaceView::keyPressEvent(QKeyEvent *event)
  157. {
  158. if (event->key() != Qt::Key_Delete) {
  159. TreeView::keyPressEvent(event);
  160. return;
  161. }
  162. const QStringList workspaces = selectedWorkspaces();
  163. if (!workspaces.contains("default")
  164. && !Utils::anyOf(workspaces, [this](const QString &workspace) {
  165. return workspace == m_manager->activeWorkspace();
  166. })) {
  167. deleteWorkspaces(workspaces);
  168. }
  169. }
  170. QStringList WorkspaceView::selectedWorkspaces() const
  171. {
  172. return Utils::transform(selectionModel()->selectedRows(), [this](const QModelIndex &index) {
  173. return m_workspaceModel.workspaceAt(index.row());
  174. });
  175. }
  176. } // namespace ADS