1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include "treemodel.h"
- #include "node.h"
- #include <QString>
- #include <QColor>
- TreeModel::TreeModel(QObject *parent)
- : QAbstractItemModel(parent)
- {
- _rootItem = new QObject(this);
- }
- //QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
- //{
- // // FIXME: Implement me!
- //}
- QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
- {
- if(!hasIndex(row,column,parent))
- {return QModelIndex();}
- QObject* parentObj = objByIndex(parent);
- return createIndex(row,column,parentObj->children().at(row));
- }
- QModelIndex TreeModel::parent(const QModelIndex &index) const
- {
- QObject* childObj = objByIndex(index);
- QObject* parentObj = childObj->parent();
- if(parentObj ==_rootItem)
- {return QModelIndex();}
- QObject* grandParentObj = parentObj->parent();
- int row = grandParentObj->children().indexOf(parentObj);
- return createIndex(row, 0, parentObj);
- }
- int TreeModel::rowCount(const QModelIndex &parent) const
- {
- // if (!parent.isValid())
- // return 0;
- return objByIndex(parent)->children().count();
- }
- int TreeModel::columnCount(const QModelIndex &parent) const
- {
- // if (!parent.isValid())
- // return 0;
- Q_UNUSED(parent);
- return m_columns.count();
- }
- QVariant TreeModel::data(const QModelIndex &index, int role) const
- {
- if (!index.isValid())
- return QVariant();
- if(role == Qt::DisplayRole)
- {
- return objByIndex(index)->property(m_columns.at(index.column()).toUtf8());
- }
- if (role == Qt::ForegroundRole )
- {
- if ( static_cast<Node*>(objByIndex(index))->status() )
- {
- return QVariant( QColor( Qt::black ) );
- }
- return QVariant( QColor( Qt::gray ) );
- }
- return QVariant();
- }
- void TreeModel::AddItem(QObject *item, const QModelIndex &parentIndex)
- {
- beginInsertRows(parentIndex,rowCount(parentIndex),rowCount(parentIndex));
- item->setParent(objByIndex(parentIndex));
- endInsertRows();
- }
- QObject *TreeModel::objByIndex(const QModelIndex &index) const
- {
- if(!index.isValid())
- return _rootItem;
- return static_cast<QObject*>(index.internalPointer());
- }
- QModelIndex TreeModel::IndexById(QString id) const
- {
- QModelIndex ind = this->match(this->index(0,1),Qt::DisplayRole,QVariant(id), -1).first();
- return ind;
- }
|