~ubuntu-branches/ubuntu/trusty/bibletime/trusty

« back to all changes in this revision

Viewing changes to src/frontend/btmenuview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden
  • Date: 2010-01-10 22:21:36 UTC
  • mfrom: (1.3.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100110222136-905hza76230hperg
Tags: 2.5-1
New upstream version 2.5 (Closes: #564551).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********
 
2
*
 
3
* In the name of the Father, and of the Son, and of the Holy Spirit.
 
4
*
 
5
* This file is part of BibleTime's source code, http://www.bibletime.info/.
 
6
*
 
7
* Copyright 1999-2009 by the BibleTime developers.
 
8
* The BibleTime source code is licensed under the GNU General Public License
 
9
* version 2.0.
 
10
*
 
11
**********/
 
12
 
 
13
#include "frontend/btmenuview.h"
 
14
 
 
15
#include <QActionGroup>
 
16
 
 
17
 
 
18
BtMenuView::BtMenuView(QWidget *parent)
 
19
    : QMenu(parent), m_model(0), m_actions(0)
 
20
{
 
21
    connect(this, SIGNAL(aboutToShow()),
 
22
            this, SLOT(slotAboutToShow()));
 
23
    connect(this, SIGNAL(aboutToHide()),
 
24
            this, SLOT(slotAboutToHide()));
 
25
}
 
26
 
 
27
BtMenuView::~BtMenuView() {
 
28
    if (m_actions != 0) {
 
29
        delete m_actions;
 
30
    }
 
31
}
 
32
 
 
33
void BtMenuView::setModel(QAbstractItemModel *model) {
 
34
    m_model = model;
 
35
}
 
36
 
 
37
void BtMenuView::buildMenu(QMenu *parentMenu, const QModelIndex &parent) {
 
38
    Q_ASSERT(m_model != 0);
 
39
 
 
40
    int children = m_model->rowCount(parent);
 
41
    for (int i = 0; i < children; i++) {
 
42
        QModelIndex child(m_model->index(i, 0, parent));
 
43
        QVariant displayData(m_model->data(child, Qt::DisplayRole));
 
44
        QVariant iconData(m_model->data(child, Qt::DecorationRole));
 
45
        QVariant toolTipData(m_model->data(child, Qt::ToolTipRole));
 
46
        QVariant statusTipData(m_model->data(child, Qt::StatusTipRole));
 
47
        QVariant whatsThisData(m_model->data(child, Qt::WhatsThisRole));
 
48
 
 
49
        if (m_model->rowCount(child) > 0) {
 
50
            QMenu *childMenu = new QMenu(parentMenu);
 
51
 
 
52
            // Set text:
 
53
            if (qVariantCanConvert<QString>(displayData)) {
 
54
                childMenu->setTitle(displayData.toString());
 
55
            }
 
56
 
 
57
            // Set icon:
 
58
            if (qVariantCanConvert<QIcon>(iconData)) {
 
59
                childMenu->setIcon(iconData.value<QIcon>());
 
60
            }
 
61
 
 
62
            // Set tooltip:
 
63
            if (qVariantCanConvert<QString>(toolTipData)) {
 
64
                childMenu->setToolTip(toolTipData.toString());
 
65
            }
 
66
 
 
67
            // Set status tip:
 
68
            if (qVariantCanConvert<QString>(statusTipData)) {
 
69
                childMenu->setStatusTip(statusTipData.toString());
 
70
            }
 
71
 
 
72
            // Set whatsthis:
 
73
            if (qVariantCanConvert<QString>(whatsThisData)) {
 
74
                childMenu->setWhatsThis(whatsThisData.toString());
 
75
            }
 
76
 
 
77
            parentMenu->addMenu(childMenu);
 
78
            buildMenu(childMenu, child);
 
79
        } else {
 
80
            QAction *childAction = new QAction(m_actions);
 
81
 
 
82
            // Set text:
 
83
            if (qVariantCanConvert<QString>(displayData)) {
 
84
                childAction->setText(displayData.toString());
 
85
            }
 
86
 
 
87
            // Set icon:
 
88
            if (qVariantCanConvert<QIcon>(iconData)) {
 
89
                childAction->setIcon(iconData.value<QIcon>());
 
90
            }
 
91
 
 
92
            // Set tooltip:
 
93
            if (qVariantCanConvert<QString>(toolTipData)) {
 
94
                childAction->setToolTip(toolTipData.toString());
 
95
            }
 
96
 
 
97
            // Set status tip:
 
98
            if (qVariantCanConvert<QString>(statusTipData)) {
 
99
                childAction->setStatusTip(statusTipData.toString());
 
100
            }
 
101
 
 
102
            // Set whatsthis:
 
103
            if (qVariantCanConvert<QString>(whatsThisData)) {
 
104
                childAction->setWhatsThis(whatsThisData.toString());
 
105
            }
 
106
 
 
107
            // Set checkable:
 
108
            if (m_model->flags(child).testFlag(Qt::ItemIsUserCheckable)) {
 
109
                childAction->setCheckable(true);
 
110
            }
 
111
 
 
112
            // Set checked:
 
113
            QVariant checkData(m_model->data(child, Qt::CheckStateRole));
 
114
            bool ok;
 
115
            Qt::CheckState state = (Qt::CheckState) checkData.toInt(&ok);
 
116
            if (ok) {
 
117
                childAction->setChecked(state == Qt::Checked);
 
118
            }
 
119
 
 
120
 
 
121
            // Map index
 
122
            m_indexMap[childAction] = QPersistentModelIndex(child);
 
123
 
 
124
            // Add action to action group:
 
125
            m_actions->addAction(childAction);
 
126
 
 
127
            // Add action to menu:
 
128
            parentMenu->addAction(childAction);
 
129
        }
 
130
    }
 
131
}
 
132
 
 
133
void BtMenuView::slotAboutToShow() {
 
134
    if (m_actions != 0) {
 
135
        delete m_actions;
 
136
        m_actions = 0;
 
137
    }
 
138
    m_indexMap.clear();
 
139
 
 
140
    if (m_model == 0) return;
 
141
 
 
142
    m_actions = new QActionGroup(this);
 
143
    connect(m_actions, SIGNAL(triggered(QAction*)),
 
144
            this, SLOT(slotActionTriggered(QAction*)));
 
145
 
 
146
    QModelIndex parentIndex;
 
147
    buildMenu(this, parentIndex);
 
148
}
 
149
 
 
150
void BtMenuView::slotAboutToHide() {
 
151
    clear();
 
152
}
 
153
 
 
154
void BtMenuView::slotActionTriggered(QAction *action) {
 
155
    Q_ASSERT(m_indexMap.contains(action));
 
156
    QPersistentModelIndex itemIndex(m_indexMap.value(action));
 
157
    if (itemIndex.isValid()) {
 
158
        emit triggered(itemIndex);
 
159
    }
 
160
}
 
161