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

« back to all changes in this revision

Viewing changes to src/frontend/displaywindow/btmodulechooserbar.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
* This file is part of BibleTime's source code, http://www.bibletime.info/.
 
4
*
 
5
* Copyright 1999-2008 by the BibleTime developers.
 
6
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
 
7
*
 
8
**********/
 
9
 
 
10
#include "frontend/displaywindow/btmodulechooserbar.h"
 
11
#include "frontend/displaywindow/btmodulechooserbutton.h"
 
12
#include "creadwindow.h"
 
13
 
 
14
#include <QAction>
 
15
#include <QDebug>
 
16
#include <QList>
 
17
#include <QToolBar>
 
18
 
 
19
 
 
20
BtModuleChooserBar::BtModuleChooserBar(QStringList useModules, CSwordModuleInfo::ModuleType type, CReadWindow *parent)
 
21
        : QToolBar(parent),
 
22
        BtWindowModuleChooser(parent, type),
 
23
        m_idCounter(0) {
 
24
 
 
25
    qDebug() << "BtModuleChooserBar::BtModuleChooserBar";
 
26
    setAllowedAreas(Qt::TopToolBarArea);
 
27
    setFloatable(false);
 
28
    setModules(useModules);
 
29
    connect(parent, SIGNAL(sigModuleListSet(QStringList)), SLOT(slotBackendModulesChanged()));
 
30
    connect(parent, SIGNAL(sigModuleListChanged()), SLOT(slotWindowModulesChanged()));
 
31
}
 
32
 
 
33
void BtModuleChooserBar::slotBackendModulesChanged() {
 
34
    backendModulesChanged();
 
35
}
 
36
 
 
37
void BtModuleChooserBar::backendModulesChanged() {
 
38
    m_modules = m_window->getModuleList();
 
39
 
 
40
    adjustButtonCount();
 
41
 
 
42
    //recreate all menus from scratch
 
43
    for (int i = 0; i < m_buttonList.count(); i++) {
 
44
        BtModuleChooserButton* button = m_buttonList.at(i);
 
45
        QString moduleName = (i >= m_modules.count()) ? QString::null : m_modules.at(i);
 
46
        qDebug() << "refresh button's menu:" << moduleName << i;
 
47
        button->recreateMenu(m_modules, moduleName, i);
 
48
    }
 
49
}
 
50
 
 
51
void BtModuleChooserBar::slotWindowModulesChanged() {
 
52
    windowModulesChanged();
 
53
}
 
54
 
 
55
void BtModuleChooserBar::adjustButtonCount(bool adjustToZero) {
 
56
    //qDebug() << "BtModuleChooserBar::ajustButtonCount";
 
57
    int buttonCountDifference = 0;
 
58
    if (adjustToZero) {
 
59
        buttonCountDifference = m_buttonList.count();
 
60
    }
 
61
    else {
 
62
        buttonCountDifference = m_buttonList.count() - (m_modules.count() + 1);
 
63
    }
 
64
    if (m_moduleType == CSwordModuleInfo::GenericBook && !adjustToZero) {
 
65
        buttonCountDifference = (1 - m_buttonList.count()) * -1;
 
66
    }
 
67
    //if there are more buttons than modules, delete buttons
 
68
    if (buttonCountDifference > 0) {
 
69
        for (int j = 0; j < buttonCountDifference; j++) {
 
70
            //qDebug() << "delete first button, " << j;
 
71
            // it should be safe to delete the button later
 
72
            BtModuleChooserButton* b = m_buttonList.takeFirst();
 
73
            b->setParent(0);
 
74
            b->deleteLater();
 
75
        }
 
76
    }
 
77
    // if there are more modules than buttons, add buttons
 
78
    if (buttonCountDifference < 0) {
 
79
        for (int i = (buttonCountDifference * (-1)); i > 0; i--) {
 
80
            addButton();
 
81
        }
 
82
    }
 
83
}
 
84
 
 
85
void BtModuleChooserBar::windowModulesChanged() {
 
86
    //qDebug() << "BtModuleChooserBar::windowModulesChanged";
 
87
    m_modules = m_window->getModuleList();
 
88
    adjustButtonCount();
 
89
    updateButtonMenus();
 
90
}
 
91
 
 
92
BtModuleChooserButton* BtModuleChooserBar::addButton() {
 
93
    //qDebug() << "BtModuleChooserBar::addButton";
 
94
    BtModuleChooserButton* b = new BtModuleChooserButton(this, m_moduleType);
 
95
    QAction* a = addWidget(b);
 
96
    m_buttonList.append(b);
 
97
 
 
98
    // the button sends signals directly to the window which then signals back when the module
 
99
    // list has changed
 
100
    connect(b, SIGNAL(sigModuleAdd(int, QString)), m_window, SLOT(slotAddModule(int, QString)));
 
101
    connect(b, SIGNAL(sigModuleReplace(int, QString)), m_window, SLOT(slotReplaceModule(int, QString)));
 
102
    connect(b, SIGNAL(sigModuleRemove(int)), m_window, SLOT(slotRemoveModule(int)));
 
103
 
 
104
    a->setVisible(true);
 
105
    return b;
 
106
}
 
107
 
 
108
/** Sets the modules which are chosen in this module chooser bar. */
 
109
void BtModuleChooserBar::setModules( QStringList useModules ) {
 
110
    qDebug() << "BtModuleChooserBar::setModules";
 
111
    m_modules = useModules;
 
112
    adjustButtonCount(true);
 
113
 
 
114
    //if (!useModules.count()) return;
 
115
    for (int i = 0; i < useModules.count(); i++) {
 
116
        addButton();
 
117
    }
 
118
    if (!(m_moduleType == CSwordModuleInfo::GenericBook)) {
 
119
        addButton(); // for ADD button
 
120
    }
 
121
    updateButtonMenus();
 
122
    qDebug() << "BtModuleChooserBar::setModules end";
 
123
}
 
124
 
 
125
void BtModuleChooserBar::updateButtonMenus() {
 
126
    //qDebug() << "BtModuleChooserBar::updateMenuItems";
 
127
 
 
128
    for (int i = 0; i < m_buttonList.count(); i++) {
 
129
        BtModuleChooserButton* button = m_buttonList.at(i);
 
130
        QString moduleName = (i >= m_modules.count()) ? QString::null : m_modules.at(i);
 
131
        //qDebug() << "refresh button's menu:" << moduleName << i;
 
132
        button->updateMenu(m_modules, moduleName, i);
 
133
    }
 
134
}