PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/qbrew-0.4.1/src/helpviewer.cpp

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