OSDN Git Service

fcd9222b8785ae153d6906bff8d44b32ae4216a8
[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                 setProperty("hideTree", true);\r
70                 List<String> labels = new ArrayList<String>();\r
71                 labels.add("Notebooks");\r
72                 labels.add("");\r
73                 setAcceptDrops(true);\r
74                 setDragEnabled(true);\r
75                 setColumnCount(2);\r
76                 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);\r
77                 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);\r
78                 header().setMovable(false);\r
79                 setHeaderLabels(labels);\r
80                 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);\r
81                 // If we want to mimic Evernote's notebook selection\r
82                 if (Global.mimicEvernoteInterface) {\r
83                         setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection);\r
84                 } else\r
85                         setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
86 \r
87 //      int width = Global.getColumnWidth("notebookTreeName");\r
88 //              if (width>0)\r
89 //                      setColumnWidth(0, width);\r
90 //              previousMouseOver = new QTreeWidgetItem();\r
91         }\r
92         \r
93         public void selectNotebook(QTreeWidgetItem item) {\r
94                 QTreeWidgetItem root = invisibleRootItem();\r
95                 QTreeWidgetItem child;\r
96                 \r
97                 for (int i=0; i<root.childCount(); i++) {\r
98                         child = root.child(i); \r
99                         if (child.text(2).equals(item.text(2))) {\r
100                                 child.setSelected(true);\r
101                                 return;\r
102                         }\r
103                 }\r
104                 \r
105         }\r
106         \r
107         public boolean selectGuid(String guid) {\r
108                 QTreeWidgetItem root = invisibleRootItem();\r
109                 QTreeWidgetItem child;\r
110 \r
111                 for (int i=0; i<root.childCount(); i++) {\r
112                         child = root.child(i);\r
113                         if (child.text(2).equals(guid)) {\r
114                                 child.setSelected(true);\r
115                                 return true;\r
116                         }\r
117                 }\r
118                 return false;\r
119         }\r
120         \r
121         \r
122         public void load(List<Notebook> books, List<String> localBooks) {\r
123         Notebook book;\r
124         QTreeWidgetItem child;\r
125         clear();\r
126         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
127         QIcon blueIcon = new QIcon(iconPath+"notebook-blue.png");\r
128         QIcon greenIcon = new QIcon(iconPath+"notebook-green.png");\r
129         QIcon redIcon = new QIcon(iconPath+"notebook-red.png");\r
130         QIcon yellowIcon = new QIcon(iconPath+"notebook-yellow.png");\r
131         \r
132         if (books == null)\r
133                 return;\r
134         Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
135         for (int i=0; i<books.size(); i++) {\r
136                 book = books.get(i);\r
137                 child = new QTreeWidgetItem();\r
138                 child.setText(0, book.getName());\r
139                 child.setIcon(0, greenIcon);\r
140                 if (localBooks.contains(book.getGuid()))\r
141                         child.setIcon(0, yellowIcon);\r
142                 if (localBooks.contains(book.getGuid()) && \r
143                                 (book.getName().equalsIgnoreCase("Conflicting Changes") ||\r
144                                  book.getName().equalsIgnoreCase("Conflicting Changes (Local)")))\r
145                                         child.setIcon(0, redIcon);\r
146                 if (book.isPublished())\r
147                         child.setIcon(0, blueIcon);\r
148                 child.setTextAlignment(1, ra.value());\r
149                 child.setText(2, book.getGuid());\r
150                 addTopLevelItem(child);\r
151         }\r
152 \r
153         sortItems(0, SortOrder.AscendingOrder); \r
154         if (Global.mimicEvernoteInterface) {\r
155                 child = new QTreeWidgetItem();\r
156                 child.setIcon(0, greenIcon);\r
157                 child.setText(0, "All Notebooks");\r
158 //              child.setText(1, "0");\r
159                 child.setText(2, "");\r
160                 child.setTextAlignment(1, ra.value());\r
161                 insertTopLevelItem(0,child);\r
162         }\r
163         resizeColumnToContents(0);\r
164         resizeColumnToContents(1);\r
165         }\r
166         // update the display with the current number of notes\r
167         public void updateCounts(List<Notebook> books, List<NotebookCounter> counts) {\r
168                 QTreeWidgetItem root = invisibleRootItem();\r
169                 QTreeWidgetItem child;\r
170                 \r
171                 QBrush blue = new QBrush();\r
172                 QBrush black = new QBrush();\r
173                 black.setColor(QColor.black);\r
174                 if (Global.tagBehavior().equalsIgnoreCase("ColorActive") && !Global.mimicEvernoteInterface)\r
175                         blue.setColor(QColor.blue);\r
176                 else\r
177                         blue.setColor(QColor.black);\r
178                 int total=0;\r
179                 \r
180 //              for (int i=0; i<counts.size(); i++) {\r
181 //                      total=total+counts.get(i).getCount();\r
182 //              }\r
183                 \r
184                 int size = books.size();\r
185                 if (Global.mimicEvernoteInterface)\r
186                         size++;\r
187                 \r
188                 for (int i=0; i<size; i++) {\r
189                         child = root.child(i); \r
190                         if (child != null) {\r
191                                 String guid = child.text(2);\r
192                                 child.setText(1,"0");\r
193                                 child.setForeground(0, black);\r
194                                 child.setForeground(1, black);\r
195                                 for (int j=0; j<counts.size(); j++) {\r
196                                         if (counts.get(j).getGuid().equals(guid)) {\r
197                                                 child.setText(1, new Integer(counts.get(j).getCount()).toString());\r
198                                                 total = total+counts.get(j).getCount();\r
199                                                 if (counts.get(j).getCount() > 0) {\r
200                                                         child.setForeground(0, blue);\r
201                                                         child.setForeground(1, blue);\r
202                                                 }\r
203                                         }\r
204 //                                      if (guid.equals("") && Global.mimicEvernoteInterface) {\r
205 //                                              child.setText(1, new Integer(total).toString());\r
206 //                                      }\r
207                                 }\r
208                         }\r
209                 }\r
210                 \r
211                 for (int i=0; i<size; i++) {\r
212                         child = root.child(i); \r
213                         if (child != null) {\r
214                                 String guid = child.text(2);\r
215                                 if (guid.equals("") && Global.mimicEvernoteInterface) \r
216                                         child.setText(1, new Integer(total).toString());\r
217                         }\r
218                 }\r
219         }\r
220         // Return a list of the notebook guids, ordered by the current display order.\r
221         public List<String> getNotebookGuids() {\r
222                 List<String> names = new ArrayList<String>();\r
223                 QTreeWidgetItem root = invisibleRootItem();\r
224                 QTreeWidgetItem child;\r
225                 for (int i=0; i<root.childCount(); i++) {\r
226                         child = root.child(i);\r
227                         String text = child.text(2);\r
228                         names.add(text);\r
229                 }\r
230                 return names;\r
231         }\r
232         \r
233         @Override\r
234         public void contextMenuEvent(QContextMenuEvent event) {\r
235                 QMenu menu = new QMenu(this);\r
236                 menu.addAction(addAction);\r
237                 menu.addAction(editAction);\r
238                 menu.addAction(deleteAction);\r
239                 menu.exec(event.globalPos());\r
240         }\r
241         \r
242         \r
243         @Override\r
244         public void dragEnterEvent(QDragEnterEvent event) {\r
245                 if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
246                         event.accept();\r
247                         return;\r
248                 }\r
249         }\r
250         \r
251         \r
252          @Override\r
253         protected void dragMoveEvent(QDragMoveEvent event) {\r
254                // if (event.mimeData().hasFormat("text/plain") &&\r
255                      //event.answerRect().intersects(dropFrame.geometry()))\r
256                         QTreeWidgetItem treeItem = itemAt(event.pos().x(), event.pos().y());\r
257                         if (treeItem != null) {\r
258 /*                              if (!previousMouseOver.text(0).equalsIgnoreCase(treeItem.text(0))) {\r
259                                         previousMouseOver.setSelected(previousMouseOverWasSelected);\r
260                                         previousMouseOverWasSelected = treeItem.isSelected();\r
261                                         previousMouseOver = treeItem;\r
262                                         blockSignals(true);\r
263                                         treeItem.setSelected(true);\r
264                                         blockSignals(false);\r
265                                 }\r
266 */                              \r
267                         }\r
268                         if (event.mimeData().hasFormat("application/x-nevernote-note")) {\r
269                                 if (event.answerRect().intersects(childrenRect()))\r
270                                         event.acceptProposedAction();\r
271                                 return;\r
272                         }\r
273             }\r
274 \r
275         \r
276         @Override\r
277         public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {\r
278                 if (data.hasFormat("application/x-nevernote-note")) {\r
279                         QByteArray d = data.data("application/x-nevernote-note");\r
280                         String s = d.toString();\r
281                         String noteGuidArray[] = s.split(" ");\r
282                         for (String element : noteGuidArray) {\r
283                                 if (!parent.text(0).equalsIgnoreCase("All Notebooks"))\r
284                                         noteSignal.notebookChanged.emit(element.trim(), parent.text(2));\r
285                         }\r
286                         return true;\r
287                 }\r
288                 return false;\r
289         }\r
290 }\r