OSDN Git Service

- Gui cleanup - Rework count threads to reduce SQL overhead & improve performance.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / SavedSearchTreeWidget.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.SavedSearch;\r
27 import com.trolltech.qt.core.Qt.SortOrder;\r
28 import com.trolltech.qt.gui.QAbstractItemView;\r
29 import com.trolltech.qt.gui.QAction;\r
30 import com.trolltech.qt.gui.QContextMenuEvent;\r
31 import com.trolltech.qt.gui.QIcon;\r
32 import com.trolltech.qt.gui.QMenu;\r
33 import com.trolltech.qt.gui.QTreeWidget;\r
34 import com.trolltech.qt.gui.QTreeWidgetItem;\r
35 \r
36 public class SavedSearchTreeWidget extends QTreeWidget {\r
37         private QAction editAction;\r
38         private QAction deleteAction;\r
39         private QAction addAction;\r
40         private QAction iconAction;\r
41         private HashMap<String, QIcon>  icons;\r
42         \r
43         \r
44         public SavedSearchTreeWidget() {\r
45 //              setAcceptDrops(true);\r
46 //              setDragEnabled(true);\r
47                 setProperty("hideTree", true);\r
48                 header().setStyleSheet("QHeaderView::section {border: 0.0em;}");\r
49                 setAcceptDrops(false);\r
50                 setDragEnabled(false);\r
51 //              setDragDropMode(QAbstractItemView.DragDropMode.DragDrop);\r
52         setHeaderLabel(tr("Saved Searches"));\r
53         setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
54         }\r
55         \r
56         public void setEditAction(QAction e) {\r
57                 editAction = e;\r
58         }\r
59         public void setDeleteAction(QAction d) {\r
60                 deleteAction = d;\r
61         }\r
62         public void setAddAction(QAction a) {\r
63                 addAction = a;\r
64         }\r
65         public void setIconAction(QAction a) {\r
66                 iconAction = a;\r
67         }\r
68         public void setIcons(HashMap<String, QIcon> i) {\r
69                 icons = i;\r
70         }\r
71         \r
72         public void load(List<SavedSearch> tempList) {\r
73         SavedSearch search;\r
74         List<NTreeWidgetItem> index = new ArrayList<NTreeWidgetItem>();\r
75                 \r
76         //Clear out the tree & reload\r
77         clear();\r
78         String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
79                 QIcon icon = new QIcon(iconPath+"search.png");\r
80         \r
81                 for (int i=0; i<tempList.size(); i++) {\r
82                         search = tempList.get(i);\r
83                         NTreeWidgetItem child = new NTreeWidgetItem();\r
84                         child.setText(0, search.getName());\r
85                 if (icons != null && !icons.containsKey(search.getGuid())) {\r
86                         child.setIcon(0, icon);\r
87                     } else {\r
88                         child.setIcon(0, icons.get(search.getGuid()));\r
89                         }\r
90                         child.setText(1, search.getGuid());\r
91                         index.add(child);\r
92                         addTopLevelItem(child);\r
93                 } \r
94         sortItems(0, SortOrder.AscendingOrder);\r
95         }\r
96 \r
97         \r
98         public boolean selectGuid(String guid) {\r
99                 QTreeWidgetItem root = invisibleRootItem();\r
100                 QTreeWidgetItem child;\r
101 \r
102                 for (int i=0; i<root.childCount(); i++) {\r
103                         child = root.child(i);\r
104                         if (child.text(1).equals(guid)) {\r
105                                 child.setSelected(true);\r
106                                 return true;\r
107                         }\r
108                 }\r
109                 return false;\r
110         }\r
111         \r
112         \r
113         @Override\r
114         public void contextMenuEvent(QContextMenuEvent event) {\r
115                 QMenu menu = new QMenu(this);\r
116                 menu.addAction(addAction);\r
117                 menu.addAction(editAction);\r
118                 menu.addAction(deleteAction);\r
119                 menu.addSeparator();\r
120                 menu.addAction(iconAction);\r
121                 menu.exec(event.globalPos());\r
122         }\r
123         \r
124         \r
125         public void selectSavedSearch(QTreeWidgetItem item) {\r
126                 QTreeWidgetItem root = invisibleRootItem();\r
127                 QTreeWidgetItem child;\r
128                 \r
129                 for (int i=0; i<root.childCount(); i++) {\r
130                         child = root.child(i); \r
131                         if (child.text(1).equals(item.text(1))) {\r
132                                 child.setSelected(true);\r
133                                 return;\r
134                         }\r
135                 }\r
136         }\r
137 }\r