File indexing completed on 2024-05-19 05:45:01

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 
0021 #include "commitmodel.h"
0022 #include "commitmodelhelper.h"
0023 
0024 #include "svnqt/commititem.h"
0025 
0026 #include <KLocalizedString>
0027 
0028 CommitModel::CommitModel(const svn::CommitItemList &aList, QObject *parent)
0029     : QAbstractItemModel(parent)
0030 {
0031     setCommitData(aList);
0032 }
0033 
0034 /*********************
0035  * Begin CommitModel *
0036  *********************/
0037 CommitModel::CommitModel(const CommitActionEntries &_checked, const CommitActionEntries &_notchecked, QObject *parent)
0038     : QAbstractItemModel(parent)
0039 {
0040     setCommitData(_checked, _notchecked);
0041 }
0042 
0043 void CommitModel::setCommitData(const svn::CommitItemList &aList)
0044 {
0045     if (!m_List.isEmpty()) {
0046         beginRemoveRows(QModelIndex(), 0, m_List.count() - 1);
0047         m_List.clear();
0048         endRemoveRows();
0049     }
0050 
0051     if (!aList.isEmpty()) {
0052         m_List.reserve(aList.size());
0053         beginInsertRows(QModelIndex(), 0, aList.size() - 1);
0054         for (const auto &item : aList) {
0055             m_List.append(CommitModelNodePtr(new CommitModelNode(item)));
0056         }
0057         endInsertRows();
0058     }
0059 }
0060 
0061 void CommitModel::setCommitData(const CommitActionEntries &checked, const CommitActionEntries &notchecked)
0062 {
0063     if (!m_List.isEmpty()) {
0064         beginRemoveRows(QModelIndex(), 0, m_List.count() - 1);
0065         m_List.clear();
0066         endRemoveRows();
0067     }
0068 
0069     const int totalSize = checked.size() + notchecked.size();
0070     if (totalSize > 0) {
0071         m_List.reserve(totalSize);
0072         beginInsertRows(QModelIndex(), 0, totalSize - 1);
0073         for (const auto &entry : checked) {
0074             m_List.append(CommitModelNodePtr(new CommitModelNode(entry, true)));
0075         }
0076         for (const auto &entry : notchecked) {
0077             m_List.append(CommitModelNodePtr(new CommitModelNode(entry, false)));
0078         }
0079         endInsertRows();
0080     }
0081 }
0082 
0083 int CommitModel::ActionColumn() const
0084 {
0085     return 0;
0086 }
0087 
0088 int CommitModel::ItemColumn() const
0089 {
0090     return 1;
0091 }
0092 
0093 CommitModelNodePtr CommitModel::node(const QModelIndex &index)
0094 {
0095     if (!index.isValid() || index.row() >= m_List.count()) {
0096         return CommitModelNodePtr();
0097     }
0098     return m_List.at(index.row());
0099 }
0100 
0101 CommitActionEntries CommitModel::checkedEntries() const
0102 {
0103     CommitActionEntries res;
0104     for (const auto &entry : m_List) {
0105         if (entry->checked()) {
0106             res.append(entry->actionEntry());
0107         }
0108     }
0109     return res;
0110 }
0111 
0112 void CommitModel::markItems(bool mark, CommitActionEntry::ACTION_TYPE _type)
0113 {
0114     QVariant v = mark ? int(Qt::Checked) : int(Qt::Unchecked);
0115     for (int i = 0; i < m_List.count(); ++i) {
0116         if (m_List.at(i)->actionEntry().type() & _type) {
0117             QModelIndex _index = index(i, 0, QModelIndex());
0118             setData(_index, v, Qt::CheckStateRole);
0119         }
0120     }
0121 }
0122 
0123 /*!
0124     \fn CommitModel::removeEntries(const QStringList&)
0125  */
0126 void CommitModel::removeEntries(const QStringList &_items)
0127 {
0128     QStringList items = _items;
0129     // items is normally much smaller than m_List, therefore
0130     // iterate over the items in the inner loop
0131     for (int j = m_List.count() - 1; j >= 0; --j) {
0132         const QString aeName = m_List.at(j)->actionEntry().name();
0133         for (int i = items.size() - 1; i >= 0; --i) {
0134             if (aeName == items.at(i)) {
0135                 beginRemoveRows(QModelIndex(), j, j);
0136                 m_List.remove(j);
0137                 endRemoveRows();
0138                 items.removeAt(i);
0139                 break; // break inner loop
0140             }
0141         }
0142         if (items.isEmpty())
0143             break;
0144     }
0145 }
0146 
0147 const CommitModelNodePtr CommitModel::dataForRow(int row) const
0148 {
0149     if (row < 0 || row >= m_List.size())
0150         return CommitModelNodePtr();
0151     return m_List.at(row);
0152 }
0153 
0154 /************************************
0155  * begin overload of Model methods  *
0156  ************************************/
0157 QModelIndex CommitModel::index(int row, int column, const QModelIndex & /*parent*/) const
0158 {
0159     if (row < 0 || row >= m_List.count()) {
0160         return QModelIndex();
0161     }
0162     const CommitModelNodePtr &n = m_List.at(row);
0163     return createIndex(row, column, n.data());
0164 }
0165 
0166 QModelIndex CommitModel::parent(const QModelIndex &) const
0167 {
0168     // we have no tree...
0169     return QModelIndex();
0170 }
0171 
0172 QVariant CommitModel::data(const QModelIndex &index, int role) const
0173 {
0174     if (!index.isValid() || index.row() >= m_List.count() || role != Qt::DisplayRole) {
0175         return QVariant();
0176     }
0177     const CommitModelNodePtr &n = m_List.at(index.row());
0178     if (index.column() == ActionColumn()) {
0179         return n->actionEntry().action();
0180     }
0181     if (index.column() == ItemColumn()) {
0182         return n->actionEntry().name();
0183     }
0184     return QVariant();
0185 }
0186 
0187 QVariant CommitModel::headerData(int section, Qt::Orientation orientation, int role) const
0188 {
0189     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
0190         if (section == ActionColumn()) {
0191             return i18n("Action");
0192         }
0193         if (section == ItemColumn()) {
0194             return i18n("Entry");
0195         }
0196     }
0197     return QAbstractItemModel::headerData(section, orientation, role);
0198 }
0199 
0200 int CommitModel::rowCount(const QModelIndex &) const
0201 {
0202     return m_List.count();
0203 }
0204 
0205 int CommitModel::columnCount(const QModelIndex &) const
0206 {
0207     return 2;
0208 }
0209 /************************************
0210  * end overload of Model methods    *
0211  ************************************/
0212 /*********************
0213  * end CommitModel   *
0214  *********************/
0215 
0216 /************************************
0217  * begin CommitModelCheckitem       *
0218  ************************************/
0219 CommitModelCheckitem::CommitModelCheckitem(const CommitActionEntries &_checked, const CommitActionEntries &_notchecked, QObject *parent)
0220     : CommitModel(_checked, _notchecked, parent)
0221 {
0222 }
0223 
0224 int CommitModelCheckitem::ActionColumn() const
0225 {
0226     return 1;
0227 }
0228 
0229 int CommitModelCheckitem::ItemColumn() const
0230 {
0231     return 0;
0232 }
0233 
0234 Qt::ItemFlags CommitModelCheckitem::flags(const QModelIndex &index) const
0235 {
0236     if (index.isValid() && index.column() == ItemColumn()) {
0237         return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
0238     }
0239     return CommitModel::flags(index);
0240 }
0241 
0242 QVariant CommitModelCheckitem::data(const QModelIndex &index, int role) const
0243 {
0244     if (index.column() != ItemColumn() || role != Qt::CheckStateRole || !index.isValid() || index.row() >= m_List.count()) {
0245         return CommitModel::data(index, role);
0246     }
0247     if (m_List.at(index.row())->checked()) {
0248         return Qt::Checked;
0249     }
0250     return Qt::Unchecked;
0251 }
0252 
0253 bool CommitModelCheckitem::setData(const QModelIndex &index, const QVariant &value, int role)
0254 {
0255     if (index.column() != ItemColumn() || role != Qt::CheckStateRole || !index.isValid() || index.row() >= m_List.count()) {
0256         return CommitModel::setData(index, value, role);
0257     }
0258     if (value.type() == QVariant::Int) {
0259         CommitModelNodePtr _l = m_List.at(index.row());
0260         bool old = _l->checked();
0261         bool nv = value.toInt() > 0;
0262         _l->setChecked(nv);
0263         if (old != nv) {
0264             emit dataChanged(index, index, {Qt::CheckStateRole});
0265         }
0266         return old != nv;
0267     }
0268     return false;
0269 }
0270 /************************************
0271  * end CommitModelCheckitem         *
0272  ************************************/
0273 
0274 /***************************
0275  * Begin CommitFilterModel *
0276  ***************************/
0277 void CommitFilterModel::setSourceModel(QAbstractItemModel *sourceModel)
0278 {
0279     m_sourceModel = qobject_cast<CommitModel *>(sourceModel);
0280     QSortFilterProxyModel::setSourceModel(sourceModel);
0281 }
0282 
0283 bool CommitFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0284 {
0285     if (!m_sourceModel || source_parent.isValid())
0286         return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0287     const CommitModelNodePtr node = m_sourceModel->dataForRow(source_row);
0288     return ((node->actionEntry().type() & m_visibleTypes) != 0);
0289 }
0290 
0291 void CommitFilterModel::hideItems(bool bHide, CommitActionEntry::ACTION_TYPE aType)
0292 {
0293     const CommitActionEntry::ActionTypes curVisibleTypes = m_visibleTypes;
0294     if (bHide) {
0295         m_visibleTypes &= ~aType;
0296     } else {
0297         m_visibleTypes |= aType;
0298     }
0299     if (m_visibleTypes != curVisibleTypes) {
0300         invalidateFilter();
0301     }
0302 }
0303 
0304 /*************************
0305  * end CommitFilterModel *
0306  *************************/
0307 
0308 #include "moc_commitmodel.cpp"