Warning, file /utilities/konsole/src/CheckableSessionModel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2008 Robert Knight <robertknight@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "CheckableSessionModel.h"
0008 
0009 using namespace Konsole;
0010 
0011 CheckableSessionModel::CheckableSessionModel(QObject *parent)
0012     : SessionListModel(parent)
0013     , _checkedSessions(QSet<Session *>())
0014     , _fixedSessions(QSet<Session *>())
0015     , _checkColumn(0)
0016 {
0017 }
0018 
0019 void CheckableSessionModel::setCheckColumn(int column)
0020 {
0021     beginResetModel();
0022     _checkColumn = column;
0023     endResetModel();
0024 }
0025 
0026 Qt::ItemFlags CheckableSessionModel::flags(const QModelIndex &index) const
0027 {
0028     auto *session = static_cast<Session *>(index.internalPointer());
0029 
0030     if (_fixedSessions.contains(session)) {
0031         return SessionListModel::flags(index) & ~Qt::ItemIsEnabled;
0032     }
0033     return SessionListModel::flags(index) | Qt::ItemIsUserCheckable;
0034 }
0035 
0036 QVariant CheckableSessionModel::data(const QModelIndex &index, int role) const
0037 {
0038     if (role == Qt::CheckStateRole && index.column() == _checkColumn) {
0039         auto *session = static_cast<Session *>(index.internalPointer());
0040         return QVariant::fromValue(static_cast<int>(_checkedSessions.contains(session) ? Qt::Checked : Qt::Unchecked));
0041     }
0042     return SessionListModel::data(index, role);
0043 }
0044 
0045 bool CheckableSessionModel::setData(const QModelIndex &index, const QVariant &value, int role)
0046 {
0047     if (role == Qt::CheckStateRole && index.column() == _checkColumn) {
0048         auto *session = static_cast<Session *>(index.internalPointer());
0049 
0050         if (_fixedSessions.contains(session)) {
0051             return false;
0052         }
0053 
0054         if (value.toInt() == Qt::Checked) {
0055             _checkedSessions.insert(session);
0056         } else {
0057             _checkedSessions.remove(session);
0058         }
0059 
0060         Q_EMIT dataChanged(index, index);
0061         return true;
0062     }
0063     return SessionListModel::setData(index, value, role);
0064 }
0065 
0066 void CheckableSessionModel::setCheckedSessions(const QSet<Session *> &sessions)
0067 {
0068     beginResetModel();
0069     _checkedSessions = sessions;
0070     endResetModel();
0071 }
0072 
0073 QSet<Session *> CheckableSessionModel::checkedSessions() const
0074 {
0075     return _checkedSessions;
0076 }
0077 
0078 void CheckableSessionModel::setCheckable(Session *session, bool checkable)
0079 {
0080     beginResetModel();
0081 
0082     if (!checkable) {
0083         _fixedSessions.insert(session);
0084     } else {
0085         _fixedSessions.remove(session);
0086     }
0087 
0088     endResetModel();
0089 }
0090 
0091 void CheckableSessionModel::sessionRemoved(Session *session)
0092 {
0093     _checkedSessions.remove(session);
0094     _fixedSessions.remove(session);
0095 }
0096 
0097 #include "moc_CheckableSessionModel.cpp"