/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */ #include #include "smtreemodel.h" #include "smtreeitem.h" SmTreeModel::SmTreeModel(const QStringList &headers, QObject *parent) : QAbstractItemModel(parent), mRootItem(0){ mHeaders = headers; mRootItem = new SmTreeItem(headers.count()); } SmTreeModel::~SmTreeModel(){ delete mRootItem; } int SmTreeModel::rowCount(const QModelIndex &parent) const{ SmTreeItem *parentItem = itemAt(parent); return parentItem->childCount(); } int SmTreeModel::columnCount(const QModelIndex &parent) const{ Q_UNUSED(parent); return mRootItem->columnCount(); } QModelIndex SmTreeModel::index(int row, int column, const QModelIndex &parent) const{ if(parent.isValid() && (parent.column() != 0)){ return QModelIndex(); } SmTreeItem *parentItem = itemAt(parent); SmTreeItem *childItem = parentItem->child(row); if(childItem){ return createIndex(row, column, childItem); } return QModelIndex(); } QModelIndex SmTreeModel::parent(const QModelIndex &child) const{ if(!child.isValid()){ return QModelIndex(); } SmTreeItem *childItem = itemAt(child); SmTreeItem *parentItem = childItem->parent(); if(parentItem == mRootItem){ return QModelIndex(); } return createIndex(parentItem->row(), 0, parentItem); } Qt::ItemFlags SmTreeModel::flags(const QModelIndex &index) const{ if(!index.isValid()){ return 0; } return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; } QVariant SmTreeModel::headerData(int section, Qt::Orientation orientation, int role) const{ if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){ return mHeaders.at(section); } return QVariant(); } bool SmTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){ if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){ mRootItem->setData(section, value); emit headerDataChanged(orientation, section, section); return true; } return false; } QVariant SmTreeModel::data(const QModelIndex &index, int role) const{ if(!index.isValid()){ return QVariant(); } SmTreeItem *item = itemAt(index); if(role == Qt::DisplayRole){ return item->data(index.column()); } if(role == Qt::EditRole){ return item->data(index.column()); } if(role == Qt::DecorationRole){ if(index.column() == 0){ return QIcon(":/dildo.png"); } } return QVariant(); } bool SmTreeModel::setData(const QModelIndex &index, const QVariant &value, int role){ if(role != Qt::EditRole){ return false; } SmTreeItem *item = itemAt(index); item->setData(index.column(), value); emit dataChanged(index, index); return true; } bool SmTreeModel::setRoot(SmTreeItem *rootItem){ if(mRootItem){ beginResetModel(); delete mRootItem; mRootItem = rootItem; endResetModel(); return true; } return false; } SmTreeItem *SmTreeModel::parentItem(const QModelIndex &child) const{ QModelIndex parent = child.parent(); if(parent == QModelIndex()){ return mRootItem; } return static_cast(child.parent().internalPointer()); } bool SmTreeModel::insertRows(int row, int count, const QModelIndex &parent){ SmTreeItem *parentItem = itemAt(parent); bool retval; if(row > parentItem->childCount()){ return false; } beginInsertRows(parent, row, row + count - 1); for(int i = row; i < row + count; ++i){ SmTreeItem *newItem = new SmTreeItem(mRootItem->columnCount()); retval = parentItem->insertChild(i, newItem); } endInsertRows(); return retval; } bool SmTreeModel::removeRows(int row, int count, const QModelIndex &parent){ SmTreeItem *parentItem = itemAt(parent); bool retval; if(row > parentItem->childCount()){ return false; } beginRemoveRows(parent, row, row + count - 1); for(int i = row + count; i > row; --i){ retval = parentItem->removeChild(i); } endRemoveRows(); return retval; } bool SmTreeModel::addRow(const QList &data, const QModelIndex &parent){ if(data.count() != mRootItem->columnCount()){ return false; } SmTreeItem *parentItem = itemAt(parent); if(insertRows(parentItem->childCount(), 1, parent)){ SmTreeItem *child = parentItem->child(parentItem->childCount() - 1); delete child; SmTreeItem *newChild = new SmTreeItem(data, parentItem); child = newChild; QModelIndex start = index(parentItem->childCount() - 1, 0, parent); QModelIndex end = index(parentItem->childCount() - 1, parentItem->columnCount() - 1, parent); emit dataChanged(start, end); return true; } return false; } SmTreeItem *SmTreeModel::itemAt(const QModelIndex &index) const{ if(index.isValid()){ SmTreeItem *item = static_cast(index.internalPointer()); if(item){ return item; } } return mRootItem; }