OSDN Git Service

Add the ability to pin notes.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / NoteTableModel.java
index 95c84ea..3b8187e 100644 (file)
@@ -1,6 +1,5 @@
 package cx.fbn.nevernote.gui;\r
 \r
-import java.util.ArrayList;\r
 import java.util.Collections;\r
 import java.util.HashMap;\r
 import java.util.List;\r
@@ -18,6 +17,7 @@ import com.trolltech.qt.gui.QImage;
 import com.trolltech.qt.gui.QPixmap;\r
 \r
 import cx.fbn.nevernote.Global;\r
+import cx.fbn.nevernote.evernote.NoteMetadata;\r
 import cx.fbn.nevernote.filters.NoteSortFilterProxyModel;\r
 import cx.fbn.nevernote.utilities.ListManager;\r
 \r
@@ -26,15 +26,14 @@ public class NoteTableModel extends QAbstractTableModel {
        private final Object[]                  headers;\r
        private List<Note>                              noteIndex;\r
        private List<Note>                              masterNoteIndex;\r
-       private List<String>                    unsynchronizedNotes;\r
-       public HashMap<String,Integer>  titleColors;\r
+       public HashMap<String,NoteMetadata>     metaData;\r
        public NoteSortFilterProxyModel proxyModel;             // note sort model\r
        \r
        public NoteTableModel(ListManager m) {\r
                headers = new Object[Global.noteTableColumnCount];\r
                listManager = m;\r
                masterNoteIndex = null;\r
-               unsynchronizedNotes = new ArrayList<String>();\r
+               metaData = new HashMap<String,NoteMetadata>();\r
        }\r
        \r
        public List<Note> getNoteIndex() {\r
@@ -53,19 +52,10 @@ public class NoteTableModel extends QAbstractTableModel {
                proxyModel = m;\r
        }\r
        \r
-       public List<String> getUnsynchronizedNotes() {\r
-               return unsynchronizedNotes;\r
-       }\r
-       public void setUnsynchronizedNotes(List<String> list) {\r
-               unsynchronizedNotes = list;\r
-       }\r
-       \r
-       public HashMap<String, Integer> getTitleColors() {\r
-               return titleColors;\r
-       }\r
-       public void setTitleColors(HashMap<String, Integer> map) {\r
-               titleColors = map;\r
+       public void setNoteMetadata(HashMap<String, NoteMetadata> list) {\r
+               metaData = list;\r
        }\r
+\r
        \r
        @Override\r
        public int columnCount(QModelIndex arg0) {\r
@@ -82,7 +72,8 @@ public class NoteTableModel extends QAbstractTableModel {
         }\r
         case Qt.ItemDataRole.DecorationRole: {\r
                if (index.column() == Global.noteTableThumbnailPosition ||\r
-                       index.column() == Global.noteTableSynchronizedPosition)\r
+                       index.column() == Global.noteTableSynchronizedPosition ||\r
+                       index.column() == Global.noteTablePinnedPosition)\r
                        return valueAt(index.row(), index.column());\r
                else\r
                        return null;\r
@@ -90,8 +81,8 @@ public class NoteTableModel extends QAbstractTableModel {
         case Qt.ItemDataRole.BackgroundRole: {\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
+               if (metaData != null && metaData.containsKey(guid)) {\r
+                       int color = metaData.get(guid).getColor();\r
                        backgroundColor.setRgb(color);\r
                }\r
                return backgroundColor;\r
@@ -100,8 +91,8 @@ public class NoteTableModel extends QAbstractTableModel {
                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
-                       int color = titleColors.get(guid);\r
+               if (metaData != null && metaData.containsKey(guid)) {\r
+                       int color = metaData.get(guid).getColor();\r
                        backgroundColor.setRgb(color);\r
                        if (backgroundColor.rgb() == QColor.black.rgb() || backgroundColor.rgb() == QColor.blue.rgb()) \r
                                foregroundColor.setRgb(QColor.white.rgb());\r
@@ -148,9 +139,16 @@ public class NoteTableModel extends QAbstractTableModel {
                        String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
                        QIcon dotIcon = new QIcon(iconPath+"dot.png");\r
                        String guid = note.getGuid();\r
-                       for (int i=0; i<unsynchronizedNotes.size(); i++) {\r
-                               if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid)) \r
-                                       return dotIcon;\r
+                       if (metaData.containsKey(guid) && metaData.get(guid).isDirty()) \r
+                               return dotIcon;\r
+                       return null;\r
+               }\r
+               if (col == Global.noteTablePinnedPosition) {\r
+                       String guid = note.getGuid();\r
+                       if (metaData.containsKey(guid) && metaData.get(guid).isPinned()) {\r
+                               String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
+                               QIcon dotIcon = new QIcon(iconPath+"dot.png");\r
+                               return dotIcon;\r
                        }\r
                        return null;\r
                }\r
@@ -322,20 +320,25 @@ public class NoteTableModel extends QAbstractTableModel {
        }\r
 \r
        public void updateNoteGuid(String oldGuid, String newGuid) {\r
-               for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
-                       if (getMasterNoteIndex().get(i).getGuid() != null && getMasterNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
-                               getMasterNoteIndex().get(i).setGuid(newGuid);\r
-                               QModelIndex idx = createIndex(i, Global.noteTableGuidPosition, nativePointer());\r
-                               setData(idx, new String(getMasterNoteIndex().get(i).getGuid()), Qt.ItemDataRole.EditRole); \r
-                               i=getMasterNoteIndex().size()+1;\r
-                       }\r
-               }\r
+               \r
                for (int i=0; i<getNoteIndex().size(); i++) {\r
                        if (getNoteIndex().get(i).getGuid() != null && getNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
                                getNoteIndex().get(i).setGuid(newGuid);; \r
                                i=getNoteIndex().size()+1;\r
                        }\r
                }\r
+               \r
+               boolean k = true;\r
+               if (k) return;\r
+               \r
+               for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
+                       if (getMasterNoteIndex().get(i).getGuid() != null && getMasterNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
+                               getMasterNoteIndex().get(i).setGuid(newGuid);\r
+//                             QModelIndex idx = createIndex(i, Global.noteTableGuidPosition, nativePointer());\r
+//                             setData(idx, new String(getMasterNoteIndex().get(i).getGuid()), Qt.ItemDataRole.EditRole); \r
+                               i=getMasterNoteIndex().size()+1;\r
+                       }\r
+               }               \r
        }\r
        \r
        public void updateNoteNotebook(String guid, String notebookGuid) {\r
@@ -403,26 +406,8 @@ public class NoteTableModel extends QAbstractTableModel {
 \r
        public void updateNoteSyncStatus(String guid, boolean sync) {\r
                \r
-               boolean found = false;\r
-               for (int i=0; i<unsynchronizedNotes.size(); i++) {                      \r
-                       // If the note is now synchronized, but it is in the unsynchronized list, remove it\r
-                       if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {\r
-                               unsynchronizedNotes.remove(i);\r
-                               found = true;\r
-                               i=unsynchronizedNotes.size();\r
-                       }\r
-                       \r
-                       // If the note is not synchronized, but it is already in the unsynchronized list, do nothing\r
-                       if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {\r
-                               found = true;\r
-                               i=unsynchronizedNotes.size();\r
-                       }\r
-               }\r
-               \r
-               // If we've gotten through the entire list, then we consider it synchronized.  If this is \r
-               // wrong, add it to the list.\r
-               if (!sync && !found) \r
-                       unsynchronizedNotes.add(guid);\r
+               if (metaData.containsKey(guid)) \r
+                       metaData.get(guid).setDirty(!sync);\r
                \r
                // Now we need to go through the table & update it\r
                for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
@@ -439,10 +424,29 @@ public class NoteTableModel extends QAbstractTableModel {
                }\r
        }\r
        \r
-       public void addNote(Note n) {\r
+       public void updateNotePinnedStatus(String guid, boolean pinned) {\r
+               // Now we need to go through the table & update it\r
+               for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
+                       if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
+                               QModelIndex idx = createIndex(i, Global.noteTablePinnedPosition, nativePointer());\r
+                               String value;\r
+                               if (metaData.containsKey(guid));\r
+                                       metaData.get(guid).setPinned(pinned);\r
+                               if (pinned)\r
+                                       value = tr("true");\r
+                               else\r
+                                       value = tr("false");\r
+                               setData(idx, value, Qt.ItemDataRole.EditRole); \r
+                               return;\r
+                       }       \r
+               }\r
+       }\r
+\r
+       \r
+       public void addNote(Note n, NoteMetadata meta) {\r
                getNoteIndex().add(n);\r
                getMasterNoteIndex().add(n);\r
-               proxyModel.addGuid(n.getGuid());\r
+               proxyModel.addGuid(n.getGuid(), meta);\r
                proxyModel.invalidate();\r
 //             proxyModel.filter();\r
        }\r
@@ -458,10 +462,8 @@ public class NoteTableModel extends QAbstractTableModel {
                }\r
        }\r
        \r
-       public void updateNoteTitleColor(String guid, Integer color) {\r
-               getTitleColors().remove(guid);\r
-               getTitleColors().put(guid, color);\r
-               layoutChanged.emit();\r
+       public void setMetaData(HashMap<String, NoteMetadata> h) {\r
+               metaData = h;\r
        }\r
 \r
        @Override\r
@@ -473,4 +475,16 @@ public class NoteTableModel extends QAbstractTableModel {
                return new Qt.ItemFlags(flags);\r
        }\r
        \r
+       public void updateNoteTitleColor(String guid, int color) {\r
+               NoteMetadata m = metaData.get(guid);\r
+               if (m == null) {\r
+                       m = new NoteMetadata();\r
+                       m.setGuid(guid);\r
+                       metaData.put(guid, m);\r
+               }\r
+               if (metaData.containsKey(guid) && metaData.get(guid).getColor() != color) {\r
+                       metaData.get(guid).setColor(color);     \r
+                       layoutChanged.emit();\r
+               }\r
+       }\r
 }\r