tests/benchmarks/uimodels/GraphicsViewBenchmark/widgets/menu.cpp
changeset 3 41300fa6a67c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     5 **
       
     6 ** This file is part of the examples of the Qt Toolkit.
       
     7 **
       
     8 ** $QT_BEGIN_LICENSE:LGPL$
       
     9 ** No Commercial Usage
       
    10 ** This file contains pre-release code and may not be distributed.
       
    11 ** You may use this file in accordance with the terms and conditions
       
    12 ** contained in the either Technology Preview License Agreement or the
       
    13 ** Beta Release License Agreement.
       
    14 **
       
    15 ** GNU Lesser General Public License Usage
       
    16 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    17 ** General Public License version 2.1 as published by the Free Software
       
    18 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    19 ** packaging of this file.  Please review the following information to
       
    20 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    22 **
       
    23 ** In addition, as a special exception, Nokia gives you certain
       
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
       
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
       
    26 ** package.
       
    27 **
       
    28 ** GNU General Public License Usage
       
    29 ** Alternatively, this file may be used under the terms of the GNU
       
    30 ** General Public License version 3.0 as published by the Free Software
       
    31 ** Foundation and appearing in the file LICENSE.GPL included in the
       
    32 ** packaging of this file.  Please review the following information to
       
    33 ** ensure the GNU General Public License version 3.0 requirements will be
       
    34 ** met: http://www.gnu.org/copyleft/gpl.html.
       
    35 **
       
    36 ** If you are unsure which license is appropriate for your use, please
       
    37 ** contact the sales department at http://qt.nokia.com/contact.
       
    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 
       
    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 
       
    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 
       
    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 
       
    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 
       
    99 void Menu::menuShowHide()
       
   100 {
       
   101     m_isMenuVisible ? menuHide() : menuShow();
       
   102     m_isMenuVisible = !m_isMenuVisible;
       
   103 }
       
   104 
       
   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 
       
   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 
       
   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 
       
   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 }