File indexing completed on 2024-04-14 05:38:47

0001 /***************************************************************************
0002  *   Copyright (C) 2008 by Pino Toscano <pino@kde.org>                     *
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 
0010 #include "slavemodel.h"
0011 #include "altparser.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 SlaveModel::SlaveModel(QObject *parent)
0016     : QAbstractItemModel(parent)
0017     , m_item(Q_NULLPTR), m_alt(Q_NULLPTR)
0018 {
0019 }
0020 
0021 SlaveModel::~SlaveModel()
0022 {
0023 }
0024 
0025 int SlaveModel::columnCount(const QModelIndex &parent) const
0026 {
0027     return parent.isValid() ? 0 : 2;
0028 }
0029 
0030 QVariant SlaveModel::data(const QModelIndex &index, int role) const
0031 {
0032     if (!index.isValid() || !m_item)
0033         return QVariant();
0034 
0035     switch (role)
0036     {
0037         case Qt::DisplayRole:
0038         case Qt::ToolTipRole:
0039             if (index.column() == 0)
0040                 return m_item->getSlaves()->at(index.row())->slname;
0041             else if (index.column() == 1)
0042                 return m_alt ? m_alt->getSlaves().at(index.row()) : QString();
0043             break;
0044     }
0045     return QVariant();
0046 }
0047 
0048 bool SlaveModel::hasChildren(const QModelIndex &parent) const
0049 {
0050     return rowCount(parent) > 0;
0051 }
0052 
0053 QVariant SlaveModel::headerData(int section, Qt::Orientation orientation, int role) const
0054 {
0055     if (orientation == Qt::Horizontal)
0056     {
0057         switch (role)
0058         {
0059             case Qt::DisplayRole:
0060                 if (section == 0)
0061                     return i18nc("Slave name", "Name");
0062                 else if (section == 1)
0063                     return i18nc("Slave path", "Path");
0064                 break;
0065         }
0066     }
0067     else
0068     {
0069         if (role == Qt::DisplayRole)
0070             return section;
0071     }
0072     return QVariant();
0073 }
0074 
0075 QModelIndex SlaveModel::index(int row, int column, const QModelIndex &parent) const
0076 {
0077     if (parent.isValid() || !m_item || column < 0 || column >= 2
0078         || row < 0 || (row >= m_item->getSlaves()->count()))
0079         return QModelIndex();
0080 
0081     return createIndex(row, column);
0082 }
0083 
0084 QModelIndex SlaveModel::parent(const QModelIndex &) const
0085 {
0086     return QModelIndex();
0087 }
0088 
0089 int SlaveModel::rowCount(const QModelIndex &parent) const
0090 {
0091     return parent.isValid() ? 0 : (m_item ? m_item->getSlaves()->count(): 0);
0092 }
0093 
0094 void SlaveModel::setItem(Item *item)
0095 {
0096     if (item == m_item)
0097         return;
0098 
0099     beginResetModel();
0100     m_item = item;
0101     m_alt = Q_NULLPTR;
0102     endResetModel();
0103 }
0104 
0105 void SlaveModel::setAlternative(Alternative *alt)
0106 {
0107     if (!alt || !m_item || (alt->getParent() != m_item)
0108         || (alt->slavesCount() != m_item->getSlaves()->count()))
0109         return;
0110 
0111     m_alt = alt;
0112     emit dataChanged(createIndex(0, 1), createIndex(m_alt->slavesCount() - 1, 1));
0113 }
0114 
0115 #include <slavemodel.moc>