OSDN Git Service

Improved performance in the editor by not forcing a search through the note table...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / NotebookTreeWidget.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.gui;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.List;\r
24 \r
25 import com.evernote.edam.type.Notebook;\r
26 import com.trolltech.qt.core.QByteArray;\r
27 import com.trolltech.qt.core.QMimeData;\r
28 import com.trolltech.qt.core.Qt;\r
29 import com.trolltech.qt.core.Qt.SortOrder;\r
30 import com.trolltech.qt.gui.QAbstractItemView;\r
31 import com.trolltech.qt.gui.QAction;\r
32 import com.trolltech.qt.gui.QBrush;\r
33 import com.trolltech.qt.gui.QColor;\r
34 import com.trolltech.qt.gui.QContextMenuEvent;\r
35 import com.trolltech.qt.gui.QDragEnterEvent;\r
36 import com.trolltech.qt.gui.QDragMoveEvent;\r
37 import com.trolltech.qt.gui.QHeaderView;\r
38 import com.trolltech.qt.gui.QIcon;\r
39 import com.trolltech.qt.gui.QMenu;\r
40 import com.trolltech.qt.gui.QTreeWidget;\r
41 import com.trolltech.qt.gui.QTreeWidgetItem;\r
42 \r
43 import cx.fbn.nevernote.Global;\r
44 import cx.fbn.nevernote.filters.NotebookCounter;\r
45 import cx.fbn.nevernote.signals.NoteSignal;\r
46 \r
47 public class NotebookTreeWidget extends QTreeWidget {\r
48         private QAction                                 deleteAction;\r
49         private QAction                                 addAction;\r
50         private QAction                                 editAction;\r
51         public NoteSignal                               noteSignal;\r
52 //      private final QTreeWidgetItem                   previousMouseOver;\r
53 //      private boolean                                 previousMouseOverWasSelected;\r
54         \r
55         public void setAddAction(QAction a) {\r
56                 addAction = a;\r
57         }\r
58         \r
59         public void setDeleteAction(QAction d) {\r
60                 deleteAction = d;\r
61         }\r
62         \r
63         public void setEditAction(QAction e) {\r
64                 editAction = e;\r
65         }\r
66         \r
67         public NotebookTreeWidget() {\r
68                 noteSignal = new NoteSignal();\r
69                 List<String> labels = new ArrayList<String>();\r
70                 labels.add("Notebooks");\r
71                 labels.add("");\r
72                 setAcceptDrops(true);\r
73                 setDragEnabled(true);\r
74                 setColumnCount(2);\r
75                 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);\r
76                 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);\r
77                 header().setMovable(false);\r
78                 setHeaderLabels(labels);\r
79                 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);\r
80                 // If we want to mimic Evernote's notebook selection\r
81                 if (Global.mimicEvernoteInterface) {\r
82                         setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection);\r
83                 } else\r
84                         setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
85 \r
86 //      int width = Global.getColumnWidth("notebookTreeName");\r
87 //              if (width>0)\r
88 //                      setColumnWidth(0, width);\r
89 //              previousMouseOver = new QTreeWidgetItem();\r
90         }\r
91         \r
92         public void selectNotebook(QTreeWidgetItem item) {\r
93                 QTreeWidgetItem root = invisibleRootItem();\r
94                 QTreeWidgetItem child;\r
95                 \r
96                 for (int i=0; i<root.childCount(); i++) {\r
97                         child = root.child(i); \r
98                         if (child.text(2).equals(item.text(2))) {\r
99                                 child.setSelected(true);\r
100                                 return;\r
101                         }\r
102                 }\r
103                 \r
104         }\r
105         \r
106         public boolean selectGuid(String guid) {\r
107                 QTreeWidgetItem root = invisibleRootItem();\r
108                 QTreeWidgetItem child;\r
109 \r
110                 for (int i=0; i<root.childCount(); i++) {\r
111                         child = root.child(i);\r
112                         if (child.text(2).equals(guid)) {\r
113                                 child.setSelected(true);\r
114                                 return true;\r
115                         }\r
116                 }\r
117                 return false;\r
118         }\r
119         \r
120         \r
121         public void load(List<Notebook> books, List<String> localBooks) {\r
122         Notebook book;\r
123         QTreeWidgetItem child;\r
124         clear();\r
125         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
126         QIcon blueIcon = new QIcon(iconPath+"notebook-blue.png");\r
127         QIcon greenIcon = new QIcon(iconPath+"notebook-green.png");\r
128         QIcon redIcon = new QIcon(iconPath+"notebook-red.png");\r
129         QIcon yellowIcon = new QIcon(iconPath+"notebook-yellow.png");\r
130 //              QIcon icon = new QIcon(iconPath+"nevernote.png");\r
131         if (books == null)\r
132                 return;\r
133         Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
134         for (int i=0; i<books.size(); i++) {\r
135                 book = books.get(i);\r
136                 child = new QTreeWidgetItem();\r
137                 child.setText(0, book.getName());\r
138                 child.setIcon(0, greenIcon);\r
139                 if (localBooks.contains(book.getGuid()))\r
140                         child.setIcon(0, yellowIcon);\r
141                 if (localBooks.contains(book.getGuid()) && \r
142                                 (book.getName().equalsIgnoreCase("Conflicting Changes") ||\r
143                                  book.getName().equalsIgnoreCase("Conflicting Changes (Local)")))\r
144                                         child.setIcon(0, redIcon);\r
145                 if (book.isPublished())\r
146                         child.setIcon(0, blueIcon);\r
147                 child.setTextAlignment(1, ra.value());\r
148                 child.setText(2, book.getGuid());\r
149                 addTopLevelItem(child);\r
150         }\r
151 \r
152         sortItems(0, SortOrder.AscendingOrder); \r
153         if (Global.mimicEvernoteInterface) {\r
154                 child = new QTreeWidgetItem();\r
155                 child.setIcon(0, greenIcon);\r
156                 child.setText(0, "All Notebooks");\r
157 //              child.setText(1, "0");\r
158                 child.setText(2, "");\r
159                 child.setTextAlignment(1, ra.value());\r
160                 insertTopLevelItem(0,child);\r
161         }\r
162         resizeColumnToContents(0);\r
163         resizeColumnToContents(1);\r
164         }\r
165         // update the display with the current number of notes\r
166         public void updateCounts(List<Notebook> books, List<NotebookCounter> counts) {\r
167                 QTreeWidgetItem root = invisibleRootItem();\r
168                 QTreeWidgetItem child;\r
169                 \r
170                 QBrush blue = new QBrush();\r
171                 QBrush black = new QBrush();\r
172                 black.setColor(QColor.black);\r
173                 if (Global.tagBehavior().equalsIgnoreCase("ColorActive") && !Global.mimicEvernoteInterface)\r
174                         blue.setColor(QColor.blue);\r
175                 else\r
176                         blue.setColor(QColor.black);\r
177                 int total=0;\r
178                 \r
179 //              for (int i=0; i<counts.size(); i++) {\r
180 //                      total=total+counts.get(i).getCount();\r
181 //              }\r
182                 \r
183                 int size = books.size();\r
184                 if (Global.mimicEvernoteInterface)\r
185                         size++;\r
186                 \r
187                 for (int i=0; i<size; i++) {\r
188                         child = root.child(i); \r
189                         if (child != null) {\r
190                                 String guid = child.text(2);\r
191                                 child.setText(1,"0");\r
192                                 child.setForeground(0, black);\r
193                                 child.setForeground(1, black);\r
194                                 for (int j=0; j<counts.size(); j++) {\r
195                                         if (counts.get(j).getGuid().equals(guid)) {\r
196                                                 child.setText(1, new Integer(counts.get(j).getCount()).toString());\r
197                                                 total = total+counts.get(j).getCount();\r
198                                                 if (counts.get(j).getCount() > 0) {\r
199                                                         child.setForeground(0, blue);\r
200                                                         child.setForeground(1, blue);\r
201                                                 }\r
202                                         }\r
203 //                                      if (guid.equals("") && Global.mimicEvernoteInterface) {\r
204 //                                              child.setText(1, new Integer(total).toString());\r
205 //                                      }\r
206                                 }\r
207                         }\r
208                 }\r
209                 \r
210                 for (int i=0; i<size; i++) {\r
211                         child = root.child(i); \r
212                         if (child != null) {\r
213                                 String guid = child.text(2);\r
214                                 if (guid.equals("") && Global.mimicEvernoteInterface) \r
215                                         child.setText(1, new Integer(total).toString());\r
216                         }\r
217                 }\r
218         }\r
219         // Return a list of the notebook guids, ordered by the current display order.\r
220         public List<String> getNotebookGuids() {\r
221                 List<String> names = new ArrayList<String>();\r
222                 QTreeWidgetItem root = invisibleRootItem();\r
223                 QTreeWidgetItem child;\r
224                 for (int i=0; i<root.childCount(); i++) {\r
225                         child = root.child(i);\r
226                         String text = child.text(2);\r
227                         names.add(text);\r
228                 }\r
229                 return names;\r
230         }\r
231         \r
232         @Override\r
233         public void contextMenuEvent(QContextMenuEvent event) {\r
234                 QMenu menu = new QMenu(this);\r
235                 menu.addAction(addAction);\r
236                 menu.addAction(editAction);\r
237                 menu.addAction(deleteAction);\r
238                 menu.exec(event.globalPos());\r
239         }\r
240         \r
241         \r
242         @Override\r
243         public void dragEnterEvent(QDragEnterEvent event) {\r
244                 if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
245                         event.accept();\r
246                         return;\r
247                 }\r
248         }\r
249         \r
250         \r
251          @Override\r
252         protected void dragMoveEvent(QDragMoveEvent event) {\r
253                // if (event.mimeData().hasFormat("text/plain") &&\r
254                      //event.answerRect().intersects(dropFrame.geometry()))\r
255                         QTreeWidgetItem treeItem = itemAt(event.pos().x(), event.pos().y());\r
256                         if (treeItem != null) {\r
257 /*                              if (!previousMouseOver.text(0).equalsIgnoreCase(treeItem.text(0))) {\r
258                                         previousMouseOver.setSelected(previousMouseOverWasSelected);\r
259                                         previousMouseOverWasSelected = treeItem.isSelected();\r
260                                         previousMouseOver = treeItem;\r
261                                         blockSignals(true);\r
262                                         treeItem.setSelected(true);\r
263                                         blockSignals(false);\r
264                                 }\r
265 */                              \r
266                         }\r
267                         if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
268                                 if (event.answerRect().intersects(childrenRect()))\r
269                                         event.acceptProposedAction();\r
270                                 return;\r
271                         }\r
272             }\r
273 \r
274         \r
275         @Override\r
276         public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {\r
277                 if (data.hasFormat("application/x-nevernote-note")) {\r
278                         QByteArray d = data.data("application/x-nevernote-note");\r
279                         String s = d.toString();\r
280                         String noteGuidArray[] = s.split(" ");\r
281                         for (String element : noteGuidArray) {\r
282                                 if (!parent.text(0).equalsIgnoreCase("All Notebooks"))\r
283                                         noteSignal.notebookChanged.emit(element.trim(), parent.text(2));\r
284                         }\r
285                         return true;\r
286                 }\r
287                 return false;\r
288         }\r
289 }\r