summaryrefslogtreecommitdiff
path: root/src/frontend/displaywindow/cdisplaywindow.cpp
blob: 1285205cad3dd809d7cabb141949ffc8f19c4b8d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2016 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/displaywindow/cdisplaywindow.h"

#include <QCloseEvent>
#include <QDebug>
#include <QMdiSubWindow>
#include <QMenu>
#include <QStringList>
#include <QWidget>
#include "backend/config/btconfig.h"
#include "backend/keys/cswordkey.h"
#include "bibletime.h"
#include "bibletimeapp.h"
#include "frontend/cmdiarea.h"
#include "frontend/display/cdisplay.h"
#include "frontend/displaywindow/bttoolbarpopupaction.h"
#include "frontend/displaywindow/btmodulechooserbar.h"
#include "frontend/displaywindow/btdisplaysettingsbutton.h"
#include "frontend/keychooser/ckeychooser.h"
#include "frontend/keychooser/bthistory.h"
#include "frontend/searchdialog/csearchdialog.h"
#include "util/cresmgr.h"


namespace {

inline QWidget * getProfileWindow(QWidget * w) {
    for (; w; w = w->parentWidget())
        if (QMdiSubWindow * const sw = qobject_cast<QMdiSubWindow *>(w))
            return sw;
    return nullptr;
}

}


CDisplayWindow::CDisplayWindow(const QList<CSwordModuleInfo *> & modules, CMDIArea * parent)
        : QMainWindow(parent),
        m_actionCollection(nullptr),
        m_mdi(parent),
        m_keyChooser(nullptr),
        m_swordKey(nullptr),
        m_isReady(false),
        m_moduleChooserBar(nullptr),
        m_mainToolBar(nullptr),
        m_buttonsToolBar(nullptr),
        m_formatToolBar(nullptr),
        m_headerBar(nullptr),
        m_popupMenu(nullptr),
        m_displayWidget(nullptr),
        m_history(nullptr) {
    setAttribute(Qt::WA_DeleteOnClose); //we want to destroy this window when it is closed
    m_actionCollection = new BtActionCollection(this);
    setModules(modules);

    // Connect this to the backend module list changes
    BT_CONNECT(CSwordBackend::instance(),
               SIGNAL(sigSwordSetupChanged(CSwordBackend::SetupChangedReason)),
               SLOT(reload(CSwordBackend::SetupChangedReason)));
    BibleTime* mainwindow = btMainWindow();
    BT_CONNECT(mainwindow, SIGNAL(toggledTextWindowHeader(bool)),
               SLOT(slotShowHeader(bool)));
    BT_CONNECT(mainwindow, SIGNAL(toggledTextWindowNavigator(bool)),
               SLOT(slotShowNavigator(bool)));
    BT_CONNECT(mainwindow, SIGNAL(toggledTextWindowToolButtons(bool)),
               SLOT(slotShowToolButtons(bool)));
    BT_CONNECT(mainwindow, SIGNAL(toggledTextWindowModuleChooser(bool)),
               SLOT(slotShowModuleChooser(bool)));
    BT_CONNECT(mainwindow, SIGNAL(toggledTextWindowFormatToolbar(bool)),
               SLOT(slotShowFormatToolBar(bool)));
}

CDisplayWindow::~CDisplayWindow() {
    delete m_swordKey;
    m_swordKey = nullptr;
}

BibleTime* CDisplayWindow::btMainWindow() {
    return dynamic_cast<BibleTime*>(m_mdi->parent()->parent());
}

void CDisplayWindow::setToolBarsHidden() {
    // Hide current window toolbars
    if (m_mainToolBar)
        m_mainToolBar->setHidden(true);
    if (m_buttonsToolBar)
        m_buttonsToolBar->setHidden(true);
    if (m_moduleChooserBar)
        m_moduleChooserBar->setHidden(true);
    if (m_formatToolBar)
        m_formatToolBar->setHidden(true);
}
void CDisplayWindow::clearMainWindowToolBars() {
    // Clear main window toolbars, except for works toolbar
    btMainWindow()->navToolBar()->clear();
    btMainWindow()->toolsToolBar()->clear();
    btMainWindow()->formatToolBar()->clear();
}

void CDisplayWindow::windowActivated() {
    clearMainWindowToolBars();
    setupMainWindowToolBars();
}

/** Returns the right window caption. */
const QString CDisplayWindow::windowCaption() {
    if (!m_modules.count()) {
        return QString::null;
    }

    return QString(key()->key()).append(" (").append(m_modules.join(" | ")).append(")");
}

/** Returns the used modules as a pointer list */
const BtConstModuleList CDisplayWindow::modules() const {
    return CSwordBackend::instance()->getConstPointerList(m_modules);
}

/** Store the settings of this window in the given CProfileWindow object. */
void CDisplayWindow::storeProfileSettings(QString const & windowGroup) const {
    BtConfig & conf = btConfig();

    conf.beginGroup(windowGroup);

    QWidget const * const w = getProfileWindow(parentWidget());
    BT_ASSERT(w);

    /**
      \note We don't use saveGeometry/restoreGeometry for MDI subwindows,
            because they give slightly incorrect results with some window
            managers. Might be related to Qt bug QTBUG-7634.
    */
    const QRect rect(w->x(), w->y(), w->width(), w->height());
    conf.setSessionValue<QRect>("windowRect", rect);
    conf.setSessionValue<bool>("staysOnTop",
                               w->windowFlags() & Qt::WindowStaysOnTopHint);
    conf.setSessionValue<bool>("staysOnBottom",
                               w->windowFlags() & Qt::WindowStaysOnBottomHint);
    conf.setSessionValue("maximized", w->isMaximized());

    bool hasFocus = (w == dynamic_cast<CDisplayWindow *>(mdi()->activeSubWindow()));
    conf.setSessionValue("hasFocus", hasFocus);
    // conf.setSessionValue("type", static_cast<int>(modules().first()->type()));

    // Save current key:
    if (CSwordKey * const k = key()) {
        if (sword::VerseKey * const vk = dynamic_cast<sword::VerseKey *>(k)) {
            // Save keys in english only:
            const QString oldLang = QString::fromLatin1(vk->getLocale());
            vk->setLocale("en");
            conf.setSessionValue("key", k->key());
            vk->setLocale(oldLang.toLatin1());
        } else {
            conf.setSessionValue("key", k->key());
        }
    }

    // Save list of modules:
    conf.setSessionValue("modules", m_modules);

    // Default for "not a write window":
    conf.setSessionValue("writeWindowType", int(0));

    conf.endGroup();
}

void CDisplayWindow::applyProfileSettings(const QString & windowGroup) {
    BtConfig & conf = btConfig();
    conf.beginGroup(windowGroup);
    setUpdatesEnabled(false);

    QWidget * const w = getProfileWindow(parentWidget());
    BT_ASSERT(w);

    /**
      \note We don't use restoreGeometry/saveGeometry for MDI subwindows,
            because they give slightly incorrect results with some window
            managers. Might be related to Qt bug QTBUG-7634.
    */
    const QRect rect = conf.sessionValue<QRect>("windowRect");
    w->resize(rect.width(), rect.height());
    w->move(rect.x(), rect.y());
    if (conf.sessionValue<bool>("staysOnTop", false))
        w->setWindowFlags(w->windowFlags() | Qt::WindowStaysOnTopHint);
    if (conf.sessionValue<bool>("staysOnBottom", false))
        w->setWindowFlags(w->windowFlags() | Qt::WindowStaysOnBottomHint);
    if (conf.sessionValue<bool>("maximized"))
        w->showMaximized();

    setUpdatesEnabled(true);
    conf.endGroup();
}

void CDisplayWindow::insertKeyboardActions( BtActionCollection* a ) {
    QAction* actn = new QAction(QIcon(), tr("Select all"), a);
    actn->setShortcut(QKeySequence::SelectAll);
    a->addAction("selectAll", actn);

    actn = new QAction(QIcon(), tr("Copy"), a);
    actn->setShortcut(QKeySequence::Copy);
    a->addAction("copySelectedText", actn);

    actn = new QAction(QIcon(), tr("Find..."), a);
    actn->setShortcut(QKeySequence::Find);
    a->addAction("findText", actn);

    actn = new QAction(QIcon(), tr("Change location"), a);
    actn->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_L));
    a->addAction("openLocation", actn);

    actn = new QAction(CResMgr::displaywindows::general::search::icon(),
                       tr("Search with works of this window"), a);
    actn->setShortcut(CResMgr::displaywindows::general::search::accel);
    a->addAction(CResMgr::displaywindows::general::search::actionName, actn);

    BtToolBarPopupAction* action = new BtToolBarPopupAction(
        CResMgr::displaywindows::general::backInHistory::icon(),
        tr("Back in history"),
        a
    );
    action->setShortcut(CResMgr::displaywindows::general::backInHistory::accel);
    a->addAction(CResMgr::displaywindows::general::backInHistory::actionName, action);

    action = new BtToolBarPopupAction(
        CResMgr::displaywindows::general::forwardInHistory::icon(),
        tr("Forward in history"),
        a
    );
    action->setShortcut(CResMgr::displaywindows::general::forwardInHistory::accel);
    a->addAction(CResMgr::displaywindows::general::forwardInHistory::actionName, action);
}

void CDisplayWindow::initActions() {
    BtActionCollection* ac = actionCollection();

    CDisplayWindow::insertKeyboardActions(ac);

    namespace DWG = CResMgr::displaywindows::general;
    initAction(DWG::search::actionName,
               this,
               &CDisplayWindow::slotSearchInModules);
    initAddAction("openLocation", this, &CDisplayWindow::setFocusKeyChooser);
    CDisplayConnections * const conn = displayWidget()->connectionsProxy();
    initAddAction("selectAll",
                  conn,
                  &CDisplayConnections::selectAll);
    initAddAction("copySelectedText",
                  conn,
                  &CDisplayConnections::copySelection);
    initAddAction("findText",
                  conn,
                  &CDisplayConnections::openFindTextDialog);
    initAddAction(DWG::backInHistory::actionName,
                  keyChooser()->history(),
                  &BTHistory::back);
    initAddAction(DWG::forwardInHistory::actionName,
                  keyChooser()->history(),
                  &BTHistory::fw);

    ac->readShortcuts("Displaywindow shortcuts");
}

/** Refresh the settings of this window. */
void CDisplayWindow::reload(CSwordBackend::SetupChangedReason) {
    { // First make sure all used Sword modules are still present:
        CSwordBackend & backend = *(CSwordBackend::instance());
        QMutableStringListIterator it(m_modules);
        while (it.hasNext())
            if (!backend.findModuleByName(it.next()))
                it.remove();
    }

    if (m_modules.isEmpty()) {
        close();
        return;
    }

    if (CKeyChooser * const kc = keyChooser())
        kc->setModules(modules(), false);

    lookup();

    m_actionCollection->readShortcuts("DisplayWindow shortcuts");
    m_actionCollection->readShortcuts("Readwindow shortcuts");
    emit sigModuleListSet(m_modules);
}

void CDisplayWindow::slotAddModule(int index, QString module) {
    m_modules.insert(index, module);
    lookup();
    modulesChanged();
    emit sigModuleListChanged();
}

void CDisplayWindow::slotReplaceModule(int index, QString newModule) {
    qDebug() << "CDisplayWindow::slotReplaceModule" << m_modules.at(index) << "with" << newModule;
    m_modules.replace(index, newModule);
    qDebug() << "window's new module list:" << m_modules;
    lookup();
    modulesChanged();
    emit sigModuleListChanged();
}

void CDisplayWindow::slotRemoveModule(int index) {
    m_modules.removeAt(index);
    lookup();
    modulesChanged();
    emit sigModuleListChanged();
}

/** Sets the new display options for this window. */
void CDisplayWindow::setDisplayOptions(const DisplayOptions &displayOptions) {
    m_displayOptions = displayOptions;
    emit sigDisplayOptionsChanged(m_displayOptions);
}

/** Sets the new filter options of this window. */
void CDisplayWindow::setFilterOptions(const FilterOptions &filterOptions) {
    m_filterOptions = filterOptions;
    emit sigFilterOptionsChanged(m_filterOptions);
}

/** Returns true if the window may be closed. */
bool CDisplayWindow::queryClose() {
    return true;
}

/** Sets the keychooser widget for this display window. */
void CDisplayWindow::setKeyChooser( CKeyChooser* ck ) {
    m_keyChooser = ck;
}

/** Sets the new sword key. */
void CDisplayWindow::setKey( CSwordKey* key ) {
    BT_ASSERT(key);
    m_swordKey = key;
}

BTHistory* CDisplayWindow::history() {
    if (m_history == nullptr)
        m_history = new BTHistory(this);
    return m_history;
}

void CDisplayWindow::modulesChanged() {
    // this would only set the stringlist again
    //if (moduleChooserBar()) { //necessary for write windows
    //setModules( m_moduleChooserBar->getModuleList() );
    //}
    if (modules().isEmpty()) {
        close();
    }
    else {
        emit sigModulesChanged(modules());
        key()->setModule(modules().first());
        keyChooser()->setModules(modules());
    }
}

/** Sets the module chooser bar. */
void CDisplayWindow::setModuleChooserBar( BtModuleChooserBar* bar ) {
    if (m_moduleChooserBar) {
        m_moduleChooserBar->deleteLater();
    }

    //if a new bar should be set!
    if (bar) {
        m_moduleChooserBar = bar;
        bar->setWindowTitle(tr("Work chooser buttons"));
        bar->setLayoutDirection(Qt::LeftToRight);
        bar->setVisible(btConfig().sessionValue<bool>("GUI/showTextWindowModuleSelectorButtons", true));
    }
}

/** Setup the module header of text area. */
void CDisplayWindow::setHeaderBar( QToolBar* header ) {
    m_headerBar = header;
    header->setMovable(false);
    header->setWindowTitle(tr("Text area header"));
    header->setVisible(btConfig().sessionValue<bool>("GUI/showTextWindowHeaders", true));
}

/** Sets the modules. */
void CDisplayWindow::setModules( const QList<CSwordModuleInfo*>& newModules ) {
    m_modules.clear();

    Q_FOREACH(CSwordModuleInfo const * const mod, newModules)
        m_modules.append(mod->name());
}

/** Initialize the window. Call this method from the outside, because calling this in the constructor is not possible! */
bool CDisplayWindow::init() {
    initView();
    setMinimumSize( 100, 100 );

    setWindowTitle(windowCaption());
    //setup focus stuff.
    setFocusPolicy(Qt::ClickFocus);
    parentWidget()->setFocusPolicy(Qt::ClickFocus);
    initActions();
    initToolbars();
    if (!btConfig().sessionValue<bool>("GUI/showToolbarsInEachWindow", true))
        setToolBarsHidden();
    btMainWindow()->clearMdiToolBars();
    clearMainWindowToolBars();
    initConnections();
    setupPopupMenu();

    m_filterOptions = btConfig().getFilterOptions();
    m_displayOptions = btConfig().getDisplayOptions();
    emit sigDisplayOptionsChanged(m_displayOptions);
    emit sigFilterOptionsChanged(m_filterOptions);
    emit sigModulesChanged(modules());

    m_isReady = true;
    return true;
}

static void prepareToolBar(QToolBar* bar, const QString& title, bool visible) {
    bar->setAllowedAreas(Qt::TopToolBarArea);
    bar->setFloatable(false);
    bar->setWindowTitle(title);
    bar->setVisible(visible);
}

/** Setup the Navigation toolbar. */
void CDisplayWindow::setMainToolBar( QToolBar* bar ) {
    prepareToolBar(bar, tr("Navigation"), btConfig().sessionValue<bool>("GUI/showTextWindowNavigator", true));
    m_mainToolBar = bar;
}

/** Setup the Tools toolbar. */
void CDisplayWindow::setButtonsToolBar( QToolBar* bar ) {
    prepareToolBar(bar, tr("Tool"), btConfig().sessionValue<bool>("GUI/showTextWindowToolButtons", true));
    m_buttonsToolBar = bar;
}

/** Setup the Format toolbar. */
void CDisplayWindow::setFormatToolBar( QToolBar* bar ) {
    prepareToolBar(bar, tr("Format"), true );
    m_formatToolBar = bar;
}

/** Sets the display settings button. */
void CDisplayWindow::setDisplaySettingsButton(BtDisplaySettingsButton *button) {
    BT_CONNECT(this,   SIGNAL(sigDisplayOptionsChanged(DisplayOptions const &)),
               button, SLOT(setDisplayOptions(DisplayOptions const &)));
    BT_CONNECT(this,   SIGNAL(sigFilterOptionsChanged(FilterOptions const &)),
               button, SLOT(setFilterOptions(FilterOptions const &)));
    BT_CONNECT(this,   SIGNAL(sigModulesChanged(BtConstModuleList const &)),
               button, SLOT(setModules(BtConstModuleList const &)));

    button->setDisplayOptions(displayOptions(), false);
    button->setFilterOptions(filterOptions(), false);
    button->setModules(modules());

    BT_CONNECT(button, SIGNAL(sigFilterOptionsChanged(FilterOptions const &)),
               this,   SLOT(setFilterOptions(FilterOptions const &)));
    BT_CONNECT(button, SIGNAL(sigDisplayOptionsChanged(DisplayOptions const &)),
               this,   SLOT(setDisplayOptions(DisplayOptions const &)));
    BT_CONNECT(button, SIGNAL(sigChanged()),
               this,   SLOT(lookup()));
}

void CDisplayWindow::slotShowHeader(bool show) {
    if (headerBar())
        headerBar()->setVisible(show);
}

void CDisplayWindow::slotShowNavigator(bool show) {
    if (mainToolBar())
        mainToolBar()->setVisible(show);
}

void CDisplayWindow::slotShowToolButtons(bool show) {
    if (buttonsToolBar())
        buttonsToolBar()->setVisible(show);
}

void CDisplayWindow::slotShowModuleChooser(bool show) {
    if (moduleChooserBar())
        moduleChooserBar()->setVisible(show);
}

void CDisplayWindow::slotShowFormatToolBar(bool show) {
    if (formatToolBar())
        formatToolBar()->setVisible(show);
}

/** Lookup the current key. Used to refresh the display. */
void CDisplayWindow::lookup() {
    lookupSwordKey( key() );
}

void CDisplayWindow::lookupModKey( const QString& moduleName, const QString& keyName ) {
    if (!isReady()) {
        return;
    }

    CSwordModuleInfo *m = CSwordBackend::instance()->findModuleByName(moduleName);
    if (!m) {
        return; /// \todo check if this is correct behavior
    }

    /// \todo check for containsRef compat
    if (m && modules().contains(m)) {
        key()->setKey(keyName);
        keyChooser()->setKey(key()); //the key chooser does send an update signal
        emit sigKeyChanged(key());
    }
    else {     //given module not displayed in this window
        //if the module is displayed in another display window we assume a wrong drop
        //create a new window for the given module
        BibleTime *mainWindow = btMainWindow();
        BT_ASSERT(mainWindow);
        mainWindow->createReadDisplayWindow(m, keyName);
    }
}

void CDisplayWindow::lookupKey( const QString& keyName ) {
    /* This function is called for example after a bookmark was dropped on this window
    */
    BT_ASSERT(modules().first());

    lookupModKey(modules().first()->name(), keyName);
}

/** Update the status of the popup menu entries. */
void CDisplayWindow::updatePopupMenu() {
    /// \todo Verify this should be empty and comment.
}


///** Returns the installed popup menu. */
QMenu* CDisplayWindow::popup() {
    // qWarning("CReadWindow::popup()");
    if (!m_popupMenu) {
        m_popupMenu = new QMenu(this);
        BT_CONNECT(m_popupMenu, SIGNAL(aboutToShow()),
                   this,        SLOT(updatePopupMenu()));
        if (displayWidget()) {
            displayWidget()->installPopup(m_popupMenu);
        }
        /*   else {
            qWarning("CDisplayWindow:: can't instal popup menu");
            }*/
    }
    return m_popupMenu;
}

/** Sets the display widget used by this display window. */
void CDisplayWindow::setDisplayWidget( CDisplay* newDisplay ) {
    m_displayWidget = newDisplay;
}

void CDisplayWindow::closeEvent(QCloseEvent* e) {
    if (!queryClose()) {
        e->ignore();
    }
    else {
        e->accept();
    }
}

void CDisplayWindow::slotSearchInModules() {
    Search::CSearchDialog::openDialog(modules());
}

void CDisplayWindow::printAll() {
    m_displayWidget->connectionsProxy()->printAll( m_displayOptions, m_filterOptions);
}

void CDisplayWindow::printAnchorWithText() {
    m_displayWidget->connectionsProxy()->printAnchorWithText( m_displayOptions, m_filterOptions);
}

void CDisplayWindow::setFocusKeyChooser() {
    keyChooser()->setFocus();
}