File indexing completed on 2024-04-28 05:50:48

0001 /*
0002     This source file is part of Konsole, a terminal emulator.
0003 
0004     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Own
0010 #include "SessionListModel.h"
0011 
0012 // KDE
0013 #include <KLocalizedString>
0014 
0015 #include <QIcon>
0016 
0017 // Konsole
0018 #include "Session.h"
0019 
0020 using Konsole::Session;
0021 using Konsole::SessionListModel;
0022 
0023 SessionListModel::SessionListModel(QObject *parent)
0024     : QAbstractListModel(parent)
0025 {
0026 }
0027 
0028 void SessionListModel::setSessions(const QList<Session *> &sessions)
0029 {
0030     beginResetModel();
0031     _sessions = sessions;
0032 
0033     for (Session *session : sessions) {
0034         connect(session, &Konsole::Session::finished, this, &Konsole::SessionListModel::sessionFinished);
0035     }
0036 
0037     endResetModel();
0038 }
0039 
0040 QVariant SessionListModel::data(const QModelIndex &index, int role) const
0041 {
0042     Q_ASSERT(index.isValid());
0043 
0044     int row = index.row();
0045     int column = index.column();
0046 
0047     Q_ASSERT(row >= 0 && row < _sessions.count());
0048     Q_ASSERT(column >= 0 && column < 2);
0049 
0050     switch (role) {
0051     case Qt::DisplayRole:
0052         if (column == 1) {
0053             // This code is duplicated from SessionController.cpp
0054             QString title = _sessions[row]->title(Session::DisplayedTitleRole);
0055 
0056             // special handling for the "%w" marker which is replaced with the
0057             // window title set by the shell
0058             title.replace(QLatin1String("%w"), _sessions[row]->userTitle());
0059             // special handling for the "%#" marker which is replaced with the
0060             // number of the shell
0061             title.replace(QLatin1String("%#"), QString::number(_sessions[row]->sessionId()));
0062             return title;
0063         } else if (column == 0) {
0064             return _sessions[row]->sessionId();
0065         }
0066         break; // Due to the above 'column' constraints, this is never reached.
0067     case Qt::DecorationRole:
0068         if (column == 1) {
0069             return QIcon::fromTheme(_sessions[row]->iconName());
0070         } else {
0071             return QVariant();
0072         }
0073     }
0074 
0075     return QVariant();
0076 }
0077 
0078 QVariant SessionListModel::headerData(int section, Qt::Orientation orientation, int role) const
0079 {
0080     if (role != Qt::DisplayRole) {
0081         return QVariant();
0082     }
0083 
0084     if (orientation == Qt::Vertical) {
0085         return QVariant();
0086     }
0087     switch (section) {
0088     case 0:
0089         return i18nc("@item:intable The session index", "Number");
0090     case 1:
0091         return i18nc("@item:intable The session title", "Title");
0092     default:
0093         return QVariant();
0094     }
0095 }
0096 
0097 int SessionListModel::columnCount(const QModelIndex &) const
0098 {
0099     return 2;
0100 }
0101 
0102 int SessionListModel::rowCount(const QModelIndex &) const
0103 {
0104     return _sessions.count();
0105 }
0106 
0107 QModelIndex SessionListModel::parent(const QModelIndex &) const
0108 {
0109     return {};
0110 }
0111 
0112 void SessionListModel::sessionFinished(Session *session)
0113 {
0114     int row = _sessions.indexOf(session);
0115 
0116     if (row != -1) {
0117         beginRemoveRows(QModelIndex(), row, row);
0118         sessionRemoved(session);
0119         _sessions.removeAt(row);
0120         endRemoveRows();
0121     }
0122 }
0123 
0124 QModelIndex SessionListModel::index(int row, int column, const QModelIndex &parent) const
0125 {
0126     if (hasIndex(row, column, parent)) {
0127         return createIndex(row, column, _sessions[row]);
0128     }
0129     return {};
0130 }
0131 
0132 #include "moc_SessionListModel.cpp"