OSDN Git Service

8c0ca288f424e9b355a083fe8f1e3fbcb6fed4a9
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / NoteSortFilterProxyModel.java
1 /*\r
2  * This file is part of NixNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\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
12  *\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
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.filters;\r
21 \r
22 import java.util.HashMap;\r
23 \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
31 \r
32 import cx.fbn.nevernote.Global;\r
33 import cx.fbn.nevernote.evernote.NoteMetadata;\r
34 \r
35 public class NoteSortFilterProxyModel extends QSortFilterProxyModel {\r
36         private final HashMap<String, NoteMetadata> guids;\r
37         private final HashMap<String, NoteMetadata> pinnedGuids;\r
38         public Signal2<Integer,Integer> sortChanged;\r
39         public boolean blocked;\r
40         \r
41         public NoteSortFilterProxyModel(QObject parent) {\r
42                 super(parent);\r
43                 guids = new HashMap<String, NoteMetadata>();\r
44                 pinnedGuids = new HashMap<String, NoteMetadata>();\r
45                 setDynamicSortFilter(true);\r
46                 sortChanged = new Signal2<Integer,Integer>();\r
47         }\r
48         public void clear() {\r
49                 guids.clear();\r
50         }\r
51         public void addGuid(String guid, NoteMetadata meta) {\r
52                 if (!guids.containsKey(guid))\r
53                         guids.put(guid, meta);\r
54                 if (meta!= null && pinnedGuids != null && meta.isPinned() == true && !pinnedGuids.containsKey(guid))\r
55                         pinnedGuids.put(guid, meta);\r
56         }\r
57         public void filter() {\r
58                 invalidateFilter();\r
59         }\r
60         @Override\r
61         protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {\r
62                 QAbstractItemModel model = sourceModel();\r
63                 QModelIndex guidIndex = sourceModel().index(sourceRow, Global.noteTableGuidPosition);\r
64                 String guid = (String)model.data(guidIndex);\r
65                 \r
66                 if (guids.containsKey(guid) || pinnedGuids.containsKey(guid))\r
67                         return true;\r
68                 else\r
69                         return false;\r
70         }\r
71         \r
72         \r
73         @Override\r
74         public void sort(int col, Qt.SortOrder order) {\r
75                 if (col != Global.noteTableThumbnailPosition) {\r
76                         if (!blocked)   {\r
77                                 sortChanged.emit(col, order.value());    // Signal that the sort order has been modified\r
78                         }\r
79                         super.sort(col,order);\r
80                 }\r
81         }\r
82         \r
83         @Override\r
84         protected boolean lessThan(QModelIndex left, QModelIndex right) {\r
85                 Object leftData = sourceModel().data(left);\r
86                 Object rightData = sourceModel().data(right);\r
87                 \r
88                 if (rightData == null)\r
89                         return true;\r
90                 if (leftData instanceof QIcon)\r
91                         return true;\r
92                 if (leftData instanceof QImage && rightData instanceof QImage)\r
93                         return true;\r
94                 if (leftData instanceof Long && rightData instanceof Long) {\r
95                           Long leftLong = (Long)leftData;\r
96                           Long rightLong = (Long)rightData;\r
97                           return leftLong.compareTo(rightLong) < 0;            \r
98                 }\r
99                 if (leftData instanceof String && rightData instanceof String) {\r
100                         String leftString = (String)leftData;\r
101                         String rightString = (String)rightData;\r
102                         return leftString.toLowerCase().compareTo(rightString.toLowerCase()) < 0;\r
103                 }\r
104                 \r
105                 return super.lessThan(left, right);\r
106         }\r
107 }