~adamreichold/qpdfview/trunk

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
/*

Copyright 2018 Marshall Banana
Copyright 2013 Benjamin Eltzner
Copyright 2013, 2018 Adam Reichold

This file is part of qpdfview.

qpdfview is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

qpdfview is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "helpdialog.h"

#include <QApplication>
#include <QDialogButtonBox>
#include <QDir>
#include <QLineEdit>
#include <QPushButton>
#include <QTextBrowser>
#include <QVBoxLayout>

#include "settings.h"

namespace qpdfview
{

HelpDialog::HelpDialog(QWidget* parent) : QDialog(parent)
{
    setWindowTitle(tr("Help") + QLatin1String(" - qpdfview"));

    m_textBrowser = new QTextBrowser(this);
    m_textBrowser->setTextInteractionFlags(Qt::TextBrowserInteraction | Qt::TextSelectableByKeyboard);
    m_textBrowser->setSearchPaths(QStringList() << QDir(QApplication::applicationDirPath()).filePath(APP_DIR_DATA_PATH) << DATA_INSTALL_PATH << ":/");

    //: Please replace by file name of localized help if available, e.g. "help_fr.html".
    m_textBrowser->setSource(QUrl::fromLocalFile(tr("help.html")));

    if(m_textBrowser->document()->isEmpty())
    {
        m_textBrowser->setSource(QUrl::fromLocalFile("help.html"));
    }

    m_dialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this);
    connect(m_dialogButtonBox, SIGNAL(accepted()), SLOT(accept()));
    connect(m_dialogButtonBox, SIGNAL(rejected()), SLOT(reject()));

    m_searchLineEdit = new QLineEdit(this);
    connect(m_searchLineEdit, SIGNAL(returnPressed()), SLOT(on_findNext_triggered()));
    connect(m_searchLineEdit, SIGNAL(textEdited(QString)), SLOT(on_search_textEdited()));

    m_findPreviousButton = m_dialogButtonBox->addButton(tr("Find previous"), QDialogButtonBox::ActionRole);
    m_findPreviousButton->setShortcut(QKeySequence::FindPrevious);
    connect(m_findPreviousButton, SIGNAL(clicked()), SLOT(on_findPrevious_triggered()));

    m_findNextButton = m_dialogButtonBox->addButton(tr("Find next"), QDialogButtonBox::ActionRole);
    m_findNextButton->setShortcut(QKeySequence::FindNext);
    connect(m_findNextButton, SIGNAL(clicked()), SLOT(on_findNext_triggered()));

    // Default buttons would interfere with search funtionality...
    foreach(QAbstractButton* abstractButton, m_dialogButtonBox->buttons())
    {
        QPushButton* pushButton = qobject_cast< QPushButton* >(abstractButton);

        if(pushButton != 0)
        {
            pushButton->setAutoDefault(false);
            pushButton->setDefault(false);
        }
    }

    setLayout(new QVBoxLayout(this));
    layout()->addWidget(m_textBrowser);
    layout()->addWidget(m_searchLineEdit);
    layout()->addWidget(m_dialogButtonBox);

    resize(Settings::instance()->mainWindow().contentsDialogSize(sizeHint()));

    m_searchLineEdit->setFocus();
}

HelpDialog::~HelpDialog()
{
    Settings::instance()->mainWindow().setContentsDialogSize(size());
}

void HelpDialog::on_findPrevious_triggered()
{
    if(!m_searchLineEdit->text().isEmpty() && m_textBrowser->find(m_searchLineEdit->text(), QTextDocument::FindBackward))
    {
        m_textBrowser->setFocus();

        m_searchLineEdit->setStyleSheet(QLatin1String("background-color: #80ff80"));
    }
    else
    {
        m_searchLineEdit->setStyleSheet(QLatin1String("background-color: #ff8080"));
    }
}

void HelpDialog::on_findNext_triggered()
{
    if(!m_searchLineEdit->text().isEmpty() && m_textBrowser->find(m_searchLineEdit->text()))
    {
        m_textBrowser->setFocus();

        m_searchLineEdit->setStyleSheet(QLatin1String("background-color: #80ff80"));
    }
    else
    {
        m_searchLineEdit->setStyleSheet(QLatin1String("background-color: #ff8080"));

    }
}

void HelpDialog::on_search_textEdited()
{
    m_searchLineEdit->setStyleSheet(QString());
}

} // qpdfview