OSDN Git Service

520f3e5ac4cf531b4489b9c9b993ec3aadc2382e
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TrashTreeWidget.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.List;\r
24 \r
25 import com.trolltech.qt.core.QByteArray;\r
26 import com.trolltech.qt.core.Qt;\r
27 import com.trolltech.qt.gui.QAbstractItemView;\r
28 import com.trolltech.qt.gui.QAction;\r
29 import com.trolltech.qt.gui.QBrush;\r
30 import com.trolltech.qt.gui.QColor;\r
31 import com.trolltech.qt.gui.QContextMenuEvent;\r
32 import com.trolltech.qt.gui.QDragEnterEvent;\r
33 import com.trolltech.qt.gui.QHeaderView;\r
34 import com.trolltech.qt.gui.QIcon;\r
35 import com.trolltech.qt.gui.QMenu;\r
36 import com.trolltech.qt.gui.QTreeWidget;\r
37 import com.trolltech.qt.gui.QTreeWidgetItem;\r
38 \r
39 public class TrashTreeWidget extends QTreeWidget {\r
40         private QAction emptyAction;\r
41         private QTreeWidgetItem trashItem;\r
42         private Integer trashCount;     \r
43         private final String iconPath;\r
44         private final QIcon trashIcon;\r
45         private final QIcon trashFullIcon;\r
46         \r
47         public void setEmptyAction(QAction a) {\r
48                 emptyAction = a;\r
49         }\r
50         \r
51         \r
52         public TrashTreeWidget() {\r
53         trashCount =  0;\r
54         setProperty("hideTree", true);\r
55         iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
56         trashIcon = new QIcon(iconPath+"trash.png");\r
57         trashFullIcon = new QIcon(iconPath+"trash-full.png");\r
58         header().hide();\r
59         setMaximumHeight(30);\r
60         setMinimumHeight(30);\r
61         }\r
62         \r
63         public void updateCounts(Integer cnt) {\r
64                 QBrush gray = new QBrush();\r
65                 gray.setColor(QColor.gray);\r
66                 QBrush black = new QBrush();\r
67                 black.setColor(QColor.black);\r
68                 \r
69                 trashCount = cnt;\r
70                 trashItem.setText(1, trashCount.toString());\r
71                 header().resizeSection(1, 0);\r
72                 if (trashCount > 0) {\r
73                         trashItem.setForeground(0, black);                      \r
74                         trashItem.setForeground(1, black);\r
75                         trashItem.setIcon(0, trashFullIcon);\r
76                 } else {\r
77                         trashItem.setIcon(0,trashIcon);\r
78                         trashItem.setForeground(0, gray);                       \r
79                         trashItem.setForeground(1, gray);                                               \r
80                 }\r
81         }\r
82         \r
83         public void load() {\r
84         trashItem = new QTreeWidgetItem();\r
85         trashItem.setIcon(0, trashIcon);\r
86         trashItem.setText(0, "Trash");\r
87         Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
88         trashItem.setTextAlignment(1, ra.value());\r
89         List<String> headers = new ArrayList<String>();\r
90         headers.add("");\r
91         headers.add("");\r
92         setHeaderLabels(headers);\r
93                 setColumnCount(2);\r
94                 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);\r
95                 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);\r
96                 header().setMovable(false);\r
97         setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
98         addTopLevelItem(trashItem);\r
99
100         }\r
101 \r
102         @Override\r
103         public void contextMenuEvent(QContextMenuEvent event) {\r
104                 QMenu menu = new QMenu(this);\r
105                 menu.addAction(emptyAction);\r
106                 menu.exec(event.globalPos());\r
107         }\r
108         \r
109         \r
110         @Override\r
111         public void dragEnterEvent(QDragEnterEvent event) {\r
112                 event.mimeData().setData("application/x-nevernote-trash", new QByteArray(currentItem().text(1)));\r
113                 event.accept();\r
114         }\r
115 }\r