findresultmodel.cpp
author insilmaril
Tue, 06 Apr 2010 13:30:07 +0000
changeset 843 2d36a7bb0867
parent 842 bec082472471
permissions -rw-r--r--
(Very) minor changes for debugging output
insilmaril@825
     1
#include <QtGui>
insilmaril@825
     2
insilmaril@825
     3
#include "findresultitem.h"
insilmaril@825
     4
#include "findresultmodel.h"
insilmaril@829
     5
#include "treeitem.h"
insilmaril@825
     6
insilmaril@825
     7
FindResultModel::FindResultModel( QObject *parent)
insilmaril@825
     8
    : QAbstractItemModel(parent)
insilmaril@825
     9
{
insilmaril@825
    10
    QVector<QVariant> rootData;
insilmaril@829
    11
	rootData << "Heading";
insilmaril@825
    12
    rootItem = new FindResultItem(rootData);
insilmaril@825
    13
}
insilmaril@825
    14
insilmaril@825
    15
FindResultModel::~FindResultModel()
insilmaril@825
    16
{
insilmaril@825
    17
    delete rootItem;
insilmaril@825
    18
}
insilmaril@825
    19
insilmaril@829
    20
void FindResultModel::clear()
insilmaril@829
    21
{
insilmaril@829
    22
	if (rootItem->childCount()>0)
insilmaril@829
    23
		removeRows (0,rowCount (QModelIndex ()));
insilmaril@829
    24
}
insilmaril@829
    25
insilmaril@825
    26
int FindResultModel::columnCount(const QModelIndex & /* parent */) const
insilmaril@825
    27
{
insilmaril@825
    28
    return rootItem->columnCount();
insilmaril@825
    29
}
insilmaril@825
    30
insilmaril@825
    31
QVariant FindResultModel::data(const QModelIndex &index, int role) const
insilmaril@825
    32
{
insilmaril@825
    33
    if (!index.isValid())
insilmaril@825
    34
        return QVariant();
insilmaril@825
    35
insilmaril@825
    36
    if (role != Qt::DisplayRole && role != Qt::EditRole)
insilmaril@825
    37
        return QVariant();
insilmaril@825
    38
insilmaril@825
    39
    FindResultItem *item = getItem(index);
insilmaril@825
    40
insilmaril@825
    41
    return item->data(index.column());
insilmaril@825
    42
}
insilmaril@825
    43
insilmaril@825
    44
Qt::ItemFlags FindResultModel::flags(const QModelIndex &index) const
insilmaril@825
    45
{
insilmaril@825
    46
    if (!index.isValid())
insilmaril@825
    47
        return 0;
insilmaril@825
    48
insilmaril@825
    49
    return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
insilmaril@825
    50
}
insilmaril@825
    51
insilmaril@825
    52
QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
insilmaril@825
    53
                               int role) const
insilmaril@825
    54
{
insilmaril@825
    55
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
insilmaril@825
    56
        return rootItem->data(section);
insilmaril@825
    57
insilmaril@825
    58
    return QVariant();
insilmaril@825
    59
}
insilmaril@825
    60
insilmaril@829
    61
QModelIndex FindResultModel::index (FindResultItem *fri)
insilmaril@829
    62
{
insilmaril@829
    63
	if (!fri->parent())
insilmaril@829
    64
		return QModelIndex();
insilmaril@829
    65
	else	
insilmaril@829
    66
		return createIndex (fri->row(),0,fri);
insilmaril@829
    67
}
insilmaril@829
    68
insilmaril@829
    69
insilmaril@825
    70
QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
insilmaril@825
    71
{
insilmaril@825
    72
    if (parent.isValid() && parent.column() != 0)
insilmaril@825
    73
        return QModelIndex();
insilmaril@825
    74
insilmaril@825
    75
    FindResultItem *parentItem = getItem(parent);
insilmaril@825
    76
insilmaril@825
    77
    FindResultItem *childItem = parentItem->child(row);
insilmaril@825
    78
    if (childItem)
insilmaril@825
    79
        return createIndex(row, column, childItem);
insilmaril@825
    80
    else
insilmaril@825
    81
        return QModelIndex();
insilmaril@825
    82
}
insilmaril@825
    83
insilmaril@825
    84
bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
insilmaril@825
    85
{
insilmaril@825
    86
    bool success;
insilmaril@825
    87
insilmaril@825
    88
    beginInsertColumns(parent, position, position + columns - 1);
insilmaril@825
    89
    success = rootItem->insertColumns(position, columns);
insilmaril@825
    90
    endInsertColumns();
insilmaril@825
    91
insilmaril@825
    92
    return success;
insilmaril@825
    93
}
insilmaril@825
    94
insilmaril@825
    95
bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
insilmaril@825
    96
{
insilmaril@825
    97
    FindResultItem *parentItem = getItem(parent);
insilmaril@825
    98
    bool success;
insilmaril@825
    99
insilmaril@825
   100
    beginInsertRows(parent, position, position + rows - 1);
insilmaril@825
   101
    success = parentItem->insertChildren(position, rows, rootItem->columnCount());
insilmaril@825
   102
    endInsertRows();
insilmaril@825
   103
insilmaril@825
   104
    return success;
insilmaril@825
   105
}
insilmaril@825
   106
insilmaril@825
   107
QModelIndex FindResultModel::parent(const QModelIndex &index) const
insilmaril@825
   108
{
insilmaril@825
   109
    if (!index.isValid())
insilmaril@825
   110
        return QModelIndex();
insilmaril@825
   111
insilmaril@825
   112
    FindResultItem *childItem = getItem(index);
insilmaril@825
   113
    FindResultItem *parentItem = childItem->parent();
insilmaril@825
   114
insilmaril@825
   115
    if (parentItem == rootItem)
insilmaril@825
   116
        return QModelIndex();
insilmaril@825
   117
insilmaril@825
   118
    return createIndex(parentItem->childNumber(), 0, parentItem);
insilmaril@825
   119
}
insilmaril@825
   120
insilmaril@825
   121
bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
insilmaril@825
   122
{
insilmaril@825
   123
    bool success;
insilmaril@825
   124
insilmaril@825
   125
    beginRemoveColumns(parent, position, position + columns - 1);
insilmaril@825
   126
    success = rootItem->removeColumns(position, columns);
insilmaril@825
   127
    endRemoveColumns();
insilmaril@825
   128
insilmaril@825
   129
    if (rootItem->columnCount() == 0)
insilmaril@825
   130
        removeRows(0, rowCount());
insilmaril@825
   131
insilmaril@825
   132
    return success;
insilmaril@825
   133
}
insilmaril@825
   134
insilmaril@825
   135
bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
insilmaril@825
   136
{
insilmaril@825
   137
    FindResultItem *parentItem = getItem(parent);
insilmaril@825
   138
    bool success = true;
insilmaril@825
   139
insilmaril@825
   140
    beginRemoveRows(parent, position, position + rows - 1);
insilmaril@825
   141
    success = parentItem->removeChildren(position, rows);
insilmaril@825
   142
    endRemoveRows();
insilmaril@825
   143
insilmaril@825
   144
    return success;
insilmaril@825
   145
}
insilmaril@825
   146
insilmaril@825
   147
int FindResultModel::rowCount(const QModelIndex &parent) const
insilmaril@825
   148
{
insilmaril@825
   149
    FindResultItem *parentItem = getItem(parent);
insilmaril@825
   150
insilmaril@825
   151
    return parentItem->childCount();
insilmaril@825
   152
}
insilmaril@825
   153
insilmaril@825
   154
bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
insilmaril@825
   155
                        int role)
insilmaril@825
   156
{
insilmaril@825
   157
    if (role != Qt::EditRole)
insilmaril@825
   158
        return false;
insilmaril@825
   159
insilmaril@825
   160
    FindResultItem *item = getItem(index);
insilmaril@825
   161
    bool result = item->setData(index.column(), value);
insilmaril@825
   162
insilmaril@825
   163
    if (result)
insilmaril@825
   164
        emit dataChanged(index, index);
insilmaril@825
   165
insilmaril@825
   166
    return result;
insilmaril@825
   167
}
insilmaril@825
   168
insilmaril@825
   169
bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
insilmaril@825
   170
                              const QVariant &value, int role)
insilmaril@825
   171
{
insilmaril@825
   172
    if (role != Qt::EditRole || orientation != Qt::Horizontal)
insilmaril@825
   173
        return false;
insilmaril@825
   174
insilmaril@825
   175
    bool result = rootItem->setData(section, value);
insilmaril@825
   176
insilmaril@825
   177
    if (result)
insilmaril@825
   178
        emit headerDataChanged(orientation, section, section);
insilmaril@825
   179
insilmaril@825
   180
    return result;
insilmaril@825
   181
}
insilmaril@825
   182
insilmaril@842
   183
FindResultItem* FindResultModel::getItem(const QModelIndex &index) const
insilmaril@841
   184
{
insilmaril@841
   185
    if (index.isValid()) {
insilmaril@841
   186
        FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
insilmaril@841
   187
        if (item) return item;
insilmaril@841
   188
    }
insilmaril@841
   189
    return rootItem;
insilmaril@841
   190
}
insilmaril@841
   191
insilmaril@842
   192
FindResultItem *FindResultModel::findTreeItem(TreeItem *)
insilmaril@841
   193
{
insilmaril@842
   194
	return NULL;	// FIXME-3 not used so far
insilmaril@841
   195
}
insilmaril@841
   196
insilmaril@842
   197
FindResultItem*  FindResultModel::addItem (TreeItem *ti)
insilmaril@825
   198
{
insilmaril@842
   199
	FindResultItem *ni=NULL;
insilmaril@829
   200
	if (ti)
insilmaril@829
   201
	{
insilmaril@842
   202
		QModelIndex parix (index (rootItem));
insilmaril@829
   203
		
insilmaril@842
   204
		emit (layoutAboutToBeChanged() );
insilmaril@825
   205
insilmaril@842
   206
		int n=rowCount (parix);
insilmaril@842
   207
		beginInsertRows (parix,n,n);
insilmaril@842
   208
		if (rootItem->insertChildren (n,1,0) )
insilmaril@841
   209
		{
insilmaril@842
   210
			QModelIndex ix=index(n,0,QModelIndex());
insilmaril@842
   211
			setData (ix,QVariant(ti->getHeading()),Qt::EditRole);
insilmaril@842
   212
			ni=getItem(ix);
insilmaril@842
   213
			ni->setOriginal (ti);
insilmaril@829
   214
		}
insilmaril@842
   215
		endInsertRows ();
insilmaril@842
   216
insilmaril@842
   217
		emit (layoutChanged() );
insilmaril@829
   218
	}
insilmaril@842
   219
	return ni;
insilmaril@829
   220
}
insilmaril@825
   221
insilmaril@842
   222
FindResultItem*  FindResultModel::addSubItem (FindResultItem *parent,const QString &s, TreeItem *pi, int i)
insilmaril@829
   223
{
insilmaril@842
   224
	FindResultItem *ni=NULL;
insilmaril@842
   225
	if (pi && parent)
insilmaril@829
   226
	{
insilmaril@842
   227
		QModelIndex parix ( index (parent));
insilmaril@829
   228
		
insilmaril@842
   229
		emit (layoutAboutToBeChanged() );
insilmaril@825
   230
insilmaril@842
   231
		int n=rowCount (parix);
insilmaril@842
   232
		beginInsertRows (parix,n,n);
insilmaril@842
   233
insilmaril@842
   234
		QModelIndex ix;
insilmaril@842
   235
		if (parent->insertChildren (n,1,0))
insilmaril@842
   236
		{
insilmaril@842
   237
			ix=index(n,0,parix);
insilmaril@842
   238
			setData (ix,QVariant(s),Qt::EditRole);
insilmaril@842
   239
			ni=getItem(ix);
insilmaril@842
   240
			ni->setOriginal (pi);
insilmaril@842
   241
			ni->setOriginalIndex (i);
insilmaril@829
   242
		}
insilmaril@842
   243
		endInsertRows ();
insilmaril@842
   244
		emit (layoutChanged() );
insilmaril@829
   245
	}
insilmaril@842
   246
	return ni;
insilmaril@829
   247
}
insilmaril@825
   248
insilmaril@842
   249
void FindResultModel::setSearchString( const QString &s)
insilmaril@837
   250
{
insilmaril@842
   251
	searchString=s;
insilmaril@837
   252
}
insilmaril@825
   253
insilmaril@842
   254
QString FindResultModel::getSearchString()
insilmaril@842
   255
{
insilmaril@842
   256
	return searchString;
insilmaril@842
   257
}
insilmaril@842
   258
insilmaril@842
   259
void FindResultModel::setSearchFlags( QTextDocument::FindFlags f)
insilmaril@842
   260
{
insilmaril@842
   261
	searchFlags=f;
insilmaril@842
   262
}
insilmaril@842
   263
insilmaril@842
   264
QTextDocument::FindFlags FindResultModel::getSearchFlags()
insilmaril@842
   265
{
insilmaril@842
   266
	return searchFlags;
insilmaril@842
   267
}
insilmaril@842
   268