OSDN Git Service

Correct problem parsing notes where carriage returns did not separate some XML lines...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / filters / NoteSortFilterProxyModel.java
1 /*\r
2  * This file is part of NeverNote \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 import java.util.Map;\r
24 \r
25 import com.trolltech.qt.core.QAbstractItemModel;\r
26 import com.trolltech.qt.core.QModelIndex;\r
27 import com.trolltech.qt.core.QObject;\r
28 import com.trolltech.qt.gui.QSortFilterProxyModel;\r
29 \r
30 import cx.fbn.nevernote.Global;\r
31 \r
32 public class NoteSortFilterProxyModel extends QSortFilterProxyModel {\r
33         private final Map<String,String> guids;\r
34         private String dateFormat;\r
35         \r
36         public NoteSortFilterProxyModel(QObject parent) {\r
37                 super(parent);\r
38                 guids = new HashMap<String,String>();\r
39                 dateFormat = Global.getDateFormat() + " " + Global.getTimeFormat();\r
40                 setDynamicSortFilter(true);\r
41 //              logger = new ApplicationLogger("filter.log");\r
42         }\r
43         public void clear() {\r
44                 guids.clear();\r
45         }\r
46         public void addGuid(String guid) {\r
47 //              if (!guids.containsKey(guid))\r
48                         guids.put(guid, null);\r
49         }\r
50         public void filter() {\r
51                 dateFormat = Global.getDateFormat() + " " + Global.getTimeFormat();\r
52                 invalidateFilter();\r
53         }\r
54         @Override\r
55         protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {\r
56                 if (guids.size() == 0)\r
57                         return false;\r
58                 QAbstractItemModel model = sourceModel();\r
59                 QModelIndex guidIndex = sourceModel().index(sourceRow, Global.noteTableGuidPosition);\r
60                 String guid = (String)model.data(guidIndex);\r
61                 \r
62                 if (guids.containsKey(guid))\r
63                         return true;\r
64                 else\r
65                         return false;\r
66         }\r
67         \r
68         @Override\r
69         protected boolean lessThan(QModelIndex left, QModelIndex right) {\r
70                 Object leftData = sourceModel().data(left);\r
71                 Object rightData = sourceModel().data(right);\r
72                 \r
73                 if (leftData instanceof Long && rightData instanceof Long) {\r
74                           Long leftLong = (Long)leftData;\r
75                           Long rightLong = (Long)rightData;\r
76                           return leftLong.compareTo(rightLong) < 0;            \r
77                 }\r
78                 if (leftData instanceof String && rightData instanceof String) {\r
79                         String leftString = (String)leftData;\r
80                         String rightString = (String)rightData;\r
81                         return leftString.compareTo(rightString) < 0;\r
82                 }\r
83                 \r
84                 return super.lessThan(left, right);\r
85         }\r
86 }