aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui/usermanual.cpp
blob: 3985b6cd42361fdacbfbcf179388192abc6cb70a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <QDesktopServices>
#include <QShortcut>
#include <QFile>
#include <QDebug>

#include "usermanual.h"
#include "ui_usermanual.h"

#include "helpers.h"

UserManual::UserManual(QWidget *parent) : QMainWindow(parent),
	ui(new Ui::UserManual)
{
	ui->setupUi(this);

	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()), parent, SLOT(close()));

	QAction *actionShowSearch = new QAction(this);
	actionShowSearch->setShortcut(Qt::CTRL + Qt::Key_F);
	actionShowSearch->setShortcutContext(Qt::WidgetShortcut);
	addAction(actionShowSearch);

	QAction *actionHideSearch = new QAction(this);
	actionHideSearch->setShortcut(Qt::Key_Escape);
	actionHideSearch->setShortcutContext(Qt::WidgetShortcut);
	addAction(actionHideSearch);

	setWindowTitle(tr("User Manual"));

	ui->webView->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");
		QUrl url(manual.fileName());
		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, QWebPage::FindWrapsAroundDocument | 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;
}