/* 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 #include #include "smtreemodel.h" #include "smtreeitem.h" #include "smglobals.h" SmTreeModel::SmTreeModel(const QStringList &headers, QObject *parent) : QAbstractItemModel(parent), mRootItem(0){ mHeaders = headers; for(int i = 0; i < mHeaders.size(); ++i){ mHeaderData.insert(mHeaders.at(i), i); } mRootItem = new SmTreeItem(headers.count()); QSettings s; QString iconName = s.value("ui/foldericon", "Dildo").toString(); const QHash icons = SmGlobals::instance()->icons(); mDecorationIcon = QIcon(icons.value(iconName)); } 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{ //this is totally bogus! Why? Tentatively leaving it in for now... 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(); } const QHash SmTreeModel::headerData() const{ return mHeaderData; } bool SmTreeModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){ if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)){ mHeaders[section] = value.toString(); 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 mDecorationIcon; } } if(role == Qt::ForegroundRole){ QColor fgCol = item->foregroundColor().value(); return QBrush(fgCol); } 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; } QModelIndex SmTreeModel::find(const QVariant &value, int column, const QModelIndex &parent) const{ SmTreeItem *parentItem = 0; if(!parent.isValid()){ parentItem = root(); }else{ parentItem = static_cast(parent.internalPointer()); } for(int i = 0; i < parentItem->childCount(); ++i){ SmTreeItem *child = parentItem->child(i); if(child->data(column) == value){ return index(i, column, parent); } } return QModelIndex(); } QModelIndex SmTreeModel::findRecursive(const QVariant &value, int column, const QModelIndex &parent) const{ SmTreeItem *parentItem = 0; if(!parent.isValid()){ parentItem = mRootItem; }else{ parentItem = static_cast(parent.internalPointer()); } if(parentItem->data(column) == value){ return parent; } for(int i = 0; i < parentItem->childCount(); ++i){ SmTreeItem *child = parentItem->child(i); if(child->data(column) == value){ return createIndex(i, column, child); } if(child->childCount()){ return findRecursive(value, column, createIndex(i, column, child)); } } QModelIndex next = index(parent.row() + 1, column, parent.parent()); if(!next.isValid()){ return QModelIndex(); } return findRecursive(value, column, next); } 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()); } void SmTreeModel::reparent(const QModelIndex &idx, const QModelIndex &newParent, bool sorted){ if(!idx.isValid()){ return; } SmTreeItem *item = static_cast(idx.internalPointer()); QList data; for(int i = 0; i < item->columnCount(); ++i){ data << item->data(i); } QPersistentModelIndex pNewParent = newParent; removeRows(idx.row(), 1, idx.parent()); addRow(data, pNewParent, sorted); } 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(), parentItem); 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 - 1); } endRemoveRows(); return retval; } bool SmTreeModel::addRow(const QList &data, const QModelIndex &parent, bool sorted){ if(data.count() != mRootItem->columnCount()){ return false; } SmTreeItem *parentItem = itemAt(parent); int where = 0; if(sorted){ for(int i = 0; i < parentItem->childCount(); ++i){ if(parentItem->child(i)->data(0).toString() > data.at(0).toString()){ where = i; break; } } } if(insertRows(where, 1, parent)){ SmTreeItem *child = parentItem->child(where); for(int i = 0; i < data.count(); ++i){ child->setData(i, data.at(i)); } 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; }