summaryrefslogtreecommitdiff
path: root/src/frontend/bookshelfmanager/btconfigdialog.cpp
blob: e5d0fd1a10cf53cc3cc851b21fc9e74dacf8b4e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2014 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/bookshelfmanager/btconfigdialog.h"

#include <QDialog>
#include <QDialogButtonBox>
#include <QEvent>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QListView>
#include <QStackedWidget>
#include <QVBoxLayout>


BtConfigDialog::BtConfigDialog(QWidget* parent, Qt::WindowFlags flags)
        : QDialog(parent, flags)
        , m_buttonBoxRuler(0)
        , m_buttonBox(0)
        , m_maxItemWidth(0)
        , m_previousPageIndex(-2)
{
    m_contentsList = new QListWidget(this);
    m_contentsList->setViewMode(QListView::IconMode);
    m_contentsList->setMovement(QListView::Static);

    m_pageWidget = new QStackedWidget(this);
    m_pageWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);

    m_pageLayout = new QVBoxLayout;
    m_pageLayout->addWidget(m_pageWidget);

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(m_contentsList);
    mainLayout->addLayout(m_pageLayout);

    connect(m_contentsList, SIGNAL(currentRowChanged(int)),
            this,           SLOT(slotChangePage(int)));
}

void BtConfigDialog::addPage(Page* pageWidget) {

    m_pageWidget->addWidget(pageWidget);

    QListWidgetItem* item = new QListWidgetItem(m_contentsList);
    item->setIcon(pageWidget->icon());
    item->setText(pageWidget->headerText());
    item->setTextAlignment(Qt::AlignHCenter);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    pageWidget->setListWidgetItem(item);

    //set the list width - it may bee too wide (if there were no pages) or too narrow
    if (m_maxItemWidth < m_contentsList->visualItemRect(item).width()) {
        m_maxItemWidth = m_contentsList->visualItemRect(item).width();
        m_contentsList->setFixedWidth( m_maxItemWidth + (m_contentsList->frameWidth()*2) );
    }
    // all items should has the same width
    for (int i = 0; i < m_contentsList->count(); ++i) {
        m_contentsList->item(i)->setSizeHint(QSize(m_maxItemWidth, m_contentsList->visualItemRect(m_contentsList->item(i)).height()) );
    }

    setCurrentPage(m_contentsList->row(item));
}

void BtConfigDialog::setButtonBox(QDialogButtonBox *box) {
    Q_ASSERT(box != 0);
    Q_ASSERT(m_buttonBox == 0);
    Q_ASSERT(m_buttonBoxRuler == 0);

    m_buttonBox = box;

    // First add a horizontal ruler:
    m_buttonBoxRuler = new QFrame(this);
    m_buttonBoxRuler->setGeometry(QRect(1, 1, 1, 3));
    m_buttonBoxRuler->setFrameShape(QFrame::HLine);
    m_buttonBoxRuler->setFrameShadow(QFrame::Sunken);
    m_buttonBoxRuler->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    m_pageLayout->addWidget(m_buttonBoxRuler);

    // Add button box:
    m_pageLayout->addWidget(box);
}

void BtConfigDialog::setCurrentPage(int newIndex) {
    if (m_previousPageIndex != newIndex) {
        m_previousPageIndex = newIndex;
        m_contentsList->setCurrentRow(newIndex);
        m_pageWidget->setCurrentIndex(newIndex);
    }
}

void BtConfigDialog::slotChangePage(int newIndex) {
    /*
      This check is in place here because this slot is indirectly called by the
      setCurrentPage method.
    */
    if (m_previousPageIndex != newIndex) {
        m_previousPageIndex = newIndex;
        m_pageWidget->setCurrentIndex(newIndex);
    }
}