2 * This file is part of NixNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
20 package cx.fbn.nevernote.filters;
\r
22 import java.util.TreeSet;
\r
24 import com.trolltech.qt.core.QAbstractItemModel;
\r
25 import com.trolltech.qt.core.QModelIndex;
\r
26 import com.trolltech.qt.core.QObject;
\r
27 import com.trolltech.qt.core.Qt;
\r
28 import com.trolltech.qt.gui.QIcon;
\r
29 import com.trolltech.qt.gui.QImage;
\r
30 import com.trolltech.qt.gui.QSortFilterProxyModel;
\r
32 import cx.fbn.nevernote.Global;
\r
34 public class NoteSortFilterProxyModel extends QSortFilterProxyModel {
\r
35 private final TreeSet<String> guids;
\r
36 public Signal2<Integer,Integer> sortChanged;
\r
37 public boolean blocked;
\r
39 public NoteSortFilterProxyModel(QObject parent) {
\r
41 boolean blocked = false;
\r
42 guids = new TreeSet<String>();
\r
43 setDynamicSortFilter(true);
\r
44 sortChanged = new Signal2<Integer,Integer>();
\r
45 // logger = new ApplicationLogger("filter.log");
\r
47 public void clear() {
\r
50 public void addGuid(String guid) {
\r
51 // if (!guids.containsKey(guid))
\r
54 public void filter() {
\r
58 protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {
\r
59 if (guids.size() == 0)
\r
61 QAbstractItemModel model = sourceModel();
\r
62 QModelIndex guidIndex = sourceModel().index(sourceRow, Global.noteTableGuidPosition);
\r
63 String guid = (String)model.data(guidIndex);
\r
65 if (guids.contains(guid))
\r
73 public void sort(int col, Qt.SortOrder order) {
\r
74 if (col != Global.noteTableThumbnailPosition) {
\r
76 sortChanged.emit(col, order.value()); // Signal that the sort order has been modified
\r
78 super.sort(col,order);
\r
83 protected boolean lessThan(QModelIndex left, QModelIndex right) {
\r
84 Object leftData = sourceModel().data(left);
\r
85 Object rightData = sourceModel().data(right);
\r
87 if (rightData == null)
\r
89 if (leftData instanceof QIcon)
\r
91 if (leftData instanceof QImage && rightData instanceof QImage)
\r
93 if (leftData instanceof Long && rightData instanceof Long) {
\r
94 Long leftLong = (Long)leftData;
\r
95 Long rightLong = (Long)rightData;
\r
96 return leftLong.compareTo(rightLong) < 0;
\r
98 if (leftData instanceof String && rightData instanceof String) {
\r
99 String leftString = (String)leftData;
\r
100 String rightString = (String)rightData;
\r
101 return leftString.compareTo(rightString) < 0;
\r
104 return super.lessThan(left, right);
\r