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

0001 /*
0002  *  Copyright (c) 2007 Christian Loose <christian.loose@kdemail.net>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #include "watchersmodel.h"
0020 
0021 #include <KLocalizedString>
0022 
0023 #include "misc.h"
0024 
0025 WatchersModel::WatchersModel(const QStringList &data, QObject *parent)
0026     : QAbstractTableModel(parent)
0027 {
0028     parseData(data);
0029 }
0030 
0031 int WatchersModel::columnCount(const QModelIndex & /*parent*/) const
0032 {
0033     return 5;
0034 }
0035 
0036 int WatchersModel::rowCount(const QModelIndex & /*parent*/) const
0037 {
0038     return m_list.count();
0039 }
0040 
0041 QVariant WatchersModel::data(const QModelIndex &index, int role) const
0042 {
0043     if (!index.isValid() || index.row() < 0 || index.row() >= m_list.count())
0044         return {};
0045 
0046     WatchersEntry entry = m_list.at(index.row());
0047 
0048     if (role == Qt::DisplayRole) {
0049         switch (index.column()) {
0050         case FileColumn:
0051             return entry.file;
0052         case WatcherColumn:
0053             return entry.watcher;
0054         default:
0055             return {};
0056         }
0057     }
0058 
0059     if (role == Qt::CheckStateRole) {
0060         switch (index.column()) {
0061         case EditColumn:
0062             return entry.edit ? Qt::Checked : Qt::Unchecked;
0063         case UneditColumn:
0064             return entry.unedit ? Qt::Checked : Qt::Unchecked;
0065         case CommitColumn:
0066             return entry.commit ? Qt::Checked : Qt::Unchecked;
0067         default:
0068             return {};
0069         }
0070     }
0071 
0072     return {};
0073 }
0074 
0075 QVariant WatchersModel::headerData(int section, Qt::Orientation orientation, int role) const
0076 {
0077     // only provide text for the headers
0078     if (role != Qt::DisplayRole)
0079         return {};
0080 
0081     if (orientation == Qt::Horizontal) {
0082         switch (section) {
0083         case FileColumn:
0084             return i18n("File");
0085         case WatcherColumn:
0086             return i18n("Watcher");
0087         case EditColumn:
0088             return i18n("Edit");
0089         case UneditColumn:
0090             return i18n("Unedit");
0091         case CommitColumn:
0092             return i18n("Commit");
0093         default:
0094             return {};
0095         }
0096     }
0097 
0098     // Numbered vertical header
0099     return QString(section);
0100 }
0101 
0102 void WatchersModel::parseData(const QStringList &data)
0103 {
0104     foreach (const QString &line, data) {
0105         // parse the output line
0106         QStringList list = splitLine(line);
0107 
0108         // ignore empty lines and unknown files
0109         if (list.isEmpty() || list[0] == "?")
0110             continue;
0111 
0112         WatchersEntry entry;
0113         entry.file = list[0];
0114         entry.watcher = list[1];
0115         entry.edit = list.contains("edit");
0116         entry.unedit = list.contains("unedit");
0117         entry.commit = list.contains("commit");
0118 
0119         m_list.append(entry);
0120     }
0121 }
0122 
0123 WatchersSortModel::WatchersSortModel(QObject *parent)
0124     : QSortFilterProxyModel(parent)
0125 {
0126 }
0127 
0128 bool WatchersSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0129 {
0130     QVariant leftData = sourceModel()->data(left, Qt::CheckStateRole);
0131     QVariant rightData = sourceModel()->data(right, Qt::CheckStateRole);
0132 
0133     if (!leftData.isValid())
0134         return QSortFilterProxyModel::lessThan(left, right);
0135 
0136     return leftData.toInt() < rightData.toInt();
0137 }