File indexing completed on 2024-04-21 14:43:55

0001 /*************************************************************************************
0002  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (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 "spacesmodel.h"
0020 
0021 //KDE includes
0022 #include <KLocalizedString>
0023 
0024 //local includes
0025 #include "spaceitem.h"
0026 
0027 SpacesModel::SpacesModel(QObject *parent)
0028     : QAbstractListModel(parent),  m_itemCanCallModelRemoveItem(true)
0029 {}
0030 
0031 Qt::ItemFlags SpacesModel::flags(const QModelIndex &) const
0032 {
0033     return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
0034 }
0035 
0036 QVariant SpacesModel::headerData(int section, Qt::Orientation orientation, int role) const
0037 {
0038     if(role==Qt::DisplayRole && orientation==Qt::Horizontal) {
0039         switch(section) 
0040         {
0041             case 0: return i18nc("@title:column", "Name");
0042             case 1: return i18nc("@title:column", "Description");
0043         }
0044     }
0045     return QVariant();
0046 }
0047 
0048 QVariant SpacesModel::data( const QModelIndex &index, int role) const
0049 {
0050     if (!index.isValid())
0051         return QVariant();
0052 
0053     switch (role)
0054     {
0055         case Qt::StatusTipRole:  return m_items.at(index.row())->timestamp().toString("%A %l:%M %p %B %Y");
0056         case Qt::DecorationRole: if (index.column() == 0)  return m_items.at(index.row())->thumbnail(); break;
0057         case Qt::EditRole: 
0058         case Qt::DisplayRole: 
0059             if (index.column() == 0)
0060                 return m_items.at(index.row())->name();
0061             else if (index.column() == 1)
0062                 return m_items.at(index.row())->description();
0063             
0064 //         case Qt::StatusTipRole: return m_items.at(index.row())->description(); //TODO GSOC agregar un prefix algo como space descrp: txttt
0065     }
0066 
0067     return QVariant();
0068 }
0069 
0070 bool SpacesModel::setData(const QModelIndex& index, const QVariant& value, int role)
0071 {
0072     //TODO edit also description (column/section #2)
0073     if (index.isValid() && role == Qt::EditRole) 
0074     {
0075         m_items[index.row()]->setName(value.toString());
0076         emit dataChanged(index, index);
0077         return true;
0078     }
0079     return false;
0080 }
0081 
0082 int SpacesModel::rowCount(const QModelIndex &idx) const
0083 {
0084     return idx.isValid() ? 0 : m_items.count();
0085 }
0086 
0087 int SpacesModel::columnCount(const QModelIndex& parent) const
0088 {
0089     if(parent.isValid())
0090         return 0;
0091     else
0092         return 2;
0093 }
0094 
0095 bool SpacesModel::removeRows(int row, int count, const QModelIndex& parent)
0096 {
0097     if(parent.isValid())
0098         return false;
0099 
0100     Q_ASSERT(row+count<=m_items.size());
0101     beginRemoveRows(QModelIndex(), row, row+count-1);
0102 
0103     for (int i = 0; i < count; ++i) 
0104     {
0105         SpaceItem *tmpcurve = m_items[row];
0106 
0107         m_itemCanCallModelRemoveItem = false;
0108 
0109         if (!tmpcurve->m_inDestructorSoDontDeleteMe)
0110         {
0111             delete tmpcurve;
0112             tmpcurve = nullptr;
0113         }
0114 
0115         m_itemCanCallModelRemoveItem = true;
0116 
0117         m_items.removeAt(row);
0118     }
0119 
0120     endRemoveRows();
0121     
0122     return true;
0123 }
0124 
0125 SpaceItem* SpacesModel::addSpace(Analitza::Dimension dim, const QString & title, const QString &description, const QPixmap &thumbnail)
0126 {
0127     SpaceItem* ret = new SpaceItem(dim);
0128     ret->setName(title);
0129     ret->setDescription(description);
0130     ret->setThumbnail(thumbnail);
0131 
0132     beginInsertRows (QModelIndex(), m_items.count(), m_items.count());
0133     ret->setModel(this);
0134     m_items.append(ret);
0135     endInsertRows();
0136 
0137     return ret;
0138 }
0139 
0140 SpaceItem* SpacesModel::space(int row) const
0141 {
0142     Q_ASSERT(row >=0 && row<rowCount());
0143     return m_items[row];
0144 }
0145 
0146 void SpacesModel::emitChanged(SpaceItem* it)
0147 {
0148     int row = m_items.indexOf(it);
0149     QModelIndex idx = index(row);
0150     emit dataChanged(idx, idx);
0151 }
0152 
0153 #include "moc_spacesmodel.cpp"