OSDN Git Service

Add color names to color menu.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / ColorMenu.java
1 /*\r
2  * This file is part of NixNote \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.List;\r
23 \r
24 import com.trolltech.qt.core.QSize;\r
25 import com.trolltech.qt.gui.QAction;\r
26 import com.trolltech.qt.gui.QColor;\r
27 import com.trolltech.qt.gui.QIcon;\r
28 import com.trolltech.qt.gui.QMenu;\r
29 import com.trolltech.qt.gui.QPixmap;\r
30 import com.trolltech.qt.gui.QWidget;\r
31 \r
32 public class ColorMenu extends Object {\r
33         \r
34         private final QMenu menu;\r
35         QWidget parent;\r
36         QColor currentColor;\r
37         \r
38         public ColorMenu(QWidget b) {\r
39                 menu = new QMenu();\r
40                 parent = b;\r
41                 populateList();\r
42                 currentColor = new QColor("black");\r
43         }\r
44         \r
45         public void setDefault(QColor color) {\r
46                 currentColor = color;\r
47         }\r
48                 \r
49         private void populateList() {\r
50                 List<String> colorNames = QColor.colorNames();\r
51                 for(int i=0; i<colorNames.size(); i++) {\r
52                         QColor color = new QColor(colorNames.get(i));\r
53                         QPixmap pix = new QPixmap(new QSize(22, 22));\r
54                         pix.fill(color);\r
55                         QAction newAction = new QAction(new QIcon(pix), "", parent);\r
56                         newAction.setToolTip(colorNames.get(i));\r
57                         newAction.setText(colorNames.get(i));\r
58                         newAction.hovered.connect(this, "itemHovered()");\r
59                         menu.addAction(newAction);\r
60                 }\r
61         }\r
62         \r
63         @SuppressWarnings("unused")\r
64         private void itemHovered() {\r
65                 if (menu.activeAction() != null && menu.activeAction().toolTip() != null)\r
66                         currentColor = new QColor(menu.activeAction().toolTip());\r
67         }\r
68         \r
69         public QColor getColor() {\r
70                 return currentColor;\r
71         }\r
72         \r
73         public QMenu getMenu() {\r
74                 return menu;\r
75         }\r
76 }\r