OSDN Git Service

Retain expanded state of notebooks & tags across sync.
authorRandy Baumgarte <randy@fbn.cx>
Thu, 27 Jan 2011 16:53:12 +0000 (11:53 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Sun, 30 Jan 2011 13:37:23 +0000 (08:37 -0500)
src/cx/fbn/nevernote/gui/NotebookTreeWidget.java
src/cx/fbn/nevernote/gui/TagTreeWidget.java

index 807802c..e46baaa 100644 (file)
@@ -184,6 +184,15 @@ public class NotebookTreeWidget extends QTreeWidget {
        public void load(List<Notebook> books, List<String> localBooks) {\r
        Notebook book;\r
        QTreeWidgetItem child;\r
+       \r
+       /* First, let's find out which stacks are expanded */\r
+       QTreeWidgetItem root =  invisibleRootItem();\r
+       List<String> expandedStacks = new ArrayList<String>();\r
+       for (int i=0; i<root.childCount(); i++) {\r
+               if (root.child(i).isExpanded())\r
+                       expandedStacks.add(root.child(i).text(0));\r
+       }\r
+       \r
        clear();\r
        stacks.clear();\r
        \r
@@ -236,8 +245,20 @@ public class NotebookTreeWidget extends QTreeWidget {
        }\r
        resizeColumnToContents(0);\r
        resizeColumnToContents(1);\r
+       \r
+       // Finally, expand the stacks back out\r
+       root = invisibleRootItem();\r
+       for (int i=0; i<root.childCount(); i++) {\r
+               for (int j=0; j<expandedStacks.size(); j++) {\r
+                       if (root.child(i).text(0).equalsIgnoreCase(expandedStacks.get(j))) {\r
+                               expandItem(root.child(i));\r
+                               j=expandedStacks.size();\r
+                       }\r
+               }\r
+       }\r
        }\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
index ccee2a8..5cc0896 100644 (file)
@@ -143,11 +143,44 @@ public class TagTreeWidget extends QTreeWidget {
                        return linkedIcon;\r
        }\r
        \r
+       List<String> findExpandedTags(QTreeWidgetItem item) {\r
+               List<String> list = new ArrayList<String>();\r
+               if (item.isExpanded()) \r
+                       list.add(item.text(0));\r
+               for (int i=0; i<item.childCount(); i++) {\r
+                       List<String> childrenList = findExpandedTags(item.child(i));\r
+                       for (int j=0; j<childrenList.size(); j++) {\r
+                               list.add(childrenList.get(j));\r
+                       }\r
+               }\r
+               \r
+               return list;\r
+       }\r
+       \r
+       void expandTags(QTreeWidgetItem item, List<String> expandedTags) {\r
+               for (int i=0; i<item.childCount(); i++) {\r
+                       expandTags(item.child(i), expandedTags);\r
+               }\r
+               \r
+               for (int i=0; i<expandedTags.size(); i++) {\r
+                       if (expandedTags.get(i).equalsIgnoreCase(item.text(0))) {\r
+                               expandItem(item);\r
+                               i=expandedTags.size();\r
+                       }\r
+               }\r
+       }\r
+       \r
        public void load(List<Tag> tags) {\r
        Tag tag;\r
        List<QTreeWidgetItem> index = new ArrayList<QTreeWidgetItem>();\r
        QTreeWidgetItem child;\r
                        \r
+       /* First, let's find out which stacks are expanded */\r
+       QTreeWidgetItem root =  invisibleRootItem();\r
+       List<String> expandedTags = findExpandedTags(root);\r
+\r
+\r
+       \r
        //Clear out the tree & reload\r
        clear();\r
        String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
@@ -203,6 +236,8 @@ public class TagTreeWidget extends QTreeWidget {
        resizeColumnToContents(0);\r
        resizeColumnToContents(1);\r
        sortItems(0, SortOrder.AscendingOrder);\r
+       \r
+       expandTags(invisibleRootItem(), expandedTags);\r
        }\r
        // Show (unhide) all tags\r
        public void showAllTags(boolean value) {\r