Extract user manual web view into a separate class

Provide search functionality which addresses ticket #391

Signed-off-by: Sergey Starosek <sergey.starosek@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Sergey Starosek 2014-01-09 19:21:29 +02:00 committed by Dirk Hohndel
parent e5ae3fca7f
commit 8b00f8ffe6
4 changed files with 296 additions and 3 deletions

93
qt-ui/usermanual.cpp Normal file
View file

@ -0,0 +1,93 @@
#include <QDesktopServices>
#include "usermanual.h"
#include "ui_usermanual.h"
#include "../helpers.h"
UserManual::UserManual(QWidget *parent) :
QWidget(parent),
ui(new Ui::UserManual)
{
ui->setupUi(this);
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);
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateExternalLinks);
QString searchPath = getSubsurfaceDataPath("Documentation");
if (searchPath != "") {
QUrl url(searchPath.append("/user-manual.html"));
ui->webView->setWindowTitle(tr("User Manual"));
ui->webView->setWindowIcon(QIcon(":/subsurface-icon"));
ui->webView->setUrl(url);
} else {
ui->webView->setHtml(tr("Cannot find the Subsurface manual"));
}
ui->searchPanel->setParent(this);
ui->searchPanel->hide();
connect(actionShowSearch, SIGNAL(triggered(bool)), this, SLOT(showSearchPanel()));
connect(actionHideSearch, SIGNAL(triggered(bool)), this, SLOT(hideSearchPanel()));
connect(ui->webView, SIGNAL(linkClicked(QUrl)), this, SLOT(linkClickedSlot(QUrl)));
connect(ui->searchEdit, SIGNAL(textChanged(QString)), this, SLOT(searchTextChanged(QString)));
connect(ui->findNext, SIGNAL(clicked()), this, SLOT(searchNext()));
connect(ui->findPrev, SIGNAL(clicked()), this, SLOT(searchPrev()));
}
void UserManual::showSearchPanel()
{
ui->searchPanel->show();
ui->searchEdit->setFocus();
ui->searchEdit->selectAll();
}
void UserManual::hideSearchPanel()
{
ui->searchPanel->hide();
}
void UserManual::search(QString text, QWebPage::FindFlags flags = 0)
{
if (ui->webView->findText(text, flags) || text.length() == 0) {
ui->searchEdit->setStyleSheet("");
} else {
ui->searchEdit->setStyleSheet("QLineEdit{background: red;}");
}
}
void UserManual::searchTextChanged(QString text) {
bool hasText = text.length() > 0;
ui->findPrev->setEnabled(hasText);
ui->findNext->setEnabled(hasText);
search(text);
}
void UserManual::searchNext()
{
search(ui->searchEdit->text());
}
void UserManual::searchPrev()
{
search(ui->searchEdit->text(), QWebPage::FindBackward);
}
void UserManual::linkClickedSlot(QUrl url)
{
QDesktopServices::openUrl(url);
}
UserManual::~UserManual()
{
delete ui;
}

32
qt-ui/usermanual.h Normal file
View file

@ -0,0 +1,32 @@
#ifndef USERMANUAL_H
#define USERMANUAL_H
#include <QMainWindow>
#include <QWebPage>
namespace Ui {
class UserManual;
}
class UserManual : public QWidget
{
Q_OBJECT
public:
explicit UserManual(QWidget *parent = 0);
~UserManual();
private slots:
void showSearchPanel();
void hideSearchPanel();
void searchTextChanged(QString);
void searchNext();
void searchPrev();
void linkClickedSlot(QUrl url);
private:
Ui::UserManual *ui;
void search(QString, QWebPage::FindFlags);
};
#endif // USERMANUAL_H

165
qt-ui/usermanual.ui Normal file
View file

@ -0,0 +1,165 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UserManual</class>
<widget class="QWidget" name="UserManual">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>834</width>
<height>599</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QFrame" name="searchPanel">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>230</width>
<height>40</height>
</size>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="searchEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="findPrev">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="go-previous">
<normaloff/>
</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="findNext">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="go-next">
<normaloff/>
</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="findClose">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="window-close">
<normaloff/>
</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWebView" name="webView" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QWebView</class>
<extends>QWidget</extends>
<header>qwebview.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>findClose</sender>
<signal>clicked()</signal>
<receiver>searchPanel</receiver>
<slot>hide()</slot>
<hints>
<hint type="sourcelabel">
<x>261</x>
<y>568</y>
</hint>
<hint type="destinationlabel">
<x>192</x>
<y>554</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View file

@ -56,7 +56,8 @@ HEADERS = \
webservice.h \
qt-ui/divelogimportdialog.h \
qt-ui/tagwidget.h \
qt-ui/groupedlineedit.h
qt-ui/groupedlineedit.h \
qt-ui/usermanual.h
SOURCES = \
deco.c \
@ -105,7 +106,8 @@ SOURCES = \
uemis-downloader.c \
qt-ui/divelogimportdialog.cpp \
qt-ui/tagwidget.cpp \
qt-ui/groupedlineedit.cpp
qt-ui/groupedlineedit.cpp \
qt-ui/usermanual.cpp
linux*: SOURCES += linux.c
mac: SOURCES += macos.c
@ -124,7 +126,8 @@ FORMS = \
qt-ui/shifttimes.ui \
qt-ui/webservices.ui \
qt-ui/tableview.ui \
qt-ui/divelogimportdialog.ui
qt-ui/divelogimportdialog.ui \
qt-ui/usermanual.ui
RESOURCES = subsurface.qrc