OSDN Git Service

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