findresultmodel.cpp
author insilmaril
Tue, 09 Mar 2010 13:09:05 +0000
changeset 833 bfacef6f29c1
parent 831 25a950c2eb98
child 837 5ecd0462f76b
permissions -rw-r--r--
Bugfix: Previous checkin caused toggleScroll to fail
     1 #include <QtGui>
     2 
     3 #include "findresultitem.h"
     4 #include "findresultmodel.h"
     5 #include "treeitem.h"
     6 
     7 FindResultModel::FindResultModel( QObject *parent)
     8     : QAbstractItemModel(parent)
     9 {
    10     QVector<QVariant> rootData;
    11 	rootData << "Heading";
    12     rootItem = new FindResultItem(rootData);
    13 }
    14 
    15 FindResultModel::~FindResultModel()
    16 {
    17     delete rootItem;
    18 }
    19 
    20 void FindResultModel::clear()
    21 {
    22 	if (rootItem->childCount()>0)
    23 		removeRows (0,rowCount (QModelIndex ()));
    24 }
    25 
    26 int FindResultModel::columnCount(const QModelIndex & /* parent */) const
    27 {
    28     return rootItem->columnCount();
    29 }
    30 
    31 QVariant FindResultModel::data(const QModelIndex &index, int role) const
    32 {
    33     if (!index.isValid())
    34         return QVariant();
    35 
    36     if (role != Qt::DisplayRole && role != Qt::EditRole)
    37         return QVariant();
    38 
    39     FindResultItem *item = getItem(index);
    40 
    41     return item->data(index.column());
    42 }
    43 
    44 Qt::ItemFlags FindResultModel::flags(const QModelIndex &index) const
    45 {
    46     if (!index.isValid())
    47         return 0;
    48 
    49     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    50 }
    51 
    52 FindResultItem *FindResultModel::getItem(const QModelIndex &index) const
    53 {
    54     if (index.isValid()) {
    55         FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
    56         if (item) return item;
    57     }
    58     return rootItem;
    59 }
    60 
    61 QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
    62                                int role) const
    63 {
    64     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    65         return rootItem->data(section);
    66 
    67     return QVariant();
    68 }
    69 
    70 QModelIndex FindResultModel::index (FindResultItem *fri)
    71 {
    72 	if (!fri->parent())
    73 		return QModelIndex();
    74 	else	
    75 		return createIndex (fri->row(),0,fri);
    76 }
    77 
    78 
    79 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
    80 {
    81     if (parent.isValid() && parent.column() != 0)
    82         return QModelIndex();
    83 
    84     FindResultItem *parentItem = getItem(parent);
    85 
    86     FindResultItem *childItem = parentItem->child(row);
    87     if (childItem)
    88         return createIndex(row, column, childItem);
    89     else
    90         return QModelIndex();
    91 }
    92 
    93 bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
    94 {
    95     bool success;
    96 
    97     beginInsertColumns(parent, position, position + columns - 1);
    98     success = rootItem->insertColumns(position, columns);
    99     endInsertColumns();
   100 
   101     return success;
   102 }
   103 
   104 bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
   105 {
   106     FindResultItem *parentItem = getItem(parent);
   107     bool success;
   108 
   109     beginInsertRows(parent, position, position + rows - 1);
   110     success = parentItem->insertChildren(position, rows, rootItem->columnCount());
   111     endInsertRows();
   112 
   113     return success;
   114 }
   115 
   116 QModelIndex FindResultModel::parent(const QModelIndex &index) const
   117 {
   118     if (!index.isValid())
   119         return QModelIndex();
   120 
   121     FindResultItem *childItem = getItem(index);
   122     FindResultItem *parentItem = childItem->parent();
   123 
   124     if (parentItem == rootItem)
   125         return QModelIndex();
   126 
   127     return createIndex(parentItem->childNumber(), 0, parentItem);
   128 }
   129 
   130 bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
   131 {
   132     bool success;
   133 
   134     beginRemoveColumns(parent, position, position + columns - 1);
   135     success = rootItem->removeColumns(position, columns);
   136     endRemoveColumns();
   137 
   138     if (rootItem->columnCount() == 0)
   139         removeRows(0, rowCount());
   140 
   141     return success;
   142 }
   143 
   144 bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
   145 {
   146     FindResultItem *parentItem = getItem(parent);
   147     bool success = true;
   148 
   149     beginRemoveRows(parent, position, position + rows - 1);
   150     success = parentItem->removeChildren(position, rows);
   151     endRemoveRows();
   152 
   153     return success;
   154 }
   155 
   156 int FindResultModel::rowCount(const QModelIndex &parent) const
   157 {
   158     FindResultItem *parentItem = getItem(parent);
   159 
   160     return parentItem->childCount();
   161 }
   162 
   163 bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
   164                         int role)
   165 {
   166     if (role != Qt::EditRole)
   167         return false;
   168 
   169     FindResultItem *item = getItem(index);
   170     bool result = item->setData(index.column(), value);
   171 
   172     if (result)
   173         emit dataChanged(index, index);
   174 
   175     return result;
   176 }
   177 
   178 bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
   179                               const QVariant &value, int role)
   180 {
   181     if (role != Qt::EditRole || orientation != Qt::Horizontal)
   182         return false;
   183 
   184     bool result = rootItem->setData(section, value);
   185 
   186     if (result)
   187         emit headerDataChanged(orientation, section, section);
   188 
   189     return result;
   190 }
   191 
   192 void FindResultModel::addItem (TreeItem *ti)
   193 {
   194 	if (ti)
   195 	{
   196 		QModelIndex ix (index (rootItem));
   197 		//QAbstractItemModel *resultsModel = view->model();
   198 		
   199 		if (!insertRow(ix.row()+1, ix.parent()))
   200 			return;
   201 
   202 		for (int column = 0; column < columnCount(ix.parent()); ++column) {
   203 			QModelIndex child = index(ix.row()+1, column, ix.parent());
   204 			setData(child, QVariant(ti->getHeading()), Qt::EditRole);
   205 			getItem(child)->setOriginal (ti);
   206 		}
   207 	}
   208 }
   209 
   210 void FindResultModel::addItem (const QString &s)
   211 {
   212 	if (!s.isEmpty())
   213 	{
   214 		QModelIndex ix ( index (rootItem));
   215 		
   216 		if (!insertRow(ix.row()+1, ix.parent()))
   217 			return;
   218 
   219 		for (int column = 0; column < columnCount(ix.parent()); ++column) {
   220 			QModelIndex child = index(ix.row()+1, column, ix.parent());
   221 			setData(child, QVariant(s), Qt::EditRole);
   222 		}
   223 	}
   224 }
   225 
   226