File: helpviewer.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (131 lines) | stat: -rw-r--r-- 4,189 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
  helpviewer.cpp
  -------------------
  General purpose help file viewer
  -------------------
  Copyright 2007-2008 David Johnson
  Please see the header file for copyright and license information.
***************************************************************************/

#include <QDesktopServices>
#include <QFileInfo>
#include <QLocale>
#include <QPrintDialog>
#include <QPrinter>
#include <QTextBrowser>

#include "resource.h"
#include "helpviewer.h"

using namespace Resource;

//////////////////////////////////////////////////////////////////////////////
// Construction                                                             //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// HelpViewer()
// ------------
// Constructor

HelpViewer::HelpViewer(const QString &home, QWidget *parent)
    : QMainWindow(parent), browser_(0), printer_(0)
{
    setUnifiedTitleAndToolBarOnMac(true);
    ui.setupUi(this);

    browser_ = new QTextBrowser(this);

    QFileInfo hinfo(home);
    QString helppath = hinfo.absolutePath();
    QString helpfile = hinfo.fileName();
    browser_->setSearchPaths(QStringList(helppath));
    if (!helpfile.isEmpty()) browser_->setSource(helpfile);


    textChanged();
    setCentralWidget(browser_);

    // setup actions
    connect(ui.actionback, SIGNAL(triggered()), browser_, SLOT(backward()));
    connect(ui.actionforward, SIGNAL(triggered()), browser_, SLOT(forward()));
    connect(ui.actiongohome, SIGNAL(triggered()), browser_, SLOT(home()));
    connect(ui.actionfileprint, SIGNAL(triggered()), this, SLOT(print()));
    connect(ui.actionquit, SIGNAL(triggered()), this, SLOT(close()));

    // setup connections
    connect(browser_, SIGNAL(backwardAvailable(bool)),
            ui.actionback, SLOT(setEnabled(bool)));
    connect(browser_, SIGNAL(forwardAvailable(bool)),
            ui.actionforward, SLOT(setEnabled(bool)));
    connect(browser_, SIGNAL(textChanged()),
            this, SLOT(textChanged()));
    connect(browser_, SIGNAL(highlighted(const QString&)),
            statusBar(), SLOT(showMessage(const QString&)));
    connect(browser_, SIGNAL(anchorClicked(const QUrl&)),
            this, SLOT(anchorClicked(const QUrl&)));

    browser_->setFocus();
}

//////////////////////////////////////////////////////////////////////////////
// ~HelpViewer()
// -------------
// Destructor

HelpViewer::~HelpViewer()
{
    if (browser_) delete browser_;
    if (printer_) delete printer_;
}

//////////////////////////////////////////////////////////////////////////////
// textChanged()
// -------------
// The displayed document has changed, so change caption

void HelpViewer::textChanged()
{
    QString title = browser_->documentTitle().simplified(); // cleanup
    setWindowTitle(tr("%1 Help - %2").arg(TITLE.constData()).arg(title));
}

//////////////////////////////////////////////////////////////////////////////
// print()
// -------
// Print the contents of the help window

void HelpViewer::print()
{
    // Note: we are not sharing printer with application
    if (!printer_) {
        printer_ = new QPrinter(QPrinter::HighResolution);
        printer_->setFullPage(true);
        // for convenience, default to US_Letter for US/Canada
        switch (QLocale::system().country()) {
          case QLocale::AnyCountry:
          case QLocale::Canada:
          case QLocale::UnitedStates:
          case QLocale::UnitedStatesMinorOutlyingIslands:
              printer_->setPageSize(QPrinter::Letter); break;
          default:
              printer_->setPageSize(QPrinter::A4); break;
        }
    }

    QPrintDialog *pdialog = new QPrintDialog(printer_, this);
    if (pdialog->exec() == QDialog::Accepted) {
        browser_->document()->print(printer_);
    }
    delete pdialog;
}

void HelpViewer::anchorClicked(const QUrl &link)
{
    if (link.scheme().isEmpty()) {
        browser_->setSource(link);
    } else {
        QDesktopServices::openUrl(link);
        browser_->setSource(browser_->source());
    }
}