From cc8cdd87c805455f3fc86c55cf0eceaa0ed0bad0 Mon Sep 17 00:00:00 2001 From: Jekyll Wu Date: Fri, 20 Apr 2012 10:30:25 +0800 Subject: [PATCH] Move class SessionModelList into its own files --- src/CMakeLists.txt | 1 + src/CopyInputDialog.h | 1 + src/SessionListModel.cpp | 134 +++++++++++++++++++++++++++++++++++++++ src/SessionListModel.h | 78 +++++++++++++++++++++++ src/SessionManager.cpp | 98 +--------------------------- src/SessionManager.h | 44 ------------- 6 files changed, 216 insertions(+), 140 deletions(-) create mode 100644 src/SessionListModel.cpp create mode 100644 src/SessionListModel.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 767920da5..7647f3cb6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -96,6 +96,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/tests/CTestCustom.cmake) Session.cpp SessionController.cpp SessionManager.cpp + SessionListModel.cpp ShellCommand.cpp TabTitleFormatButton.cpp TerminalCharacterDecoder.cpp diff --git a/src/CopyInputDialog.h b/src/CopyInputDialog.h index 8e35f4fa7..2c81c329d 100644 --- a/src/CopyInputDialog.h +++ b/src/CopyInputDialog.h @@ -30,6 +30,7 @@ // Konsole #include "SessionManager.h" #include "Session.h" +#include "SessionListModel.h" namespace Ui { diff --git a/src/SessionListModel.cpp b/src/SessionListModel.cpp new file mode 100644 index 000000000..6ebc85075 --- /dev/null +++ b/src/SessionListModel.cpp @@ -0,0 +1,134 @@ +/* + This source file is part of Konsole, a terminal emulator. + + Copyright 2006-2008 by Robert Knight + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +// Own +#include "SessionListModel.h" + +// KDE +#include +#include + +// Konsole +#include "Session.h" + +using Konsole::Session; +using Konsole::SessionListModel; + +SessionListModel::SessionListModel(QObject* parent) + : QAbstractListModel(parent) +{ +} + +void SessionListModel::setSessions(const QList& sessions) +{ + _sessions = sessions; + + foreach(Session * session, sessions) { + connect(session, SIGNAL(finished()), this, SLOT(sessionFinished())); + } + + reset(); +} + +QVariant SessionListModel::data(const QModelIndex& index, int role) const +{ + Q_ASSERT(index.isValid()); + + int row = index.row(); + int column = index.column(); + + Q_ASSERT(row >= 0 && row < _sessions.count()); + Q_ASSERT(column >= 0 && column < 2); + + switch (role) { + case Qt::DisplayRole: + if (column == 1) + return _sessions[row]->title(Session::DisplayedTitleRole); + else if (column == 0) + return _sessions[row]->sessionId(); + break; + case Qt::DecorationRole: + if (column == 1) + return KIcon(_sessions[row]->iconName()); + else + return QVariant(); + } + + return QVariant(); +} + +QVariant SessionListModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Vertical) + return QVariant(); + else { + switch (section) { + case 0: + return i18nc("@item:intable The session index", "Number"); + case 1: + return i18nc("@item:intable The session title", "Title"); + default: + return QVariant(); + } + } +} + +int SessionListModel::columnCount(const QModelIndex&) const +{ + return 2; +} + +int SessionListModel::rowCount(const QModelIndex&) const +{ + return _sessions.count(); +} + +QModelIndex SessionListModel::parent(const QModelIndex&) const +{ + return QModelIndex(); +} + +void SessionListModel::sessionFinished() +{ + Session* session = qobject_cast(sender()); + int row = _sessions.indexOf(session); + + if (row != -1) { + beginRemoveRows(QModelIndex(), row, row); + sessionRemoved(session); + _sessions.removeAt(row); + endRemoveRows(); + } +} + +QModelIndex SessionListModel::index(int row, int column, const QModelIndex& parent) const +{ + if (hasIndex(row, column, parent)) + return createIndex(row, column, _sessions[row]); + else + return QModelIndex(); +} + +#include "SessionListModel.moc" diff --git a/src/SessionListModel.h b/src/SessionListModel.h new file mode 100644 index 000000000..04972cb01 --- /dev/null +++ b/src/SessionListModel.h @@ -0,0 +1,78 @@ +/* + This source file is part of Konsole, a terminal emulator. + + Copyright 2006-2008 by Robert Knight + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#ifndef SESSIONLISTMODEL_H +#define SESSIONLISTMODEL_H + +// Qt +#include +#include + +namespace Konsole +{ + +class Session; + +/** + * Item-view model which contains a flat list of sessions. + * After constructing the model, call setSessions() to set the sessions displayed + * in the list. When a session ends (after emitting the finished() signal) it is + * automatically removed from the list. + * + * The internal pointer for each item in the model (index.internalPointer()) is the + * associated Session* + */ +class SessionListModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit SessionListModel(QObject* parent = 0); + + /** + * Sets the list of sessions displayed in the model. + * To display all sessions that are currently running in the list, + * call setSessions(SessionManager::instance()->sessions()) + */ + void setSessions(const QList& sessions); + + // reimplemented from QAbstractItemModel + virtual QModelIndex index(int row, int column, const QModelIndex& parent) const; + virtual QVariant data(const QModelIndex& index, int role) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, + int role) const; + virtual int columnCount(const QModelIndex& parent) const; + virtual int rowCount(const QModelIndex& parent) const; + virtual QModelIndex parent(const QModelIndex& index) const; + +protected: + virtual void sessionRemoved(Session*) {} + +private slots: + void sessionFinished(); + +private: + QList _sessions; +}; + +} + +#endif //SESSIONLISTMODEL_H diff --git a/src/SessionManager.cpp b/src/SessionManager.cpp index 5e2444daa..da333ede3 100644 --- a/src/SessionManager.cpp +++ b/src/SessionManager.cpp @@ -28,17 +28,15 @@ #include // KDE -#include -#include #include +#include #include #include -#include // Konsole #include "Session.h" -#include "History.h" #include "ProfileManager.h" +#include "History.h" #include "Enumeration.h" using namespace Konsole; @@ -333,98 +331,6 @@ Session* SessionManager::idToSession(int id) return 0; } -SessionListModel::SessionListModel(QObject* parent) - : QAbstractListModel(parent) -{ -} - -void SessionListModel::setSessions(const QList& sessions) -{ - _sessions = sessions; - - foreach(Session * session, sessions) { - connect(session, SIGNAL(finished()), this, SLOT(sessionFinished())); - } - - reset(); -} -QVariant SessionListModel::data(const QModelIndex& index, int role) const -{ - Q_ASSERT(index.isValid()); - - int row = index.row(); - int column = index.column(); - - Q_ASSERT(row >= 0 && row < _sessions.count()); - Q_ASSERT(column >= 0 && column < 2); - - switch (role) { - case Qt::DisplayRole: - if (column == 1) - return _sessions[row]->title(Session::DisplayedTitleRole); - else if (column == 0) - return _sessions[row]->sessionId(); - break; - case Qt::DecorationRole: - if (column == 1) - return KIcon(_sessions[row]->iconName()); - else - return QVariant(); - } - - return QVariant(); -} -QVariant SessionListModel::headerData(int section, Qt::Orientation orientation, - int role) const -{ - if (role != Qt::DisplayRole) - return QVariant(); - - if (orientation == Qt::Vertical) - return QVariant(); - else { - switch (section) { - case 0: - return i18nc("@item:intable The session index", "Number"); - case 1: - return i18nc("@item:intable The session title", "Title"); - default: - return QVariant(); - } - } -} - -int SessionListModel::columnCount(const QModelIndex&) const -{ - return 2; -} -int SessionListModel::rowCount(const QModelIndex&) const -{ - return _sessions.count(); -} -QModelIndex SessionListModel::parent(const QModelIndex&) const -{ - return QModelIndex(); -} -void SessionListModel::sessionFinished() -{ - Session* session = qobject_cast(sender()); - int row = _sessions.indexOf(session); - - if (row != -1) { - beginRemoveRows(QModelIndex(), row, row); - sessionRemoved(session); - _sessions.removeAt(row); - endRemoveRows(); - } -} -QModelIndex SessionListModel::index(int row, int column, const QModelIndex& parent) const -{ - if (hasIndex(row, column, parent)) - return createIndex(row, column, _sessions[row]); - else - return QModelIndex(); -} #include "SessionManager.moc" diff --git a/src/SessionManager.h b/src/SessionManager.h index bd4a136c5..a7ad0fe79 100644 --- a/src/SessionManager.h +++ b/src/SessionManager.h @@ -23,10 +23,8 @@ #define SESSIONMANAGER_H // Qt -#include #include #include -#include // Konsole #include "Profile.h" @@ -160,47 +158,5 @@ private: bool _modifiedPropertiesOnly; }; -/** - * Item-view model which contains a flat list of sessions. - * After constructing the model, call setSessions() to set the sessions displayed - * in the list. When a session ends (after emitting the finished() signal) it is - * automatically removed from the list. - * - * The internal pointer for each item in the model (index.internalPointer()) is the - * associated Session* - */ -class SessionListModel : public QAbstractListModel -{ - Q_OBJECT - -public: - explicit SessionListModel(QObject* parent = 0); - - /** - * Sets the list of sessions displayed in the model. - * To display all sessions that are currently running in the list, - * call setSessions(SessionManager::instance()->sessions()) - */ - void setSessions(const QList& sessions); - - // reimplemented from QAbstractItemModel - virtual QModelIndex index(int row, int column, const QModelIndex& parent) const; - virtual QVariant data(const QModelIndex& index, int role) const; - virtual QVariant headerData(int section, Qt::Orientation orientation, - int role) const; - virtual int columnCount(const QModelIndex& parent) const; - virtual int rowCount(const QModelIndex& parent) const; - virtual QModelIndex parent(const QModelIndex& index) const; - -protected: - virtual void sessionRemoved(Session*) {} - -private slots: - void sessionFinished(); - -private: - QList _sessions; -}; - } #endif //SESSIONMANAGER_H