| 1 | /*************************************************************************** |
| 2 | helpviewer.cpp |
| 3 | ------------------- |
| 4 | General purpose help file viewer |
| 5 | ------------------- |
| 6 | Copyright 2007-2008 David Johnson |
| 7 | Please see the header file for copyright and license information. |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QDesktopServices> |
| 11 | #include <QFileInfo> |
| 12 | #include <QLocale> |
| 13 | #include <QPrintDialog> |
| 14 | #include <QPrinter> |
| 15 | #include <QTextBrowser> |
| 16 | |
| 17 | #include "resource.h" |
| 18 | #include "helpviewer.h" |
| 19 | |
| 20 | using namespace Resource; |
| 21 | |
| 22 | ////////////////////////////////////////////////////////////////////////////// |
| 23 | // Construction // |
| 24 | ////////////////////////////////////////////////////////////////////////////// |
| 25 | |
| 26 | ////////////////////////////////////////////////////////////////////////////// |
| 27 | // HelpViewer() |
| 28 | // ------------ |
| 29 | // Constructor |
| 30 | |
| 31 | HelpViewer::HelpViewer(const QString &home, QWidget *parent) |
| 32 | : QMainWindow(parent), browser_(0), printer_(0) |
| 33 | { |
| 34 | setUnifiedTitleAndToolBarOnMac(true); |
| 35 | ui.setupUi(this); |
| 36 | |
| 37 | browser_ = new QTextBrowser(this); |
| 38 | |
| 39 | QFileInfo hinfo(home); |
| 40 | QString helppath = hinfo.absolutePath(); |
| 41 | QString helpfile = hinfo.fileName(); |
| 42 | browser_->setSearchPaths(QStringList(helppath)); |
| 43 | if (!helpfile.isEmpty()) browser_->setSource(helpfile); |
| 44 | |
| 45 | |
| 46 | textChanged(); |
| 47 | setCentralWidget(browser_); |
| 48 | |
| 49 | // setup actions |
| 50 | connect(ui.actionback, SIGNAL(triggered()), browser_, SLOT(backward())); |
| 51 | connect(ui.actionforward, SIGNAL(triggered()), browser_, SLOT(forward())); |
| 52 | connect(ui.actiongohome, SIGNAL(triggered()), browser_, SLOT(home())); |
| 53 | connect(ui.actionfileprint, SIGNAL(triggered()), this, SLOT(print())); |
| 54 | connect(ui.actionquit, SIGNAL(triggered()), this, SLOT(close())); |
| 55 | |
| 56 | // setup connections |
| 57 | connect(browser_, SIGNAL(backwardAvailable(bool)), |
| 58 | ui.actionback, SLOT(setEnabled(bool))); |
| 59 | connect(browser_, SIGNAL(forwardAvailable(bool)), |
| 60 | ui.actionforward, SLOT(setEnabled(bool))); |
| 61 | connect(browser_, SIGNAL(textChanged()), |
| 62 | this, SLOT(textChanged())); |
| 63 | connect(browser_, SIGNAL(highlighted(const QString&)), |
| 64 | statusBar(), SLOT(showMessage(const QString&))); |
| 65 | connect(browser_, SIGNAL(anchorClicked(const QUrl&)), |
| 66 | this, SLOT(anchorClicked(const QUrl&))); |
| 67 | |
| 68 | browser_->setFocus(); |
| 69 | } |
| 70 | |
| 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | // ~HelpViewer() |
| 73 | // ------------- |
| 74 | // Destructor |
| 75 | |
| 76 | HelpViewer::~HelpViewer() |
| 77 | { |
| 78 | if (browser_) delete browser_; |
| 79 | if (printer_) delete printer_; |
| 80 | } |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | // textChanged() |
| 84 | // ------------- |
| 85 | // The displayed document has changed, so change caption |
| 86 | |
| 87 | void HelpViewer::textChanged() |
| 88 | { |
| 89 | QString title = browser_->documentTitle().simplified(); // cleanup |
| 90 | setWindowTitle(tr("%1 Help - %2").arg(TITLE.constData()).arg(title)); |
| 91 | } |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | // print() |
| 95 | // ------- |
| 96 | // Print the contents of the help window |
| 97 | |
| 98 | void HelpViewer::print() |
| 99 | { |
| 100 | // Note: we are not sharing printer with application |
| 101 | if (!printer_) { |
| 102 | printer_ = new QPrinter(QPrinter::HighResolution); |
| 103 | printer_->setFullPage(true); |
| 104 | // for convenience, default to US_Letter for US/Canada |
| 105 | switch (QLocale::system().country()) { |
| 106 | case QLocale::AnyCountry: |
| 107 | case QLocale::Canada: |
| 108 | case QLocale::UnitedStates: |
| 109 | case QLocale::UnitedStatesMinorOutlyingIslands: |
| 110 | printer_->setPageSize(QPrinter::Letter); break; |
| 111 | default: |
| 112 | printer_->setPageSize(QPrinter::A4); break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | QPrintDialog *pdialog = new QPrintDialog(printer_, this); |
| 117 | if (pdialog->exec() == QDialog::Accepted) { |
| 118 | browser_->document()->print(printer_); |
| 119 | } |
| 120 | delete pdialog; |
| 121 | } |
| 122 | |
| 123 | void HelpViewer::anchorClicked(const QUrl &link) |
| 124 | { |
| 125 | if (link.scheme().isEmpty()) { |
| 126 | browser_->setSource(link); |
| 127 | } else { |
| 128 | QDesktopServices::openUrl(link); |
| 129 | browser_->setSource(browser_->source()); |
| 130 | } |
| 131 | } |