TreeViewFind: Search in all columns

Extend searching from a specified column to all columns.

Change-Id: I939867611dc79376237f3599baab28b408620b62
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Aurindam Jana
2012-03-23 11:42:10 +01:00
committed by hjk
parent 0949e083eb
commit ab985b0ee9
2 changed files with 46 additions and 28 deletions

View File

@@ -39,11 +39,10 @@ namespace Find {
struct ItemModelFindPrivate
{
explicit ItemModelFindPrivate(QTreeView *view, int role, int column)
explicit ItemModelFindPrivate(QTreeView *view, int role)
: m_view(view)
, m_incrementalWrappedState(false),
m_role(role),
m_column(column)
m_role(role)
{
}
@@ -51,11 +50,10 @@ struct ItemModelFindPrivate
QModelIndex m_incrementalFindStart;
bool m_incrementalWrappedState;
int m_role;
int m_column;
};
TreeViewFind::TreeViewFind(QTreeView *view, int role, int column)
: d(new ItemModelFindPrivate(view, role, column))
TreeViewFind::TreeViewFind(QTreeView *view, int role)
: d(new ItemModelFindPrivate(view, role))
{
}
@@ -166,14 +164,17 @@ IFindSupport::Result TreeViewFind::find(const QString &searchTxt,
QModelIndex resultIndex;
QModelIndex currentIndex = d->m_view->currentIndex();
QModelIndex index = currentIndex;
int currentRow = currentIndex.row();
bool backward = (flags & QTextDocument::FindBackward);
if (wrapped)
*wrapped = false;
bool anyWrapped = false;
bool stepWrapped = false;
if (!startFromCurrentIndex)
index = followingIndex(index, backward, &stepWrapped);
else
currentRow = -1;
do {
anyWrapped |= stepWrapped; // update wrapped state if we actually stepped to next/prev item
if (index.isValid()) {
@@ -184,11 +185,17 @@ IFindSupport::Result TreeViewFind::find(const QString &searchTxt,
QRegExp searchExpr = QRegExp(searchTxt,
(sensitive ? Qt::CaseSensitive :
Qt::CaseInsensitive));
if (searchExpr.indexIn(text) != -1)
if (searchExpr.indexIn(text) != -1
&& d->m_view->model()->flags(index) & Qt::ItemIsSelectable
&& currentRow != index.row())
resultIndex = index;
} else {
QTextDocument doc(text);
if (!doc.find(searchTxt, 0, flags).isNull())
if (!doc.find(searchTxt, 0,
flags & (Find::FindCaseSensitively |
Find::FindWholeWords)).isNull()
&& d->m_view->model()->flags(index) & Qt::ItemIsSelectable
&& currentRow != index.row())
resultIndex = index;
}
}
@@ -226,17 +233,23 @@ QModelIndex TreeViewFind::nextIndex(const QModelIndex &idx, bool *wrapped) const
QModelIndex current = idx;
while (!nextIndex.isValid()) {
int row = current.row();
int column = current.column();
current = current.parent();
if (row + 1 < model->rowCount(current)) {
// Same parent has another child
nextIndex = model->index(row + 1, 0, current);
if (column + 1 < model->columnCount(current)) {
nextIndex = model->index(row, column + 1, current);
} else {
// go up one parent
if (!current.isValid()) {
// we start from the beginning
if (wrapped)
*wrapped = true;
nextIndex = model->index(0, 0);
if (row + 1 < model->rowCount(current)) {
// Same parent has another child
nextIndex = model->index(row + 1, 0, current);
} else {
// go up one parent
if (!current.isValid()) {
// we start from the beginning
if (wrapped)
*wrapped = true;
nextIndex = model->index(0, 0);
}
}
}
}
@@ -252,21 +265,27 @@ QModelIndex TreeViewFind::prevIndex(const QModelIndex &idx, bool *wrapped) const
QAbstractItemModel *model = d->m_view->model();
if (current.isValid()) {
int row = current.row();
if (row > 0) {
current = model->index(row - 1, 0, current.parent());
int column = current.column();
if (column > 0) {
current = model->index(row, column - 1, current.parent());
} else {
current = current.parent();
checkForChildren = !current.isValid();
if (checkForChildren && wrapped) {
// we start from the end
*wrapped = true;
if (row > 0) {
current = model->index(row - 1, model->columnCount(current.parent()) - 1,
current.parent());
} else {
current = current.parent();
checkForChildren = !current.isValid();
if (checkForChildren && wrapped) {
// we start from the end
*wrapped = true;
}
}
}
}
if (checkForChildren) {
// traverse down the hierarchy
while (int rc = model->rowCount(current)) {
current = model->index(rc - 1, d->m_column, current);
current = model->index(rc - 1, model->columnCount(current) - 1, current);
}
}
return current;

View File

@@ -48,8 +48,7 @@ class FIND_EXPORT TreeViewFind : public Find::IFindSupport
{
Q_OBJECT
public:
explicit TreeViewFind(QTreeView *view, int role = Qt::DisplayRole,
int column = 0);
explicit TreeViewFind(QTreeView *view, int role = Qt::DisplayRole);
virtual ~TreeViewFind();
bool supportsReplace() const;