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

View File

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