WSL/SLF GitLab Repository

HelpWindow.cc 5.15 KB
Newer Older
Mathias Bavay's avatar
Mathias Bavay committed
1
//SPDX-License-Identifier: GPL-3.0-or-later
Michael Reisecker's avatar
Michael Reisecker committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*****************************************************************************/
/*  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/>.
*/

20
21
22
23
#include <src/gui/AboutWindow.h>
#include <src/gui/HelpWindow.h>
#include <src/main/inishell.h>
#include <src/main/XMLReader.h>
Michael Reisecker's avatar
Michael Reisecker committed
24
25

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

#ifdef DEBUG
	#include <iostream>
#endif

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

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

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

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

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

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

Michael Reisecker's avatar
Michael Reisecker committed
94
95
96
/**
 * @brief Build the Help window's menu items.
 */
97
98
void HelpWindow::createMenu()
{
99
100
	/* FILE menu */
	QMenu *menu_file = this->menuBar()->addMenu(tr("&File"));
101
	auto *file_close = new QAction(getIcon("application-exit"), tr("&Close help"), menu_file);
Michael Reisecker's avatar
Michael Reisecker committed
102
	file_close->setShortcut(QKeySequence::Close);
103
104
	menu_file->addAction(file_close);
	connect(file_close, &QAction::triggered, this, [=]{ this->close(); });
105

106
107
	/* VIEW menu */
	QMenu *menu_view = this->menuBar()->addMenu(tr("&View"));
108
	view_user_guide_ = new QAction(tr("&User guide"), menu_view); //no icon to get checkboxes
109
110
111
112
113
	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(); });
114
	view_dev_help_ = new QAction(tr("&Developer's guide"), menu_view);
115
	view_dev_help_->setCheckable(true);
116
	view_dev_help_->setShortcut(Qt::Key_F2);
117
118
	menu_view->addAction(view_dev_help_);
	connect(view_dev_help_, &QAction::triggered, this, [=]{ loadHelp(true); });
119
	menu_view->addSeparator();
Michael Reisecker's avatar
Michael Reisecker committed
120
#if !defined Q_OS_MAC //we have this from the main window in unified menu
121
122
123
124
125
	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
126
#endif
127
128
129
	auto *view_online_help = new QAction(tr("View &online help..."), menu_view);
	menu_view->addAction(view_online_help);
	connect(view_online_help, &QAction::triggered, this,
130
		[=]{ QDesktopServices::openUrl(QUrl("https://gitlabext.wsl.ch/snow-models/inishell/-/wikis/home")); });
Michael Reisecker's avatar
Michael Reisecker committed
131
132
133
	auto *view_file_bugreport_ = new QAction(tr("File &bug report..."), menu_file);
	menu_view->addAction(view_file_bugreport_);
	connect(view_file_bugreport_, &QAction::triggered,
134
		[]{ QDesktopServices::openUrl(QUrl("https://gitlabext.wsl.ch/snow-models/inishell/-/issues")); });
135
	auto *show_paper = new QAction(tr("View Inishell &paper..."), menu_view);
136
	menu_view->addAction(show_paper);
137
	connect(show_paper, &QAction::triggered, this, [=]{ QDesktopServices::openUrl(QUrl("https://doi.org/10.5194/gmd-15-365-2022")); });
138
}
139

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