summaryrefslogtreecommitdiff
path: root/src/frontend/cmodulechooserdialog.cpp
blob: e7b96c84090a8d81756d314c0201c533fa4c078f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2009 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "cmodulechooserdialog.h"

#include "backend/drivers/cswordmoduleinfo.h"
#include "backend/managers/cswordbackend.h"
#include "backend/btmoduletreeitem.h"

#include "util/cpointers.h"
#include "util/cresmgr.h"
#include "util/ctoolclass.h"
#include "util/dialogutil.h"
#include "util/directoryutil.h"
#include "backend/config/cbtconfig.h"

#include <QDialog>
#include <QButtonGroup>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QTreeWidget>
#include <QVBoxLayout>
#include <QStringList>
#include <QDebug>
#include <QHeaderView>
#include <QLabel>


CModuleChooserDialog::CModuleChooserDialog( QWidget* parent, QString title, QString label, QList<CSwordModuleInfo*>*  allModules)
	: QDialog(parent),
	m_title(title),
	m_labelText(label)
{
	m_grouping = (BTModuleTreeItem::Grouping)CBTConfig::get(CBTConfig::bookshelfGrouping);
	m_filters = QList<BTModuleTreeItem::Filter*>();
	if (!allModules) {
		m_moduleList = CPointers::backend()->moduleList();
	}
	else m_moduleList = *allModules;
}

/**
* Call init() after the constructor, either in the end of your own constructor or from outside.
*/
void CModuleChooserDialog::init()
{
	//Set the flag to destroy when closed - otherwise eats memory
	setAttribute(Qt::WA_DeleteOnClose);
	setWindowTitle(m_title);
	initView();
	initTree();
}

/** Initializes the view of this dialog */
void CModuleChooserDialog::initView()
{
	//TODO: choose the button text?
	
	QVBoxLayout *vboxLayout;
	QHBoxLayout *hboxLayout;
	QSpacerItem *spacerItem;

	vboxLayout = new QVBoxLayout(this);

	QLabel* label = CToolClass::explanationLabel(this, QString::null, m_labelText);
	vboxLayout->addWidget(label);

	m_moduleChooser = new QTreeWidget(this);
	m_moduleChooser->header()->hide();

	vboxLayout->addWidget(m_moduleChooser);

	hboxLayout = new QHBoxLayout();

	spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
	hboxLayout->addItem(spacerItem);

	m_buttonBox = new QDialogButtonBox(this);
	m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
	util::prepareDialogBox(m_buttonBox);
	hboxLayout->addWidget(m_buttonBox);

	vboxLayout->addLayout(hboxLayout);
	
	QObject::connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(slotOk()) );
	//The QDialog doc is a bit unclear but calling reject also destroys the dialog
	// in this situation.
	QObject::connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject()) );
}


void CModuleChooserDialog::initTree()
{
	//qDebug("CModuleChooserDialog::initTree");

	// See BTModuleTreeItem documentation.
	BTModuleTreeItem root(m_filters, m_grouping, &m_moduleList);
	createModuleTree(&root, m_moduleChooser->invisibleRootItem());
	
}

void CModuleChooserDialog::createModuleTree(BTModuleTreeItem* item, QTreeWidgetItem* widgetItem)
{
	foreach (BTModuleTreeItem* i, item->children()) {
		createModuleTree(i, new QTreeWidgetItem(widgetItem));
	}
	if (item->type() != BTModuleTreeItem::Root) {
		widgetItem->setText(0, item->text());
		if (item->type() == BTModuleTreeItem::Category || item->type() == BTModuleTreeItem::Language) {
			widgetItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsTristate);
		}
		if (item->type() == BTModuleTreeItem::Module) {
			initModuleItem(item, widgetItem);
		}
	}
}

			
/** Emits the signal with the list of the selected modules. */
void CModuleChooserDialog::slotOk()
{
	Q_ASSERT(m_moduleChooser);
	//create the list of selected modules
	QList<CSwordModuleInfo*> mods;
	QTreeWidgetItemIterator it( m_moduleChooser );
	for ( ; *it; ++it ) {
		//add the module to list if the box is checked
		if ((*it)->checkState(0) == Qt::Checked) {
			qDebug("was checked");
			for (QList<CSwordModuleInfo*>::iterator all_iter(m_moduleList.begin()); all_iter != m_moduleList.end(); ++all_iter) {
				if ((*all_iter)->name() == (*it)->text(0)) {
					qDebug("append");
					mods.append(*all_iter);
					break;
				}
			}
			
		}
	}

	// The selection is handled first, then the dialog is closed and destroyed.
	emit modulesChanged(mods, m_moduleChooser);
	QDialog::done(QDialog::Accepted);
}

QPushButton* CModuleChooserDialog::okButton()
{
	return m_buttonBox->button(QDialogButtonBox::Ok);
}