OSDN Git Service

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