OSDN Git Service

46f55f6f54018c09270a577663607c52f45dec8c
[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                 header().setStyleSheet("QHeaderView::section {border: 0.0em;}");\r
82                 db = d;\r
83                 selectionSignal = new Signal0();\r
84                 tagSignal = new TagSignal();\r
85                 noteSignal = new NoteSignal();\r
86                 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);\r
87         setHeaderLabels(headers);\r
88 \r
89 //      setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
90         setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection);\r
91         \r
92         selectedTag = "";\r
93         itemClicked.connect(this, "itemClicked()");\r
94                 int width = Global.getColumnWidth("tagTreeName");\r
95                 if (width>0)\r
96                         setColumnWidth(0, width);\r
97 \r
98                 \r
99         }\r
100         \r
101         public void setEditAction(QAction e) {\r
102                 editAction = e;\r
103         }\r
104         public void setDeleteAction(QAction d) {\r
105                 deleteAction = d;\r
106         }\r
107         public void setAddAction(QAction a) {\r
108                 addAction = a;\r
109         }\r
110         public void setIconAction(QAction i) {\r
111                 iconAction = i;\r
112         }\r
113         \r
114         // Insert a new tag into the tree.  This is used when we dynamically add a \r
115         // new tag after the full tag tree has been built.  It only adds to the\r
116         // top level.\r
117         public void insertTag(String name, String guid) {\r
118         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
119                 QIcon icon = new QIcon(iconPath+"tag.png");\r
120                 NTreeWidgetItem child;\r
121                 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
122                 \r
123                 // Build new tag & add it\r
124                 child = new NTreeWidgetItem();\r
125                 child.setText(0, name);\r
126                 child.setIcon(0,icon);\r
127                 child.setText(2, guid);\r
128                 child.setTextAlignment(1, ra.value());\r
129                 addTopLevelItem(child);\r
130                 \r
131                 // Resort the list\r
132                 resizeColumnToContents(0);\r
133         resizeColumnToContents(1);\r
134         sortItems(0, SortOrder.AscendingOrder);\r
135         }\r
136         \r
137         private QIcon findDefaultIcon(String guid) {\r
138         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
139                 QIcon icon = new QIcon(iconPath+"tag.png");\r
140                 QIcon linkedIcon = new QIcon(iconPath+"tag-orange.png");\r
141 \r
142                 if (db.getTagTable().getNotebookGuid(guid) == null || \r
143                                 db.getTagTable().getNotebookGuid(guid).equals(""))\r
144                         return icon;\r
145                 else\r
146                         return linkedIcon;\r
147         }\r
148         \r
149         List<String> findExpandedTags(QTreeWidgetItem item) {\r
150                 List<String> list = new ArrayList<String>();\r
151                 if (item.isExpanded()) \r
152                         list.add(item.text(0));\r
153                 for (int i=0; i<item.childCount(); i++) {\r
154                         List<String> childrenList = findExpandedTags(item.child(i));\r
155                         for (int j=0; j<childrenList.size(); j++) {\r
156                                 list.add(childrenList.get(j));\r
157                         }\r
158                 }\r
159                 \r
160                 return list;\r
161         }\r
162         \r
163         void expandTags(QTreeWidgetItem item, List<String> expandedTags) {\r
164                 for (int i=0; i<item.childCount(); i++) {\r
165                         expandTags(item.child(i), expandedTags);\r
166                 }\r
167                 \r
168                 for (int i=0; i<expandedTags.size(); i++) {\r
169                         if (expandedTags.get(i).equalsIgnoreCase(item.text(0))) {\r
170                                 expandItem(item);\r
171                                 i=expandedTags.size();\r
172                         }\r
173                 }\r
174         }\r
175         \r
176         public void load(List<Tag> tags) {\r
177         Tag tag;\r
178         List<NTreeWidgetItem> index = new ArrayList<NTreeWidgetItem>();\r
179         NTreeWidgetItem child;\r
180                         \r
181         /* First, let's find out which stacks are expanded */\r
182         QTreeWidgetItem root =  invisibleRootItem();\r
183         List<String> expandedTags = findExpandedTags(root);\r
184 \r
185 \r
186         \r
187         //Clear out the tree & reload\r
188         clear();\r
189         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
190                 QIcon icon = new QIcon(iconPath+"tag.png");\r
191         \r
192                 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
193         \r
194                 // Create a copy.  We delete them out as they are found\r
195                 List<Tag> tempList = new ArrayList<Tag>();\r
196                 for (int i=0; i<tags.size(); i++) {\r
197                         tempList.add(tags.get(i));\r
198                 }\r
199                 \r
200         while (tempList.size() > 0) {\r
201                 for (int i=0; i<tempList.size(); i++) {\r
202                         tag = tempList.get(i);\r
203                         if (tag.getParentGuid()==null || tag.getParentGuid().equals("")) {\r
204                                 child = new NTreeWidgetItem();\r
205                                 child.setText(0, tag.getName());\r
206                                 if (icons != null && !icons.containsKey(tag.getGuid())) {\r
207                                         child.setIcon(0, findDefaultIcon(tag.getGuid()));\r
208                                 } else {\r
209                                         child.setIcon(0, icons.get(tag.getGuid()));\r
210                                 }\r
211 \r
212                                 child.setText(2, tag.getGuid());\r
213                                 child.setTextAlignment(1, ra.value());\r
214                                 index.add(child);\r
215                                 addTopLevelItem(child);\r
216                                 tempList.remove(i);\r
217                         } else {\r
218                                 // We need to find the parent\r
219                                 for (int j=0; j<index.size(); j++) {\r
220                                         if (index.get(j).text(2).equals(tag.getParentGuid())) {\r
221                                         child = new NTreeWidgetItem();\r
222                                         child.setText(0, tag.getName());\r
223                                         child.setIcon(0, icon);\r
224                                         child.setText(2, tag.getGuid());\r
225                                         child.setTextAlignment(1, ra.value());\r
226                                         if (icons != null && !icons.containsKey(tag.getGuid())) {\r
227                                                 child.setIcon(0, findDefaultIcon(tag.getGuid()));\r
228                                         } else {\r
229                                                 child.setIcon(0, icons.get(tag.getGuid()));\r
230                                         }\r
231                                         tempList.remove(i);\r
232                                         index.add(child);                                               \r
233                                         index.get(j).addChild(child);\r
234                                         }\r
235                                 }\r
236                         }\r
237                 } \r
238         }\r
239         resizeColumnToContents(0);\r
240         resizeColumnToContents(1);\r
241         sortItems(0, SortOrder.AscendingOrder);\r
242         \r
243         expandTags(invisibleRootItem(), expandedTags);\r
244         }\r
245         // Show (unhide) all tags\r
246         public void showAllTags(boolean value) {\r
247                 showAllTags = value;\r
248         }\r
249         public void unhideAllTags() {\r
250                 MatchFlags flags = new MatchFlags();\r
251                 flags.set(MatchFlag.MatchWildcard);\r
252                 flags.set(MatchFlag.MatchRecursive);\r
253                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
254                 for (int i=0; i<children.size(); i++) {\r
255                         children.get(i).setHidden(false);\r
256                 }\r
257         }\r
258         // update the display with the current number of notes\r
259         public void updateCounts(List<TagCounter> counts) {\r
260                                 \r
261                 MatchFlags flags = new MatchFlags();\r
262                 flags.set(MatchFlag.MatchWildcard);\r
263                 flags.set(MatchFlag.MatchRecursive);\r
264 //              List<QTreeWidgetItem> children = new ArrayList<QTreeWidgetItem>();\r
265                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
266                 \r
267                 QBrush black = new QBrush();\r
268                 black.setColor(QColor.black);\r
269                 QBrush blue = new QBrush();\r
270                 blue.setColor(QColor.blue);\r
271                 if (!Global.tagBehavior().equalsIgnoreCase("ColorActive"))\r
272                         blue.setColor(QColor.black);\r
273                 \r
274                 for (int i=0; i<children.size(); i++) {\r
275                         children.get(i).setText(1,"0");\r
276                         children.get(i).setForeground(0, black);                        \r
277                         children.get(i).setForeground(1, black);\r
278                         if (!showAllTags && (Global.tagBehavior().equalsIgnoreCase("HideInactiveCount") || Global.tagBehavior().equalsIgnoreCase("NoHideInactiveCount")))\r
279                                 children.get(i).setHidden(true);\r
280                         else\r
281                                 children.get(i).setHidden(false);\r
282                         if (children.get(i).isSelected())\r
283                                 children.get(i).setHidden(false);\r
284                 }\r
285                 for (int i=0; i<counts.size(); i++) {\r
286                         for (int j=0; j<children.size(); j++) {\r
287                                 String guid = children.get(j).text(2);\r
288                                 if (counts.get(i).getGuid().equals(guid)) {\r
289                                         children.get(j).setText(1, new Integer(counts.get(i).getCount()).toString());\r
290                                         if (counts.get(i).getCount() > 0 || children.get(j).isSelected()) {\r
291                                                 children.get(j).setForeground(0, blue);                 \r
292                                                 children.get(j).setForeground(1, blue);\r
293                                                 QTreeWidgetItem parent = children.get(j);\r
294                                                 while (parent != null) {\r
295                                                         parent.setForeground(0, blue);                  \r
296                                                         parent.setForeground(1, blue);\r
297                                                         parent.setHidden(false);\r
298                                                         parent = parent.parent();\r
299                                                 }\r
300                                         }\r
301                                 }\r
302                         }\r
303                 }\r
304         }\r
305 \r
306         \r
307         public boolean selectGuid(String guid) {\r
308                 MatchFlags flags = new MatchFlags();\r
309                 flags.set(MatchFlag.MatchWildcard);\r
310                 flags.set(MatchFlag.MatchRecursive);\r
311 //              List<QTreeWidgetItem> children = new ArrayList<QTreeWidgetItem>();\r
312                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
313 \r
314                 for (int i=0; i<children.size(); i++) {\r
315                         if (children.get(i).text(2).equals(guid)) {\r
316                                 children.get(i).setSelected(true);\r
317                                 return true;\r
318                         }\r
319                 }\r
320                 return false;\r
321         }\r
322         \r
323          @Override\r
324          protected void dragMoveEvent(QDragMoveEvent event) {\r
325                 if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
326                         if (event.answerRect().intersects(childrenRect()))\r
327                                 event.acceptProposedAction();\r
328                         return;\r
329                 }\r
330          }\r
331 \r
332         \r
333         @Override\r
334         public void dragEnterEvent(QDragEnterEvent event) {\r
335                 if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
336                         event.accept();\r
337                         return;\r
338                 }\r
339                 if (event.source() == this) {\r
340                         if (Global.tagBehavior().equals("HideInactiveCount")) {\r
341                                 event.ignore();\r
342                                 return;\r
343                         }\r
344                         event.mimeData().setData("application/x-nevernote-tag", new QByteArray(currentItem().text(2)));\r
345                         event.accept();\r
346                         return;\r
347                 }\r
348                 event.ignore();\r
349         }\r
350 \r
351         @Override\r
352         public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {\r
353                 if (data.hasFormat("application/x-nevernote-tag")) {\r
354                         QByteArray d = data.data("application/x-nevernote-tag");\r
355                         String current = d.toString();\r
356                         \r
357                         // Check we don't do a dumb thing like move a parent to a child of itself\r
358                         if (!checkParent(parent, current))\r
359                                 return false;\r
360                         QTreeWidgetItem newChild;\r
361                         if (parent == null) {\r
362 //                              tagSignal.changeParent.emit(current, "");\r
363                                 db.getTagTable().updateTagParent(current, "");\r
364                                 newChild = new QTreeWidgetItem(this);\r
365                         } else {\r
366 //                              tagSignal.changeParent.emit(current, parent.text(2));\r
367                                 db.getTagTable().updateTagParent(current, parent.text(2));\r
368                                 newChild = new QTreeWidgetItem(parent);\r
369                         }\r
370                         copyTreeItem(currentItem(), newChild);\r
371                         currentItem().setHidden(true);\r
372                         sortItems(0, SortOrder.AscendingOrder);\r
373                         return true;\r
374                 }\r
375                 \r
376                 // If we are dropping a note\r
377                 if (data.hasFormat("application/x-nevernote-note")) {\r
378                         String notebookGuid = db.getTagTable().getNotebookGuid(parent.text(2));\r
379                         QByteArray d = data.data("application/x-nevernote-note");\r
380                         String s = d.toString();\r
381                         String noteGuidArray[] = s.split(" ");\r
382                         for (String element : noteGuidArray) {\r
383                                 Note n = db.getNoteTable().getNote(element.trim(), false, false, false, false, false);\r
384                                 \r
385                                 // Check that...\r
386                                 // 1.) Check that tag isn't already assigned to that note\r
387                                 // 2.) Check that that tag is valid for that notebook or the tag isn't notebook specific\r
388                                 // 3.) Check that the notebook isn't read only.\r
389                                 if (!db.getNoteTable().noteTagsTable.checkNoteNoteTags(element.trim(), parent.text(2)) &&\r
390                                                 (notebookGuid == null || n.getNotebookGuid().equalsIgnoreCase(notebookGuid) || notebookGuid.equals("")) &&\r
391                                                 !db.getNotebookTable().isReadOnly(n.getNotebookGuid())) {\r
392                                         db.getNoteTable().noteTagsTable.saveNoteTag(element.trim(), parent.text(2));\r
393                                         noteSignal.tagsAdded.emit(element.trim(), parent.text(2));\r
394                                 }\r
395                         }\r
396                         //tagSignal.listChanged.emit();\r
397                         \r
398                         return true;\r
399                 }\r
400                 return false;\r
401         }\r
402         \r
403         @Override\r
404         public void contextMenuEvent(QContextMenuEvent event) {\r
405                 QMenu menu = new QMenu(this);\r
406                 menu.addAction(addAction);\r
407                 menu.addAction(editAction);\r
408                 menu.addAction(deleteAction);\r
409                 menu.addSeparator();\r
410                 menu.addAction(iconAction);\r
411                 menu.exec(event.globalPos());\r
412         }\r
413         \r
414         public void setIcons(HashMap<String, QIcon> i) {\r
415                 icons = i;\r
416         }\r
417         \r
418         // Copy an individual item within the tree.  I need to do this because\r
419         // Qt doesn't call the dropMimeData on a move, just a copy.\r
420         private void copyTreeItem(QTreeWidgetItem source, QTreeWidgetItem target) {\r
421                 target.setText(0, source.text(0));\r
422                 target.setIcon(0, source.icon(0));\r
423                 target.setText(1, source.text(1));\r
424                 target.setText(2, source.text(2));\r
425                 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
426                 target.setTextAlignment(1, ra.value());\r
427                 \r
428                 for (int i=0; i<source.childCount(); i++) {\r
429                         QTreeWidgetItem newChild = new QTreeWidgetItem(target);\r
430                         copyTreeItem(source.child(i), newChild);\r
431                         source.child(i).setHidden(true);\r
432                 }\r
433                 return;\r
434         }\r
435         \r
436         // Check that we don't copy a parent as a child of a current child.\r
437         private boolean checkParent(QTreeWidgetItem parent, String child) {\r
438                 if (parent != null)\r
439                         if (parent.text(2).equals(child))\r
440                                 return false;\r
441                 if (parent == null)\r
442                         return true;\r
443                 return checkParent(parent.parent(), child);\r
444         }\r
445 \r
446 \r
447         public void selectSavedSearch(QTreeWidgetItem item) {\r
448                 MatchFlags flags = new MatchFlags();\r
449                 flags.set(MatchFlag.MatchWildcard);\r
450                 flags.set(MatchFlag.MatchRecursive);\r
451                 List <QTreeWidgetItem>  children = findItems("*", flags);\r
452                 \r
453                 for (int j=0; j<children.size(); j++) {\r
454                         String guid = children.get(j).text(2);\r
455                         if (item.text(2).equals(guid)) {\r
456                                 children.get(j).setSelected(true);\r
457                         }\r
458                 }\r
459         }\r
460 \r
461         @SuppressWarnings("unused")\r
462         private void itemClicked() {\r
463                 \r
464                 List<QTreeWidgetItem> selectedItem = selectedItems();\r
465                 if (selectedItem.size() == 1) {\r
466                         if (selectedItem.get(0).text(0).equalsIgnoreCase(selectedTag) && !rightButtonClicked) {\r
467                                 selectedTag = "";\r
468                                 clearSelection();\r
469                         } else {\r
470                                 selectedTag = selectedItem.get(0).text(0);\r
471                         }\r
472                         \r
473                 }\r
474                 selectionSignal.emit();\r
475         }\r
476 \r
477         \r
478         @Override\r
479         public void mousePressEvent(QMouseEvent e) {\r
480                 if (e.button() == Qt.MouseButton.RightButton)\r
481                         rightButtonClicked = true;\r
482                 else\r
483                         rightButtonClicked = false;\r
484                 super.mousePressEvent(e);\r
485         }\r
486 \r
487         \r
488 }\r