OSDN Git Service

ebecb4508da3245cb6e8bb2d875f4ccc8bb3d97b
[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         }\r
60         \r
61         public void updateCounts(Integer cnt) {\r
62                 QBrush gray = new QBrush();\r
63                 gray.setColor(QColor.gray);\r
64                 QBrush black = new QBrush();\r
65                 black.setColor(QColor.black);\r
66                 \r
67                 trashCount = cnt;\r
68                 trashItem.setText(1, trashCount.toString());\r
69                 header().resizeSection(1, 0);\r
70                 if (trashCount > 0) {\r
71                         trashItem.setForeground(0, black);                      \r
72                         trashItem.setForeground(1, black);\r
73                         trashItem.setIcon(0, trashFullIcon);\r
74                 } else {\r
75                         trashItem.setIcon(0,trashIcon);\r
76                         trashItem.setForeground(0, gray);                       \r
77                         trashItem.setForeground(1, gray);                                               \r
78                 }\r
79         }\r
80         \r
81         public void load() {\r
82         trashItem = new QTreeWidgetItem();\r
83         trashItem.setIcon(0, trashIcon);\r
84         trashItem.setText(0, "Trash");\r
85         Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
86         trashItem.setTextAlignment(1, ra.value());\r
87         List<String> headers = new ArrayList<String>();\r
88         headers.add("");\r
89         headers.add("");\r
90         setHeaderLabels(headers);\r
91                 setColumnCount(2);\r
92                 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);\r
93                 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);\r
94                 header().setMovable(false);\r
95         setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
96         addTopLevelItem(trashItem);\r
97
98         }\r
99 \r
100         @Override\r
101         public void contextMenuEvent(QContextMenuEvent event) {\r
102                 QMenu menu = new QMenu(this);\r
103                 menu.addAction(emptyAction);\r
104                 menu.exec(event.globalPos());\r
105         }\r
106         \r
107         \r
108         @Override\r
109         public void dragEnterEvent(QDragEnterEvent event) {\r
110                 event.mimeData().setData("application/x-nevernote-trash", new QByteArray(currentItem().text(1)));\r
111                 event.accept();\r
112         }\r
113 }\r