QtBase  v6.3.1
menu.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include <QGraphicsView>
30 #include <QGraphicsWidget>
31 #include <QGraphicsLinearLayout>
32 #include <QList>
33 
34 #include "button.h"
35 #include "menu.h"
36 #include "themeevent.h"
37 #include "theme.h"
38 
39 static const int MinMenuItemWidth = 150;
40 static const int MinMenuItemHeight = 40;
41 
43  m_Parent(parent), m_Layout(new QGraphicsLinearLayout(Qt::Vertical, this)),
44  m_ButtonContainer(0), m_isMenuVisible(false), m_currentSelectedIndex(-1)
45 {
46  init();
47 }
48 
50 {
51  for(int i = 0; i < m_ButtonContainer->count(); i++ ) {
52  delete m_ButtonContainer->at(i);
53  }
54  m_ButtonContainer->clear();
55 
56  delete m_ButtonContainer;
57  m_ButtonContainer = 0;
58 }
59 
60 void Menu::init()
61 {
62  m_ButtonContainer = new QList<Button*>;
63 
64  m_Layout->setContentsMargins(0,0,0,0);
65  m_Layout->setSpacing(0);
66 
67  setMinimumWidth(150);
68 
69  setLayout(m_Layout);
70 
71  connect(Theme::p(), SIGNAL(themeChanged()), this, SLOT(themeChange()));
72 }
73 
74 Button* Menu::addMenuItem(const QString itemName, QObject* receiver, const char* member)
75 {
76  Button* button = new Button(itemName ,this);
77  button->setVisible(m_isMenuVisible);
78  connect(button, SIGNAL(clicked(bool)), receiver, member);
79  connect(button, SIGNAL(clicked(bool)), this, SLOT(menuShowHide()));
80  m_ButtonContainer->append(button);
81  button->setMinimumWidth(MinMenuItemWidth);
82  button->setMinimumHeight(MinMenuItemHeight);
83  return button;
84 }
85 
87 {
88  m_isMenuVisible ? menuHide() : menuShow();
89  m_isMenuVisible = !m_isMenuVisible;
90 }
91 
92 void Menu::menuShow()
93 {
94  for(int i = 0; i < m_ButtonContainer->count(); i++) {
95  Button* button = m_ButtonContainer->at(i);
96  m_Layout->addItem(button);
97  button->show();
98  }
99 }
100 
101 void Menu::menuHide()
102 {
103  for(int i = 0; i < m_ButtonContainer->count(); i++) {
104  Button* button = m_ButtonContainer->at(i);
105  button->select(false);
106  button->hide();
107  m_Layout->removeItem(button);
108  }
109  m_currentSelectedIndex = -1;
110 }
111 
113 {
114  QPixmap pixmap = Theme::p()->pixmap("status_field_middle.svg",
115  QSize(MinMenuItemWidth, MinMenuItemHeight));
116 
117  for(int i = 0; i < m_ButtonContainer->count(); i++) {
118  Button* button = m_ButtonContainer->at(i);
119  button->setBackground(pixmap);
120  }
121  update();
122 }
123 
125 {
126  //S60 3.x specific
127  if(event->key() == 16777235 ) { //Up Arrow
128  if(m_currentSelectedIndex > 0) { //One step up
129  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
130  button->select(false);
131  button->update();
132 
133  m_currentSelectedIndex--;
134  button = m_ButtonContainer->at(m_currentSelectedIndex);
135  button->select(true);
136  button->update();
137  }
138  else { //Jump to bottom
139  if(m_currentSelectedIndex >= 0) {
140  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
141  button->select(false);
142  button->update();
143  }
144  m_currentSelectedIndex = m_ButtonContainer->count() -1;
145  if(m_currentSelectedIndex >= 0) {
146  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
147  button->select(true);
148  button->update();
149  }
150  }
151  }
152 
153  if(event->key() == 16777237 ) { //Down Arrow
154  if (m_currentSelectedIndex < m_ButtonContainer->count()-1) { //One step down
155  if(m_currentSelectedIndex >= 0) {
156  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
157  button->select(false);
158  button->update();
159  }
160  m_currentSelectedIndex++;
161  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
162  button->select(true);
163  button->update();
164  }
165  else { //Jump to top
166  if(m_currentSelectedIndex >= 0) {
167  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
168  button->select(false);
169  button->update();
170  m_currentSelectedIndex = 0;
171  button = m_ButtonContainer->at(m_currentSelectedIndex);
172  button->select(true);
173  button->update();
174  }
175  }
176  }
177 
178  if(event->key() == 17825792 || event->key() == 16842752 || //LSK, center key or enter
179  event->key() == 16777221 ) {
180  if(m_currentSelectedIndex >= 0) {
181  Button* button = m_ButtonContainer->at(m_currentSelectedIndex);
182  button->click();
183  }
184  }
185 
186  if(event->key() == 17825793 ) { //RSK
187  menuShowHide();
188  }
189 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
[0]
Definition: button.h:58
void themeChange()
Definition: menu.cpp:112
Menu(QGraphicsView *parent)
Definition: menu.cpp:42
void menuShowHide()
Definition: menu.cpp:86
~Menu()
Definition: menu.cpp:49
virtual void keyPressEvent(QKeyEvent *event)
Definition: menu.cpp:124
Button * addMenuItem(const QString itemName, QObject *receiver, const char *member)
Definition: menu.cpp:74
void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
void setMinimumWidth(qreal width)
The QGraphicsLinearLayout class provides a horizontal or vertical layout for managing widgets in Grap...
void removeItem(QGraphicsLayoutItem *item)
void setSpacing(qreal spacing)
void addItem(QGraphicsLayoutItem *item)
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
void setLayout(QGraphicsLayout *layout)
The QKeyEvent class describes a key event.
Definition: qevent.h:471
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
qsizetype count() const noexcept
Definition: qlist.h:415
void append(parameter_type t)
Definition: qlist.h:469
void clear()
Definition: qlist.h:445
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QPixmap class is an off-screen image representation that can be used as a paint device.
Definition: qpixmap.h:63
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
void setMinimumWidth(int minw)
Definition: qwidget.cpp:4118
void hide()
Definition: qwidget.cpp:8078
void setMinimumHeight(int minh)
Definition: qwidget.cpp:4127
void show()
Definition: qwidget.cpp:7825
virtual void setVisible(bool visible)
Definition: qwidget.cpp:8198
void update()
Definition: qwidget.cpp:10977
QPixmap pixmap(const QString filename="", QSize size=QSize(0, 0))
Definition: theme.cpp:204
static Theme * p()
Definition: theme.cpp:64
#define this
Definition: dialogs.cpp:56
QPushButton * button
[2]
Definition: qnamespace.h:55
@ Vertical
Definition: qnamespace.h:125
Button
Definition: qmessagebox.cpp:85
#define SLOT(a)
Definition: qobjectdefs.h:87
#define SIGNAL(a)
Definition: qobjectdefs.h:88
GLenum GLenum GLsizei count
struct _cl_event * event
Definition: qopenglext.h:2998
widget render & pixmap
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent