1 /* vi: set sw=4 ts=4:
2  *
3  * Copyright (C) 2001 - 2012 Christian Hohnstaedt.
4  *
5  * All rights reserved.
6  */
7 
8 #include "Help.h"
9 #include "lib/func.h"
10 
11 #include <QDebug>
12 #include <QDialog>
13 #include <QHelpEngine>
14 #include <QDialogButtonBox>
15 
Help()16 Help::Help() : QWidget(NULL)
17 {
18 	setupUi(this);
19 	setWindowTitle(XCA_TITLE);
20 	textbox->setSearchPaths(QStringList(getDocDir()));
21 	textbox->setOpenExternalLinks(true);
22 	textbox->clearHistory();
23 	helpengine = new QHelpEngineCore(getDocDir() + "/xca.qhc");
24 }
25 
~Help()26 Help::~Help()
27 {
28 	delete helpengine;
29 }
30 
display(const QUrl & url)31 void Help::display(const QUrl &url)
32 {
33 #if QT_VERSION < 0x050000
34 	QString path = url.path();
35 	int pos = path.lastIndexOf("/");
36 	if (pos != -1)
37 		path = path.mid(pos+1);
38 	textbox->setSource(QUrl(path));
39 #else
40 	textbox->setSource(QUrl(url.fileName()));
41 #endif
42 	textbox->scrollToAnchor(url.fragment());
43 	show();
44 	raise();
45 }
46 
content()47 void Help::content()
48 {
49 	display(QUrl("qthelp://org.sphinx.xca/doc/index.html"));
50 }
51 
url_by_ctx(const QString & ctx) const52 QMap<QString, QUrl> Help::url_by_ctx(const QString &ctx) const
53 {
54 	return helpengine->linksForIdentifier(QString("%1.%1").arg(ctx));
55 }
56 
contexthelp(const QString & context)57 void Help::contexthelp(const QString &context)
58 {
59 	QMap<QString, QUrl> helpctx = url_by_ctx(context);
60 
61 	if (helpctx.count())
62 		display(helpctx.constBegin().value());
63 }
64 
contexthelp()65 void Help::contexthelp()
66 {
67 	QObject *o = sender();
68 	if (!o)
69 		return;
70 	QString ctx = o->property("help_ctx").toString();
71 	if (ctx.isEmpty())
72 		return;
73 	contexthelp(ctx);
74 }
75 
register_ctxhelp_button(QDialog * dlg,const QString & help_ctx) const76 void Help::register_ctxhelp_button(QDialog *dlg, const QString &help_ctx) const
77 {
78 	QDialogButtonBox *buttonBox =
79 			 dlg->findChild<QDialogButtonBox*>("buttonBox");
80 
81 	if (!buttonBox || help_ctx.isEmpty())
82 		return;
83 
84 	dlg->setWindowModality(Qt::WindowModal);
85 	buttonBox->addButton(QDialogButtonBox::Help);
86 	buttonBox->setProperty("help_ctx", QVariant(help_ctx));
87 	connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(contexthelp()));
88 
89 	if (url_by_ctx(help_ctx).count() == 0) {
90 		qWarning() << "Unknown help context: " << help_ctx;
91 		buttonBox->button(QDialogButtonBox::Help)->setEnabled(false);
92 	}
93 }
94