subsurface/qt-ui/usermanual.cpp
Tomaz Canabrava 927bc5d601 Fixed SearchBar layout and icons
This is almost a rewrite of the Search function on the WebView
the old code had a few uneeded things, mostly being a subclass
of QMainWindow instead of the QWebView - this makes the code
use a tiny bit less ram.

The SearchBox was also moved to an own class ( we can use it
later to filter the contents of the DiveList for instance )

and a forced use of the pixmaps for the Mac and Windows platform
was added.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2014-06-30 16:04:24 -07:00

120 lines
3.7 KiB
C++

#include <QDesktopServices>
#include <QShortcut>
#include <QFile>
#include <QDebug>
#include "usermanual.h"
#include "helpers.h"
SearchBar::SearchBar(QWidget *parent): QWidget(parent)
{
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();
}
void SearchBar::enableButtons(const QString &s)
{
ui.findPrev->setEnabled(s.length());
ui.findNext->setEnabled(s.length());
}
UserManual::UserManual(QWidget *parent) : QWebView(parent)
{
QShortcut *closeKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
connect(closeKey, SIGNAL(activated()), this, SLOT(close()));
QShortcut *quitKey = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
connect(quitKey, SIGNAL(activated()), qApp, SLOT(quit()));
QAction *actionShowSearch = new QAction(this);
actionShowSearch->setShortcut(Qt::CTRL + Qt::Key_F);
actionShowSearch->setShortcutContext(Qt::WindowShortcut);
addAction(actionShowSearch);
QAction *actionHideSearch = new QAction(this);
actionHideSearch->setShortcut(Qt::Key_Escape);
actionHideSearch->setShortcutContext(Qt::WindowShortcut);
addAction(actionHideSearch);
setWindowTitle(tr("User Manual"));
page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
QString searchPath = getSubsurfaceDataPath("Documentation");
if (searchPath.size()) {
// 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");
if (!manual.exists()) {
setHtml(tr("Cannot find the Subsurface manual"));
} else {
QString urlString = QString("file:///") + manual.fileName();
setUrl(QUrl(urlString, QUrl::TolerantMode));
}
} else {
setHtml(tr("Cannot find the Subsurface manual"));
}
searchBar = new SearchBar(this);
searchBar->hide();
connect(actionShowSearch, SIGNAL(triggered(bool)), searchBar, SLOT(show()));
connect(actionHideSearch, SIGNAL(triggered(bool)), searchBar, SLOT(hide()));
connect(this, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClickedSlot(QUrl)));
connect(searchBar, SIGNAL(searchTextChanged(QString)), this, SLOT(searchTextChanged(QString)));
connect(searchBar, SIGNAL(searchNext()), this, SLOT(searchNext()));
connect(searchBar, SIGNAL(searchPrev()), this, SLOT(searchPrev()));
}
void UserManual::search(QString text, QWebPage::FindFlags flags = 0)
{
if (findText(text, QWebPage::FindWrapsAroundDocument | flags) || text.length() == 0) {
searchBar->setStyleSheet("");
} else {
searchBar->setStyleSheet("QLineEdit{background: red;}");
}
}
void UserManual::searchTextChanged(const QString& text)
{
mLastText = text;
search(text);
}
void UserManual::searchNext()
{
search(mLastText);
}
void UserManual::searchPrev()
{
search(mLastText, QWebPage::FindBackward);
}
void UserManual::linkClickedSlot(QUrl url)
{
QDesktopServices::openUrl(url);
}
UserManual::~UserManual()
{
}