1 package cx.fbn.nevernote.gui;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collections;
\r
5 import java.util.HashMap;
\r
6 import java.util.List;
\r
8 import com.evernote.edam.type.Note;
\r
9 import com.trolltech.qt.core.QDateTime;
\r
10 import com.trolltech.qt.core.QModelIndex;
\r
11 import com.trolltech.qt.core.Qt;
\r
12 import com.trolltech.qt.gui.QAbstractTableModel;
\r
13 import com.trolltech.qt.gui.QColor;
\r
15 import cx.fbn.nevernote.Global;
\r
16 import cx.fbn.nevernote.filters.NoteSortFilterProxyModel;
\r
17 import cx.fbn.nevernote.utilities.ListManager;
\r
19 public class NoteTableModel extends QAbstractTableModel {
\r
20 private final ListManager listManager;
\r
21 private final Object[] headers;
\r
22 private List<Note> noteIndex;
\r
23 private List<Note> masterNoteIndex;
\r
24 private List<String> unsynchronizedNotes;
\r
25 public HashMap<String,Integer> titleColors;
\r
26 public NoteSortFilterProxyModel proxyModel; // note sort model
\r
28 public NoteTableModel(ListManager m) {
\r
29 headers = new Object[Global.noteTableColumnCount];
\r
31 masterNoteIndex = null;
\r
32 unsynchronizedNotes = new ArrayList<String>();
\r
35 public List<Note> getNoteIndex() {
\r
38 public void setNoteIndex(List<Note> list) {
\r
41 public List<Note> getMasterNoteIndex() {
\r
42 return masterNoteIndex;
\r
44 public void setMasterNoteIndex(List<Note> list) {
\r
45 masterNoteIndex = list;
\r
47 public void setSortProxyModel(NoteSortFilterProxyModel m) {
\r
51 public List<String> getUnsynchronizedNotes() {
\r
52 return unsynchronizedNotes;
\r
54 public void setUnsynchronizedNotes(List<String> list) {
\r
55 unsynchronizedNotes = list;
\r
58 public HashMap<String, Integer> getTitleColors() {
\r
61 public void setTitleColors(HashMap<String, Integer> map) {
\r
66 public int columnCount(QModelIndex arg0) {
\r
67 return Global.noteTableColumnCount;
\r
71 public Object data(QModelIndex index, int role) {
\r
75 case Qt.ItemDataRole.DisplayRole: {
\r
76 return valueAt(index.row(), index.column());
\r
78 case Qt.ItemDataRole.BackgroundRole: {
\r
79 String guid = (String)valueAt(index.row(), Global.noteTableGuidPosition);
\r
80 QColor backgroundColor = new QColor(QColor.white);
\r
81 if (titleColors != null && titleColors.containsKey(guid)) {
\r
82 int color = titleColors.get(guid);
\r
83 backgroundColor.setRgb(color);
\r
85 return backgroundColor;
\r
87 case Qt.ItemDataRole.ForegroundRole: {
\r
88 String guid = (String)valueAt(index.row(), Global.noteTableGuidPosition);
\r
89 QColor backgroundColor = new QColor(QColor.white);
\r
90 QColor foregroundColor = new QColor(QColor.black);
\r
91 if (titleColors != null && titleColors.containsKey(guid)) {
\r
92 int color = titleColors.get(guid);
\r
93 backgroundColor.setRgb(color);
\r
94 if (backgroundColor.rgb() == QColor.black.rgb() || backgroundColor.rgb() == QColor.blue.rgb())
\r
95 foregroundColor.setRgb(QColor.white.rgb());
\r
97 return foregroundColor;
\r
106 public int rowCount(QModelIndex arg0) {
\r
107 if (listManager != null && listManager.getMasterNoteIndex() != null)
\r
108 return listManager.getMasterNoteIndex().size();
\r
114 private Object valueAt(int row, int col) {
\r
115 Note note = listManager.getMasterNoteIndex().get(row);
\r
117 if (col == Global.noteTableGuidPosition)
\r
118 return note.getGuid();
\r
119 if (col == Global.noteTableCreationPosition)
\r
120 return note.getCreated();
\r
121 if (col == Global.noteTableChangedPosition)
\r
122 return note.getUpdated();
\r
123 if (col == Global.noteTableSubjectDatePosition) {
\r
124 if (note.getAttributes().getSubjectDate() > 0)
\r
125 return note.getAttributes().getSubjectDate();
\r
127 return note.getCreated();
\r
129 if (col == Global.noteTableTitlePosition)
\r
130 return note.getTitle();
\r
131 if (col == Global.noteTableAuthorPosition)
\r
132 return note.getAttributes().getAuthor();
\r
133 if (col == Global.noteTableSourceUrlPosition)
\r
134 return note.getAttributes().getSourceURL();
\r
135 if (col == Global.noteTableSynchronizedPosition) {
\r
136 String guid = note.getGuid();
\r
137 for (int i=0; i<unsynchronizedNotes.size(); i++) {
\r
138 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid))
\r
143 if (col == Global.noteTableTagPosition) {
\r
144 String tags = new String();
\r
145 for (int i=0; i<note.getTagNamesSize(); i++) {
\r
146 tags = tags + note.getTagNames().get(i);
\r
147 if (i!=note.getTagNamesSize()-1)
\r
148 tags = tags + ", ";
\r
152 if (col == Global.noteTableNotebookPosition) {
\r
153 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
\r
154 if (listManager.getNotebookIndex().get(i).getGuid().equals(note.getNotebookGuid()))
\r
155 return listManager.getNotebookIndex().get(i).getName();
\r
163 public java.lang.Object headerData(int section, Qt.Orientation orientation, int role) {
\r
164 if (role != Qt.ItemDataRole.DisplayRole)
\r
167 if (orientation == Qt.Orientation.Horizontal && section < headers.length) {
\r
168 return headers[section];
\r
176 public boolean setHeaderData(int section, Qt.Orientation orientation, Object value, int role) {
\r
177 if (orientation == Qt.Orientation.Horizontal && section < headers.length && role == Qt.ItemDataRole.DisplayRole) {
\r
178 headers[section] = value;
\r
179 headerDataChanged.emit(orientation, section, section);
\r
186 // Update a note title
\r
187 public void updateNoteTitle(String guid, String title) {
\r
188 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
189 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
190 getMasterNoteIndex().get(i).setTitle(title);
\r
191 QModelIndex idx = createIndex(i, Global.noteTableTitlePosition, nativePointer());
\r
192 setData(idx, title, Qt.ItemDataRole.EditRole);
\r
193 i = getMasterNoteIndex().size();
\r
196 // Update the list tables
\r
197 for (int i=0; i<getNoteIndex().size(); i++) {
\r
198 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
199 getNoteIndex().get(i).setTitle(title);
\r
200 i = getNoteIndex().size();
\r
205 // Update a note title
\r
206 public void updateNoteTags(String guid, List<String> tags, List<String> names) {
\r
207 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
208 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
209 getMasterNoteIndex().get(i).setTagGuids(tags);
\r
210 getMasterNoteIndex().get(i).setTagNames(names);
\r
211 String display = new String("");
\r
212 Collections.sort(names);
\r
213 for (int j=0; j<names.size(); j++) {
\r
214 display = display+names.get(j);
\r
215 if (j+2<names.size()) {
\r
216 display = display+Global.tagDelimeter+" ";
\r
219 QModelIndex idx = createIndex(i, Global.noteTableTagPosition, nativePointer());
\r
220 setData(idx, display, Qt.ItemDataRole.EditRole);
\r
221 i = getMasterNoteIndex().size();
\r
227 public void updateNoteCreatedDate(String guid, QDateTime date) {
\r
229 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
230 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
231 getMasterNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);
\r
232 QModelIndex idx = createIndex(i, Global.noteTableCreationPosition, nativePointer());
\r
233 setData(idx, new Long(getMasterNoteIndex().get(i).getCreated()), Qt.ItemDataRole.EditRole);
\r
234 i = getMasterNoteIndex().size();
\r
238 // Update the list tables
\r
239 for (int i=0; i<getNoteIndex().size(); i++) {
\r
240 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
241 getNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);
\r
242 i = getNoteIndex().size();
\r
247 public void updateNoteSubjectDate(String guid, QDateTime date) {
\r
249 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
250 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
251 getMasterNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);
\r
252 QModelIndex idx = createIndex(i, Global.noteTableSubjectDatePosition, nativePointer());
\r
253 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole);
\r
254 i = getMasterNoteIndex().size();
\r
258 // Update the list tables
\r
259 for (int i=0; i<getNoteIndex().size(); i++) {
\r
260 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
261 getNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);
\r
262 i = getNoteIndex().size();
\r
267 public void updateNoteChangedDate(String guid, QDateTime date) {
\r
268 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
269 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
270 getMasterNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);
\r
271 QModelIndex idx = createIndex(i, Global.noteTableChangedPosition, nativePointer());
\r
272 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole);
\r
273 i = getMasterNoteIndex().size();
\r
276 // Update the list tables
\r
277 for (int i=0; i<getNoteIndex().size(); i++) {
\r
278 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
279 getNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);
\r
280 i = getNoteIndex().size();
\r
285 public void updateNoteGuid(String oldGuid, String newGuid) {
\r
286 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
287 if (getMasterNoteIndex().get(i).getGuid() != null && getMasterNoteIndex().get(i).getGuid().equals(oldGuid)) {
\r
288 getMasterNoteIndex().get(i).setGuid(newGuid);
\r
289 QModelIndex idx = createIndex(i, Global.noteTableGuidPosition, nativePointer());
\r
290 setData(idx, new String(getMasterNoteIndex().get(i).getGuid()), Qt.ItemDataRole.EditRole);
\r
291 i=getMasterNoteIndex().size()+1;
\r
294 for (int i=0; i<getNoteIndex().size(); i++) {
\r
295 if (getNoteIndex().get(i).getGuid() != null && getNoteIndex().get(i).getGuid().equals(oldGuid)) {
\r
296 getNoteIndex().get(i).setGuid(newGuid);;
\r
297 i=getNoteIndex().size()+1;
\r
302 public void updateNoteNotebook(String guid, String notebookGuid) {
\r
303 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
304 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
305 getMasterNoteIndex().get(i).setNotebookGuid(notebookGuid);
\r
306 for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
\r
307 if (listManager.getNotebookIndex().get(j).getGuid().equals(notebookGuid)) {
\r
308 String name = listManager.getNotebookIndex().get(j).getName();
\r
309 QModelIndex idx = createIndex(i, Global.noteTableNotebookPosition, nativePointer());
\r
310 setData(idx, name, Qt.ItemDataRole.EditRole);
\r
311 j=listManager.getNotebookIndex().size();
\r
314 i=getMasterNoteIndex().size();
\r
317 for (int i=0; i<getNoteIndex().size(); i++) {
\r
318 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
319 getNoteIndex().get(i).setNotebookGuid(notebookGuid);
\r
320 i=getNoteIndex().size();
\r
325 public void updateNoteAuthor(String guid, String author) {
\r
326 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
327 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
328 getMasterNoteIndex().get(i).getAttributes().setAuthor(author);
\r
329 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);
\r
330 QModelIndex idx = createIndex(i, Global.noteTableAuthorPosition, nativePointer());
\r
331 setData(idx, author, Qt.ItemDataRole.EditRole);
\r
332 i = getMasterNoteIndex().size();
\r
335 // Update the list tables
\r
336 for (int i=0; i<getNoteIndex().size(); i++) {
\r
337 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
338 getNoteIndex().get(i).getAttributes().setAuthor(author);
\r
339 getNoteIndex().get(i).getAttributes().setAuthorIsSet(true);
\r
340 i = getNoteIndex().size();
\r
345 public void updateNoteSourceUrl(String guid, String url) {
\r
346 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
347 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
348 getMasterNoteIndex().get(i).getAttributes().setSourceURL(url);
\r
349 getMasterNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);
\r
350 QModelIndex idx = createIndex(i, Global.noteTableSourceUrlPosition, nativePointer());
\r
351 setData(idx, url, Qt.ItemDataRole.EditRole);
\r
352 i = getMasterNoteIndex().size();
\r
355 // Update the list tables
\r
356 for (int i=0; i<getNoteIndex().size(); i++) {
\r
357 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
358 getNoteIndex().get(i).getAttributes().setSourceURL(url);
\r
359 getNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);
\r
360 i = getNoteIndex().size();
\r
365 public void updateNoteSyncStatus(String guid, boolean sync) {
\r
367 boolean found = false;
\r
368 for (int i=0; i<unsynchronizedNotes.size(); i++) {
\r
369 // If the note is now synchronized, but it is in the unsynchronized list, remove it
\r
370 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {
\r
371 unsynchronizedNotes.remove(i);
\r
373 i=unsynchronizedNotes.size();
\r
376 // If the note is not synchronized, but it is already in the unsynchronized list, do nothing
\r
377 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {
\r
379 i=unsynchronizedNotes.size();
\r
383 // If we've gotten through the entire list, then we consider it synchronized. If this is
\r
384 // wrong, add it to the list.
\r
385 if (!sync && !found)
\r
386 unsynchronizedNotes.add(guid);
\r
388 // Now we need to go through the table & update it
\r
389 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
390 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
391 QModelIndex idx = createIndex(i, Global.noteTableSynchronizedPosition, nativePointer());
\r
394 value = tr("false");
\r
396 value = tr("true");
\r
397 setData(idx, value, Qt.ItemDataRole.EditRole);
\r
403 public void addNote(Note n) {
\r
404 getNoteIndex().add(n);
\r
405 getMasterNoteIndex().add(n);
\r
406 proxyModel.addGuid(n.getGuid());
\r
407 proxyModel.invalidate();
\r
408 // proxyModel.filter();
\r
413 public boolean setData(QModelIndex index, Object value,int role) {
\r
414 if (role == Qt.ItemDataRole.EditRole) {
\r
415 dataChanged.emit(index, index);
\r
418 return super.setData(index, value, role);
\r
422 public void updateNoteTitleColor(String guid, Integer color) {
\r
423 getTitleColors().remove(guid);
\r
424 getTitleColors().put(guid, color);
\r
425 layoutChanged.emit();
\r
429 public Qt.ItemFlags flags(QModelIndex index) {
\r
430 Qt.ItemFlag flags[] = { Qt.ItemFlag.ItemIsEnabled,
\r
431 Qt.ItemFlag.ItemIsDragEnabled,
\r
432 Qt.ItemFlag.ItemIsSelectable };
\r
434 return new Qt.ItemFlags(flags);
\r