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.core.Qt.AspectRatioMode;
\r
13 import com.trolltech.qt.core.Qt.TransformationMode;
\r
14 import com.trolltech.qt.gui.QAbstractTableModel;
\r
15 import com.trolltech.qt.gui.QColor;
\r
16 import com.trolltech.qt.gui.QIcon;
\r
17 import com.trolltech.qt.gui.QImage;
\r
18 import com.trolltech.qt.gui.QPixmap;
\r
20 import cx.fbn.nevernote.Global;
\r
21 import cx.fbn.nevernote.filters.NoteSortFilterProxyModel;
\r
22 import cx.fbn.nevernote.utilities.ListManager;
\r
24 public class NoteTableModel extends QAbstractTableModel {
\r
25 private final ListManager listManager;
\r
26 private final Object[] headers;
\r
27 private List<Note> noteIndex;
\r
28 private List<Note> masterNoteIndex;
\r
29 private List<String> unsynchronizedNotes;
\r
30 public HashMap<String,Integer> titleColors;
\r
31 public NoteSortFilterProxyModel proxyModel; // note sort model
\r
33 public NoteTableModel(ListManager m) {
\r
34 headers = new Object[Global.noteTableColumnCount];
\r
36 masterNoteIndex = null;
\r
37 unsynchronizedNotes = new ArrayList<String>();
\r
40 public List<Note> getNoteIndex() {
\r
43 public void setNoteIndex(List<Note> list) {
\r
46 public List<Note> getMasterNoteIndex() {
\r
47 return masterNoteIndex;
\r
49 public void setMasterNoteIndex(List<Note> list) {
\r
50 masterNoteIndex = list;
\r
52 public void setSortProxyModel(NoteSortFilterProxyModel m) {
\r
56 public List<String> getUnsynchronizedNotes() {
\r
57 return unsynchronizedNotes;
\r
59 public void setUnsynchronizedNotes(List<String> list) {
\r
60 unsynchronizedNotes = list;
\r
63 public HashMap<String, Integer> getTitleColors() {
\r
66 public void setTitleColors(HashMap<String, Integer> map) {
\r
71 public int columnCount(QModelIndex arg0) {
\r
72 return Global.noteTableColumnCount;
\r
76 public Object data(QModelIndex index, int role) {
\r
80 case Qt.ItemDataRole.DisplayRole: {
\r
81 return valueAt(index.row(), index.column());
\r
83 case Qt.ItemDataRole.DecorationRole: {
\r
84 if (index.column() == Global.noteTableThumbnailPosition ||
\r
85 index.column() == Global.noteTableSynchronizedPosition)
\r
86 return valueAt(index.row(), index.column());
\r
90 case Qt.ItemDataRole.BackgroundRole: {
\r
91 String guid = (String)valueAt(index.row(), Global.noteTableGuidPosition);
\r
92 QColor backgroundColor = new QColor(QColor.white);
\r
93 if (titleColors != null && titleColors.containsKey(guid)) {
\r
94 int color = titleColors.get(guid);
\r
95 backgroundColor.setRgb(color);
\r
97 return backgroundColor;
\r
99 case Qt.ItemDataRole.ForegroundRole: {
\r
100 String guid = (String)valueAt(index.row(), Global.noteTableGuidPosition);
\r
101 QColor backgroundColor = new QColor(QColor.white);
\r
102 QColor foregroundColor = new QColor(QColor.black);
\r
103 if (titleColors != null && titleColors.containsKey(guid)) {
\r
104 int color = titleColors.get(guid);
\r
105 backgroundColor.setRgb(color);
\r
106 if (backgroundColor.rgb() == QColor.black.rgb() || backgroundColor.rgb() == QColor.blue.rgb())
\r
107 foregroundColor.setRgb(QColor.white.rgb());
\r
109 return foregroundColor;
\r
118 public int rowCount(QModelIndex arg0) {
\r
119 if (listManager != null && listManager.getMasterNoteIndex() != null)
\r
120 return listManager.getMasterNoteIndex().size();
\r
126 private Object valueAt(int row, int col) {
\r
127 Note note = listManager.getMasterNoteIndex().get(row);
\r
129 if (col == Global.noteTableGuidPosition)
\r
130 return note.getGuid();
\r
131 if (col == Global.noteTableCreationPosition)
\r
132 return note.getCreated();
\r
133 if (col == Global.noteTableChangedPosition)
\r
134 return note.getUpdated();
\r
135 if (col == Global.noteTableSubjectDatePosition) {
\r
136 if (note.getAttributes().getSubjectDate() > 0)
\r
137 return note.getAttributes().getSubjectDate();
\r
139 return note.getCreated();
\r
141 if (col == Global.noteTableTitlePosition)
\r
142 return note.getTitle();
\r
143 if (col == Global.noteTableAuthorPosition)
\r
144 return note.getAttributes().getAuthor();
\r
145 if (col == Global.noteTableSourceUrlPosition)
\r
146 return note.getAttributes().getSourceURL();
\r
147 if (col == Global.noteTableSynchronizedPosition) {
\r
148 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
149 QIcon dotIcon = new QIcon(iconPath+"dot.png");
\r
150 String guid = note.getGuid();
\r
151 for (int i=0; i<unsynchronizedNotes.size(); i++) {
\r
152 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid))
\r
157 if (col == Global.noteTableTagPosition) {
\r
158 String tags = new String();
\r
159 for (int i=0; i<note.getTagNamesSize(); i++) {
\r
160 tags = tags + note.getTagNames().get(i);
\r
161 if (i!=note.getTagNamesSize()-1)
\r
162 tags = tags + ", ";
\r
166 if (col == Global.noteTableNotebookPosition) {
\r
167 for (int i=0; i<listManager.getNotebookIndex().size(); i++) {
\r
168 if (listManager.getNotebookIndex().get(i).getGuid().equals(note.getNotebookGuid()))
\r
169 return listManager.getNotebookIndex().get(i).getName();
\r
172 if (col == Global.noteTableGuidPosition) {
\r
173 return note.getGuid();
\r
175 if (col == Global.noteTableThumbnailPosition) {
\r
176 if (!Global.enableThumbnails())
\r
178 if (Global.getListView() == Global.View_List_Wide) {
\r
179 // QImage img = listManager.getThumbnail(note.getGuid());
\r
180 QPixmap img = listManager.getThumbnailPixmap(note.getGuid());
\r
182 return img.scaled(Global.smallThumbnailSize,
\r
183 AspectRatioMode.KeepAspectRatio, TransformationMode.SmoothTransformation);
\r
188 QImage img = listManager.getThumbnail(note.getGuid());
\r
190 return img.scaled(Global.largeThumbnailSize,
\r
191 AspectRatioMode.KeepAspectRatio, TransformationMode.SmoothTransformation);
\r
202 public java.lang.Object headerData(int section, Qt.Orientation orientation, int role) {
\r
203 if (role != Qt.ItemDataRole.DisplayRole)
\r
206 if (orientation == Qt.Orientation.Horizontal && section < headers.length) {
\r
207 return headers[section];
\r
215 public boolean setHeaderData(int section, Qt.Orientation orientation, Object value, int role) {
\r
216 if (orientation == Qt.Orientation.Horizontal && section < headers.length && role == Qt.ItemDataRole.DisplayRole) {
\r
217 headers[section] = value;
\r
218 headerDataChanged.emit(orientation, section, section);
\r
225 // Update a note title
\r
226 public void updateNoteTitle(String guid, String title) {
\r
227 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
228 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
229 getMasterNoteIndex().get(i).setTitle(title);
\r
230 QModelIndex idx = createIndex(i, Global.noteTableTitlePosition, nativePointer());
\r
231 setData(idx, title, Qt.ItemDataRole.EditRole);
\r
232 i = getMasterNoteIndex().size();
\r
235 // Update the list tables
\r
236 for (int i=0; i<getNoteIndex().size(); i++) {
\r
237 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
238 getNoteIndex().get(i).setTitle(title);
\r
239 i = getNoteIndex().size();
\r
244 // Update a note title
\r
245 public void updateNoteTags(String guid, List<String> tags, List<String> names) {
\r
246 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
247 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
248 getMasterNoteIndex().get(i).setTagGuids(tags);
\r
249 getMasterNoteIndex().get(i).setTagNames(names);
\r
250 String display = new String("");
\r
251 Collections.sort(names);
\r
252 for (int j=0; j<names.size(); j++) {
\r
253 display = display+names.get(j);
\r
254 if (j+2<names.size()) {
\r
255 display = display+Global.tagDelimeter+" ";
\r
258 QModelIndex idx = createIndex(i, Global.noteTableTagPosition, nativePointer());
\r
259 setData(idx, display, Qt.ItemDataRole.EditRole);
\r
260 i = getMasterNoteIndex().size();
\r
266 public void updateNoteCreatedDate(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).setCreated((long)date.toTime_t()*1000);
\r
271 QModelIndex idx = createIndex(i, Global.noteTableCreationPosition, nativePointer());
\r
272 setData(idx, new Long(getMasterNoteIndex().get(i).getCreated()), Qt.ItemDataRole.EditRole);
\r
273 i = getMasterNoteIndex().size();
\r
277 // Update the list tables
\r
278 for (int i=0; i<getNoteIndex().size(); i++) {
\r
279 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
280 getNoteIndex().get(i).setCreated((long)date.toTime_t()*1000);
\r
281 i = getNoteIndex().size();
\r
286 public void updateNoteSubjectDate(String guid, QDateTime date) {
\r
288 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
289 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
290 getMasterNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);
\r
291 QModelIndex idx = createIndex(i, Global.noteTableSubjectDatePosition, nativePointer());
\r
292 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole);
\r
293 i = getMasterNoteIndex().size();
\r
297 // Update the list tables
\r
298 for (int i=0; i<getNoteIndex().size(); i++) {
\r
299 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
300 getNoteIndex().get(i).getAttributes().setSubjectDate((long)date.toTime_t()*1000);
\r
301 i = getNoteIndex().size();
\r
306 public void updateNoteChangedDate(String guid, QDateTime date) {
\r
307 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
308 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
309 getMasterNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);
\r
310 QModelIndex idx = createIndex(i, Global.noteTableChangedPosition, nativePointer());
\r
311 setData(idx, new Long(getMasterNoteIndex().get(i).getAttributes().getSubjectDate()), Qt.ItemDataRole.EditRole);
\r
312 i = getMasterNoteIndex().size();
\r
315 // Update the list tables
\r
316 for (int i=0; i<getNoteIndex().size(); i++) {
\r
317 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
318 getNoteIndex().get(i).setUpdated((long)date.toTime_t()*1000);
\r
319 i = getNoteIndex().size();
\r
324 public void updateNoteGuid(String oldGuid, String newGuid) {
\r
326 for (int i=0; i<getNoteIndex().size(); i++) {
\r
327 if (getNoteIndex().get(i).getGuid() != null && getNoteIndex().get(i).getGuid().equals(oldGuid)) {
\r
328 getNoteIndex().get(i).setGuid(newGuid);;
\r
329 i=getNoteIndex().size()+1;
\r
336 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
337 if (getMasterNoteIndex().get(i).getGuid() != null && getMasterNoteIndex().get(i).getGuid().equals(oldGuid)) {
\r
338 getMasterNoteIndex().get(i).setGuid(newGuid);
\r
339 // QModelIndex idx = createIndex(i, Global.noteTableGuidPosition, nativePointer());
\r
340 // setData(idx, new String(getMasterNoteIndex().get(i).getGuid()), Qt.ItemDataRole.EditRole);
\r
341 i=getMasterNoteIndex().size()+1;
\r
346 public void updateNoteNotebook(String guid, String notebookGuid) {
\r
347 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
348 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
349 getMasterNoteIndex().get(i).setNotebookGuid(notebookGuid);
\r
350 for (int j=0; j<listManager.getNotebookIndex().size(); j++) {
\r
351 if (listManager.getNotebookIndex().get(j).getGuid().equals(notebookGuid)) {
\r
352 String name = listManager.getNotebookIndex().get(j).getName();
\r
353 QModelIndex idx = createIndex(i, Global.noteTableNotebookPosition, nativePointer());
\r
354 setData(idx, name, Qt.ItemDataRole.EditRole);
\r
355 j=listManager.getNotebookIndex().size();
\r
358 i=getMasterNoteIndex().size();
\r
361 for (int i=0; i<getNoteIndex().size(); i++) {
\r
362 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
363 getNoteIndex().get(i).setNotebookGuid(notebookGuid);
\r
364 i=getNoteIndex().size();
\r
369 public void updateNoteAuthor(String guid, String author) {
\r
370 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
371 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
372 getMasterNoteIndex().get(i).getAttributes().setAuthor(author);
\r
373 getMasterNoteIndex().get(i).getAttributes().setAltitudeIsSet(true);
\r
374 QModelIndex idx = createIndex(i, Global.noteTableAuthorPosition, nativePointer());
\r
375 setData(idx, author, Qt.ItemDataRole.EditRole);
\r
376 i = getMasterNoteIndex().size();
\r
379 // Update the list tables
\r
380 for (int i=0; i<getNoteIndex().size(); i++) {
\r
381 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
382 getNoteIndex().get(i).getAttributes().setAuthor(author);
\r
383 getNoteIndex().get(i).getAttributes().setAuthorIsSet(true);
\r
384 i = getNoteIndex().size();
\r
389 public void updateNoteSourceUrl(String guid, String url) {
\r
390 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
391 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
392 getMasterNoteIndex().get(i).getAttributes().setSourceURL(url);
\r
393 getMasterNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);
\r
394 QModelIndex idx = createIndex(i, Global.noteTableSourceUrlPosition, nativePointer());
\r
395 setData(idx, url, Qt.ItemDataRole.EditRole);
\r
396 i = getMasterNoteIndex().size();
\r
399 // Update the list tables
\r
400 for (int i=0; i<getNoteIndex().size(); i++) {
\r
401 if (getNoteIndex().get(i).getGuid().equals(guid)) {
\r
402 getNoteIndex().get(i).getAttributes().setSourceURL(url);
\r
403 getNoteIndex().get(i).getAttributes().setSourceURLIsSet(true);
\r
404 i = getNoteIndex().size();
\r
409 public void updateNoteSyncStatus(String guid, boolean sync) {
\r
411 boolean found = false;
\r
412 for (int i=0; i<unsynchronizedNotes.size(); i++) {
\r
413 // If the note is now synchronized, but it is in the unsynchronized list, remove it
\r
414 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {
\r
415 unsynchronizedNotes.remove(i);
\r
417 i=unsynchronizedNotes.size();
\r
420 // If the note is not synchronized, but it is already in the unsynchronized list, do nothing
\r
421 if (unsynchronizedNotes.get(i).equalsIgnoreCase(guid) && sync) {
\r
423 i=unsynchronizedNotes.size();
\r
427 // If we've gotten through the entire list, then we consider it synchronized. If this is
\r
428 // wrong, add it to the list.
\r
429 if (!sync && !found)
\r
430 unsynchronizedNotes.add(guid);
\r
432 // Now we need to go through the table & update it
\r
433 for (int i=0; i<getMasterNoteIndex().size(); i++) {
\r
434 if (getMasterNoteIndex().get(i).getGuid().equals(guid)) {
\r
435 QModelIndex idx = createIndex(i, Global.noteTableSynchronizedPosition, nativePointer());
\r
438 value = tr("false");
\r
440 value = tr("true");
\r
441 setData(idx, value, Qt.ItemDataRole.EditRole);
\r
447 public void addNote(Note n) {
\r
448 getNoteIndex().add(n);
\r
449 getMasterNoteIndex().add(n);
\r
450 proxyModel.addGuid(n.getGuid());
\r
451 proxyModel.invalidate();
\r
452 // proxyModel.filter();
\r
457 public boolean setData(QModelIndex index, Object value,int role) {
\r
458 if (role == Qt.ItemDataRole.EditRole) {
\r
459 dataChanged.emit(index, index);
\r
462 return super.setData(index, value, role);
\r
466 public void updateNoteTitleColor(String guid, Integer color) {
\r
467 getTitleColors().remove(guid);
\r
468 getTitleColors().put(guid, color);
\r
469 layoutChanged.emit();
\r
473 public Qt.ItemFlags flags(QModelIndex index) {
\r
474 Qt.ItemFlag flags[] = { Qt.ItemFlag.ItemIsEnabled,
\r
475 Qt.ItemFlag.ItemIsDragEnabled,
\r
476 Qt.ItemFlag.ItemIsSelectable };
\r
478 return new Qt.ItemFlags(flags);
\r