WSL/SLF GitLab Repository

HelpWindow.cc 4.81 KB
Newer Older
Michael Reisecker's avatar
Michael Reisecker committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*****************************************************************************/
/*  Copyright 2021 WSL Institute for Snow and Avalanche Research  SLF-DAVOS  */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   INIshell is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with INIshell.  If not, see <http://www.gnu.org/licenses/>.
*/

19
#include "AboutWindow.h"
20
#include "HelpWindow.h"
Michael Reisecker's avatar
Michael Reisecker committed
21
22
23
24
#include "src/main/inishell.h"
#include "src/main/XMLReader.h"

#include <QCoreApplication>
25
#include <QDesktopServices>
26
#include <QMenuBar>
Michael Reisecker's avatar
Michael Reisecker committed
27
28
29
30
31

#ifdef DEBUG
	#include <iostream>
#endif

Michael Reisecker's avatar
Michael Reisecker committed
32
33
34
35
36
/**
 * @class HelpWindow
 * @brief Constructor for the Help window.
 * @param[in] parent The window's parent.
 */
37
38
HelpWindow::HelpWindow(QMainWindow *parent) : QMainWindow(parent)
{
Michael Reisecker's avatar
Michael Reisecker committed
39
40

	/* create a scroll area and put a Group in it */
41
42
	help_tab_ = new SectionTab();
	this->setCentralWidget(help_tab_);
Michael Reisecker's avatar
Michael Reisecker committed
43

Michael Reisecker's avatar
Michael Reisecker committed
44
	this->setWindowTitle(tr("Help") + " ~ " + QCoreApplication::applicationName());
45
	createMenu();
Michael Reisecker's avatar
Michael Reisecker committed
46
47
}

Michael Reisecker's avatar
Michael Reisecker committed
48
49
/**
 * @brief Open and show the help file.
50
 * @param[in] dev If true, load the developer's guide. If false, load the user guide.
Michael Reisecker's avatar
Michael Reisecker committed
51
 */
52
53
void HelpWindow::loadHelp(const bool &dev)
{
Michael Reisecker's avatar
Michael Reisecker committed
54
	static bool is_loaded = false;
Michael Reisecker's avatar
Michael Reisecker committed
55
56
	if (!is_loaded | dev) {
		help_tab_->clear();
Michael Reisecker's avatar
Michael Reisecker committed
57
58
		XMLReader xmlr;
		QString err;
Michael Reisecker's avatar
Michael Reisecker committed
59
60
61
62
		QString help_file( "help.xml" );
		if (dev)
			help_file = "help_dev.xml";
		xmlr.read(":doc/" + help_file, err);
63
64
65
66
#ifdef DEBUG
		if (!err.isEmpty())
			qDebug() << "There is an error in a help XML: " << err;
#endif //def DEBUG
Michael Reisecker's avatar
Michael Reisecker committed
67
		QDomDocument xml(xmlr.getXml());
68
69
		buildGui(xml, help_tab_);
		is_loaded = true && !dev; //reload after opening dev
Michael Reisecker's avatar
Michael Reisecker committed
70
	}
71

72
73
	view_dev_help_->setChecked(dev);
	view_user_guide_->setChecked(!dev);
Michael Reisecker's avatar
Michael Reisecker committed
74
75
	this->show();
	this->raise();
Michael Reisecker's avatar
Michael Reisecker committed
76
}
77
78

/**
79
 * @brief Listener for key events.
80
 * @details Close the help on pressing ESC or display the logger.
81
 * @param[in] event The key press event that is received.
82
83
84
 */
void HelpWindow::keyPressEvent(QKeyEvent *event)
{
85
	if (event->key() == Qt::Key_Escape || keyToSequence(event) == QKeySequence::Close) {
86
		this->close();
87
88
89
90
	} else if (event->modifiers() == Qt::CTRL && event->key() == Qt::Key_L) {
		getMainWindow()->getLogger()->show();
		getMainWindow()->getLogger()->raise();
	}
91
}
92

Michael Reisecker's avatar
Michael Reisecker committed
93
94
95
/**
 * @brief Build the Help window's menu items.
 */
96
97
void HelpWindow::createMenu()
{
98
99
100
101
102
103
104
	/* FILE menu */
	QMenu *menu_file = this->menuBar()->addMenu(tr("&File"));
	auto *file_bugreport_ = new QAction(tr("File &bug report..."), menu_file);
	menu_file->addAction(file_bugreport_);
	connect(file_bugreport_, &QAction::triggered,
		[]{ QDesktopServices::openUrl(QUrl("https://models.slf.ch/p/inishell-ng/issues/")); });
	menu_file->addSeparator();
105
	auto *file_close = new QAction(getIcon("application-exit"), tr("&Close help"), menu_file);
Michael Reisecker's avatar
Michael Reisecker committed
106
	file_close->setShortcut(QKeySequence::Close);
107
108
	menu_file->addAction(file_close);
	connect(file_close, &QAction::triggered, this, [=]{ this->close(); });
109

110
111
	/* VIEW menu */
	QMenu *menu_view = this->menuBar()->addMenu(tr("&View"));
112
	view_user_guide_ = new QAction(tr("&User guide"), menu_view); //no icon to get checkboxes
113
114
115
116
117
	view_user_guide_->setCheckable(true);
	view_user_guide_->setChecked(true);
	menu_view->addAction(view_user_guide_);
	view_user_guide_->setShortcut(QKeySequence::HelpContents);
	connect(view_user_guide_, &QAction::triggered, this, [=]{ loadHelp(); });
118
	view_dev_help_ = new QAction(tr("&Developer's guide"), menu_view);
119
	view_dev_help_->setCheckable(true);
120
	view_dev_help_->setShortcut(Qt::Key_F2);
121
122
	menu_view->addAction(view_dev_help_);
	connect(view_dev_help_, &QAction::triggered, this, [=]{ loadHelp(true); });
123
	menu_view->addSeparator();
Michael Reisecker's avatar
Michael Reisecker committed
124
#if !defined Q_OS_MAC //we have this from the main window in unified menu
125
126
127
128
129
	auto *help_about = new QAction(getIcon("help-about"), tr("&About"), menu_view);
	help_about->setMenuRole(QAction::AboutRole);
	menu_view->addAction(help_about);
	connect(help_about, &QAction::triggered, this, &HelpWindow::helpAbout);
	help_about->setShortcut(QKeySequence::WhatsThis);
Michael Reisecker's avatar
Michael Reisecker committed
130
#endif
131
132
133
134
	auto *show_paper = new QAction(tr("Show &paper..."), menu_view);
	menu_view->addAction(show_paper);
	connect(show_paper, &QAction::triggered, this, [=]{ QDesktopServices::openUrl(QUrl(":doc/paper.pdf")); });
	show_paper->setEnabled(false);
135
}
136

Michael Reisecker's avatar
Michael Reisecker committed
137
138
139
/**
 * @brief Display the 'About' window.
 */
140
141
void HelpWindow::helpAbout()
{
142
	getMainWindow()->helpAbout(); //parent to main to avoid showing twice
143
}