OSDN Git Service

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