2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
20 package cx.fbn.nevernote.gui;
\r
22 import java.util.ArrayList;
\r
23 import java.util.HashMap;
\r
24 import java.util.List;
\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 import com.trolltech.qt.gui.QTreeWidgetItem.ChildIndicatorPolicy;
\r
45 import cx.fbn.nevernote.Global;
\r
46 import cx.fbn.nevernote.filters.NotebookCounter;
\r
47 import cx.fbn.nevernote.signals.NoteSignal;
\r
48 import cx.fbn.nevernote.sql.DatabaseConnection;
\r
50 public class NotebookTreeWidget extends QTreeWidget {
\r
51 private QAction deleteAction;
\r
52 private QAction addAction;
\r
53 private QAction editAction;
\r
54 private QAction iconAction;
\r
55 private QAction stackAction;
\r
56 public NoteSignal noteSignal;
\r
57 private HashMap<String, QIcon> icons;
\r
58 private final DatabaseConnection db;
\r
59 private final HashMap<String, QTreeWidgetItem> stacks;
\r
60 // private final QTreeWidgetItem previousMouseOver;
\r
61 // private boolean previousMouseOverWasSelected;
\r
63 public void setAddAction(QAction a) {
\r
67 public void setDeleteAction(QAction d) {
\r
71 public void setEditAction(QAction e) {
\r
75 public void setStackAction(QAction e) {
\r
79 public void setIconAction(QAction e) {
\r
83 public NotebookTreeWidget(DatabaseConnection db) {
\r
84 noteSignal = new NoteSignal();
\r
86 // setProperty("hideTree", true);
\r
87 List<String> labels = new ArrayList<String>();
\r
88 labels.add("Notebooks");
\r
90 setAcceptDrops(true);
\r
91 setDragEnabled(true);
\r
93 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);
\r
94 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);
\r
95 header().setMovable(false);
\r
96 setHeaderLabels(labels);
\r
97 setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);
\r
98 // If we want to mimic Evernote's notebook selection
\r
99 if (Global.mimicEvernoteInterface) {
\r
100 setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection);
\r
102 setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection);
\r
104 stacks = new HashMap<String, QTreeWidgetItem>();
\r
105 // int width = Global.getColumnWidth("notebookTreeName");
\r
107 // setColumnWidth(0, width);
\r
108 // previousMouseOver = new QTreeWidgetItem();
\r
111 public void selectNotebook(QTreeWidgetItem item) {
\r
112 QTreeWidgetItem root = invisibleRootItem();
\r
113 QTreeWidgetItem child;
\r
115 for (int i=0; i<root.childCount(); i++) {
\r
116 child = root.child(i);
\r
117 if (child.text(2).equals(item.text(2))) {
\r
118 child.setSelected(true);
\r
125 public boolean selectGuid(String guid) {
\r
126 QTreeWidgetItem root = invisibleRootItem();
\r
127 QTreeWidgetItem child;
\r
129 for (int i=0; i<root.childCount(); i++) {
\r
130 child = root.child(i);
\r
131 if (child.text(2).equals(guid)) {
\r
132 child.setSelected(true);
\r
139 public void setIcons(HashMap<String, QIcon> i) {
\r
143 public QIcon findDefaultIcon(String guid, String name, List<String> localBooks, boolean isPublished) {
\r
144 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
145 QIcon blueIcon = new QIcon(iconPath+"notebook-blue.png");
\r
146 QIcon greenIcon = new QIcon(iconPath+"notebook-green.png");
\r
147 QIcon redIcon = new QIcon(iconPath+"notebook-red.png");
\r
148 QIcon yellowIcon = new QIcon(iconPath+"notebook-yellow.png");
\r
150 if (localBooks.contains(guid)) {
\r
154 if (localBooks.contains(guid) &&
\r
155 (name.equalsIgnoreCase("Conflicting Changes") ||
\r
156 name.equalsIgnoreCase("Conflicting Changes (Local)")))
\r
164 public void load(List<Notebook> books, List<String> localBooks) {
\r
166 QTreeWidgetItem child;
\r
172 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);
\r
173 for (int i=0; i<books.size(); i++) {
\r
174 book = books.get(i);
\r
175 child = new QTreeWidgetItem();
\r
176 child.setChildIndicatorPolicy(ChildIndicatorPolicy.DontShowIndicatorWhenChildless);
\r
177 child.setText(0, book.getName());
\r
178 if (icons != null && !icons.containsKey(book.getGuid())) {
\r
179 QIcon icon = findDefaultIcon(book.getGuid(), book.getName(), localBooks, book.isPublished());
\r
180 child.setIcon(0, icon);
\r
182 child.setIcon(0, icons.get(book.getGuid()));
\r
184 child.setTextAlignment(1, ra.value());
\r
185 child.setText(2, book.getGuid());
\r
186 if (book.getStack() == null || book.getStack().equalsIgnoreCase(""))
\r
187 addTopLevelItem(child);
\r
189 String stackName = book.getStack();
\r
190 QTreeWidgetItem parent;
\r
191 if (!stacks.containsKey(stackName)) {
\r
192 parent = createStackIcon(stackName, ra);
\r
193 addTopLevelItem(parent);
\r
194 stacks.put(stackName, parent);
\r
196 parent = stacks.get(stackName);
\r
197 parent.addChild(child);
\r
202 sortItems(0, SortOrder.AscendingOrder);
\r
203 if (Global.mimicEvernoteInterface) {
\r
204 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
205 QIcon allIcon = db.getSystemIconTable().getIcon("All Notebooks", "ALLNOTEBOOK");
\r
207 if (allIcon == null)
\r
208 allIcon = new QIcon(iconPath+"notebook-green.png");
\r
210 child = new QTreeWidgetItem();
\r
211 child.setIcon(0, allIcon);
\r
212 child.setText(0, "All Notebooks");
\r
213 child.setText(2, "");
\r
214 child.setTextAlignment(1, ra.value());
\r
215 insertTopLevelItem(0,child);
\r
217 resizeColumnToContents(0);
\r
218 resizeColumnToContents(1);
\r
221 // update the display with the current number of notes
\r
222 public void updateCounts(List<Notebook> books, List<NotebookCounter> counts) {
\r
223 QTreeWidgetItem root = invisibleRootItem();
\r
224 QTreeWidgetItem child;
\r
226 QBrush blue = new QBrush();
\r
227 QBrush black = new QBrush();
\r
228 black.setColor(QColor.black);
\r
229 if (Global.tagBehavior().equalsIgnoreCase("ColorActive") && !Global.mimicEvernoteInterface)
\r
230 blue.setColor(QColor.blue);
\r
232 blue.setColor(QColor.black);
\r
236 int size = books.size();
\r
237 if (Global.mimicEvernoteInterface)
\r
240 for (int i=0; i<size; i++) {
\r
241 child = root.child(i);
\r
242 if (child != null && child.childCount() > 0) {
\r
243 int count = child.childCount();
\r
244 QTreeWidgetItem parent = child;
\r
245 int localTotal = 0;
\r
246 for (int j=0; j<count; j++) {
\r
247 child = parent.child(j);
\r
248 int childCount = updateCounts(child, books, counts, blue, black);
\r
249 total = total+childCount;
\r
250 localTotal = localTotal+childCount;
\r
252 parent.setText(1, new Integer(localTotal).toString());
\r
254 total = total+updateCounts(child, books, counts, blue, black);
\r
257 for (int i=0; i<size; i++) {
\r
258 child = root.child(i);
\r
259 if (child != null) {
\r
260 String guid = child.text(2);
\r
261 if (guid.equals("") && Global.mimicEvernoteInterface)
\r
262 child.setText(1, new Integer(total).toString());
\r
267 private int updateCounts(QTreeWidgetItem child, List<Notebook> books, List<NotebookCounter> counts, QBrush blue, QBrush black) {
\r
269 if (child != null) {
\r
270 String guid = child.text(2);
\r
271 child.setText(1,"0");
\r
272 child.setForeground(0, black);
\r
273 child.setForeground(1, black);
\r
274 for (int j=0; j<counts.size(); j++) {
\r
275 if (counts.get(j).getGuid().equals(guid)) {
\r
276 child.setText(1, new Integer(counts.get(j).getCount()).toString());
\r
277 total = counts.get(j).getCount();
\r
278 if (counts.get(j).getCount() > 0) {
\r
279 child.setForeground(0, blue);
\r
280 child.setForeground(1, blue);
\r
288 // Return a list of the notebook guids, ordered by the current display order.
\r
289 public List<String> getNotebookGuids() {
\r
290 List<String> names = new ArrayList<String>();
\r
291 QTreeWidgetItem root = invisibleRootItem();
\r
292 QTreeWidgetItem child;
\r
293 for (int i=0; i<root.childCount(); i++) {
\r
294 child = root.child(i);
\r
295 String text = child.text(2);
\r
302 public void contextMenuEvent(QContextMenuEvent event) {
\r
303 QMenu menu = new QMenu(this);
\r
304 menu.addAction(addAction);
\r
305 menu.addAction(editAction);
\r
306 menu.addAction(deleteAction);
\r
307 menu.addAction(stackAction);
\r
308 menu.addSeparator();
\r
309 menu.addAction(iconAction);
\r
310 menu.exec(event.globalPos());
\r
315 public void dragEnterEvent(QDragEnterEvent event) {
\r
316 if (event.mimeData().hasFormat("application/x-nevernote-note")) {
\r
320 if (event.source() == this) {
\r
321 event.mimeData().setData("application/x-nevernote-notebook", new QByteArray(currentItem().text(2)));
\r
322 List<QTreeWidgetItem> selected = selectedItems();
\r
323 for (int i=0; i<selected.size(); i++) {
\r
324 if (selected.get(i).text(2).equalsIgnoreCase("STACK") ||
\r
325 selected.get(i).text(2).equals("")) {
\r
338 protected void dragMoveEvent(QDragMoveEvent event) {
\r
339 // if (event.mimeData().hasFormat("text/plain") &&
\r
340 //event.answerRect().intersects(dropFrame.geometry()))
\r
341 QTreeWidgetItem treeItem = itemAt(event.pos().x(), event.pos().y());
\r
342 if (treeItem != null) {
\r
343 /* if (!previousMouseOver.text(0).equalsIgnoreCase(treeItem.text(0))) {
\r
344 previousMouseOver.setSelected(previousMouseOverWasSelected);
\r
345 previousMouseOverWasSelected = treeItem.isSelected();
\r
346 previousMouseOver = treeItem;
\r
347 blockSignals(true);
\r
348 treeItem.setSelected(true);
\r
349 blockSignals(false);
\r
353 if (event.mimeData().hasFormat("application/x-nevernote-note")) {
\r
354 if (event.answerRect().intersects(childrenRect()))
\r
355 event.acceptProposedAction();
\r
362 public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {
\r
363 if (data.hasFormat("application/x-nevernote-notebook")) {
\r
367 if (data.hasFormat("application/x-nevernote-notebook")) {
\r
368 QByteArray d = data.data("application/x-nevernote-notebook");
\r
369 String current = d.toString();
\r
371 // If dropping to the top level, then remove the stack
\r
372 if (parent == null) {
\r
373 db.getNotebookTable().clearStack(current);
\r
377 // If trying to drop under the "All notebooks" then ignore
\r
378 if (parent.text(2).equals(""))
\r
382 // If we are NOT droping directly onto the stack icon
\r
383 // we need to find the stack widget
\r
385 QTreeWidgetItem stackItem;
\r
386 List<QTreeWidgetItem> currentItems = selectedItems();
\r
387 if (!parent.text(2).equalsIgnoreCase("STACK")) {
\r
389 // If a parent stack exists, then use it.
\r
390 if (parent.parent() != null) {
\r
391 stackName = parent.parent().text(0);
\r
392 stackItem = parent.parent();
\r
395 currentItems.add(parent);
\r
396 // If a stack doesn't exist, then we need to create one
\r
397 stackName = "New Stack";
\r
398 // Find a new stack name that isn't in use
\r
399 for (int i=1; i<101; i++) {
\r
400 if (stacks.containsKey(stackName))
\r
401 stackName = "New Stack(" +new Integer(i).toString() + ")";
\r
405 db.getNotebookTable().setStack(parent.text(2), stackName);
\r
406 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);
\r
407 stackItem = createStackIcon(stackName, ra);
\r
408 addTopLevelItem(stackItem);
\r
411 stackName = parent.text(0);
\r
412 stackItem = parent;
\r
415 List<QTreeWidgetItem> newItems = new ArrayList<QTreeWidgetItem>();
\r
416 for (int i=0; i<currentItems.size(); i++) {
\r
417 newItems.add(copyTreeItem(currentItems.get(i)));
\r
418 currentItems.get(i).setHidden(true);
\r
420 db.getNotebookTable().setStack(current, stackName);
\r
421 stackItem.addChildren(newItems);
\r
427 // If we are dropping a note onto a notebook
\r
428 if (data.hasFormat("application/x-nevernote-note")) {
\r
429 QByteArray d = data.data("application/x-nevernote-note");
\r
430 String s = d.toString();
\r
431 String noteGuidArray[] = s.split(" ");
\r
432 for (String element : noteGuidArray) {
\r
433 if (!parent.text(0).equalsIgnoreCase("All Notebooks") &&
\r
434 !parent.text(2).equalsIgnoreCase("STACK"))
\r
435 noteSignal.notebookChanged.emit(element.trim(), parent.text(2));
\r
443 private QTreeWidgetItem createStackIcon(String stackName, Qt.Alignment ra) {
\r
444 String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
446 stackIcon = db.getSystemIconTable().getIcon(stackName, "STACK");
\r
448 if (stackIcon == null)
\r
449 stackIcon = new QIcon(iconPath+"books2.png");
\r
450 QTreeWidgetItem parent = new QTreeWidgetItem();
\r
451 stacks.put(stackName, parent);
\r
452 parent.setText(0, stackName);
\r
453 parent.setIcon(0, stackIcon);
\r
454 parent.setText(2, "STACK");
\r
455 parent.setTextAlignment(1, ra.value());
\r
461 // Copy an individual item within the tree. I need to do this because
\r
462 // Qt doesn't call the dropMimeData on a move, just a copy.
\r
463 private QTreeWidgetItem copyTreeItem(QTreeWidgetItem source) {
\r
464 QTreeWidgetItem target = new QTreeWidgetItem(this);
\r
465 target.setText(0, source.text(0));
\r
466 target.setIcon(0, source.icon(0));
\r
467 target.setText(1, source.text(1));
\r
468 target.setText(2, source.text(2));
\r
469 Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);
\r
470 target.setTextAlignment(1, ra.value());
\r
471 source.setHidden(true);
\r