From b8f198a985d83e97c40f2e845c23c728187488d0 Mon Sep 17 00:00:00 2001 From: Tim Jenssen Date: Mon, 26 Sep 2016 12:44:25 +0200 Subject: [PATCH] Sessions: introduce SessionView This will replace the simple session list in SessionManager UI. Change-Id: Idec2fa2e4629b9986a5d274d6da5129e779e2100 Reviewed-by: Eike Ziller --- .../projectexplorer/projectexplorer.pro | 2 + .../projectexplorer/projectexplorer.qbs | 1 + src/plugins/projectexplorer/sessionmodel.cpp | 10 ++ src/plugins/projectexplorer/sessionmodel.h | 5 +- src/plugins/projectexplorer/sessionview.cpp | 134 ++++++++++++++++++ src/plugins/projectexplorer/sessionview.h | 65 +++++++++ 6 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 src/plugins/projectexplorer/sessionview.cpp create mode 100644 src/plugins/projectexplorer/sessionview.h diff --git a/src/plugins/projectexplorer/projectexplorer.pro b/src/plugins/projectexplorer/projectexplorer.pro index 526c1fca5c6..1aa114ce5c0 100644 --- a/src/plugins/projectexplorer/projectexplorer.pro +++ b/src/plugins/projectexplorer/projectexplorer.pro @@ -77,6 +77,7 @@ HEADERS += projectexplorer.h \ buildprogress.h \ projectnodes.h \ sessiondialog.h \ + sessionview.h \ projectwizardpage.h \ buildstepspage.h \ nodesvisitor.h \ @@ -224,6 +225,7 @@ SOURCES += projectexplorer.cpp \ buildprogress.cpp \ projectnodes.cpp \ sessiondialog.cpp \ + sessionview.cpp \ projectwizardpage.cpp \ buildstepspage.cpp \ nodesvisitor.cpp \ diff --git a/src/plugins/projectexplorer/projectexplorer.qbs b/src/plugins/projectexplorer/projectexplorer.qbs index 0650a70d7d9..345f173ffff 100644 --- a/src/plugins/projectexplorer/projectexplorer.qbs +++ b/src/plugins/projectexplorer/projectexplorer.qbs @@ -137,6 +137,7 @@ Project { "selectablefilesmodel.cpp", "selectablefilesmodel.h", "session.cpp", "session.h", "sessionmodel.cpp", "sessionmodel.h", + "sessionview.cpp", "sessionview.h", "sessiondialog.cpp", "sessiondialog.h", "sessiondialog.ui", "settingsaccessor.cpp", "settingsaccessor.h", "showineditortaskhandler.cpp", "showineditortaskhandler.h", diff --git a/src/plugins/projectexplorer/sessionmodel.cpp b/src/plugins/projectexplorer/sessionmodel.cpp index 23c3095a7cd..66226de8f8e 100644 --- a/src/plugins/projectexplorer/sessionmodel.cpp +++ b/src/plugins/projectexplorer/sessionmodel.cpp @@ -45,6 +45,16 @@ SessionModel::SessionModel(QObject *parent) this, &SessionModel::resetSessions); } +int SessionModel::indexOfSession(const QString &session) +{ + return SessionManager::sessions().indexOf(session); +} + +QString SessionModel::sessionAt(int row) +{ + return SessionManager::sessions().value(row, QString()); +} + int SessionModel::rowCount(const QModelIndex &) const { return SessionManager::sessions().count(); diff --git a/src/plugins/projectexplorer/sessionmodel.h b/src/plugins/projectexplorer/sessionmodel.h index 0d1f597351c..a1829d804e9 100644 --- a/src/plugins/projectexplorer/sessionmodel.h +++ b/src/plugins/projectexplorer/sessionmodel.h @@ -41,7 +41,10 @@ public: explicit SessionModel(QObject *parent = nullptr); - int rowCount(const QModelIndex &parent) const override; + int indexOfSession(const QString &session); + QString sessionAt(int row); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; QHash roleNames() const override; diff --git a/src/plugins/projectexplorer/sessionview.cpp b/src/plugins/projectexplorer/sessionview.cpp new file mode 100644 index 00000000000..a47f92f85d0 --- /dev/null +++ b/src/plugins/projectexplorer/sessionview.cpp @@ -0,0 +1,134 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "sessionview.h" + +#include "session.h" + +#include +#include +#include + +namespace ProjectExplorer { +namespace Internal { + +// custom item delegate class +class RemoveItemFocusDelegate : public QStyledItemDelegate +{ +public: + RemoveItemFocusDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) { + } + +protected: + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; +}; + +void RemoveItemFocusDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const +{ + QStyleOptionViewItem opt = option; + opt.state &= ~QStyle::State_HasFocus; + QStyledItemDelegate::paint(painter, opt, index); +} + +SessionView::SessionView(QWidget *parent) + : QTreeView(parent) +{ + setItemDelegate(new RemoveItemFocusDelegate(this)); + setSelectionBehavior(QAbstractItemView::SelectRows); + setSelectionMode(QAbstractItemView::SingleSelection); + setWordWrap(false); + setRootIsDecorated(false); + + setModel(&m_sessionModel); + + QItemSelection firstRow(m_sessionModel.index(0,0), m_sessionModel.index(0, 0)); + selectionModel()->select(firstRow, + QItemSelectionModel::QItemSelectionModel::SelectCurrent); + + connect(this, &QTreeView::activated, [this](const QModelIndex &index){ + emit activated(m_sessionModel.sessionAt(index.row())); + }); + connect(selectionModel(), &QItemSelectionModel::currentRowChanged, [this] + (const QModelIndex &index) { + emit selected(m_sessionModel.sessionAt(index.row())); + }); + connect(&m_sessionModel, &SessionModel::sessionSwitched, + this, &SessionView::sessionSwitched); + + connect(&m_sessionModel, &SessionModel::modelReset, + this, &SessionView::selectActiveSession); + } + +void SessionView::createNewSession() +{ + m_sessionModel.newSession(); +} + +void SessionView::deleteCurrentSession() +{ + m_sessionModel.deleteSession(currentSession()); +} + +void SessionView::cloneCurrentSession() +{ + m_sessionModel.cloneSession(currentSession()); +} + +void SessionView::renameCurrentSession() +{ + m_sessionModel.renameSession(currentSession()); +} + +void SessionView::switchToCurrentSession() +{ + m_sessionModel.switchToSession(currentSession()); +} + +QString SessionView::currentSession() +{ + return m_sessionModel.sessionAt(selectionModel()->currentIndex().row()); +} + +SessionModel *SessionView::sessionModel() +{ + return &m_sessionModel; +} + +void SessionView::selectActiveSession() +{ + int row = m_sessionModel.indexOfSession(SessionManager::activeSession()); + selectionModel()->setCurrentIndex(model()->index(row, 0), + QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows); +} + +void SessionView::showEvent(QShowEvent *event) +{ + QTreeView::showEvent(event); + selectActiveSession(); + setFocus(); +} + +} // namespace Internal +} // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/sessionview.h b/src/plugins/projectexplorer/sessionview.h new file mode 100644 index 00000000000..efbc576e969 --- /dev/null +++ b/src/plugins/projectexplorer/sessionview.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include "sessionmodel.h" + +#include +#include + +namespace ProjectExplorer { +namespace Internal { + +class SessionView : public QTreeView { + Q_OBJECT + +public: + explicit SessionView(QWidget *parent = Q_NULLPTR); + + void createNewSession(); + void deleteCurrentSession(); + void cloneCurrentSession(); + void renameCurrentSession(); + void switchToCurrentSession(); + + QString currentSession(); + SessionModel* sessionModel(); + void selectActiveSession(); + +protected: + void showEvent(QShowEvent* event) override; + +signals: + void activated(const QString &session); + void selected(const QString &session); + void sessionSwitched(); + +private: + SessionModel m_sessionModel; +}; + +} // namespace Internal +} // namespace ProjectExplorer