OSDN Git Service

Merge remote-tracking branch 'origin/develop'
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TrashTreeWidget.java
1 /*\r
2  * This file is part of NixNote/NeighborNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * Copyright 2013 Yuki Takahashi\r
5  * \r
6  * This file may be licensed under the terms of of the\r
7  * GNU General Public License Version 2 (the ``GPL'').\r
8  *\r
9  * Software distributed under the License is distributed\r
10  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
11  * express or implied. See the GPL for the specific language\r
12  * governing rights and limitations.\r
13  *\r
14  * You should have received a copy of the GPL along with this\r
15  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
16  * or write to the Free Software Foundation, Inc.,\r
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
18  *\r
19 */\r
20 \r
21 package cx.fbn.nevernote.gui;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 \r
26 import com.trolltech.qt.core.QByteArray;\r
27 import com.trolltech.qt.core.Qt;\r
28 import com.trolltech.qt.gui.QAbstractItemView;\r
29 import com.trolltech.qt.gui.QAction;\r
30 import com.trolltech.qt.gui.QBrush;\r
31 import com.trolltech.qt.gui.QColor;\r
32 import com.trolltech.qt.gui.QContextMenuEvent;\r
33 import com.trolltech.qt.gui.QDragEnterEvent;\r
34 import com.trolltech.qt.gui.QHeaderView;\r
35 import com.trolltech.qt.gui.QIcon;\r
36 import com.trolltech.qt.gui.QMenu;\r
37 import com.trolltech.qt.gui.QTreeWidget;\r
38 import com.trolltech.qt.gui.QTreeWidgetItem;\r
39 \r
40 public class TrashTreeWidget extends QTreeWidget {\r
41         private QAction emptyAction;\r
42         private QTreeWidgetItem trashItem;\r
43         private Integer trashCount;     \r
44         private final String iconPath;\r
45         private final QIcon trashIcon;\r
46         private final QIcon trashFullIcon;\r
47         \r
48         public void setEmptyAction(QAction a) {\r
49                 emptyAction = a;\r
50         }\r
51         \r
52         \r
53         public TrashTreeWidget() {\r
54         trashCount =  0;\r
55         setProperty("hideTree", true);\r
56         iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
57         trashIcon = new QIcon(iconPath+"trash.png");\r
58         trashFullIcon = new QIcon(iconPath+"trash-full.png");\r
59         header().hide();\r
60         //setMaximumHeight(30);\r
61         setMinimumHeight(30);\r
62         }\r
63         \r
64         public void updateCounts(Integer cnt) {\r
65                 QBrush gray = new QBrush();\r
66                 gray.setColor(QColor.gray);\r
67                 QBrush black = new QBrush();\r
68                 black.setColor(QColor.black);\r
69                 \r
70                 trashCount = cnt;\r
71                 trashItem.setText(1, trashCount.toString());\r
72                 header().resizeSection(1, 0);\r
73                 if (trashCount > 0) {\r
74                         trashItem.setForeground(0, black);                      \r
75                         trashItem.setForeground(1, black);\r
76                         trashItem.setIcon(0, trashFullIcon);\r
77                 } else {\r
78                         trashItem.setIcon(0,trashIcon);\r
79                         trashItem.setForeground(0, gray);                       \r
80                         trashItem.setForeground(1, gray);                                               \r
81                 }\r
82         }\r
83         \r
84         public void load() {\r
85         trashItem = new QTreeWidgetItem();\r
86         trashItem.setIcon(0, trashIcon);\r
87         trashItem.setText(0, tr("Trash"));\r
88         Qt.Alignment ra = new Qt.Alignment(Qt.AlignmentFlag.AlignRight);\r
89         trashItem.setTextAlignment(1, ra.value());\r
90         List<String> headers = new ArrayList<String>();\r
91         headers.add("");\r
92         headers.add("");\r
93         setHeaderLabels(headers);\r
94                 setColumnCount(2);\r
95                 header().setResizeMode(0, QHeaderView.ResizeMode.ResizeToContents);\r
96                 header().setResizeMode(1, QHeaderView.ResizeMode.Stretch);\r
97                 header().setMovable(false);\r
98         setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection);\r
99         addTopLevelItem(trashItem);\r
100
101         }\r
102 \r
103         @Override\r
104         public void contextMenuEvent(QContextMenuEvent event) {\r
105                 QMenu menu = new QMenu(this);\r
106                 menu.addAction(emptyAction);\r
107                 menu.exec(event.globalPos());\r
108         }\r
109         \r
110         \r
111         @Override\r
112         public void dragEnterEvent(QDragEnterEvent event) {\r
113                 event.mimeData().setData("application/x-nevernote-trash", new QByteArray(currentItem().text(1)));\r
114                 event.accept();\r
115         }\r
116         \r
117         public QTreeWidgetItem getTrashItem() {\r
118                 return trashItem;\r
119         }\r
120 }\r