OSDN Git Service

Add the ability to add & remove notebook stacks.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / NotebookTreeWidget.java
index 86a645c..5fcd3a1 100644 (file)
@@ -45,14 +45,18 @@ import com.trolltech.qt.gui.QTreeWidgetItem.ChildIndicatorPolicy;
 import cx.fbn.nevernote.Global;\r
 import cx.fbn.nevernote.filters.NotebookCounter;\r
 import cx.fbn.nevernote.signals.NoteSignal;\r
+import cx.fbn.nevernote.sql.DatabaseConnection;\r
 \r
 public class NotebookTreeWidget extends QTreeWidget {\r
        private QAction                                 deleteAction;\r
        private QAction                                 addAction;\r
        private QAction                                 editAction;\r
        private QAction                                 iconAction;\r
+       private QAction                                 stackAction;\r
        public NoteSignal                               noteSignal;\r
        private HashMap<String, QIcon>  icons;\r
+       private final DatabaseConnection                db;\r
+       private List<NotebookCounter> lastCounts;\r
        private final HashMap<String, QTreeWidgetItem>  stacks;\r
 //     private final QTreeWidgetItem                   previousMouseOver;\r
 //     private boolean                                 previousMouseOverWasSelected;\r
@@ -69,12 +73,17 @@ public class NotebookTreeWidget extends QTreeWidget {
                editAction = e;\r
        }\r
        \r
+       public void setStackAction(QAction e) {\r
+               stackAction = e;\r
+       }\r
+       \r
        public void setIconAction(QAction e) {\r
                iconAction = e;\r
        }\r
        \r
-       public NotebookTreeWidget() {\r
+       public NotebookTreeWidget(DatabaseConnection db) {\r
                noteSignal = new NoteSignal();\r
+               this.db = db;\r
 //             setProperty("hideTree", true);\r
                List<String> labels = new ArrayList<String>();\r
                labels.add("Notebooks");\r
@@ -157,6 +166,7 @@ public class NotebookTreeWidget extends QTreeWidget {
        Notebook book;\r
        QTreeWidgetItem child;\r
        clear();\r
+       stacks.clear();\r
        \r
        if (books == null)\r
                return;\r
@@ -180,15 +190,9 @@ public class NotebookTreeWidget extends QTreeWidget {
                        String stackName = book.getStack();\r
                        QTreeWidgetItem parent;\r
                        if (!stacks.containsKey(stackName)) {\r
-                               String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
-                       QIcon stackIcon = new QIcon(iconPath+"books2.png");\r
-                               parent = new QTreeWidgetItem();\r
-                               stacks.put(stackName, parent);\r
-                               parent.setText(0, stackName);\r
-                               parent.setIcon(0, stackIcon);\r
-                               parent.setText(2, "STACK");\r
-                               parent.setTextAlignment(1, ra.value());\r
+                               parent = createStackIcon(stackName, ra);\r
                                addTopLevelItem(parent);\r
+                               stacks.put(stackName, parent);\r
                        } else\r
                                parent = stacks.get(stackName);\r
                        parent.addChild(child);\r
@@ -214,9 +218,9 @@ public class NotebookTreeWidget extends QTreeWidget {
 \r
        // update the display with the current number of notes\r
        public void updateCounts(List<Notebook> books, List<NotebookCounter> counts) {\r
+               lastCounts = counts;\r
                QTreeWidgetItem root = invisibleRootItem();\r
                QTreeWidgetItem child;\r
-               HashMap<String, Integer> stackCounts = new HashMap<String, Integer>();\r
                \r
                QBrush blue = new QBrush();\r
                QBrush black = new QBrush();\r
@@ -299,6 +303,7 @@ public class NotebookTreeWidget extends QTreeWidget {
                menu.addAction(addAction);\r
                menu.addAction(editAction);\r
                menu.addAction(deleteAction);\r
+               menu.addAction(stackAction);\r
                menu.addSeparator();\r
                menu.addAction(iconAction);\r
                menu.exec(event.globalPos());\r
@@ -311,6 +316,20 @@ public class NotebookTreeWidget extends QTreeWidget {
                        event.accept();\r
                        return;\r
                }\r
+               if (event.source() == this) {\r
+                       event.mimeData().setData("application/x-nevernote-notebook", new QByteArray(currentItem().text(2)));\r
+                       List<QTreeWidgetItem> selected = selectedItems();\r
+                       for (int i=0; i<selected.size(); i++) {\r
+                               if (selected.get(i).text(2).equalsIgnoreCase("STACK") || \r
+                                       selected.get(i).text(2).equals("")) {\r
+                                               event.ignore();\r
+                                               return;\r
+                                       }\r
+                       }\r
+                       event.accept();\r
+                       return;\r
+               }\r
+               event.ignore();\r
        }\r
        \r
        \r
@@ -340,16 +359,113 @@ public class NotebookTreeWidget extends QTreeWidget {
        \r
        @Override\r
        public boolean dropMimeData(QTreeWidgetItem parent, int index, QMimeData data, Qt.DropAction action) {\r
+               if (data.hasFormat("application/x-nevernote-notebook")) {\r
+                       return false;\r
+               }\r
+               \r
+               if (data.hasFormat("application/x-nevernote-notebook")) {\r
+                       QByteArray d = data.data("application/x-nevernote-notebook");\r
+                       String current = d.toString();\r
+                       \r
+                       // If dropping to the top level, then remove the stack\r
+                       if (parent == null) {\r
+                               db.getNotebookTable().clearStack(current);\r
+                               return true;\r
+                       } \r
+                       \r
+                       // If trying to drop under the "All notebooks" then ignore\r
+                       if (parent.text(2).equals(""))\r
+                               return false;\r
+                       \r
+                       \r
+                       // If we are NOT droping directly onto the stack icon\r
+                       // we need to find the stack widget\r
+                       String stackName;\r
+                       QTreeWidgetItem stackItem;\r
+                       List<QTreeWidgetItem> currentItems = selectedItems();\r
+                       if (!parent.text(2).equalsIgnoreCase("STACK")) {\r
+                               \r
+                               // If a parent stack exists, then use it.\r
+                               if (parent.parent() != null) {\r
+                                       stackName = parent.parent().text(0);\r
+                                       stackItem = parent.parent();\r
+                               } else {\r
+                                       \r
+                                       currentItems.add(parent);\r
+                                       // If a stack doesn't exist, then we need to create one\r
+                                       stackName = "New Stack";        \r
+                                       // Find a new stack name that isn't in use\r
+                                       for (int i=1; i<101; i++) {\r
+                                               if (stacks.containsKey(stackName))\r
+                                                       stackName = "New Stack(" +new Integer(i).toString() + ")";\r
+                                               else\r
+                                                       break;\r
+                                       }\r
+                                       db.getNotebookTable().setStack(parent.text(2), stackName);\r
+                                       Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
+                                       stackItem = createStackIcon(stackName, ra);\r
+                                       addTopLevelItem(stackItem);\r
+                               }\r
+                       } else {\r
+                               stackName = parent.text(0);\r
+                               stackItem = parent;\r
+                       }\r
+                       \r
+                       List<QTreeWidgetItem> newItems = new ArrayList<QTreeWidgetItem>();\r
+                       for (int i=0; i<currentItems.size(); i++) {\r
+                               newItems.add(copyTreeItem(currentItems.get(i)));\r
+                               currentItems.get(i).setHidden(true);\r
+                       }\r
+                       db.getNotebookTable().setStack(current, stackName);             \r
+                       stackItem.addChildren(newItems);\r
+                       \r
+                       return true;\r
+               }\r
+               \r
+               \r
+               // If we are dropping a note onto a notebook\r
                if (data.hasFormat("application/x-nevernote-note")) {\r
                        QByteArray d = data.data("application/x-nevernote-note");\r
                        String s = d.toString();\r
                        String noteGuidArray[] = s.split(" ");\r
                        for (String element : noteGuidArray) {\r
-                               if (!parent.text(0).equalsIgnoreCase("All Notebooks"))\r
+                               if (!parent.text(0).equalsIgnoreCase("All Notebooks") && \r
+                                               !parent.text(2).equalsIgnoreCase("STACK"))\r
                                        noteSignal.notebookChanged.emit(element.trim(), parent.text(2));\r
                        }\r
                        return true;\r
                }\r
                return false;\r
        }\r
+       \r
+\r
+       private QTreeWidgetItem createStackIcon(String stackName, Qt.Alignment ra) {\r
+               String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
+               QIcon stackIcon = new QIcon(iconPath+"books2.png");\r
+               QTreeWidgetItem parent = new QTreeWidgetItem();\r
+               stacks.put(stackName, parent);\r
+               parent.setText(0, stackName);\r
+               parent.setIcon(0, stackIcon);\r
+               parent.setText(2, "STACK");\r
+               parent.setTextAlignment(1, ra.value());\r
+               return parent;\r
+       }\r
+\r
+       \r
+       \r
+       // Copy an individual item within the tree.  I need to do this because\r
+       // Qt doesn't call the dropMimeData on a move, just a copy.\r
+       private QTreeWidgetItem copyTreeItem(QTreeWidgetItem source) {\r
+               QTreeWidgetItem target = new QTreeWidgetItem(this);\r
+               target.setText(0, source.text(0));\r
+               target.setIcon(0, source.icon(0));\r
+               target.setText(1, source.text(1));\r
+               target.setText(2, source.text(2));\r
+               Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
+               target.setTextAlignment(1, ra.value());\r
+               source.setHidden(true);\r
+\r
+               return target;\r
+       }\r
+\r
 }\r