findresultmodel.cpp
author convert-repo
Fri, 23 Jul 2010 16:43:49 +0000
changeset 849 988f1908a7c4
parent 842 bec082472471
permissions -rw-r--r--
update tags
     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 QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
    53                                int role) const
    54 {
    55     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    56         return rootItem->data(section);
    57 
    58     return QVariant();
    59 }
    60 
    61 QModelIndex FindResultModel::index (FindResultItem *fri)
    62 {
    63 	if (!fri->parent())
    64 		return QModelIndex();
    65 	else	
    66 		return createIndex (fri->row(),0,fri);
    67 }
    68 
    69 
    70 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
    71 {
    72     if (parent.isValid() && parent.column() != 0)
    73         return QModelIndex();
    74 
    75     FindResultItem *parentItem = getItem(parent);
    76 
    77     FindResultItem *childItem = parentItem->child(row);
    78     if (childItem)
    79         return createIndex(row, column, childItem);
    80     else
    81         return QModelIndex();
    82 }
    83 
    84 bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
    85 {
    86     bool success;
    87 
    88     beginInsertColumns(parent, position, position + columns - 1);
    89     success = rootItem->insertColumns(position, columns);
    90     endInsertColumns();
    91 
    92     return success;
    93 }
    94 
    95 bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
    96 {
    97     FindResultItem *parentItem = getItem(parent);
    98     bool success;
    99 
   100     beginInsertRows(parent, position, position + rows - 1);
   101     success = parentItem->insertChildren(position, rows, rootItem->columnCount());
   102     endInsertRows();
   103 
   104     return success;
   105 }
   106 
   107 QModelIndex FindResultModel::parent(const QModelIndex &index) const
   108 {
   109     if (!index.isValid())
   110         return QModelIndex();
   111 
   112     FindResultItem *childItem = getItem(index);
   113     FindResultItem *parentItem = childItem->parent();
   114 
   115     if (parentItem == rootItem)
   116         return QModelIndex();
   117 
   118     return createIndex(parentItem->childNumber(), 0, parentItem);
   119 }
   120 
   121 bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
   122 {
   123     bool success;
   124 
   125     beginRemoveColumns(parent, position, position + columns - 1);
   126     success = rootItem->removeColumns(position, columns);
   127     endRemoveColumns();
   128 
   129     if (rootItem->columnCount() == 0)
   130         removeRows(0, rowCount());
   131 
   132     return success;
   133 }
   134 
   135 bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
   136 {
   137     FindResultItem *parentItem = getItem(parent);
   138     bool success = true;
   139 
   140     beginRemoveRows(parent, position, position + rows - 1);
   141     success = parentItem->removeChildren(position, rows);
   142     endRemoveRows();
   143 
   144     return success;
   145 }
   146 
   147 int FindResultModel::rowCount(const QModelIndex &parent) const
   148 {
   149     FindResultItem *parentItem = getItem(parent);
   150 
   151     return parentItem->childCount();
   152 }
   153 
   154 bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
   155                         int role)
   156 {
   157     if (role != Qt::EditRole)
   158         return false;
   159 
   160     FindResultItem *item = getItem(index);
   161     bool result = item->setData(index.column(), value);
   162 
   163     if (result)
   164         emit dataChanged(index, index);
   165 
   166     return result;
   167 }
   168 
   169 bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
   170                               const QVariant &value, int role)
   171 {
   172     if (role != Qt::EditRole || orientation != Qt::Horizontal)
   173         return false;
   174 
   175     bool result = rootItem->setData(section, value);
   176 
   177     if (result)
   178         emit headerDataChanged(orientation, section, section);
   179 
   180     return result;
   181 }
   182 
   183 FindResultItem* FindResultModel::getItem(const QModelIndex &index) const
   184 {
   185     if (index.isValid()) {
   186         FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
   187         if (item) return item;
   188     }
   189     return rootItem;
   190 }
   191 
   192 FindResultItem *FindResultModel::findTreeItem(TreeItem *)
   193 {
   194 	return NULL;	// FIXME-3 not used so far
   195 }
   196 
   197 FindResultItem*  FindResultModel::addItem (TreeItem *ti)
   198 {
   199 	FindResultItem *ni=NULL;
   200 	if (ti)
   201 	{
   202 		QModelIndex parix (index (rootItem));
   203 		
   204 		emit (layoutAboutToBeChanged() );
   205 
   206 		int n=rowCount (parix);
   207 		beginInsertRows (parix,n,n);
   208 		if (rootItem->insertChildren (n,1,0) )
   209 		{
   210 			QModelIndex ix=index(n,0,QModelIndex());
   211 			setData (ix,QVariant(ti->getHeading()),Qt::EditRole);
   212 			ni=getItem(ix);
   213 			ni->setOriginal (ti);
   214 		}
   215 		endInsertRows ();
   216 
   217 		emit (layoutChanged() );
   218 	}
   219 	return ni;
   220 }
   221 
   222 FindResultItem*  FindResultModel::addSubItem (FindResultItem *parent,const QString &s, TreeItem *pi, int i)
   223 {
   224 	FindResultItem *ni=NULL;
   225 	if (pi && parent)
   226 	{
   227 		QModelIndex parix ( index (parent));
   228 		
   229 		emit (layoutAboutToBeChanged() );
   230 
   231 		int n=rowCount (parix);
   232 		beginInsertRows (parix,n,n);
   233 
   234 		QModelIndex ix;
   235 		if (parent->insertChildren (n,1,0))
   236 		{
   237 			ix=index(n,0,parix);
   238 			setData (ix,QVariant(s),Qt::EditRole);
   239 			ni=getItem(ix);
   240 			ni->setOriginal (pi);
   241 			ni->setOriginalIndex (i);
   242 		}
   243 		endInsertRows ();
   244 		emit (layoutChanged() );
   245 	}
   246 	return ni;
   247 }
   248 
   249 void FindResultModel::setSearchString( const QString &s)
   250 {
   251 	searchString=s;
   252 }
   253 
   254 QString FindResultModel::getSearchString()
   255 {
   256 	return searchString;
   257 }
   258 
   259 void FindResultModel::setSearchFlags( QTextDocument::FindFlags f)
   260 {
   261 	searchFlags=f;
   262 }
   263 
   264 QTextDocument::FindFlags FindResultModel::getSearchFlags()
   265 {
   266 	return searchFlags;
   267 }
   268