demos/browser/modelmenu.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the demonstration applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "modelmenu.h"
       
    43 
       
    44 #include <QtCore/QAbstractItemModel>
       
    45 #include <qdebug.h>
       
    46 
       
    47 ModelMenu::ModelMenu(QWidget * parent)
       
    48     : QMenu(parent)
       
    49     , m_maxRows(7)
       
    50     , m_firstSeparator(-1)
       
    51     , m_maxWidth(-1)
       
    52     , m_hoverRole(0)
       
    53     , m_separatorRole(0)
       
    54     , m_model(0)
       
    55 {
       
    56     connect(this, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
       
    57 }
       
    58 
       
    59 bool ModelMenu::prePopulated()
       
    60 {
       
    61     return false;
       
    62 }
       
    63 
       
    64 void ModelMenu::postPopulated()
       
    65 {
       
    66 }
       
    67 
       
    68 void ModelMenu::setModel(QAbstractItemModel *model)
       
    69 {
       
    70     m_model = model;
       
    71 }
       
    72 
       
    73 QAbstractItemModel *ModelMenu::model() const
       
    74 {
       
    75     return m_model;
       
    76 }
       
    77 
       
    78 void ModelMenu::setMaxRows(int max)
       
    79 {
       
    80     m_maxRows = max;
       
    81 }
       
    82 
       
    83 int ModelMenu::maxRows() const
       
    84 {
       
    85     return m_maxRows;
       
    86 }
       
    87 
       
    88 void ModelMenu::setFirstSeparator(int offset)
       
    89 {
       
    90     m_firstSeparator = offset;
       
    91 }
       
    92 
       
    93 int ModelMenu::firstSeparator() const
       
    94 {
       
    95     return m_firstSeparator;
       
    96 }
       
    97 
       
    98 void ModelMenu::setRootIndex(const QModelIndex &index)
       
    99 {
       
   100     m_root = index;
       
   101 }
       
   102 
       
   103 QModelIndex ModelMenu::rootIndex() const
       
   104 {
       
   105     return m_root;
       
   106 }
       
   107 
       
   108 void ModelMenu::setHoverRole(int role)
       
   109 {
       
   110     m_hoverRole = role;
       
   111 }
       
   112 
       
   113 int ModelMenu::hoverRole() const
       
   114 {
       
   115     return m_hoverRole;
       
   116 }
       
   117 
       
   118 void ModelMenu::setSeparatorRole(int role)
       
   119 {
       
   120     m_separatorRole = role;
       
   121 }
       
   122 
       
   123 int ModelMenu::separatorRole() const
       
   124 {
       
   125     return m_separatorRole;
       
   126 }
       
   127 
       
   128 Q_DECLARE_METATYPE(QModelIndex)
       
   129 void ModelMenu::aboutToShow()
       
   130 {
       
   131     if (QMenu *menu = qobject_cast<QMenu*>(sender())) {
       
   132         QVariant v = menu->menuAction()->data();
       
   133         if (v.canConvert<QModelIndex>()) {
       
   134             QModelIndex idx = qvariant_cast<QModelIndex>(v);
       
   135             createMenu(idx, -1, menu, menu);
       
   136             disconnect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
       
   137             return;
       
   138         }
       
   139     }
       
   140 
       
   141     clear();
       
   142     if (prePopulated())
       
   143         addSeparator();
       
   144     int max = m_maxRows;
       
   145     if (max != -1)
       
   146         max += m_firstSeparator;
       
   147     createMenu(m_root, max, this, this);
       
   148     postPopulated();
       
   149 }
       
   150 
       
   151 void ModelMenu::createMenu(const QModelIndex &parent, int max, QMenu *parentMenu, QMenu *menu)
       
   152 {
       
   153     if (!menu) {
       
   154         QString title = parent.data().toString();
       
   155         menu = new QMenu(title, this);
       
   156         QIcon icon = qvariant_cast<QIcon>(parent.data(Qt::DecorationRole));
       
   157         menu->setIcon(icon);
       
   158         parentMenu->addMenu(menu);
       
   159         QVariant v;
       
   160         v.setValue(parent);
       
   161         menu->menuAction()->setData(v);
       
   162         connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
       
   163         return;
       
   164     }
       
   165 
       
   166     int end = m_model->rowCount(parent);
       
   167     if (max != -1)
       
   168         end = qMin(max, end);
       
   169 
       
   170     connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(triggered(QAction*)));
       
   171     connect(menu, SIGNAL(hovered(QAction*)), this, SLOT(hovered(QAction*)));
       
   172 
       
   173     for (int i = 0; i < end; ++i) {
       
   174         QModelIndex idx = m_model->index(i, 0, parent);
       
   175         if (m_model->hasChildren(idx)) {
       
   176             createMenu(idx, -1, menu);
       
   177         } else {
       
   178             if (m_separatorRole != 0
       
   179                 && idx.data(m_separatorRole).toBool())
       
   180                 addSeparator();
       
   181             else
       
   182                 menu->addAction(makeAction(idx));
       
   183         }
       
   184         if (menu == this && i == m_firstSeparator - 1)
       
   185             addSeparator();
       
   186     }
       
   187 }
       
   188 
       
   189 QAction *ModelMenu::makeAction(const QModelIndex &index)
       
   190 {
       
   191     QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
       
   192     QAction *action = makeAction(icon, index.data().toString(), this);
       
   193     QVariant v;
       
   194     v.setValue(index);
       
   195     action->setData(v);
       
   196     return action;
       
   197 }
       
   198 
       
   199 QAction *ModelMenu::makeAction(const QIcon &icon, const QString &text, QObject *parent)
       
   200 {
       
   201     QFontMetrics fm(font());
       
   202     if (-1 == m_maxWidth)
       
   203         m_maxWidth = fm.width(QLatin1Char('m')) * 30;
       
   204     QString smallText = fm.elidedText(text, Qt::ElideMiddle, m_maxWidth);
       
   205     return new QAction(icon, smallText, parent);
       
   206 }
       
   207 
       
   208 void ModelMenu::triggered(QAction *action)
       
   209 {
       
   210     QVariant v = action->data();
       
   211     if (v.canConvert<QModelIndex>()) {
       
   212         QModelIndex idx = qvariant_cast<QModelIndex>(v);
       
   213         emit activated(idx);
       
   214     }
       
   215 }
       
   216 
       
   217 void ModelMenu::hovered(QAction *action)
       
   218 {
       
   219     QVariant v = action->data();
       
   220     if (v.canConvert<QModelIndex>()) {
       
   221         QModelIndex idx = qvariant_cast<QModelIndex>(v);
       
   222         QString hoveredString = idx.data(m_hoverRole).toString();
       
   223         if (!hoveredString.isEmpty())
       
   224             emit hovered(hoveredString);
       
   225     }
       
   226 }
       
   227