1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QGraphicsView>
43 #include <QGraphicsWidget>
44 #include <QGraphicsLinearLayout>
45 #include <QList>
46
47 #include "button.h"
48 #include "menu.h"
49 #include "themeevent.h"
50 #include "theme.h"
51
52 static const int MinMenuItemWidth = 150;
53 static const int MinMenuItemHeight = 40;
54
Menu(QGraphicsView * parent)55 Menu::Menu(QGraphicsView* parent) : QGraphicsWidget(),
56 m_Parent(parent), m_Layout(new QGraphicsLinearLayout(Qt::Vertical, this)),
57 m_ButtonContainer(0), m_isMenuVisible(false), m_currentSelectedIndex(-1)
58 {
59 init();
60 }
61
~Menu()62 Menu::~Menu()
63 {
64 for(int i = 0; i < m_ButtonContainer->count(); i++ ) {
65 delete m_ButtonContainer->at(i);
66 }
67 m_ButtonContainer->clear();
68
69 delete m_ButtonContainer;
70 m_ButtonContainer = 0;
71 }
72
init()73 void Menu::init()
74 {
75 m_ButtonContainer = new QList<Button*>;
76
77 m_Layout->setContentsMargins(0,0,0,0);
78 m_Layout->setSpacing(0);
79
80 setMinimumWidth(150);
81
82 setLayout(m_Layout);
83
84 connect(Theme::p(), SIGNAL(themeChanged()), this, SLOT(themeChange()));
85 }
86
addMenuItem(const QString itemName,QObject * receiver,const char * member)87 Button* Menu::addMenuItem(const QString itemName, QObject* receiver, const char* member)
88 {
89 Button* button = new Button(itemName ,this);
90 button->setVisible(m_isMenuVisible);
91 connect(button, SIGNAL(clicked(bool)), receiver, member);
92 connect(button, SIGNAL(clicked(bool)), this, SLOT(menuShowHide()));
93 m_ButtonContainer->append(button);
94 button->setMinimumWidth(MinMenuItemWidth);
95 button->setMinimumHeight(MinMenuItemHeight);
96 return button;
97 }
98
menuShowHide()99 void Menu::menuShowHide()
100 {
101 m_isMenuVisible ? menuHide() : menuShow();
102 m_isMenuVisible = !m_isMenuVisible;
103 }
104
menuShow()105 void Menu::menuShow()
106 {
107 for(int i = 0; i < m_ButtonContainer->count(); i++) {
108 Button* button = m_ButtonContainer->at(i);
109 m_Layout->addItem(button);
110 button->show();
111 }
112 }
113
menuHide()114 void Menu::menuHide()
115 {
116 for(int i = 0; i < m_ButtonContainer->count(); i++) {
117 Button* button = m_ButtonContainer->at(i);
118 button->select(false);
119 button->hide();
120 m_Layout->removeItem(button);
121 }
122 m_currentSelectedIndex = -1;
123 }
124
themeChange()125 void Menu::themeChange()
126 {
127 QPixmap pixmap = Theme::p()->pixmap("status_field_middle.svg",
128 QSize(MinMenuItemWidth, MinMenuItemHeight));
129
130 for(int i = 0; i < m_ButtonContainer->count(); i++) {
131 Button* button = m_ButtonContainer->at(i);
132 button->setBackground(pixmap);
133 }
134 update();
135 }
136
keyPressEvent(QKeyEvent * event)137 void Menu::keyPressEvent(QKeyEvent *event)
138 {
139 //S60 3.x specific
140 if(event->key() == 16777235 ) { //Up Arrow
141 if(m_currentSelectedIndex > 0) { //One step up
142 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
143 button->select(false);
144 button->update();
145
146 m_currentSelectedIndex--;
147 button = m_ButtonContainer->at(m_currentSelectedIndex);
148 button->select(true);
149 button->update();
150 }
151 else { //Jump to bottom
152 if(m_currentSelectedIndex >= 0) {
153 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
154 button->select(false);
155 button->update();
156 }
157 m_currentSelectedIndex = m_ButtonContainer->count() -1;
158 if(m_currentSelectedIndex >= 0) {
159 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
160 button->select(true);
161 button->update();
162 }
163 }
164 }
165
166 if(event->key() == 16777237 ) { //Down Arrow
167 if (m_currentSelectedIndex < m_ButtonContainer->count()-1) { //One step down
168 if(m_currentSelectedIndex >= 0) {
169 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
170 button->select(false);
171 button->update();
172 }
173 m_currentSelectedIndex++;
174 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
175 button->select(true);
176 button->update();
177 }
178 else { //Jump to top
179 if(m_currentSelectedIndex >= 0) {
180 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
181 button->select(false);
182 button->update();
183 m_currentSelectedIndex = 0;
184 button = m_ButtonContainer->at(m_currentSelectedIndex);
185 button->select(true);
186 button->update();
187 }
188 }
189 }
190
191 if(event->key() == 17825792 || event->key() == 16842752 || //LSK, center key or enter
192 event->key() == 16777221 ) {
193 if(m_currentSelectedIndex >= 0) {
194 Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
195 button->click();
196 }
197 }
198
199 if(event->key() == 17825793 ) { //RSK
200 menuShowHide();
201 }
202 }
203