skin: find event of menu button from list of context menu
[sdk/emulator/qemu.git] / tizen / src / ui / controller / hwkeybutton.cpp
1 /*
2  * Qt UI
3  *
4  * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * GiWoong Kim <giwoong.kim@samsung.com>
8  * Sangho Park <sangho1206.park@samsung.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02110-1301, USA.
24  *
25  * Contributors:
26  * - S-Core Co., Ltd
27  *
28  */
29
30 #include <QDebug>
31 #include <QFile>
32
33 #include "hwkeybutton.h"
34
35 extern "C" {
36 #include "emul_state.h"
37 #include "util/ui_operations.h"
38 }
39
40 #define DEFAULT_NAME "keybutton"
41
42 HWKeyButton::HWKeyButton(QWidget *parent, HardwareKey *hwKey, QSize size) :
43     QPushButton(hwKey->getName(), parent)
44 {
45     this->hwKey = hwKey;
46     setText("");
47     setFocusPolicy(Qt::NoFocus);
48
49     if (!hwKey->getNormalImagePath().isEmpty()) {
50         initialize(size);
51     } else {
52         if (QFile::exists(":images/controller-skin/" + hwKey->getName() + "_normal.png")
53         && QFile::exists(":images/controller-skin/" + hwKey->getName() + "_hover.png")
54         && QFile::exists(":images/controller-skin/" + hwKey->getName() + "_pushed.png")) {
55             initialize(hwKey->getName(), size);
56         } else {
57             setText(hwKey->getName());
58             initialize(DEFAULT_NAME, size);
59         }
60     }
61 }
62
63 void HWKeyButton::initialize(QSize size)
64 {
65     // use image file path
66     // path is absolute path
67     setStyleSheet(
68         "QPushButton {"
69             "width: " + QString::number(size.width()) + ";" +
70             "height: " + QString::number(size.height()) + ";" +
71             "border: none; color: black;"
72             "background: url(" + hwKey->getNormalImagePath() + ");"
73         "}"
74         "QPushButton:hover {"
75             "background: url(" + hwKey->getHoverImagePath() + ");"
76         "}"
77         "QPushButton:pressed {"
78             "background: url(" + hwKey->getPushImagePath() + ");"
79         "}"
80         "QToolTip {"
81             "color:#2E2E2E; background-color:#CCCCCC;"
82         "}"
83     );
84 }
85
86 void HWKeyButton::initialize(QString name, QSize size)
87 {
88     // use key button name
89     // path is relative path
90     setStyleSheet(
91         "QPushButton {"
92             "width: " + QString::number(size.width()) + ";" +
93             "height: " + QString::number(size.height()) + ";" +
94             "border: none; color: black;"
95             "background: url(\":images/controller-skin/" + name +"_normal.png\");"
96         "}"
97         "QPushButton:hover {"
98             "background: url(\":images/controller-skin/" + name +"_hover.png\");"
99         "}"
100         "QPushButton:pressed {"
101             "background: url(\":images/controller-skin/" + name +"_pushed.png\");"
102         "}"
103         "QToolTip {"
104             "color:#2E2E2E; background-color:#CCCCCC;"
105         "}"
106     );
107 }
108
109 void HWKeyButton::showEvent(QShowEvent *event)
110 {
111     if (!text().isEmpty()) {
112         setText(fontMetrics().elidedText(text(), Qt::ElideRight, width() * 1.2));
113     }
114 }
115
116 /* override */
117 void HWKeyButton::mousePressEvent(QMouseEvent *event)
118 {
119     const int keycode = hwKey->getKeycode();
120
121     if (keycode > 0) {
122         qDebug() << hwKey->getName() << "key button pressed:" << keycode;
123         do_hw_key_event(KEY_PRESSED, keycode);
124     }
125
126     QPushButton::mousePressEvent(event);
127 }
128
129 /* override */
130 void HWKeyButton::mouseReleaseEvent(QMouseEvent *event)
131 {
132     const int keycode = hwKey->getKeycode();
133
134     if (keycode > 0) {
135         qDebug() << hwKey->getName() << "key button released:" << keycode;
136         do_hw_key_event(KEY_RELEASED, keycode);
137     }
138
139     QPushButton::mouseReleaseEvent(event);
140 }
141
142 HWKeyButton::~HWKeyButton()
143 {
144     /* do nothing */
145 }