OSDN Git Service

Add logic to display stacks in notebook tree
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / NotebookTreeWidget.java
index c0a172f..86a645c 100644 (file)
@@ -40,6 +40,7 @@ import com.trolltech.qt.gui.QIcon;
 import com.trolltech.qt.gui.QMenu;\r
 import com.trolltech.qt.gui.QTreeWidget;\r
 import com.trolltech.qt.gui.QTreeWidgetItem;\r
+import com.trolltech.qt.gui.QTreeWidgetItem.ChildIndicatorPolicy;\r
 \r
 import cx.fbn.nevernote.Global;\r
 import cx.fbn.nevernote.filters.NotebookCounter;\r
@@ -52,6 +53,7 @@ public class NotebookTreeWidget extends QTreeWidget {
        private QAction                                 iconAction;\r
        public NoteSignal                               noteSignal;\r
        private HashMap<String, QIcon>  icons;\r
+       private final HashMap<String, QTreeWidgetItem>  stacks;\r
 //     private final QTreeWidgetItem                   previousMouseOver;\r
 //     private boolean                                 previousMouseOverWasSelected;\r
        \r
@@ -73,7 +75,7 @@ public class NotebookTreeWidget extends QTreeWidget {
        \r
        public NotebookTreeWidget() {\r
                noteSignal = new NoteSignal();\r
-               setProperty("hideTree", true);\r
+//             setProperty("hideTree", true);\r
                List<String> labels = new ArrayList<String>();\r
                labels.add("Notebooks");\r
                labels.add("");\r
@@ -91,6 +93,7 @@ public class NotebookTreeWidget extends QTreeWidget {
                } else\r
                        setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection);\r
 \r
+               stacks = new HashMap<String, QTreeWidgetItem>();\r
 //     int width = Global.getColumnWidth("notebookTreeName");\r
 //             if (width>0)\r
 //                     setColumnWidth(0, width);\r
@@ -161,6 +164,7 @@ public class NotebookTreeWidget extends QTreeWidget {
        for (int i=0; i<books.size(); i++) {\r
                        book = books.get(i);\r
                        child = new QTreeWidgetItem();\r
+                       child.setChildIndicatorPolicy(ChildIndicatorPolicy.DontShowIndicatorWhenChildless);\r
                        child.setText(0, book.getName());\r
                if (icons != null && !icons.containsKey(book.getGuid())) {\r
                        QIcon icon = findDefaultIcon(book.getGuid(), book.getName(), localBooks, book.isPublished());\r
@@ -170,7 +174,26 @@ public class NotebookTreeWidget extends QTreeWidget {
                }\r
                child.setTextAlignment(1, ra.value());\r
                child.setText(2, book.getGuid());\r
-               addTopLevelItem(child);\r
+               if (book.getStack() == null || book.getStack().equalsIgnoreCase(""))\r
+                       addTopLevelItem(child); \r
+               else {\r
+                       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
+                               addTopLevelItem(parent);\r
+                       } else\r
+                               parent = stacks.get(stackName);\r
+                       parent.addChild(child);\r
+                       \r
+               }\r
        }\r
 \r
        sortItems(0, SortOrder.AscendingOrder); \r
@@ -181,7 +204,6 @@ public class NotebookTreeWidget extends QTreeWidget {
                child = new QTreeWidgetItem();\r
                child.setIcon(0, greenIcon);\r
                child.setText(0, "All Notebooks");\r
-//             child.setText(1, "0");\r
                child.setText(2, "");\r
                child.setTextAlignment(1, ra.value());\r
                insertTopLevelItem(0,child);\r
@@ -189,10 +211,12 @@ public class NotebookTreeWidget extends QTreeWidget {
        resizeColumnToContents(0);\r
        resizeColumnToContents(1);\r
        }\r
+\r
        // update the display with the current number of notes\r
        public void updateCounts(List<Notebook> books, List<NotebookCounter> 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
@@ -203,35 +227,26 @@ public class NotebookTreeWidget extends QTreeWidget {
                        blue.setColor(QColor.black);\r
                int total=0;\r
                \r
-//             for (int i=0; i<counts.size(); i++) {\r
-//                     total=total+counts.get(i).getCount();\r
-//             }\r
                \r
                int size = books.size();\r
                if (Global.mimicEvernoteInterface)\r
                        size++;\r
                \r
                for (int i=0; i<size; i++) {\r
-                       child = root.child(i); \r
-                       if (child != null) {\r
-                               String guid = child.text(2);\r
-                               child.setText(1,"0");\r
-                               child.setForeground(0, black);\r
-                               child.setForeground(1, black);\r
-                               for (int j=0; j<counts.size(); j++) {\r
-                                       if (counts.get(j).getGuid().equals(guid)) {\r
-                                               child.setText(1, new Integer(counts.get(j).getCount()).toString());\r
-                                               total = total+counts.get(j).getCount();\r
-                                               if (counts.get(j).getCount() > 0) {\r
-                                                       child.setForeground(0, blue);\r
-                                                       child.setForeground(1, blue);\r
-                                               }\r
-                                       }\r
-//                                     if (guid.equals("") && Global.mimicEvernoteInterface) {\r
-//                                             child.setText(1, new Integer(total).toString());\r
-//                                     }\r
+                       child = root.child(i);\r
+                       if (child != null && child.childCount() > 0) {\r
+                               int count = child.childCount();\r
+                               QTreeWidgetItem parent = child;\r
+                               int localTotal = 0;\r
+                               for (int j=0; j<count; j++) {\r
+                                       child = parent.child(j);\r
+                                       int childCount = updateCounts(child, books, counts, blue, black);\r
+                                       total = total+childCount;\r
+                                       localTotal = localTotal+childCount;\r
                                }\r
-                       }\r
+                               parent.setText(1, new Integer(localTotal).toString());\r
+                       } else\r
+                               total = total+updateCounts(child, books, counts, blue, black);\r
                }\r
                \r
                for (int i=0; i<size; i++) {\r
@@ -243,6 +258,28 @@ public class NotebookTreeWidget extends QTreeWidget {
                        }\r
                }\r
        }\r
+       \r
+       private int updateCounts(QTreeWidgetItem child, List<Notebook> books, List<NotebookCounter> counts, QBrush blue, QBrush black) {\r
+               int total=0;\r
+               if (child != null) {\r
+                       String guid = child.text(2);\r
+                       child.setText(1,"0");\r
+                       child.setForeground(0, black);\r
+                       child.setForeground(1, black);\r
+                       for (int j=0; j<counts.size(); j++) {\r
+                               if (counts.get(j).getGuid().equals(guid)) {\r
+                                       child.setText(1, new Integer(counts.get(j).getCount()).toString());\r
+                                       total = counts.get(j).getCount();\r
+                                       if (counts.get(j).getCount() > 0) {\r
+                                               child.setForeground(0, blue);\r
+                                               child.setForeground(1, blue);\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               return total;\r
+       }\r
+       \r
        // Return a list of the notebook guids, ordered by the current display order.\r
        public List<String> getNotebookGuids() {\r
                List<String> names = new ArrayList<String>();\r