2 * This file is part of NeverNote
\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.HashMap;
\r
23 import java.util.Map;
\r
25 import com.trolltech.qt.core.QAbstractItemModel;
\r
26 import com.trolltech.qt.core.QDateTime;
\r
27 import com.trolltech.qt.core.QModelIndex;
\r
28 import com.trolltech.qt.core.QObject;
\r
29 import com.trolltech.qt.gui.QSortFilterProxyModel;
\r
31 import cx.fbn.nevernote.Global;
\r
33 public class NoteSortFilterProxyModel extends QSortFilterProxyModel {
\r
34 private final Map<String,String> guids;
\r
35 private String dateFormat;
\r
37 public NoteSortFilterProxyModel(QObject parent) {
\r
39 guids = new HashMap<String,String>();
\r
40 dateFormat = Global.getDateFormat() + " " + Global.getTimeFormat();
\r
41 setDynamicSortFilter(true);
\r
42 // logger = new ApplicationLogger("filter.log");
\r
44 public void clear() {
\r
47 public void addGuid(String guid) {
\r
48 // if (!guids.containsKey(guid))
\r
49 guids.put(guid, null);
\r
51 public void filter() {
\r
52 dateFormat = Global.getDateFormat() + " " + Global.getTimeFormat();
\r
56 protected boolean filterAcceptsRow(int sourceRow, QModelIndex sourceParent) {
\r
57 if (guids.size() == 0)
\r
59 QAbstractItemModel model = sourceModel();
\r
60 QModelIndex guidIndex = sourceModel().index(sourceRow, Global.noteTableGuidPosition);
\r
61 String guid = (String)model.data(guidIndex);
\r
63 if (guids.containsKey(guid))
\r
70 protected boolean lessThan(QModelIndex left, QModelIndex right) {
\r
71 Object leftData = sourceModel().data(left);
\r
72 Object rightData = sourceModel().data(right);
\r
74 if (sortColumn() == Global.noteTableCreationPosition ||
\r
75 sortColumn() == Global.noteTableChangedPosition ||
\r
76 sortColumn() == Global.noteTableSubjectDatePosition) {
\r
77 QDateTime leftDate = QDateTime.fromString(leftData.toString(), dateFormat);
\r
78 QDateTime rightDate = QDateTime.fromString(rightData.toString(), dateFormat);
\r
79 return leftDate.compareTo(rightDate) < 0;
\r
81 if (leftData instanceof String && rightData instanceof String) {
\r
82 String leftString = (String)leftData;
\r
83 String rightString = (String)rightData;
\r
84 return leftString.compareTo(rightString) < 0;
\r
87 return super.lessThan(left, right);
\r