OSDN Git Service

f901470fd15ffe5787d468d27259cdd513222454
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / NoteTableModel.java
1 package cx.fbn.nevernote.gui;\r
2 \r
3 import java.text.SimpleDateFormat;\r
4 import java.util.ArrayList;\r
5 import java.util.Collections;\r
6 import java.util.HashMap;\r
7 import java.util.List;\r
8 \r
9 import com.evernote.edam.type.Note;\r
10 import com.trolltech.qt.core.QDateTime;\r
11 import com.trolltech.qt.core.QModelIndex;\r
12 import com.trolltech.qt.core.Qt;\r
13 import com.trolltech.qt.gui.QAbstractTableModel;\r
14 import com.trolltech.qt.gui.QColor;\r
15 \r
16 import cx.fbn.nevernote.Global;\r
17 import cx.fbn.nevernote.filters.NoteSortFilterProxyModel;\r
18 import cx.fbn.nevernote.utilities.ListManager;\r
19 \r
20 public class NoteTableModel extends QAbstractTableModel {\r
21         private final ListManager               listManager;\r
22         private final Object[]                  headers;\r
23         private List<Note>                              noteIndex;\r
24         private List<Note>                              masterNoteIndex;\r
25         private List<String>                    unsynchronizedNotes;\r
26         public HashMap<String,Integer>  titleColors;\r
27         public NoteSortFilterProxyModel proxyModel;             // note sort model\r
28         \r
29         public NoteTableModel(ListManager m) {\r
30                 headers = new Object[Global.noteTableColumnCount];\r
31                 listManager = m;\r
32                 masterNoteIndex = null;\r
33                 unsynchronizedNotes = new ArrayList<String>();\r
34         }\r
35         \r
36         public List<Note> getNoteIndex() {\r
37                 return noteIndex;\r
38         }\r
39         public void setNoteIndex(List<Note> list) {\r
40                 noteIndex = list;\r
41         }\r
42         public List<Note> getMasterNoteIndex() {\r
43                 return masterNoteIndex;\r
44         }\r
45         public void setMasterNoteIndex(List<Note> list) {\r
46                 masterNoteIndex = list;\r
47         }\r
48         public void setSortProxyModel(NoteSortFilterProxyModel m) {\r
49                 proxyModel = m;\r
50         }\r
51         \r
52         public List<String> getUnsynchronizedNotes() {\r
53                 return unsynchronizedNotes;\r
54         }\r
55         public void setUnsynchronizedNotes(List<String> list) {\r
56                 unsynchronizedNotes = list;\r
57         }\r
58         \r
59         public HashMap<String, Integer> getTitleColors() {\r
60                 return titleColors;\r
61         }\r
62         public void setTitleColors(HashMap<String, Integer> map) {\r
63                 titleColors = map;\r
64         }\r
65         \r
66         @Override\r
67         public int columnCount(QModelIndex arg0) {\r
68                 return Global.noteTableColumnCount;\r
69         }\r
70 \r
71         @Override\r
72         public Object data(QModelIndex index, int role) {\r
73                 if (index == null)\r
74                         return null;\r
75         switch (role) {\r
76         case Qt.ItemDataRole.DisplayRole: {\r
77             return valueAt(index.row(), index.column());\r
78         }\r
79         case Qt.ItemDataRole.BackgroundRole: {\r
80                 String guid = valueAt(index.row(), Global.noteTableGuidPosition);\r
81                 QColor backgroundColor = new QColor(QColor.white);\r
82                 if (titleColors != null && titleColors.containsKey(guid)) {\r
83                         int color = titleColors.get(guid);\r
84                         backgroundColor.setRgb(color);\r
85                 }\r
86                 return backgroundColor;\r
87         }\r
88         case Qt.ItemDataRole.ForegroundRole: {\r
89                 String guid = valueAt(index.row(), Global.noteTableGuidPosition);\r
90                 QColor backgroundColor = new QColor(QColor.white);\r
91                 QColor foregroundColor = new QColor(QColor.black);\r
92                 if (titleColors != null && titleColors.containsKey(guid)) {\r
93                         int color = titleColors.get(guid);\r
94                         backgroundColor.setRgb(color);\r
95                         if (backgroundColor.rgb() == QColor.black.rgb() || backgroundColor.rgb() == QColor.blue.rgb()) \r
96                                 foregroundColor.setRgb(QColor.white.rgb());\r
97                 } \r
98                 return foregroundColor;\r
99         }\r
100         default:\r
101             break;\r
102         }\r
103                 return null;\r
104         }\r
105 \r
106         @Override\r
107         public int rowCount(QModelIndex arg0) {\r
108                 if (listManager != null && listManager.getMasterNoteIndex() != null) \r
109                         return listManager.getMasterNoteIndex().size();\r
110                 else\r
111                         return 0;\r
112         }\r
113 \r
114         \r
115         private String valueAt(int row, int col) {\r
116                 Note note = listManager.getMasterNoteIndex().get(row);\r
117                 \r
118                 if (col == Global.noteTableGuidPosition)\r
119                         return note.getGuid();\r
120                 if (col == Global.noteTableCreationPosition) \r
121                         return formattedDate(note.getCreated());\r
122                 if (col == Global.noteTableChangedPosition) \r
123                         return formattedDate(note.getUpdated());\r
124                 if (col == Global.noteTableSubjectDatePosition) {\r
125                         if (note.getAttributes().getSubjectDate() > 0)\r
126                                 return formattedDate(note.getAttributes().getSubjectDate());\r
127                         else\r
128                                 return formattedDate(note.getCreated());                                \r
129                 }\r
130                 if (col == Global.noteTableTitlePosition)\r
131                         return note.getTitle();\r
132                 if (col == Global.noteTableAuthorPosition)\r
133                         return note.getAttributes().getAuthor();\r
134                 if (col == Global.noteTableSourceUrlPosition)\r
135                         return note.getAttributes().getSourceURL();\r
136                 if (col == Global.noteTableSynchronizedPosition) {\r
137                         String guid = note.getGuid();\r
138                         for (int i=0; i<unsynchronizedNotes.size(); i++) {\r
139                                 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid)) \r
140                                         return "false";\r
141                         }\r
142                         return "true";\r
143                 }\r
144                 if (col == Global.noteTableTagPosition) {\r
145                         String tags = new String();\r
146                         for (int i=0; i<note.getTagNamesSize(); i++) {\r
147                                 tags = tags + note.getTagNames().get(i);\r
148                                 if (i!=note.getTagNamesSize()-1)\r
149                                         tags = tags + ", ";\r
150                         }\r
151                         return tags;\r
152                 }\r
153                 if (col == Global.noteTableNotebookPosition) {\r
154                         for (int i=0; i<listManager.getNotebookIndex().size(); i++) {\r
155                                 if (listManager.getNotebookIndex().get(i).getGuid().equals(note.getNotebookGuid()))\r
156                                         return listManager.getNotebookIndex().get(i).getName();\r
157                         }\r
158                 }\r
159                 return "";\r
160         }\r
161         \r
162         private String formattedDate(long d) {\r
163                 String fmt = Global.getDateFormat() + " " + Global.getTimeFormat();\r
164                 String dateTimeFormat = new String(fmt);\r
165                 SimpleDateFormat simple = new SimpleDateFormat(dateTimeFormat);\r
166                 \r
167                 StringBuilder date = new StringBuilder(simple.format(d));\r
168                 QDateTime created = QDateTime.fromString(date.toString(), fmt);\r
169                 return created.toString(fmt);\r
170 \r
171         }\r
172 \r
173         /*\r
174         @Override\r
175         public void sort(int column, Qt.SortOrder order) {\r
176                 layoutAboutToBeChanged.emit();\r
177                 boolean finished = false;\r
178                 while (!finished) {\r
179                         finished = true;\r
180                         for (int i=1; i<rowCount(); i++) {\r
181                                 String val1 = valueAt(i-1,column-1);\r
182                                 Note n1 = getMasterNoteIndex().get(i-1);\r
183                                 String val2 = valueAt(i,column);\r
184                                 Note n2 = getMasterNoteIndex().get(i);\r
185                                 \r
186                                 if (val1.compareTo(val2) > 0) {\r
187                                         finished = false;\r
188                                         getMasterNoteIndex().set(i-1, n2);\r
189                                         getMasterNoteIndex().set(i, n1);\r
190                                 }\r
191                         }\r
192                 }\r
193                 layoutChanged.emit();\r
194         }\r
195         */\r
196         \r
197         @Override\r
198         public java.lang.Object headerData(int section, Qt.Orientation orientation, int role) {\r
199                 if (role != Qt.ItemDataRole.DisplayRole)\r
200                         return null;\r
201                 \r
202                 if (orientation == Qt.Orientation.Horizontal && section < headers.length) { \r
203                         return headers[section];\r
204                 }\r
205                 return null;\r
206                 \r
207         }\r
208         \r
209         \r
210         @Override\r
211         public boolean setHeaderData(int section, Qt.Orientation orientation, Object value, int role) {\r
212                 if (orientation == Qt.Orientation.Horizontal && section < headers.length && role == Qt.ItemDataRole.DisplayRole) {\r
213                         headers[section] = value;\r
214                         headerDataChanged.emit(orientation, section, section);\r
215                         return true;\r
216                 }\r
217                 return false;\r
218         }\r
219 \r
220         \r
221         // Update a note title\r
222         public void updateNoteTitle(String guid, String title) {\r
223                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
224                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
225                                 getMasterNoteIndex().get(i).setTitle(title);\r
226                                 QModelIndex idx = createIndex(i, Global.noteTableTitlePosition, nativePointer());\r
227                                 setData(idx, title, Qt.ItemDataRole.EditRole); \r
228                                 i = getMasterNoteIndex().size();\r
229                         }       \r
230                 }\r
231                 // Update the list tables \r
232                 for (int i=0; i<getNoteIndex().size(); i++) {\r
233                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
234                                 getNoteIndex().get(i).setTitle(title);\r
235                                 i = getNoteIndex().size();\r
236                         }\r
237                 }\r
238         }\r
239         \r
240         // Update a note title\r
241         public void updateNoteTags(String guid, List<String> tags, List<String> names) {\r
242                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
243                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
244                                 getMasterNoteIndex().get(i).setTagGuids(tags);\r
245                                 getMasterNoteIndex().get(i).setTagNames(names);\r
246                                 String display = new String("");\r
247                                 Collections.sort(names);\r
248                                 for (int j=0; j<names.size(); j++) {\r
249                                         display = display+names.get(j);\r
250                                         if (j+2<names.size()) {\r
251                                                 display = display+Global.tagDelimeter+" ";\r
252                                         }\r
253                                 }\r
254                                 QModelIndex idx = createIndex(i, Global.noteTableTagPosition, nativePointer());\r
255                                 setData(idx, display, Qt.ItemDataRole.EditRole); \r
256                                 i = getMasterNoteIndex().size();\r
257                         }       \r
258                 }\r
259         }\r
260 \r
261         \r
262         public void updateNoteCreatedDate(String guid, QDateTime date) {\r
263                 \r
264                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
265                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
266                                 getMasterNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);\r
267                                 QModelIndex idx = createIndex(i, Global.noteTableCreationPosition, nativePointer());\r
268                                 setData(idx, new Long(getMasterNoteIndex().get(i).getCreated()), Qt.ItemDataRole.EditRole); \r
269                                 i = getMasterNoteIndex().size();\r
270                         }       \r
271                 }\r
272                 \r
273                 // Update the list tables \r
274                 for (int i=0; i<getNoteIndex().size(); i++) {\r
275                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
276                                 getNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);\r
277                                 i = getNoteIndex().size();\r
278                         }\r
279                 }\r
280         }\r
281         \r
282         public void updateNoteSubjectDate(String guid, QDateTime date) {\r
283                 \r
284                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
285                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
286                                 getMasterNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);\r
287                                 QModelIndex idx = createIndex(i, Global.noteTableSubjectDatePosition, nativePointer());\r
288                                 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole); \r
289                                 i = getMasterNoteIndex().size();\r
290                         }       \r
291                 }\r
292                 \r
293                 // Update the list tables \r
294                 for (int i=0; i<getNoteIndex().size(); i++) {\r
295                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
296                                 getNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);\r
297                                 i = getNoteIndex().size();\r
298                         }\r
299                 }\r
300         }\r
301         \r
302         public void updateNoteChangedDate(String guid, QDateTime date) {\r
303                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
304                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
305                                 getMasterNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);\r
306                                 QModelIndex idx = createIndex(i, Global.noteTableChangedPosition, nativePointer());\r
307                                 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole); \r
308                                 i = getMasterNoteIndex().size();\r
309                         }       \r
310                 }\r
311                 // Update the list tables \r
312                 for (int i=0; i<getNoteIndex().size(); i++) {\r
313                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
314                                 getNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);\r
315                                 i = getNoteIndex().size();\r
316                         }\r
317                 }\r
318         }\r
319 \r
320         public void updateNoteGuid(String oldGuid, String newGuid) {\r
321                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
322                         if (getMasterNoteIndex().get(i).getGuid() != null && getMasterNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
323                                 getMasterNoteIndex().get(i).setGuid(newGuid);\r
324                                 QModelIndex idx = createIndex(i, Global.noteTableGuidPosition, nativePointer());\r
325                                 setData(idx, new String(getMasterNoteIndex().get(i).getGuid()), Qt.ItemDataRole.EditRole); \r
326                                 i=getMasterNoteIndex().size()+1;\r
327                         }\r
328                 }\r
329                 for (int i=0; i<getNoteIndex().size(); i++) {\r
330                         if (getNoteIndex().get(i).getGuid() != null && getNoteIndex().get(i).getGuid().equals(oldGuid)) {\r
331                                 getNoteIndex().get(i).setGuid(newGuid);; \r
332                                 i=getNoteIndex().size()+1;\r
333                         }\r
334                 }\r
335         }\r
336         \r
337         public void updateNoteNotebook(String guid, String notebookGuid) {\r
338                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
339                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
340                                 getMasterNoteIndex().get(i).setNotebookGuid(notebookGuid);\r
341                                 for (int j=0; j<listManager.getNotebookIndex().size(); j++) {\r
342                                         if (listManager.getNotebookIndex().get(j).getGuid().equals(notebookGuid)) {\r
343                                                 String name = listManager.getNotebookIndex().get(j).getName();\r
344                                                 QModelIndex idx = createIndex(i, Global.noteTableNotebookPosition, nativePointer());\r
345                                                 setData(idx, name, Qt.ItemDataRole.EditRole); \r
346                                                 j=listManager.getNotebookIndex().size();\r
347                                         }\r
348                                 }\r
349                                 i=getMasterNoteIndex().size();\r
350                         }\r
351                 }\r
352                 for (int i=0; i<getNoteIndex().size(); i++) {\r
353                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
354                                 getNoteIndex().get(i).setNotebookGuid(notebookGuid);\r
355                                 i=getNoteIndex().size();\r
356                         }\r
357                 }\r
358         }\r
359 \r
360         public void updateNoteAuthor(String guid, String author) {\r
361                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
362                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
363                                 getMasterNoteIndex().get(i).getAttributes().setAuthor(author);\r
364                                 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);\r
365                                 QModelIndex idx = createIndex(i, Global.noteTableAuthorPosition, nativePointer());\r
366                                 setData(idx, author, Qt.ItemDataRole.EditRole); \r
367                                 i = getMasterNoteIndex().size();\r
368                         }       \r
369                 }\r
370                 // Update the list tables \r
371                 for (int i=0; i<getNoteIndex().size(); i++) {\r
372                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
373                                 getNoteIndex().get(i).getAttributes().setAuthor(author);\r
374                                 getNoteIndex().get(i).getAttributes().setAuthorIsSet(true);\r
375                                 i = getNoteIndex().size();\r
376                         }\r
377                 }\r
378         }\r
379         \r
380         public void updateNoteSourceUrl(String guid, String url) {\r
381                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
382                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
383                                 getMasterNoteIndex().get(i).getAttributes().setSourceURL(url);\r
384                                 getMasterNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);\r
385                                 QModelIndex idx = createIndex(i, Global.noteTableSourceUrlPosition, nativePointer());\r
386                                 setData(idx, url, Qt.ItemDataRole.EditRole); \r
387                                 i = getMasterNoteIndex().size();\r
388                         }       \r
389                 }\r
390                 // Update the list tables \r
391                 for (int i=0; i<getNoteIndex().size(); i++) {\r
392                         if (getNoteIndex().get(i).getGuid().equals(guid)) {\r
393                                 getNoteIndex().get(i).getAttributes().setSourceURL(url);\r
394                                 getNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);\r
395                                 i = getNoteIndex().size();\r
396                         }\r
397                 }\r
398         }\r
399 \r
400         public void updateNoteSyncStatus(String guid, boolean sync) {\r
401                 \r
402                 boolean found = false;\r
403                 for (int i=0; i<unsynchronizedNotes.size(); i++) {                      \r
404                         // If the note is now synchronized, but it is in the unsynchronized list, remove it\r
405                         if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {\r
406                                 unsynchronizedNotes.remove(i);\r
407                                 found = true;\r
408                                 i=unsynchronizedNotes.size();\r
409                         }\r
410                         \r
411                         // If the note is not synchronized, but it is already in the unsynchronized list, do nothing\r
412                         if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {\r
413                                 found = true;\r
414                                 i=unsynchronizedNotes.size();\r
415                         }\r
416                 }\r
417                 \r
418                 // If we've gotten through the entire list, then we consider it synchronized.  If this is \r
419                 // wrong, add it to the list.\r
420                 if (!sync && !found) \r
421                         unsynchronizedNotes.add(guid);\r
422                 \r
423                 // Now we need to go through the table & update it\r
424                 for (int i=0; i<getMasterNoteIndex().size(); i++) {\r
425                         if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {\r
426                                 QModelIndex idx = createIndex(i, Global.noteTableSynchronizedPosition, nativePointer());\r
427                                 String value;\r
428                                 if (!sync)\r
429                                         value = tr("false");\r
430                                 else\r
431                                         value = tr("true");\r
432                                 setData(idx, value, Qt.ItemDataRole.EditRole); \r
433                                 return;\r
434                         }       \r
435                 }\r
436         }\r
437         \r
438         public void addNote(Note n) {\r
439                 getNoteIndex().add(n);\r
440                 getMasterNoteIndex().add(n);\r
441                 proxyModel.addGuid(n.getGuid());\r
442                 proxyModel.invalidate();\r
443 //              proxyModel.filter();\r
444         }\r
445         \r
446         \r
447         @Override\r
448         public boolean setData(QModelIndex index, Object value,int role) {\r
449                 if (role == Qt.ItemDataRole.EditRole) {\r
450                         dataChanged.emit(index, index);\r
451                         return true;\r
452                 } else {\r
453                         return super.setData(index, value, role);\r
454                 }\r
455         }\r
456         \r
457         public void updateNoteTitleColor(String guid, Integer color) {\r
458                 getTitleColors().remove(guid);\r
459                 getTitleColors().put(guid, color);\r
460                 layoutChanged.emit();\r
461         }\r
462 \r
463         @Override\r
464         public Qt.ItemFlags flags(QModelIndex index) {\r
465                 Qt.ItemFlag flags[] = { Qt.ItemFlag.ItemIsEnabled, \r
466                                                                 Qt.ItemFlag.ItemIsDragEnabled,\r
467                                                                 Qt.ItemFlag.ItemIsSelectable };\r
468                 \r
469                 return new Qt.ItemFlags(flags);\r
470         }\r
471         \r
472 }\r