OSDN Git Service

Altered note table to use a display delegate to show dates rather than convert the...
authorRandy Baumgarte <randy@fbn.cx>
Mon, 23 Aug 2010 12:53:47 +0000 (08:53 -0400)
committerRandy Baumgarte <randy@fbn.cx>
Tue, 24 Aug 2010 00:14:33 +0000 (20:14 -0400)
src/cx/fbn/nevernote/filters/NoteSortFilterProxyModel.java
src/cx/fbn/nevernote/gui/NoteTableDateDelegate.java [new file with mode: 0644]
src/cx/fbn/nevernote/gui/NoteTableModel.java
src/cx/fbn/nevernote/gui/TableView.java

index ca65b29..9a651aa 100644 (file)
@@ -23,7 +23,6 @@ import java.util.HashMap;
 import java.util.Map;\r
 \r
 import com.trolltech.qt.core.QAbstractItemModel;\r
-import com.trolltech.qt.core.QDateTime;\r
 import com.trolltech.qt.core.QModelIndex;\r
 import com.trolltech.qt.core.QObject;\r
 import com.trolltech.qt.gui.QSortFilterProxyModel;\r
@@ -33,6 +32,7 @@ import cx.fbn.nevernote.Global;
 public class NoteSortFilterProxyModel extends QSortFilterProxyModel {\r
        private final Map<String,String> guids;\r
        private String dateFormat;\r
+       private int traceCount;\r
        \r
        public NoteSortFilterProxyModel(QObject parent) {\r
                super(parent);\r
@@ -48,6 +48,7 @@ public class NoteSortFilterProxyModel extends QSortFilterProxyModel {
                        guids.put(guid, null);\r
        }\r
        public void filter() {\r
+               traceCount = 0;\r
                dateFormat = Global.getDateFormat() + " " + Global.getTimeFormat();\r
                invalidateFilter();\r
        }\r
@@ -67,15 +68,14 @@ public class NoteSortFilterProxyModel extends QSortFilterProxyModel {
        \r
        @Override\r
        protected boolean lessThan(QModelIndex left, QModelIndex right) {\r
+               \r
                Object leftData = sourceModel().data(left);\r
                Object rightData = sourceModel().data(right);\r
                \r
-               if (sortColumn() == Global.noteTableCreationPosition || \r
-                               sortColumn() == Global.noteTableChangedPosition ||\r
-                               sortColumn() == Global.noteTableSubjectDatePosition) {\r
-                       QDateTime leftDate = QDateTime.fromString(leftData.toString(), dateFormat);\r
-                       QDateTime rightDate = QDateTime.fromString(rightData.toString(), dateFormat);\r
-                       return leftDate.compareTo(rightDate) < 0;\r
+               if (leftData instanceof Long && rightData instanceof Long) {\r
+                       Long leftLong = (Long)leftData;\r
+                       Long rightLong = (Long)rightData;\r
+                       return leftLong.compareTo(rightLong) < 0;\r
                }\r
                if (leftData instanceof String && rightData instanceof String) {\r
                        String leftString = (String)leftData;\r
diff --git a/src/cx/fbn/nevernote/gui/NoteTableDateDelegate.java b/src/cx/fbn/nevernote/gui/NoteTableDateDelegate.java
new file mode 100644 (file)
index 0000000..b1bf164
--- /dev/null
@@ -0,0 +1,26 @@
+package cx.fbn.nevernote.gui;\r
+\r
+import java.text.SimpleDateFormat;\r
+\r
+import com.trolltech.qt.core.QDateTime;\r
+import com.trolltech.qt.core.QLocale;\r
+import com.trolltech.qt.gui.QStyledItemDelegate;\r
+\r
+import cx.fbn.nevernote.Global;\r
+\r
+public class NoteTableDateDelegate extends QStyledItemDelegate {\r
+\r
+       \r
+       @Override\r
+       public String displayText(Object value, QLocale locale ) {\r
+               \r
+               String fmt = Global.getDateFormat() + " " + Global.getTimeFormat();\r
+               String dateTimeFormat = new String(fmt);\r
+               SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);\r
+               \r
+               StringBuilder date = new StringBuilder(simple.format(value));\r
+               QDateTime created = QDateTime.fromString(date.toString(), fmt);\r
+               return created.toString(fmt);\r
+        }\r
+\r
+}\r
index f901470..3090919 100644 (file)
@@ -1,6 +1,5 @@
 package cx.fbn.nevernote.gui;\r
 \r
-import java.text.SimpleDateFormat;\r
 import java.util.ArrayList;\r
 import java.util.Collections;\r
 import java.util.HashMap;\r
@@ -77,7 +76,7 @@ public class NoteTableModel extends QAbstractTableModel {
             return valueAt(index.row(), index.column());\r
         }\r
         case Qt.ItemDataRole.BackgroundRole: {\r
-               String guid = valueAt(index.row(), Global.noteTableGuidPosition);\r
+               String guid = (String)valueAt(index.row(), Global.noteTableGuidPosition);\r
                QColor backgroundColor = new QColor(QColor.white);\r
                if (titleColors != null && titleColors.containsKey(guid)) {\r
                        int color = titleColors.get(guid);\r
@@ -86,7 +85,7 @@ public class NoteTableModel extends QAbstractTableModel {
                return backgroundColor;\r
         }\r
         case Qt.ItemDataRole.ForegroundRole: {\r
-               String guid = valueAt(index.row(), Global.noteTableGuidPosition);\r
+               String guid = (String)valueAt(index.row(), Global.noteTableGuidPosition);\r
                QColor backgroundColor = new QColor(QColor.white);\r
                QColor foregroundColor = new QColor(QColor.black);\r
                if (titleColors != null && titleColors.containsKey(guid)) {\r
@@ -112,20 +111,20 @@ public class NoteTableModel extends QAbstractTableModel {
        }\r
 \r
        \r
-       private String valueAt(int row, int col) {\r
+       private Object valueAt(int row, int col) {\r
                Note note = listManager.getMasterNoteIndex().get(row);\r
                \r
                if (col == Global.noteTableGuidPosition)\r
                        return note.getGuid();\r
                if (col == Global.noteTableCreationPosition) \r
-                       return formattedDate(note.getCreated());\r
+                       return note.getCreated();\r
                if (col == Global.noteTableChangedPosition) \r
-                       return formattedDate(note.getUpdated());\r
+                       return note.getUpdated();\r
                if (col == Global.noteTableSubjectDatePosition) {\r
                        if (note.getAttributes().getSubjectDate() > 0)\r
-                               return formattedDate(note.getAttributes().getSubjectDate());\r
+                               return note.getAttributes().getSubjectDate();\r
                        else\r
-                               return formattedDate(note.getCreated());                                \r
+                               return note.getCreated();                               \r
                }\r
                if (col == Global.noteTableTitlePosition)\r
                        return note.getTitle();\r
@@ -159,40 +158,6 @@ public class NoteTableModel extends QAbstractTableModel {
                return "";\r
        }\r
        \r
-       private String formattedDate(long d) {\r
-               String fmt = Global.getDateFormat() + " " + Global.getTimeFormat();\r
-               String dateTimeFormat = new String(fmt);\r
-               SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);\r
-               \r
-               StringBuilder date = new StringBuilder(simple.format(d));\r
-               QDateTime created = QDateTime.fromString(date.toString(), fmt);\r
-               return created.toString(fmt);\r
-\r
-       }\r
-\r
-       /*\r
-       @Override\r
-       public void sort(int column, Qt.SortOrder order) {\r
-               layoutAboutToBeChanged.emit();\r
-               boolean finished = false;\r
-               while (!finished) {\r
-                       finished = true;\r
-                       for (int i=1; i<rowCount(); i++) {\r
-                               String val1 = valueAt(i-1,column-1);\r
-                               Note n1 = getMasterNoteIndex().get(i-1);\r
-                               String val2 = valueAt(i,column);\r
-                               Note n2 = getMasterNoteIndex().get(i);\r
-                               \r
-                               if (val1.compareTo(val2) > 0) {\r
-                                       finished = false;\r
-                                       getMasterNoteIndex().set(i-1, n2);\r
-                                       getMasterNoteIndex().set(i, n1);\r
-                               }\r
-                       }\r
-               }\r
-               layoutChanged.emit();\r
-       }\r
-       */\r
        \r
        @Override\r
        public java.lang.Object headerData(int section, Qt.Orientation orientation, int role) {\r
index f95a844..3f13966 100644 (file)
@@ -124,6 +124,12 @@ public class TableView extends QTableView {
                fontHeight = f.height();\r
                rowChanged = new Signal1<String>();\r
                resetViewport = new Signal0();\r
+               \r
+               NoteTableDateDelegate dateDelegate = new NoteTableDateDelegate();\r
+               setItemDelegateForColumn(Global.noteTableCreationPosition, dateDelegate);\r
+               setItemDelegateForColumn(Global.noteTableChangedPosition, dateDelegate);\r
+               setItemDelegateForColumn(Global.noteTableSubjectDatePosition, dateDelegate);\r
+               \r
        }\r
        \r
        // This should rescroll to the current item in the list when a column is \r