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

« back to all changes in this revision

Viewing changes to src/frontend/mainindex/bookshelf/cbookshelfindex.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden
  • Date: 2009-11-18 17:30:00 UTC
  • mfrom: (1.3.4 upstream) (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091118173000-endkhjz5qai88tvr
Tags: 2.4-1
* New upstream version 2.4
* debian/control: 
  - Replace incorrect bibletime-data Depends on lib4qt-gui
    with bibletime Depends on libqtgui4 (>= 4.4.0). (Closes: #556209).
  - Add Build-depends: on zlib1g-dev and libcurl4-gnutls-dev 
    (Closes: #556805).

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 "cbookshelfindex.h"
11
 
 
12
 
#include "btindexitem.h"
13
 
#include "btindexmodule.h"
14
 
#include "btindexfolder.h"
15
 
#include "chidemodulechooserdialog.h"
16
 
 
17
 
#include "backend/btmoduletreeitem.h"
18
 
#include "backend/managers/creferencemanager.h"
19
 
 
20
 
#include "frontend/searchdialog/csearchdialog.h"
21
 
#include "backend/config/cbtconfig.h"
22
 
#include "frontend/cinfodisplay.h"
23
 
#include "frontend/btaboutmoduledialog.h"
24
 
#include "frontend/cprinter.h"
25
 
#include "frontend/cdragdrop.h"
26
 
 
27
 
#include "util/cresmgr.h"
28
 
#include "util/directoryutil.h"
29
 
 
30
 
//Qt includes
31
 
#include <QDragLeaveEvent>
32
 
#include <QDragMoveEvent>
33
 
#include <QDropEvent>
34
 
#include <QHeaderView> //for hiding the header
35
 
#include <QTimer> // for delayed auto-opening of folders
36
 
#include <QList>
37
 
#include <QTreeWidget>
38
 
#include <QTreeWidgetItem>
39
 
#include <QInputDialog> // for unlocking key
40
 
#include <QDebug>
41
 
#include <QMenu>
42
 
 
43
 
#include <boost/scoped_ptr.hpp>
44
 
 
45
 
 
46
 
 
47
 
CBookshelfIndex::CBookshelfIndex(QWidget *parent)
48
 
        : QTreeWidget(parent),
49
 
        m_searchDialog(0),
50
 
        m_autoOpenFolder(0),
51
 
        m_autoOpenTimer(this)
52
 
{
53
 
        m_grouping = (BTModuleTreeItem::Grouping)CBTConfig::get(CBTConfig::bookshelfGrouping);
54
 
        m_showHidden = CBTConfig::get(CBTConfig::bookshelfShowHidden);
55
 
        setContextMenuPolicy(Qt::CustomContextMenu);
56
 
        initView();
57
 
        initConnections();
58
 
        initTree();
59
 
}
60
 
 
61
 
CBookshelfIndex::~CBookshelfIndex() {}
62
 
 
63
 
 
64
 
 
65
 
/** Initializes the view. */
66
 
void CBookshelfIndex::initView()
67
 
{       
68
 
//      qDebug("CBookshelfIndex::initView");
69
 
 
70
 
        header()->hide();
71
 
 
72
 
        setFocusPolicy(Qt::WheelFocus);
73
 
        setAcceptDrops( false ); // TODO: accept drops
74
 
        setDragEnabled( true );
75
 
        setDropIndicatorShown( true );
76
 
 
77
 
        setItemsExpandable(true);
78
 
        viewport()->setAcceptDrops(false); //TODO: accept drops
79
 
        setRootIsDecorated(false);
80
 
        setAllColumnsShowFocus(true);
81
 
        setSelectionMode(QAbstractItemView::ExtendedSelection);
82
 
 
83
 
        //setup the popup menu
84
 
        m_popup = new QMenu(viewport());
85
 
        m_popup->setTitle(tr("Bookshelf"));
86
 
 
87
 
        initActions();
88
 
 
89
 
//      qDebug("CBookshelfIndex::initView end");
90
 
}
91
 
 
92
 
void CBookshelfIndex::initActions()
93
 
{
94
 
 
95
 
        // Each action has a type attached to it as a dynamic property, see actionenum.h.
96
 
        // Menuitem and its subitems can have the same type.
97
 
        // Actions can have also "singleItemAction" property if
98
 
        // it supports only one item.
99
 
        // See contextMenu() and BTIndexItem for how these properties are used later.
100
 
 
101
 
        // Actions are added to the popup menu and also to a list for easy foreach access.
102
 
 
103
 
        QMenu* actionMenu = 0;
104
 
        QAction* action = 0;
105
 
 
106
 
        // -------------------------Grouping --------------------------------------
107
 
        actionMenu = new QMenu(tr("Grouping"), this);
108
 
        actionMenu->setIcon(util::filesystem::DirectoryUtil::getIcon(CResMgr::mainIndex::grouping::icon));
109
 
        actionMenu->setProperty("indexActionType", QVariant(Grouping));
110
 
 
111
 
        m_groupingGroup = new QActionGroup(this);
112
 
        QObject::connect(m_groupingGroup, SIGNAL(triggered(QAction*)), this, SLOT(actionChangeGrouping(QAction*)) );
113
 
 
114
 
        //TODO: set the inital checked state
115
 
        action = newQAction(tr("Category/Language"), CResMgr::mainIndex::grouping::icon, 0, 0, 0, this);
116
 
        action->setCheckable(true);
117
 
        action->setProperty("indexActionType", QVariant(Grouping));
118
 
        action->setProperty("grouping", BTModuleTreeItem::CatLangMod);
119
 
        actionMenu->addAction(action);
120
 
        m_groupingGroup->addAction(action);
121
 
        if (m_grouping == BTModuleTreeItem::CatLangMod) action->setChecked(true);
122
 
        m_actionList.append(action);
123
 
 
124
 
        action = newQAction(tr("Category"),CResMgr::mainIndex::grouping::icon, 0, 0, 0, this);
125
 
        action->setCheckable(true);
126
 
        m_groupingGroup->addAction(action);
127
 
        if (m_grouping == BTModuleTreeItem::CatMod) action->setChecked(true);
128
 
        action->setProperty("indexActionType", QVariant(Grouping));
129
 
        action->setProperty("grouping", BTModuleTreeItem::CatMod);
130
 
        actionMenu->addAction(action);
131
 
        m_actionList.append(action);
132
 
 
133
 
        action = newQAction(tr("Language/Category"),CResMgr::mainIndex::grouping::icon, 0, 0, 0, this);
134
 
        action->setCheckable(true);
135
 
        m_groupingGroup->addAction(action);
136
 
        if (m_grouping == BTModuleTreeItem::LangCatMod) action->setChecked(true);
137
 
        actionMenu->addAction(action);
138
 
        action->setProperty("indexActionType", QVariant(Grouping));
139
 
        action->setProperty("grouping", BTModuleTreeItem::LangCatMod);
140
 
        m_actionList.append(action);
141
 
 
142
 
        action = newQAction(tr("Language"),CResMgr::mainIndex::grouping::icon, 0, 0, 0, this);
143
 
        action->setCheckable(true);
144
 
        m_groupingGroup->addAction(action);
145
 
        if (m_grouping == BTModuleTreeItem::LangMod) action->setChecked(true);
146
 
        actionMenu->addAction(action);
147
 
        action->setProperty("indexActionType", QVariant(Grouping));
148
 
        action->setProperty("grouping", BTModuleTreeItem::LangMod);
149
 
        m_actionList.append(action);
150
 
 
151
 
        action = newQAction(tr("Works only"),CResMgr::mainIndex::grouping::icon, 0, 0, 0, this);
152
 
        action->setCheckable(true);
153
 
        m_groupingGroup->addAction(action);
154
 
        if (m_grouping == BTModuleTreeItem::Mod) action->setChecked(true);
155
 
        actionMenu->addAction(action);
156
 
        action->setProperty("indexActionType", QVariant(Grouping));
157
 
        action->setProperty("grouping", BTModuleTreeItem::Mod);
158
 
        m_actionList.append(action);
159
 
        
160
 
        action = m_popup->addMenu(actionMenu);
161
 
        action->setProperty("indexActionType", QVariant(Grouping));
162
 
        m_actionList.append(action);
163
 
 
164
 
 
165
 
        // ------------Hide---------------------
166
 
        action = newQAction(tr("Hide/unhide works..."),CResMgr::mainIndex::search::icon, 0, this, SLOT(actionHideModules()), this);
167
 
        action->setProperty("indexActionType", QVariant(HideModules));
168
 
        //action->setProperty("multiItemAction", QVariant(true));
169
 
        m_popup->addAction(action);
170
 
        m_actionList.append(action);
171
 
 
172
 
// -------------------Show hidden---------------------------
173
 
        action = newQAction(tr("Show hidden"),CResMgr::mainIndex::search::icon, 0, 0, 0, this);
174
 
        action->setProperty("indexActionType", QVariant(ShowAllModules));
175
 
        action->setCheckable(true);
176
 
        QObject::connect(action, SIGNAL(toggled(bool)), this, SLOT(actionShowModules(bool)));
177
 
        if (m_showHidden) action->setChecked(true); else action->setChecked(false);
178
 
        m_popup->addAction(action);
179
 
        m_actionList.append(action);
180
 
 
181
 
 
182
 
        m_popup->addSeparator();
183
 
 
184
 
        //------------------------------------------------------------
185
 
        //----------------- Actions for items ------------------------
186
 
 
187
 
        // -------------------------Edit module --------------------------------
188
 
        actionMenu = new QMenu(tr("Edit"), this);
189
 
        actionMenu->setIcon(util::filesystem::DirectoryUtil::getIcon(CResMgr::mainIndex::editModuleMenu::icon) );
190
 
//      actionMenu->setDelayed(false);
191
 
        actionMenu->setProperty("indexActionType", QVariant(EditModule));
192
 
        actionMenu->setProperty("singleItemAction", QVariant(true));
193
 
        //m_actionList.append(actionMenu);
194
 
 
195
 
        action = newQAction(tr("Plain text..."),CResMgr::mainIndex::editModulePlain::icon, 0, this, SLOT(actionEditModulePlain()), this);
196
 
        actionMenu->addAction(action);
197
 
        m_actionList.append(action);
198
 
        action->setProperty("indexActionType", QVariant(EditModule));
199
 
        action->setProperty("singleItemAction", QVariant(true));
200
 
 
201
 
        action = newQAction(tr("HTML..."),CResMgr::mainIndex::editModuleHTML::icon, 0, this, SLOT(actionEditModuleHTML()), this);
202
 
        actionMenu->addAction(action);
203
 
        m_actionList.append(action);
204
 
        action->setProperty("indexActionType", QVariant(EditModule));
205
 
        action->setProperty("singleItemAction", QVariant(true));
206
 
 
207
 
        m_actionList.append(m_popup->addMenu(actionMenu));
208
 
 
209
 
        
210
 
        // ------------------------ Misc actions -------------------------------------
211
 
        action = newQAction(tr("Search..."),CResMgr::mainIndex::search::icon, 0, this, SLOT(actionSearchInModules()), this);
212
 
        action->setProperty("indexActionType", QVariant(SearchModules));
213
 
        action->setProperty("multiItemAction", QVariant(true));
214
 
        m_popup->addAction(action);
215
 
        m_actionList.append(action);
216
 
 
217
 
        action = newQAction(tr("Unlock..."),CResMgr::mainIndex::unlockModule::icon, 0, this, SLOT(actionUnlockModule()), this);
218
 
        m_popup->addAction(action);
219
 
        action->setProperty("indexActionType", QVariant(UnlockModule));
220
 
        action->setProperty("singleItemAction", QVariant(true));
221
 
        m_actionList.append(action);
222
 
 
223
 
        action = newQAction(tr("About..."),CResMgr::mainIndex::aboutModule::icon, 0, this, SLOT(actionAboutModule()), this);
224
 
        m_popup->addAction(action);
225
 
        action->setProperty("singleItemAction", QVariant(true));
226
 
        action->setProperty("indexActionType", QVariant(AboutModule));
227
 
        m_actionList.append(action);
228
 
 
229
 
        
230
 
}
231
 
 
232
 
/** Convenience function for creating a new QAction.
233
 
* Should be replaced with something better; it was easier to make a new function
234
 
* than to modify all QAction constructors.
235
 
*/
236
 
QAction* CBookshelfIndex::newQAction(const QString& text, const QString& pix, const int /*shortcut*/, const QObject* receiver, const char* slot, QObject* parent)
237
 
{
238
 
        QAction* action = new QAction(util::filesystem::DirectoryUtil::getIcon(pix), text, parent);
239
 
        if (receiver && !QString(slot).isEmpty())
240
 
        {
241
 
                QObject::connect(action, SIGNAL(triggered()), receiver, slot);
242
 
        }
243
 
        return action;
244
 
}
245
 
 
246
 
/** Initialize the SIGNAL<->SLOT connections */
247
 
void CBookshelfIndex::initConnections()
248
 
{
249
 
        //Connect this to the backend module list changes.
250
 
        bool ok;
251
 
        ok = connect(CPointers::backend(), SIGNAL(sigSwordSetupChanged(CSwordBackend::SetupChangedReason)), SLOT(reloadSword(CSwordBackend::SetupChangedReason)));
252
 
        Q_ASSERT(ok);
253
 
        
254
 
        ok = connect(this, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(slotExecuted(QTreeWidgetItem*)));
255
 
        Q_ASSERT(ok);
256
 
 
257
 
        // Single/double click item activation is style dependend
258
 
        if (style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) == 0) 
259
 
        {
260
 
                ok = connect(this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(slotExecuted(QTreeWidgetItem*)));
261
 
                Q_ASSERT(ok);
262
 
        }
263
 
        
264
 
        ok = connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(contextMenu(const QPoint&)));
265
 
        Q_ASSERT(ok);
266
 
 
267
 
        ok = connect(&m_autoOpenTimer, SIGNAL(timeout()), this, SLOT(autoOpenTimeout()));
268
 
        Q_ASSERT(ok);
269
 
 
270
 
        ok = connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(slotModifySelection()));
271
 
        Q_ASSERT(ok);
272
 
}
273
 
 
274
 
void CBookshelfIndex::slotModifySelection()
275
 
{
276
 
        // This creates recursion if a folder is selected, but not infinite.
277
 
        //qDebug("CBookshelfIndex::slotModifySelection");
278
 
        QList<QTreeWidgetItem*> selection = selectedItems();
279
 
        foreach (QTreeWidgetItem* item, selection) {
280
 
                BTIndexFolder* folder = dynamic_cast<BTIndexFolder*>(item);
281
 
                if (folder) {
282
 
                        item->setSelected(false);
283
 
                        break;
284
 
                }
285
 
        }
286
 
}
287
 
 
288
 
/**
289
 
* Hack to get single click and selection working. See slotExecuted.
290
 
*/
291
 
void CBookshelfIndex::mouseReleaseEvent(QMouseEvent* event) {
292
 
        //qDebug("CBookshelfIndex::mouseReleaseEvent");
293
 
        //qDebug() << event->type() << event->modifiers();
294
 
        if (itemAt(event->pos())) {
295
 
                if (m_frozenModules.contains(  itemAt(event->pos())->text(0)  )) {
296
 
                        //do nothing
297
 
                        event->accept();
298
 
                        return;
299
 
                }
300
 
        }
301
 
        m_mouseReleaseEventModifiers = event->modifiers();
302
 
        QTreeWidget::mouseReleaseEvent(event);
303
 
 
304
 
}
305
 
 
306
 
/** Called when an item is clicked with mouse or activated with keyboard. */
307
 
void CBookshelfIndex::slotExecuted( QTreeWidgetItem* i )
308
 
{
309
 
        qDebug("CBookshelfIndex::slotExecuted");
310
 
 
311
 
        //HACK: checking the modifier keys from the last mouseReleaseEvent
312
 
        //depends on executing order: mouseReleaseEvent first, then itemClicked signal
313
 
        int modifiers = m_mouseReleaseEventModifiers;
314
 
        m_mouseReleaseEventModifiers = Qt::NoModifier;
315
 
        if (modifiers != Qt::NoModifier) {
316
 
                return;
317
 
        }
318
 
 
319
 
        BTIndexItem* btItem = dynamic_cast<BTIndexItem*>(i);
320
 
        if (!btItem) {
321
 
                qWarning("item was not BTIndexItem!");
322
 
                return;
323
 
        }
324
 
 
325
 
        if (BTIndexModule* m = dynamic_cast<BTIndexModule*>(i))  { //clicked on a module
326
 
                CSwordModuleInfo* mod = m->moduleInfo();
327
 
                if (!m_frozenModules.contains(mod->name())) {
328
 
                        m_frozenModules.insert(mod->name());
329
 
                        QList<CSwordModuleInfo*> modules;
330
 
                        modules.append(mod);
331
 
                        qDebug("will emit createReadDisplayWindow");
332
 
                        emit createReadDisplayWindow(modules, QString::null);
333
 
                        
334
 
                }
335
 
        } else {
336
 
                i->setExpanded( !i->isExpanded() );
337
 
        }
338
 
}
339
 
 
340
 
void CBookshelfIndex::unfreezeModules(QList<CSwordModuleInfo*> modules)
341
 
{
342
 
        foreach (CSwordModuleInfo* mInfo, modules) {
343
 
                m_frozenModules.remove(mInfo->name());
344
 
        }
345
 
}
346
 
 
347
 
/** Reimplementation. Returns the drag object for the current selection. */
348
 
QMimeData* CBookshelfIndex::dragObject()
349
 
{
350
 
        //TODO: we have to add a mime type "module" if we want to for example enable draggin a module to a displaywindow
351
 
        return 0;
352
 
}
353
 
 
354
 
 
355
 
/** Reimplementation from QTreeWidget. Returns true if the drag is acceptable for the widget. */
356
 
void CBookshelfIndex::dragEnterEvent( QDragEnterEvent* event )
357
 
{
358
 
        //qDebug("CBookshelfIndex::dragEnterEvent");
359
 
        event->acceptProposedAction();
360
 
}
361
 
 
362
 
void CBookshelfIndex::dragMoveEvent( QDragMoveEvent* event )
363
 
{
364
 
        const QPoint pos = event->pos();
365
 
 
366
 
        BTIndexItem* i = dynamic_cast<BTIndexItem*>(itemAt(pos));
367
 
        //TODO: implement accepting drop in item
368
 
        if (i && i->acceptDrop(event->mimeData()) ) {
369
 
                event->acceptProposedAction();
370
 
        } else {
371
 
                event->ignore();
372
 
        }
373
 
}
374
 
 
375
 
void CBookshelfIndex::dropEvent( QDropEvent* event )
376
 
{
377
 
        const QPoint pos = event->pos();
378
 
 
379
 
        BTIndexItem* i = dynamic_cast<BTIndexItem*>(itemAt(pos));
380
 
        //TODO: implement accepting drop in item
381
 
        if (i && i->acceptDrop(event->mimeData()) ) {
382
 
                QMenu* menu = new QMenu(this);
383
 
                QAction* openAction = menu->addAction("Open reference in new subwindow");
384
 
                QAction* searchRefAction = menu->addAction("Search for reference as crossreference");
385
 
                QAction* searchAction = menu->addAction("Search text");
386
 
                QAction* selectedAction = menu->exec(this->mapToGlobal(pos));
387
 
                if (selectedAction == openAction) {;}
388
 
                if (selectedAction == searchAction) {;}
389
 
                if (selectedAction == searchRefAction) {;}
390
 
                event->acceptProposedAction();
391
 
        } else {
392
 
                event->ignore();
393
 
        }
394
 
}
395
 
 
396
 
void CBookshelfIndex::changeEvent(QEvent* e)
397
 
{
398
 
        if (e->type() == QEvent::StyleChange) {
399
 
                // Single/double click item activation is style dependend
400
 
                QObject::disconnect(this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(slotExecuted(QTreeWidgetItem*)));
401
 
                if (style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick) == 0) {
402
 
                        QObject::connect(this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(slotExecuted(QTreeWidgetItem*)));
403
 
                }
404
 
        }
405
 
        QTreeWidget::changeEvent(e);
406
 
}
407
 
 
408
 
/** No descriptions */
409
 
void CBookshelfIndex::initTree() {
410
 
        qDebug("CBookshelfIndex::initTree");
411
 
        
412
 
        //first clean the tree
413
 
        clear();
414
 
        //m_grouping = (BTModuleTreeItem::Grouping)CBTConfig::get(CBTConfig::bookshelfGrouping);
415
 
        
416
 
        BTModuleTreeItem::HiddenOff hiddenFilter;
417
 
        QList<BTModuleTreeItem::Filter*> filters;
418
 
        if (!m_showHidden) {
419
 
                filters.append(&hiddenFilter);
420
 
        }
421
 
        BTModuleTreeItem root(filters, m_grouping);
422
 
        addToTree(&root, this->invisibleRootItem());
423
 
}
424
 
 
425
 
void CBookshelfIndex::addToTree(BTModuleTreeItem* item, QTreeWidgetItem* widgetItem)
426
 
{
427
 
        foreach (BTModuleTreeItem* i, item->children()) {
428
 
                if (i->type() == BTModuleTreeItem::Module) {
429
 
                        addToTree(i, new BTIndexModule(i, widgetItem));
430
 
                }
431
 
                else
432
 
                        addToTree(i, new BTIndexFolder(i, widgetItem));
433
 
        }
434
 
        // Possible TODO: if item is Language and it's under Category and Category is Glossaries,
435
 
        // add the second language name - but how to add other language group?
436
 
        // do we have to modify btmoduletreeitem?
437
 
}
438
 
 
439
 
 
440
 
/** No descriptions */
441
 
void CBookshelfIndex::emitModulesChosen( QList<CSwordModuleInfo*> modules, QString key ) {
442
 
        emit createReadDisplayWindow(modules, key);
443
 
}
444
 
 
445
 
 
446
 
 
447
 
/** Shows the context menu at the given position. */
448
 
void CBookshelfIndex::contextMenu(const QPoint& p) {
449
 
        qDebug("CBookshelfIndex::contextMenu");
450
 
        qDebug() << "list of actions: ";
451
 
        foreach (QAction* action, m_actionList) {
452
 
                qDebug() << action->text();
453
 
        }
454
 
        //setup menu entries depending on current selection
455
 
        QTreeWidgetItem* i = itemAt(p);
456
 
        qDebug() << "item at point: " << i;
457
 
        QList<QTreeWidgetItem *> items = selectedItems();
458
 
        //The item which was clicked may not be selected - but don't add folder
459
 
        if (i  && !dynamic_cast<BTIndexFolder*>(i) && !items.contains(i))
460
 
                items.append(i);
461
 
 
462
 
        // disable those menu entries which are item dependent,
463
 
        // dis/enable some others
464
 
        qDebug()<<"grouping action type:"<<(int)Grouping;
465
 
        foreach (QAction* action, m_actionList) {
466
 
                IndexAction actionType = (IndexAction)action->property("indexActionType").toInt();
467
 
                qDebug() << "action type:" << actionType;
468
 
                if ( actionType == Grouping ) {
469
 
                        qDebug()<<"grouping action, enabling...";
470
 
                        action->setEnabled(true);
471
 
                        qDebug()<< action->isEnabled();
472
 
                }
473
 
                else if (actionType == ShowAllModules) {
474
 
                        //enabled only if there are hidden modules
475
 
                        if (!CBTConfig::get(CBTConfig::hiddenModules).empty()) {
476
 
                                action->setEnabled(true);
477
 
                        } else {
478
 
                                action->setEnabled(false);
479
 
                        }
480
 
                        action->setChecked(m_showHidden);
481
 
                        
482
 
                }
483
 
                else if (actionType == SearchModules) {
484
 
                        action->setText(tr("Search"));
485
 
                        if (items.count() > 0)
486
 
                                action->setEnabled(true);
487
 
                        else action->setEnabled(false);
488
 
                }
489
 
                else if (actionType == HideModules) {
490
 
                        //action->setText(tr("Hide"));
491
 
                        action->setEnabled(true);
492
 
                }
493
 
                else action->setEnabled(false);
494
 
        }
495
 
        
496
 
 
497
 
        if (items.count() == 0) { 
498
 
                //special handling for no selection: do nothing
499
 
        }
500
 
 
501
 
        if (items.count() == 1) { 
502
 
                //special handling for one selected item
503
 
                qDebug("there was one selected item");
504
 
                BTIndexItem* btItem = dynamic_cast<BTIndexItem*>(items.at(0));
505
 
 
506
 
                if (btItem && !dynamic_cast<BTIndexFolder*>(btItem)) {
507
 
                        foreach (QAction* action, m_actionList) {
508
 
                                if ( (IndexAction)action->property("indexActionType").toInt() == AboutModule ) {
509
 
                                        qDebug() << "enabling action" << action->text();
510
 
                                        action->setEnabled(true);
511
 
                                }
512
 
                                else if ((IndexAction)action->property("indexActionType").toInt() == SearchModules ) {
513
 
                                        // Change the text of the menu item to reflect the module name
514
 
                                        BTIndexModule* modItem = dynamic_cast<BTIndexModule*>(btItem);
515
 
                                        if (modItem) {
516
 
                                                CSwordModuleInfo* info = modItem->moduleInfo();
517
 
                                                action->setText(tr("Search in %1...").arg(info->name()));
518
 
                                        }
519
 
                                }
520
 
                                else {
521
 
                                        qDebug() << "ask item" << items.at(0)->text(0) << "to enable the action" << action->text();
522
 
                                        btItem->enableAction(action);
523
 
                                }
524
 
                                qDebug() << "action enabled is: " << action->isEnabled();
525
 
                        }
526
 
                }
527
 
        }
528
 
        else { // more than one item
529
 
                foreach (QAction* action, m_actionList) {
530
 
                        // Change the text of some menu items to reflect multiple selection
531
 
                        if ((IndexAction)action->property("indexActionType").toInt() == SearchModules ) {
532
 
                                action->setText(tr("Search in selected..."));
533
 
                        }
534
 
                        // Enable items
535
 
                        foreach(QTreeWidgetItem* item, items) {
536
 
                                BTIndexItem* btItem = dynamic_cast<BTIndexItem*>(item);
537
 
                                if (btItem && !action->property("singleItemAction").isValid()) {
538
 
                                        btItem->enableAction(action);
539
 
                                }
540
 
                        }
541
 
                }
542
 
        }
543
 
        
544
 
        //finally, open the popup
545
 
        m_popup->exec(mapToGlobal(p));
546
 
}
547
 
 
548
 
void CBookshelfIndex::actionChangeGrouping(QAction* action)
549
 
{
550
 
        BTModuleTreeItem::Grouping grouping = (BTModuleTreeItem::Grouping)action->property("grouping").toInt();
551
 
        m_grouping = grouping;
552
 
        CBTConfig::set(CBTConfig::bookshelfGrouping, grouping);
553
 
        initTree();
554
 
}
555
 
 
556
 
 
557
 
/** Opens the searchdialog for the selected modules. */
558
 
void CBookshelfIndex::actionSearchInModules() {
559
 
        QList<QTreeWidgetItem *> items = selectedItems();
560
 
        QListIterator<QTreeWidgetItem *> it(items);
561
 
        QList<CSwordModuleInfo*> modules;
562
 
        while(it.hasNext()) {
563
 
                if (BTIndexModule* i = dynamic_cast<BTIndexModule*>(it.next())) {
564
 
                        if (i->moduleInfo()) {
565
 
                                modules.append(i->moduleInfo());
566
 
                        }
567
 
                }
568
 
        }
569
 
 
570
 
        if (modules.isEmpty()) { //get a list of useful default modules for the search if no modules were selected
571
 
                CSwordModuleInfo* m = CBTConfig::get(CBTConfig::standardBible);
572
 
                if (m) {
573
 
                        modules.append(m);
574
 
                }
575
 
        }
576
 
 
577
 
        Search::CSearchDialog::openDialog(modules, QString::null);
578
 
}
579
 
 
580
 
/** Unlocks the current module. */
581
 
void CBookshelfIndex::actionUnlockModule() {
582
 
        if (BTIndexModule* i = dynamic_cast<BTIndexModule*>(currentItem())) {
583
 
                bool ok = false;
584
 
                const QString unlockKey =
585
 
                        QInputDialog::getText(
586
 
                                this,
587
 
                                tr("Unlock Work"),
588
 
                                tr("Enter the unlock key for this work."),
589
 
                                QLineEdit::Normal,
590
 
                                i->moduleInfo()->config(CSwordModuleInfo::CipherKey),
591
 
                                &ok
592
 
                        );
593
 
                
594
 
                if (ok) {
595
 
                        i->moduleInfo()->unlock( unlockKey );
596
 
                        CPointers::backend()->reloadModules(CSwordBackend::OtherChange);
597
 
                }
598
 
        }
599
 
}
600
 
 
601
 
void CBookshelfIndex::actionShowModules(bool checked)
602
 
{
603
 
        qDebug("CBookshelfIndex::actionShowModules");
604
 
        m_showHidden = checked;
605
 
        CBTConfig::set(CBTConfig::bookshelfShowHidden, m_showHidden);
606
 
        // show hidden status is changed, notify others who may rebuild their module lists
607
 
        CPointers::backend()->notifyChange(CSwordBackend::HidedModules);
608
 
 
609
 
}
610
 
 
611
 
void CBookshelfIndex::actionHideModules()
612
 
{
613
 
        qDebug("CBookshelfIndex::actionHideModules");
614
 
        QString current;
615
 
        if (BTIndexModule* i = dynamic_cast<BTIndexModule*>(currentItem())) {
616
 
                current = i->text(0);
617
 
        }
618
 
 
619
 
        QString title(tr("Hide/Unhide Works"));
620
 
        QString label(tr("Select the works to be hidden."));
621
 
        CHideModuleChooserDialog* dlg = new CHideModuleChooserDialog(this, title, label, current);
622
 
        connect(dlg, SIGNAL(modulesChanged(QList<CSwordModuleInfo*>)),
623
 
                        this, SLOT(setHiddenModules(QList<CSwordModuleInfo*>)));
624
 
        int code = dlg->exec();
625
 
        if (code == QDialog::Accepted) {
626
 
                // notify all who may rebuild their module lists
627
 
                CPointers::backend()->notifyChange(CSwordBackend::HidedModules);
628
 
        }
629
 
}
630
 
 
631
 
 
632
 
/** Shows information about the current module. */
633
 
void CBookshelfIndex::actionAboutModule() {
634
 
        if (BTIndexModule* i = dynamic_cast<BTIndexModule*>(currentItem())) {
635
 
                BTAboutModuleDialog* dialog = new BTAboutModuleDialog(this, i->moduleInfo());
636
 
                dialog->show();
637
 
                dialog->raise();
638
 
        }
639
 
}
640
 
 
641
 
/** Reimplementation. Takes care of movable items. */
642
 
void CBookshelfIndex::startDrag(Qt::DropActions /*supportedActions*/) {
643
 
 
644
 
}
645
 
 
646
 
/** Reimplementation to support the items dragEnter and dragLeave functions. */
647
 
void CBookshelfIndex::contentsDragMoveEvent( QDragMoveEvent* /*event*/ ) {
648
 
//      //  qWarning("void CBookshelfIndex:: drag move event ( QDragLeaveEvent* e )");
649
 
//      CIndexItemBase* i = dynamic_cast<CIndexItemBase*>( itemAt( contentsToViewport(event->pos())) );
650
 
//      if (i) {
651
 
//              if (i->allowAutoOpen(event) || (i->acceptDrop(event) && i->isFolder() && i->allowAutoOpen(event) && !i->isOpen() && autoOpen()) ) {
652
 
//                      if (m_autoOpenFolder != i)  {
653
 
//                              m_autoOpenTimer.stop();
654
 
//                      }
655
 
//                      
656
 
//                      m_autoOpenFolder = i;
657
 
//                      m_autoOpenTimer.start( 400 );
658
 
//              }
659
 
//              else {
660
 
//                      m_autoOpenFolder = 0;
661
 
//              }
662
 
//      }
663
 
//      else {
664
 
//              m_autoOpenFolder = 0;
665
 
//      }
666
 
// 
667
 
//      QTreeWidget::contentsDragMoveEvent(event);
668
 
}
669
 
 
670
 
 
671
 
void CBookshelfIndex::autoOpenTimeout() {
672
 
        m_autoOpenTimer.stop();
673
 
        if (m_autoOpenFolder && !m_autoOpenFolder->isExpanded() && m_autoOpenFolder->childCount()) {
674
 
                m_autoOpenFolder->setExpanded(true);
675
 
        }
676
 
}
677
 
 
678
 
/** No descriptions */
679
 
void CBookshelfIndex::contentsDragLeaveEvent( QDragLeaveEvent* /*e*/ ) {
680
 
//      m_autoOpenTimer.stop();
681
 
//      QTreeWidget::contentsDragLeaveEvent(e);
682
 
}
683
 
 
684
 
 
685
 
/** Opens an editor window to edit the modules content. */
686
 
void CBookshelfIndex::actionEditModulePlain() {
687
 
        QList<CSwordModuleInfo*> modules;
688
 
        QList<QTreeWidgetItem *> items = selectedItems();
689
 
        QListIterator<QTreeWidgetItem *> it(items);
690
 
        //loop through items
691
 
        while(it.hasNext()) {
692
 
                if (BTIndexModule* i = dynamic_cast<BTIndexModule*>(it.next())) {
693
 
                        modules.append(i->moduleInfo());
694
 
                }
695
 
        }
696
 
        if (modules.count() == 1) {
697
 
                emit createWriteDisplayWindow(modules.first(), QString::null, CDisplayWindow::PlainTextWindow);
698
 
        };
699
 
}
700
 
 
701
 
/** Opens an editor window to edit the modules content. */
702
 
void CBookshelfIndex::actionEditModuleHTML() {
703
 
        QList<CSwordModuleInfo*> modules;
704
 
        QList<QTreeWidgetItem *> items = selectedItems();
705
 
        QListIterator<QTreeWidgetItem *> it(items);
706
 
        while(it.hasNext()) {
707
 
                if (BTIndexModule* i = dynamic_cast<BTIndexModule*>(it.next())) {
708
 
                        modules.append(i->moduleInfo());
709
 
                }
710
 
        }
711
 
        
712
 
        if (modules.count() == 1) {
713
 
                emit createWriteDisplayWindow(modules.first(), QString::null, CDisplayWindow::HTMLWindow);
714
 
        }
715
 
}
716
 
 
717
 
/** Reloads the main index's Sword dependend things like modules */
718
 
void CBookshelfIndex::reloadSword(CSwordBackend::SetupChangedReason) {
719
 
        //reload the modules
720
 
        qDebug("CBookshelfIndex::reloadSword");
721
 
        initTree();
722
 
}