src/job_editor_widget/jobpropertymodel.cpp

Go to the documentation of this file.
00001 
00008 #include <QtGui>
00009 #include "jobpropertymodel.h"
00010 #include "jobpropertyitem.h"
00011 #include <QDebug>
00012 
00018 JobPropertyModel::JobPropertyModel(QObject *parent)
00019     : QAbstractItemModel(parent),
00020     _layoutAboutToBeChanged(false)
00021 {
00022     QVector<QVariant> data;    
00023     rootItem = new JobPropertyItemRoot(this);
00024 }
00025 
00029 JobPropertyModel::~JobPropertyModel()
00030 {
00031     delete rootItem;
00032 }
00033 
00039 int JobPropertyModel::columnCount(const QModelIndex &parent) const
00040 {
00041     Q_UNUSED(parent);
00042     return rootItem->columnCount();
00043 }
00044 
00051 QVariant JobPropertyModel::data(const QModelIndex &index, int role) const
00052 {
00053     if (!index.isValid())
00054         return QVariant();
00055 
00056     if (role != Qt::DisplayRole && role != Qt::EditRole)
00057         return QVariant();
00058 
00059     JobPropertyItem *item = getItem(index);
00060 
00061     return item->data(index.column());
00062 }
00063 
00069 Qt::ItemFlags JobPropertyModel::flags(const QModelIndex &index) const
00070 {
00071     if (!index.isValid())
00072         return Qt::ItemIsEnabled;
00073 
00074     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00075 }
00076 
00082 JobPropertyItem *JobPropertyModel::getItem(const QModelIndex &index) const
00083 {
00084     if (index.isValid())
00085         return static_cast<JobPropertyItem*>(index.internalPointer());
00086     return rootItem;
00087 }
00088 
00096 QVariant JobPropertyModel::headerData(int section, Qt::Orientation orientation,
00097                                int role) const
00098 {
00099     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
00100         return rootItem->data(section);
00101 
00102     return QVariant();
00103 }
00104 
00112 QModelIndex JobPropertyModel::index(int row, int column, const QModelIndex &parent) const
00113 {
00114     if (parent.isValid() && parent.column() != 0)
00115         return QModelIndex();
00116 
00117     JobPropertyItem *parentItem = getItem(parent);
00118     JobPropertyItem *childItem = parentItem->child(row);
00119 
00120     if (childItem)
00121         return createIndex(row, column, childItem);
00122     else
00123         return QModelIndex();
00124 }
00125 
00131 QModelIndex JobPropertyModel::parent(const QModelIndex &index) const
00132 {
00133     if (!index.isValid())
00134         return QModelIndex();
00135 
00136     JobPropertyItem * childItem = getItem(index);
00137     if (childItem)
00138     {
00139         JobPropertyItem *parentItem = childItem->parent();
00140         if (parentItem == rootItem)
00141             return QModelIndex();
00142         return createIndex(parentItem->childNumber(), 0, parentItem);
00143     } else {
00144         return QModelIndex();
00145     }
00146  }
00147 
00153 int JobPropertyModel::rowCount(const QModelIndex &parent) const
00154 {
00155     JobPropertyItem *parentItem = getItem(parent);
00156 
00157     return parentItem->childCount();
00158 }
00159 
00168 bool JobPropertyModel::setData(const QModelIndex &index, const QVariant &value,
00169                         int role)
00170 {
00171     if (role != Qt::EditRole)
00172         return false;
00173 
00174     JobPropertyItem *item = getItem(index);
00175 
00176     return item->setData(index.column(), value);
00177 }
00178 
00187 bool JobPropertyModel::setHeaderData(int section, Qt::Orientation orientation,
00188                               const QVariant &value, int role)
00189 {
00190     if (role != Qt::EditRole || orientation != Qt::Horizontal)
00191         return false;
00192 
00193     return rootItem->setData(section, value);
00194 }
00195 
00199 void JobPropertyModel::emitLayoutAboutToBeChanged()
00200 {
00201     if (!_layoutAboutToBeChanged)
00202     {
00203         _layoutAboutToBeChanged = true;
00204         emit layoutAboutToBeChanged();
00205     }
00206 }
00207 
00211 void JobPropertyModel::emitLayoutChanged()
00212 {
00213     emit layoutChanged();
00214     _layoutAboutToBeChanged = false;
00215 }
00216 
00224 bool JobPropertyModel::removeRows ( int row, int count, const QModelIndex & parent )
00225 {
00226     beginRemoveRows(parent, row, row+count-1);
00227     JobPropertyItem * item = getItem(parent);
00228     for(int i=0; i<count; i++)
00229         item->deleteChild(row);
00230     endRemoveRows();
00231     return true;
00232 }

Generated on Mon Mar 16 18:46:05 2009 for QCJM by  doxygen 1.5.4