OSDN Git Service

Retain expanded state of notebooks & tags across sync.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TagTreeWidget.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009,2010 Randy Baumgarte\r
4  * Copyright 2010 Hiroshi Miura\r
5  * \r
6  * This file may be licensed under the terms of of the\r
7  * GNU General Public License Version 2 (the ``GPL'').\r
8  *\r
9  * Software distributed under the License is distributed\r
10  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
11  * express or implied. See the GPL for the specific language\r
12  * governing rights and limitations.\r
13  *\r
14  * You should have received a copy of the GPL along with this\r
15  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
16  * or write to the Free Software Foundation, Inc.,\r
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
18  *\r
19 */\r
20 \r
21 package cx.fbn.nevernote.gui;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 \r
27 import com.evernote.edam.type.Note;\r
28 import com.evernote.edam.type.Tag;\r
29 import com.trolltech.qt.core.QByteArray;\r
30 import com.trolltech.qt.core.QMimeData;\r
31 import com.trolltech.qt.core.Qt;\r
32 import com.trolltech.qt.core.Qt.MatchFlag;\r
33 import com.trolltech.qt.core.Qt.MatchFlags;\r
34 import com.trolltech.qt.core.Qt.SortOrder;\r
35 import com.trolltech.qt.gui.QAbstractItemView;\r
36 import com.trolltech.qt.gui.QAction;\r
37 import com.trolltech.qt.gui.QBrush;\r
38 import com.trolltech.qt.gui.QColor;\r
39 import com.trolltech.qt.gui.QContextMenuEvent;\r
40 import com.trolltech.qt.gui.QDragEnterEvent;\r
41 import com.trolltech.qt.gui.QDragMoveEvent;\r
42 import com.trolltech.qt.gui.QHeaderView;\r
43 import com.trolltech.qt.gui.QIcon;\r
44 import com.trolltech.qt.gui.QMenu;\r
45 import com.trolltech.qt.gui.QMouseEvent;\r
46 import com.trolltech.qt.gui.QTreeWidget;\r
47 import com.trolltech.qt.gui.QTreeWidgetItem;\r
48 \r
49 import cx.fbn.nevernote.Global;\r
50 import cx.fbn.nevernote.filters.TagCounter;\r
51 import cx.fbn.nevernote.signals.NoteSignal;\r
52 import cx.fbn.nevernote.signals.TagSignal;\r
53 import cx.fbn.nevernote.sql.DatabaseConnection;\r
54 \r
55 public class TagTreeWidget extends QTreeWidget {\r
56         private QAction editAction;\r
57         private QAction deleteAction;\r
58         private QAction addAction;\r
59         private QAction iconAction;\r
60         public TagSignal tagSignal;\r
61         public NoteSignal noteSignal;\r
62         private boolean showAllTags;\r
63         private final DatabaseConnection db;\r
64         private HashMap<String, QIcon>  icons;\r
65         public Signal0 selectionSignal;\r
66         public String selectedTag;\r
67         private boolean rightButtonClicked;\r
68         \r
69         \r
70         public TagTreeWidget(DatabaseConnection d) {\r
71                 List<String> headers = new ArrayList<String>();\r
72                 headers.add(tr("Tags"));\r
73                 headers.add("");\r
74                 showAllTags = true;\r
75                 setAcceptDrops(true);\r
76                 setDragEnabled(true);\r
77                 setColumnCount(2);\r
78                 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);\r
79                 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);\r
80                 header().setMovable(false);\r
81                 db = d;\r
82                 selectionSignal = new Signal0();\r
83                 tagSignal = new TagSignal();\r
84                 noteSignal = new NoteSignal();\r
85                 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);\r
86         setHeaderLabels(headers);\r
87 //      setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
88         setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection);\r
89         \r
90         selectedTag = "";\r
91         itemClicked.connect(this, "itemClicked()");\r
92                 int width = Global.getColumnWidth("tagTreeName");\r
93                 if (width>0)\r
94                         setColumnWidth(0, width);\r
95 \r
96         }\r
97         \r
98         public void setEditAction(QAction e) {\r
99                 editAction = e;\r
100         }\r
101         public void setDeleteAction(QAction d) {\r
102                 deleteAction = d;\r
103         }\r
104         public void setAddAction(QAction a) {\r
105                 addAction = a;\r
106         }\r
107         public void setIconAction(QAction i) {\r
108                 iconAction = i;\r
109         }\r
110         \r
111         // Insert a new tag into the tree.  This is used when we dynamically add a \r
112         // new tag after the full tag tree has been built.  It only adds to the\r
113         // top level.\r
114         public void insertTag(String name, String guid) {\r
115         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
116                 QIcon icon = new QIcon(iconPath+"tag.png");\r
117                 QTreeWidgetItem child;\r
118                 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
119                 \r
120                 // Build new tag & add it\r
121                 child = new QTreeWidgetItem();\r
122                 child.setText(0, name);\r
123                 child.setIcon(0,icon);\r
124                 child.setText(2, guid);\r
125                 child.setTextAlignment(1, ra.value());\r
126                 addTopLevelItem(child);\r
127                 \r
128                 // Resort the list\r
129                 resizeColumnToContents(0);\r
130         resizeColumnToContents(1);\r
131         sortItems(0, SortOrder.AscendingOrder);\r
132         }\r
133         \r
134         private QIcon findDefaultIcon(String guid) {\r
135         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
136                 QIcon icon = new QIcon(iconPath+"tag.png");\r
137                 QIcon linkedIcon = new QIcon(iconPath+"tag-orange.png");\r
138 \r
139                 if (db.getTagTable().getNotebookGuid(guid) == null || \r
140                                 db.getTagTable().getNotebookGuid(guid).equals(""))\r
141                         return icon;\r
142                 else\r
143                         return linkedIcon;\r
144         }\r
145         \r
146         List<String> findExpandedTags(QTreeWidgetItem item) {\r
147                 List<String> list = new ArrayList<String>();\r
148                 if (item.isExpanded()) \r
149                         list.add(item.text(0));\r
150                 for (int i=0; i<item.childCount(); i++) {\r
151                         List<String> childrenList = findExpandedTags(item.child(i));\r
152                         for (int j=0; j<childrenList.size(); j++) {\r
153                                 list.add(childrenList.get(j));\r
154                         }\r
155                 }\r
156                 \r
157                 return list;\r
158         }\r
159         \r
160         void expandTags(QTreeWidgetItem item, List<String> expandedTags) {\r
161                 for (int i=0; i<item.childCount(); i++) {\r
162                         expandTags(item.child(i), expandedTags);\r
163                 }\r
164                 \r
165                 for (int i=0; i<expandedTags.size(); i++) {\r
166                         if (expandedTags.get(i).equalsIgnoreCase(item.text(0))) {\r
167                                 expandItem(item);\r
168                                 i=expandedTags.size();\r
169                         }\r
170                 }\r
171         }\r
172         \r
173         public void load(List<Tag> tags) {\r
174         Tag tag;\r
175         List<QTreeWidgetItem> index = new ArrayList<QTreeWidgetItem>();\r
176         QTreeWidgetItem child;\r
177                         \r
178         /* First, let's find out which stacks are expanded */\r
179         QTreeWidgetItem root =  invisibleRootItem();\r
180         List<String> expandedTags = findExpandedTags(root);\r
181 \r
182 \r
183         \r
184         //Clear out the tree & reload\r
185         clear();\r
186         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
187                 QIcon icon = new QIcon(iconPath+"tag.png");\r
188         \r
189                 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
190         \r
191                 // Create a copy.  We delete them out as they are found\r
192                 List<Tag> tempList = new ArrayList<Tag>();\r
193                 for (int i=0; i<tags.size(); i++) {\r
194                         tempList.add(tags.get(i));\r
195                 }\r
196                 \r
197         while (tempList.size() > 0) {\r
198                 for (int i=0; i<tempList.size(); i++) {\r
199                         tag = tempList.get(i);\r
200                         if (tag.getParentGuid()==null || tag.getParentGuid().equals("")) {\r
201                                 child = new QTreeWidgetItem();\r
202                                 child.setText(0, tag.getName());\r
203                                 if (icons != null && !icons.containsKey(tag.getGuid())) {\r
204                                         child.setIcon(0, findDefaultIcon(tag.getGuid()));\r
205                                 } else {\r
206                                         child.setIcon(0, icons.get(tag.getGuid()));\r
207                                 }\r
208 \r
209                                 child.setText(2, tag.getGuid());\r
210                                 child.setTextAlignment(1, ra.value());\r
211                                 index.add(child);\r
212                                 addTopLevelItem(child);\r
213                                 tempList.remove(i);\r
214                         } else {\r
215                                 // We need to find the parent\r
216                                 for (int j=0; j<index.size(); j++) {\r
217                                         if (index.get(j).text(2).equals(tag.getParentGuid())) {\r
218                                         child = new QTreeWidgetItem();\r
219                                         child.setText(0, tag.getName());\r
220                                         child.setIcon(0, icon);\r
221                                         child.setText(2, tag.getGuid());\r
222                                         child.setTextAlignment(1, ra.value());\r
223                                         if (icons != null && !icons.containsKey(tag.getGuid())) {\r
224                                                 child.setIcon(0, findDefaultIcon(tag.getGuid()));\r
225                                         } else {\r
226                                                 child.setIcon(0, icons.get(tag.getGuid()));\r
227                                         }\r
228                                         tempList.remove(i);\r
229                                         index.add(child);                                               \r
230                                         index.get(j).addChild(child);\r
231                                         }\r
232                                 }\r
233                         }\r
234                 } \r
235         }\r
236         resizeColumnToContents(0);\r
237         resizeColumnToContents(1);\r
238         sortItems(0, SortOrder.AscendingOrder);\r
239         \r
240         expandTags(invisibleRootItem(), expandedTags);\r
241         }\r
242         // Show (unhide) all tags\r
243         public void showAllTags(boolean value) {\r
244                 showAllTags = value;\r
245         }\r
246         public void unhideAllTags() {\r
247                 MatchFlags flags = new MatchFlags();\r
248                 flags.set(MatchFlag.MatchWildcard);\r
249                 flags.set(MatchFlag.MatchRecursive);\r
250                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
251                 for (int i=0; i<children.size(); i++) {\r
252                         children.get(i).setHidden(false);\r
253                 }\r
254         }\r
255         // update the display with the current number of notes\r
256         public void updateCounts(List<TagCounter> counts) {\r
257                                 \r
258                 MatchFlags flags = new MatchFlags();\r
259                 flags.set(MatchFlag.MatchWildcard);\r
260                 flags.set(MatchFlag.MatchRecursive);\r
261 //              List<QTreeWidgetItem> children = new ArrayList<QTreeWidgetItem>();\r
262                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
263                 \r
264                 QBrush black = new QBrush();\r
265                 black.setColor(QColor.black);\r
266                 QBrush blue = new QBrush();\r
267                 blue.setColor(QColor.blue);\r
268                 if (!Global.tagBehavior().equalsIgnoreCase("ColorActive"))\r
269                         blue.setColor(QColor.black);\r
270                 \r
271                 for (int i=0; i<children.size(); i++) {\r
272                         children.get(i).setText(1,"0");\r
273                         children.get(i).setForeground(0, black);                        \r
274                         children.get(i).setForeground(1, black);\r
275                         if (!showAllTags && (Global.tagBehavior().equalsIgnoreCase("HideInactiveCount") || Global.tagBehavior().equalsIgnoreCase("NoHideInactiveCount")))\r
276                                 children.get(i).setHidden(true);\r
277                         else\r
278                                 children.get(i).setHidden(false);\r
279                         if (children.get(i).isSelected())\r
280                                 children.get(i).setHidden(false);\r
281                 }\r
282                 for (int i=0; i<counts.size(); i++) {\r
283                         for (int j=0; j<children.size(); j++) {\r
284                                 String guid = children.get(j).text(2);\r
285                                 if (counts.get(i).getGuid().equals(guid)) {\r
286                                         children.get(j).setText(1, new Integer(counts.get(i).getCount()).toString());\r
287                                         if (counts.get(i).getCount() > 0 || children.get(j).isSelected()) {\r
288                                                 children.get(j).setForeground(0, blue);                 \r
289                                                 children.get(j).setForeground(1, blue);\r
290                                                 QTreeWidgetItem parent = children.get(j);\r
291                                                 while (parent != null) {\r
292                                                         parent.setForeground(0, blue);                  \r
293                                                         parent.setForeground(1, blue);\r
294                                                         parent.setHidden(false);\r
295                                                         parent = parent.parent();\r
296                                                 }\r
297                                         }\r
298                                 }\r
299                         }\r
300                 }\r
301         }\r
302 \r
303         \r
304         public boolean selectGuid(String guid) {\r
305                 MatchFlags flags = new MatchFlags();\r
306                 flags.set(MatchFlag.MatchWildcard);\r
307                 flags.set(MatchFlag.MatchRecursive);\r
308 //              List<QTreeWidgetItem> children = new ArrayList<QTreeWidgetItem>();\r
309                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
310 \r
311                 for (int i=0; i<children.size(); i++) {\r
312                         if (children.get(i).text(2).equals(guid)) {\r
313                                 children.get(i).setSelected(true);\r
314                                 return true;\r
315                         }\r
316                 }\r
317                 return false;\r
318         }\r
319         \r
320          @Override\r
321          protected void dragMoveEvent(QDragMoveEvent event) {\r
322                 if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
323                         if (event.answerRect().intersects(childrenRect()))\r
324                                 event.acceptProposedAction();\r
325                         return;\r
326                 }\r
327          }\r
328 \r
329         \r
330         @Override\r
331         public void dragEnterEvent(QDragEnterEvent event) {\r
332                 if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
333                         event.accept();\r
334                         return;\r
335                 }\r
336                 if (event.source() == this) {\r
337                         if (Global.tagBehavior().equals("HideInactiveCount")) {\r
338                                 event.ignore();\r
339                                 return;\r
340                         }\r
341                         event.mimeData().setData("application/x-nevernote-tag", new QByteArray(currentItem().text(2)));\r
342                         event.accept();\r
343                         return;\r
344                 }\r
345                 event.ignore();\r
346         }\r
347 \r
348         @Override\r
349         public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {\r
350                 if (data.hasFormat("application/x-nevernote-tag")) {\r
351                         QByteArray d = data.data("application/x-nevernote-tag");\r
352                         String current = d.toString();\r
353                         \r
354                         // Check we don't do a dumb thing like move a parent to a child of itself\r
355                         if (!checkParent(parent, current))\r
356                                 return false;\r
357                         QTreeWidgetItem newChild;\r
358                         if (parent == null) {\r
359 //                              tagSignal.changeParent.emit(current, "");\r
360                                 db.getTagTable().updateTagParent(current, "");\r
361                                 newChild = new QTreeWidgetItem(this);\r
362                         } else {\r
363 //                              tagSignal.changeParent.emit(current, parent.text(2));\r
364                                 db.getTagTable().updateTagParent(current, parent.text(2));\r
365                                 newChild = new QTreeWidgetItem(parent);\r
366                         }\r
367                         copyTreeItem(currentItem(), newChild);\r
368                         currentItem().setHidden(true);\r
369                         sortItems(0, SortOrder.AscendingOrder);\r
370                         return true;\r
371                 }\r
372                 \r
373                 // If we are dropping a note\r
374                 if (data.hasFormat("application/x-nevernote-note")) {\r
375                         String notebookGuid = db.getTagTable().getNotebookGuid(parent.text(2));\r
376                         QByteArray d = data.data("application/x-nevernote-note");\r
377                         String s = d.toString();\r
378                         String noteGuidArray[] = s.split(" ");\r
379                         for (String element : noteGuidArray) {\r
380                                 Note n = db.getNoteTable().getNote(element.trim(), false, false, false, false, false);\r
381                                 \r
382                                 // Check that...\r
383                                 // 1.) Check that tag isn't already assigned to that note\r
384                                 // 2.) Check that that tag is valid for that notebook or the tag isn't notebook specific\r
385                                 // 3.) Check that the notebook isn't read only.\r
386                                 if (!db.getNoteTable().noteTagsTable.checkNoteNoteTags(element.trim(), parent.text(2)) &&\r
387                                                 (notebookGuid == null || n.getNotebookGuid().equalsIgnoreCase(notebookGuid) || notebookGuid.equals("")) &&\r
388                                                 !db.getNotebookTable().isReadOnly(n.getNotebookGuid())) {\r
389                                         db.getNoteTable().noteTagsTable.saveNoteTag(element.trim(), parent.text(2));\r
390                                         noteSignal.tagsAdded.emit(element.trim(), parent.text(2));\r
391                                 }\r
392                         }\r
393                         //tagSignal.listChanged.emit();\r
394                         \r
395                         return true;\r
396                 }\r
397                 return false;\r
398         }\r
399         \r
400         @Override\r
401         public void contextMenuEvent(QContextMenuEvent event) {\r
402                 QMenu menu = new QMenu(this);\r
403                 menu.addAction(addAction);\r
404                 menu.addAction(editAction);\r
405                 menu.addAction(deleteAction);\r
406                 menu.addSeparator();\r
407                 menu.addAction(iconAction);\r
408                 menu.exec(event.globalPos());\r
409         }\r
410         \r
411         public void setIcons(HashMap<String, QIcon> i) {\r
412                 icons = i;\r
413         }\r
414         \r
415         // Copy an individual item within the tree.  I need to do this because\r
416         // Qt doesn't call the dropMimeData on a move, just a copy.\r
417         private void copyTreeItem(QTreeWidgetItem source, QTreeWidgetItem target) {\r
418                 target.setText(0, source.text(0));\r
419                 target.setIcon(0, source.icon(0));\r
420                 target.setText(1, source.text(1));\r
421                 target.setText(2, source.text(2));\r
422                 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
423                 target.setTextAlignment(1, ra.value());\r
424                 \r
425                 for (int i=0; i<source.childCount(); i++) {\r
426                         QTreeWidgetItem newChild = new QTreeWidgetItem(target);\r
427                         copyTreeItem(source.child(i), newChild);\r
428                         source.child(i).setHidden(true);\r
429                 }\r
430                 return;\r
431         }\r
432         \r
433         // Check that we don't copy a parent as a child of a current child.\r
434         private boolean checkParent(QTreeWidgetItem parent, String child) {\r
435                 if (parent != null)\r
436                         if (parent.text(2).equals(child))\r
437                                 return false;\r
438                 if (parent == null)\r
439                         return true;\r
440                 return checkParent(parent.parent(), child);\r
441         }\r
442 \r
443 \r
444         public void selectSavedSearch(QTreeWidgetItem item) {\r
445                 MatchFlags flags = new MatchFlags();\r
446                 flags.set(MatchFlag.MatchWildcard);\r
447                 flags.set(MatchFlag.MatchRecursive);\r
448                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
449                 \r
450                 for (int j=0; j<children.size(); j++) {\r
451                         String guid = children.get(j).text(2);\r
452                         if (item.text(2).equals(guid)) {\r
453                                 children.get(j).setSelected(true);\r
454                         }\r
455                 }\r
456         }\r
457 \r
458         @SuppressWarnings("unused")\r
459         private void itemClicked() {\r
460                 \r
461                 List<QTreeWidgetItem> selectedItem = selectedItems();\r
462                 if (selectedItem.size() == 1) {\r
463                         if (selectedItem.get(0).text(0).equalsIgnoreCase(selectedTag) && !rightButtonClicked) {\r
464                                 selectedTag = "";\r
465                                 clearSelection();\r
466                         } else {\r
467                                 selectedTag = selectedItem.get(0).text(0);\r
468                         }\r
469                         \r
470                 }\r
471                 selectionSignal.emit();\r
472         }\r
473 \r
474         \r
475         @Override\r
476         public void mousePressEvent(QMouseEvent e) {\r
477                 if (e.button() == Qt.MouseButton.RightButton)\r
478                         rightButtonClicked = true;\r
479                 else\r
480                         rightButtonClicked = false;\r
481                 super.mousePressEvent(e);\r
482         }\r
483 }\r