90
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Proxy model for search and highlight items view.
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <QObject>
|
|
19 |
#include <hbcolorscheme.h>
|
|
20 |
#include <caitemmodel.h>
|
|
21 |
|
|
22 |
#include "hssearchfilterproxymodel.h"
|
|
23 |
|
|
24 |
/*!
|
|
25 |
\class HsSearchFilterProxyModel
|
|
26 |
\ingroup group_hsmenustateplugin
|
|
27 |
\brief Proxy Model for highlight search string.
|
|
28 |
Highlight search string in model item title.
|
|
29 |
\lib ?library
|
|
30 |
\see QSortFilterProxyModel
|
|
31 |
*/
|
|
32 |
|
|
33 |
/*!
|
|
34 |
Constructor.
|
|
35 |
\param parent parent for object.
|
|
36 |
*/
|
|
37 |
HsSearchFilterProxyModel::HsSearchFilterProxyModel(QObject *parent) :
|
|
38 |
QSortFilterProxyModel(parent),
|
|
39 |
mInSettingFilterString(false)
|
|
40 |
{
|
|
41 |
}
|
|
42 |
|
|
43 |
/*
|
|
44 |
Destructor.
|
|
45 |
*/
|
|
46 |
HsSearchFilterProxyModel::~HsSearchFilterProxyModel()
|
|
47 |
{
|
|
48 |
}
|
|
49 |
|
|
50 |
/*
|
|
51 |
The data() method is overridden to have a place where the highlighting
|
|
52 |
is added to the item data. In this implementation HTML-tags for
|
|
53 |
background color are added to the beginning and end of the match
|
|
54 |
in the data method using HbColorScheme.
|
|
55 |
\param index source parent index.
|
|
56 |
\param role item role.
|
|
57 |
*/
|
|
58 |
QVariant HsSearchFilterProxyModel::data(const QModelIndex &index,
|
|
59 |
int role) const
|
|
60 |
{
|
|
61 |
QColor highlight = HbColorScheme::color("qtc_lineedit_marker_normal");
|
|
62 |
QColor color = HbColorScheme::color("qtc_lineedit_selected");
|
|
63 |
|
|
64 |
if (role == Qt::DisplayRole && !mFilterCriteria.isEmpty()) {
|
|
65 |
QStringList textList =
|
|
66 |
QSortFilterProxyModel::data(index, role).toStringList();
|
|
67 |
if(!textList[0].isEmpty())
|
|
68 |
{
|
|
69 |
QString start = QString(
|
|
70 |
TAG_START).arg( highlight.name().toUpper() ).arg(color.name().toUpper());
|
|
71 |
int pos = textList[0].indexOf(mFilterCriteria, 0,Qt::CaseInsensitive);
|
|
72 |
textList[0].insert(pos, start);
|
|
73 |
textList[0].insert(pos + start.length() + mFilterCriteria.length(), TAG_END);
|
|
74 |
}
|
|
75 |
|
|
76 |
return QVariant(textList);
|
|
77 |
}
|
|
78 |
return QSortFilterProxyModel::data(index, role);
|
|
79 |
|
|
80 |
}
|
|
81 |
/*
|
|
82 |
SetFilterString is the way to set the filtering criteria that is used
|
|
83 |
in highlight filtering.
|
|
84 |
QSortFilterProxyModel does not allow overriding its own filtering
|
|
85 |
criteria setting methods, so a separate method is added in the
|
|
86 |
HighlightFilterProxyModel.
|
|
87 |
This method stores the current filtering criteria and it can be
|
|
88 |
used in the data() method in highlighting the matches.
|
|
89 |
The method also calls the QSortFilterProxyModel method for setting
|
|
90 |
the filterCriteria. This triggers the base class to do the actual
|
|
91 |
filtering.
|
|
92 |
\param index source parent index.
|
|
93 |
\param role item role.
|
|
94 |
*/
|
|
95 |
void HsSearchFilterProxyModel::setFilterString(const QString &filterCriteria)
|
|
96 |
{
|
|
97 |
mInSettingFilterString = true;
|
|
98 |
mFilterCriteria = filterCriteria;
|
|
99 |
setFilterRegExp(
|
|
100 |
QRegExp("(^|\\b)" + filterCriteria, Qt::CaseInsensitive));
|
|
101 |
mInSettingFilterString = false;
|
|
102 |
}
|
|
103 |
|
|
104 |
QString HsSearchFilterProxyModel::filterString() const
|
|
105 |
{
|
|
106 |
return mFilterCriteria;
|
|
107 |
}
|
|
108 |
|
|
109 |
/*
|
|
110 |
Reimplementated from QSortFilterProxyModel.
|
|
111 |
The filterAcceptsRow method is overridden to send a dataChanged signal
|
|
112 |
to the list view item
|
|
113 |
\param source_row row in source model.
|
|
114 |
\param source_parent source parent index.
|
|
115 |
*/
|
|
116 |
bool HsSearchFilterProxyModel::filterAcceptsRow (int source_row,
|
|
117 |
const QModelIndex &source_parent) const
|
|
118 |
{
|
|
119 |
bool retVal = QSortFilterProxyModel::filterAcceptsRow(source_row,
|
|
120 |
source_parent);
|
|
121 |
if (mInSettingFilterString && retVal) {
|
|
122 |
QModelIndex mi = mapFromSource(sourceModel()->index(source_row, 0,
|
|
123 |
source_parent));
|
|
124 |
emit const_cast<HsSearchFilterProxyModel*>(this)->dataChanged(mi, mi);
|
|
125 |
}
|
|
126 |
return retVal;
|
|
127 |
}
|
|
128 |
|
|
129 |
|
|
130 |
|