PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/desktop-widgets/usermanual.cpp

https://github.com/dirkhh/subsurface
C++ | 150 lines | 129 code | 19 blank | 2 comment | 19 complexity | 22211616a3386de95635ee6f7c12e116 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <QDesktopServices>
  3. #include <QShortcut>
  4. #include <QFile>
  5. #include "desktop-widgets/usermanual.h"
  6. #include "desktop-widgets/mainwindow.h"
  7. #include "core/qthelper.h"
  8. SearchBar::SearchBar(QWidget *parent): QWidget(parent)
  9. {
  10. ui.setupUi(this);
  11. #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
  12. ui.findNext->setIcon(QIcon(":go-down-icon"));
  13. ui.findPrev->setIcon(QIcon(":go-up-icon"));
  14. ui.findClose->setIcon(QIcon(":window-close-icon"));
  15. #endif
  16. connect(ui.findNext, SIGNAL(pressed()), this, SIGNAL(searchNext()));
  17. connect(ui.findPrev, SIGNAL(pressed()), this, SIGNAL(searchPrev()));
  18. connect(ui.searchEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchTextChanged(QString)));
  19. connect(ui.searchEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButtons(QString)));
  20. connect(ui.findClose, SIGNAL(pressed()), this, SLOT(hide()));
  21. }
  22. void SearchBar::setVisible(bool visible)
  23. {
  24. QWidget::setVisible(visible);
  25. ui.searchEdit->setFocus();
  26. }
  27. void SearchBar::enableButtons(const QString &s)
  28. {
  29. ui.findPrev->setEnabled(s.length());
  30. ui.findNext->setEnabled(s.length());
  31. }
  32. UserManual::UserManual(QWidget *parent) : QDialog(parent)
  33. {
  34. QShortcut *closeKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
  35. connect(closeKey, SIGNAL(activated()), this, SLOT(close()));
  36. QShortcut *quitKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
  37. connect(quitKey, SIGNAL(activated()), qApp, SLOT(quit()));
  38. QAction *actionShowSearch = new QAction(this);
  39. actionShowSearch->setShortcut(Qt::CTRL + Qt::Key_F);
  40. actionShowSearch->setShortcutContext(Qt::WindowShortcut);
  41. addAction(actionShowSearch);
  42. QAction *actionHideSearch = new QAction(this);
  43. actionHideSearch->setShortcut(Qt::Key_Escape);
  44. actionHideSearch->setShortcutContext(Qt::WindowShortcut);
  45. addAction(actionHideSearch);
  46. setWindowTitle(tr("User manual"));
  47. setWindowIcon(QIcon(":subsurface-icon"));
  48. userManual = new QWebView(this);
  49. QString colorBack = palette().highlight().color().name(QColor::HexRgb);
  50. QString colorText = palette().highlightedText().color().name(QColor::HexRgb);
  51. userManual->setStyleSheet(QString("QWebView { selection-background-color: %1; selection-color: %2; }")
  52. .arg(colorBack).arg(colorText));
  53. userManual->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
  54. QString searchPath = getSubsurfaceDataPath("Documentation");
  55. if (searchPath.size()) {
  56. // look for localized versions of the manual first
  57. QString lang = getUiLanguage();
  58. QString prefix = searchPath.append("/user-manual");
  59. QFile manual(prefix + "_" + lang + ".html");
  60. if (!manual.exists())
  61. manual.setFileName(prefix + "_" + lang.left(2) + ".html");
  62. if (!manual.exists())
  63. manual.setFileName(prefix + ".html");
  64. if (!manual.exists()) {
  65. userManual->setHtml(tr("Cannot find the Subsurface manual"));
  66. } else {
  67. QString urlString = QString("file:///") + manual.fileName();
  68. userManual->setUrl(QUrl(urlString, QUrl::TolerantMode));
  69. }
  70. } else {
  71. userManual->setHtml(tr("Cannot find the Subsurface manual"));
  72. }
  73. searchBar = new SearchBar(this);
  74. searchBar->hide();
  75. connect(actionShowSearch, SIGNAL(triggered(bool)), searchBar, SLOT(show()));
  76. connect(actionHideSearch, SIGNAL(triggered(bool)), searchBar, SLOT(hide()));
  77. connect(userManual, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClickedSlot(QUrl)));
  78. connect(searchBar, SIGNAL(searchTextChanged(QString)), this, SLOT(searchTextChanged(QString)));
  79. connect(searchBar, SIGNAL(searchNext()), this, SLOT(searchNext()));
  80. connect(searchBar, SIGNAL(searchPrev()), this, SLOT(searchPrev()));
  81. QVBoxLayout *vboxLayout = new QVBoxLayout();
  82. userManual->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
  83. vboxLayout->addWidget(userManual);
  84. vboxLayout->addWidget(searchBar);
  85. setLayout(vboxLayout);
  86. }
  87. void UserManual::search(QString text, QWebPage::FindFlags flags = QFlag(0))
  88. {
  89. if (userManual->findText(text, QWebPage::FindWrapsAroundDocument | flags) || text.length() == 0) {
  90. searchBar->setStyleSheet("");
  91. } else {
  92. searchBar->setStyleSheet("QLineEdit{background: red;}");
  93. }
  94. }
  95. void UserManual::searchTextChanged(const QString& text)
  96. {
  97. mLastText = text;
  98. search(text);
  99. }
  100. void UserManual::searchNext()
  101. {
  102. search(mLastText);
  103. }
  104. void UserManual::searchPrev()
  105. {
  106. search(mLastText, QWebPage::FindBackward);
  107. }
  108. void UserManual::linkClickedSlot(const QUrl& url)
  109. {
  110. QDesktopServices::openUrl(url);
  111. }
  112. #ifdef Q_OS_MAC
  113. void UserManual::showEvent(QShowEvent *e)
  114. {
  115. MainWindow *m = MainWindow::instance();
  116. filterAction = m->findChild<QAction *>(QLatin1String("actionFilterTags"), Qt::FindDirectChildrenOnly);
  117. if (filterAction != nullptr)
  118. filterAction->setShortcut(QKeySequence());
  119. closeAction = m->findChild<QAction *>(QLatin1String("actionClose"), Qt::FindDirectChildrenOnly);
  120. if (closeAction != nullptr)
  121. closeAction->setShortcut(QKeySequence());
  122. }
  123. void UserManual::hideEvent(QHideEvent *e)
  124. {
  125. if (closeAction != NULL)
  126. closeAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
  127. if (filterAction != NULL)
  128. filterAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F));
  129. closeAction = filterAction = NULL;
  130. }
  131. #endif