2014-01-09 17:21:29 +00:00
|
|
|
#include <QDesktopServices>
|
2014-04-24 21:58:58 +00:00
|
|
|
#include <QShortcut>
|
2014-06-06 19:10:34 +00:00
|
|
|
#include <QFile>
|
2014-01-09 17:21:29 +00:00
|
|
|
|
|
|
|
#include "usermanual.h"
|
2015-01-28 20:06:27 +00:00
|
|
|
#include "mainwindow.h"
|
2014-06-06 19:10:34 +00:00
|
|
|
#include "helpers.h"
|
2014-01-09 17:21:29 +00:00
|
|
|
|
2014-06-30 20:45:52 +00:00
|
|
|
SearchBar::SearchBar(QWidget *parent): QWidget(parent)
|
2014-01-09 17:21:29 +00:00
|
|
|
{
|
2014-06-30 20:45:52 +00:00
|
|
|
ui.setupUi(this);
|
|
|
|
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
|
|
|
|
ui.findNext->setIcon(QIcon(":icons/subsurface/32x32/actions/go-down.png"));
|
|
|
|
ui.findPrev->setIcon(QIcon(":icons/subsurface/32x32/actions/go-up.png"));
|
|
|
|
ui.findClose->setIcon(QIcon(":icons/subsurface/32x32/actions/window-close.png"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
connect(ui.findNext, SIGNAL(pressed()), this, SIGNAL(searchNext()));
|
|
|
|
connect(ui.findPrev, SIGNAL(pressed()), this, SIGNAL(searchPrev()));
|
|
|
|
connect(ui.searchEdit, SIGNAL(textChanged(QString)), this, SIGNAL(searchTextChanged(QString)));
|
|
|
|
connect(ui.searchEdit, SIGNAL(textChanged(QString)), this, SLOT(enableButtons(QString)));
|
|
|
|
connect(ui.findClose, SIGNAL(pressed()), this, SLOT(hide()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchBar::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
QWidget::setVisible(visible);
|
|
|
|
ui.searchEdit->setFocus();
|
|
|
|
}
|
2014-01-09 17:21:29 +00:00
|
|
|
|
2014-06-30 20:45:52 +00:00
|
|
|
void SearchBar::enableButtons(const QString &s)
|
|
|
|
{
|
|
|
|
ui.findPrev->setEnabled(s.length());
|
|
|
|
ui.findNext->setEnabled(s.length());
|
|
|
|
}
|
|
|
|
|
2014-07-11 21:01:35 +00:00
|
|
|
UserManual::UserManual(QWidget *parent) : QWidget(parent)
|
2014-06-30 20:45:52 +00:00
|
|
|
{
|
2014-05-22 18:40:22 +00:00
|
|
|
QShortcut *closeKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
|
2014-04-24 21:58:58 +00:00
|
|
|
connect(closeKey, SIGNAL(activated()), this, SLOT(close()));
|
2014-05-22 18:40:22 +00:00
|
|
|
QShortcut *quitKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
|
2014-06-30 20:45:52 +00:00
|
|
|
connect(quitKey, SIGNAL(activated()), qApp, SLOT(quit()));
|
2014-04-24 21:58:58 +00:00
|
|
|
|
2014-01-09 17:21:29 +00:00
|
|
|
QAction *actionShowSearch = new QAction(this);
|
|
|
|
actionShowSearch->setShortcut(Qt::CTRL + Qt::Key_F);
|
2014-06-25 12:07:02 +00:00
|
|
|
actionShowSearch->setShortcutContext(Qt::WindowShortcut);
|
2014-01-09 17:21:29 +00:00
|
|
|
addAction(actionShowSearch);
|
|
|
|
|
|
|
|
QAction *actionHideSearch = new QAction(this);
|
|
|
|
actionHideSearch->setShortcut(Qt::Key_Escape);
|
2014-06-25 12:07:02 +00:00
|
|
|
actionHideSearch->setShortcutContext(Qt::WindowShortcut);
|
2014-01-09 17:21:29 +00:00
|
|
|
addAction(actionHideSearch);
|
|
|
|
|
2015-01-25 15:03:19 +00:00
|
|
|
setWindowTitle(tr("User manual"));
|
2014-07-18 10:57:14 +00:00
|
|
|
setWindowIcon(QIcon(":/subsurface-icon"));
|
|
|
|
|
2014-07-11 21:01:35 +00:00
|
|
|
userManual = new QWebView(this);
|
2015-10-13 15:06:51 +00:00
|
|
|
QString colorBack = palette().highlight().color().name(QColor::HexRgb);
|
|
|
|
QString colorText = palette().highlightedText().color().name(QColor::HexRgb);
|
|
|
|
userManual->setStyleSheet(QString("QWebView { selection-background-color: %1; selection-color: %2; }")
|
|
|
|
.arg(colorBack).arg(colorText));
|
2014-07-11 21:01:35 +00:00
|
|
|
userManual->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
|
2014-01-09 17:21:29 +00:00
|
|
|
QString searchPath = getSubsurfaceDataPath("Documentation");
|
2014-05-11 00:37:18 +00:00
|
|
|
if (searchPath.size()) {
|
2014-06-06 19:10:34 +00:00
|
|
|
// look for localized versions of the manual first
|
|
|
|
QString lang = uiLanguage(NULL);
|
|
|
|
QString prefix = searchPath.append("/user-manual");
|
|
|
|
QFile manual(prefix + "_" + lang + ".html");
|
|
|
|
if (!manual.exists())
|
|
|
|
manual.setFileName(prefix + "_" + lang.left(2) + ".html");
|
|
|
|
if (!manual.exists())
|
|
|
|
manual.setFileName(prefix + ".html");
|
2014-06-09 20:42:16 +00:00
|
|
|
if (!manual.exists()) {
|
2014-07-11 21:01:35 +00:00
|
|
|
userManual->setHtml(tr("Cannot find the Subsurface manual"));
|
2014-06-09 20:42:16 +00:00
|
|
|
} else {
|
|
|
|
QString urlString = QString("file:///") + manual.fileName();
|
2014-07-11 21:01:35 +00:00
|
|
|
userManual->setUrl(QUrl(urlString, QUrl::TolerantMode));
|
2014-06-09 20:42:16 +00:00
|
|
|
}
|
2014-01-09 17:21:29 +00:00
|
|
|
} else {
|
2014-07-11 21:01:35 +00:00
|
|
|
userManual->setHtml(tr("Cannot find the Subsurface manual"));
|
2014-01-09 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 20:45:52 +00:00
|
|
|
searchBar = new SearchBar(this);
|
|
|
|
searchBar->hide();
|
|
|
|
connect(actionShowSearch, SIGNAL(triggered(bool)), searchBar, SLOT(show()));
|
|
|
|
connect(actionHideSearch, SIGNAL(triggered(bool)), searchBar, SLOT(hide()));
|
2014-07-11 21:01:35 +00:00
|
|
|
connect(userManual, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClickedSlot(QUrl)));
|
2014-06-30 20:45:52 +00:00
|
|
|
connect(searchBar, SIGNAL(searchTextChanged(QString)), this, SLOT(searchTextChanged(QString)));
|
|
|
|
connect(searchBar, SIGNAL(searchNext()), this, SLOT(searchNext()));
|
|
|
|
connect(searchBar, SIGNAL(searchPrev()), this, SLOT(searchPrev()));
|
2014-07-11 21:01:35 +00:00
|
|
|
|
|
|
|
QVBoxLayout *vboxLayout = new QVBoxLayout();
|
2014-07-16 07:58:02 +00:00
|
|
|
userManual->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
|
2014-07-11 21:01:35 +00:00
|
|
|
vboxLayout->addWidget(userManual);
|
|
|
|
vboxLayout->addWidget(searchBar);
|
|
|
|
setLayout(vboxLayout);
|
2014-01-09 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserManual::search(QString text, QWebPage::FindFlags flags = 0)
|
|
|
|
{
|
2014-07-11 21:01:35 +00:00
|
|
|
if (userManual->findText(text, QWebPage::FindWrapsAroundDocument | flags) || text.length() == 0) {
|
2014-06-30 20:45:52 +00:00
|
|
|
searchBar->setStyleSheet("");
|
2014-01-09 17:21:29 +00:00
|
|
|
} else {
|
2014-06-30 20:45:52 +00:00
|
|
|
searchBar->setStyleSheet("QLineEdit{background: red;}");
|
2014-01-09 17:21:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-30 20:45:52 +00:00
|
|
|
void UserManual::searchTextChanged(const QString& text)
|
2014-02-28 04:09:57 +00:00
|
|
|
{
|
2014-06-30 20:45:52 +00:00
|
|
|
mLastText = text;
|
2014-01-09 17:21:29 +00:00
|
|
|
search(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserManual::searchNext()
|
|
|
|
{
|
2014-06-30 20:45:52 +00:00
|
|
|
search(mLastText);
|
2014-01-09 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserManual::searchPrev()
|
|
|
|
{
|
2014-06-30 20:45:52 +00:00
|
|
|
search(mLastText, QWebPage::FindBackward);
|
2014-01-09 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 21:01:35 +00:00
|
|
|
void UserManual::linkClickedSlot(const QUrl& url)
|
2014-01-09 17:21:29 +00:00
|
|
|
{
|
|
|
|
QDesktopServices::openUrl(url);
|
|
|
|
}
|
2015-01-28 20:06:27 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
void UserManual::showEvent(QShowEvent *e) {
|
|
|
|
filterAction = NULL;
|
|
|
|
closeAction = NULL;
|
|
|
|
MainWindow *m = MainWindow::instance();
|
|
|
|
Q_FOREACH (QObject *o, m->children()) {
|
|
|
|
if (o->objectName() == "actionFilterTags") {
|
|
|
|
filterAction = qobject_cast<QAction*>(o);
|
|
|
|
filterAction->setShortcut(QKeySequence());
|
|
|
|
} else if (o->objectName() == "actionClose") {
|
|
|
|
closeAction = qobject_cast<QAction*>(o);
|
|
|
|
closeAction->setShortcut(QKeySequence());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void UserManual::hideEvent(QHideEvent *e) {
|
|
|
|
if (closeAction != NULL)
|
|
|
|
closeAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
|
|
|
|
if (filterAction != NULL)
|
|
|
|
filterAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F));
|
|
|
|
closeAction = filterAction = NULL;
|
|
|
|
}
|
|
|
|
#endif
|