WSL/SLF GitLab Repository

common.cc 4.55 KB
Newer Older
1
2
3
4
5
/*****************************************************************************/
/*  Copyright 2019 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
6
   it under the terms of the GNU General Public License as published by
7
8
9
10
11
12
   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
13
   GNU General Public License for more details.
14

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

19
#include "common.h"
20
#include "colors.h"
21
#include "src/main/os.h"
22
#include "src/main/settings.h"
23
24
25

#include <QDir>
#include <QFileInfo>
26
27
28

namespace html {

29
30
31
32
33
/**
 * @brief Add HTML embolden tags to text.
 * @param[in] text Text to add embolden tags to.
 * @return Text enclosed by embolden tags.
 */
34
35
36
37
38
QString bold(const QString &text)
{
	return "<b>" + text + "</b>";
}

39
40
41
42
43
/**
 * @brief Add HTML color tags to text.
 * @param[in] text Text to add color tags to.
 * @return Text enclosed by color tags.
 */
44
45
QString color(const QString &text, const QString &color)
{
46
	const QString strRet( "<font color=\"" + colors::getQColor(color).name() + "\">" + text + "</font>");
47
48
49
50
	return strRet;
}

} //namespace html
51

52
53
/**
 * @brief Retrieve the message of an event that wants to communicate.
54
 * @details This class is used for example by the XML schema validation.
55
56
57
58
59
 * @param[in] type Type of the message (unused).
 * @param[in] description A text description of what has happened.
 * @param[in] identifier Identifier for the message (unused).
 * @param[in] location The location something has happened at (e. g. line number for text errors).
 */
60
61
62
void MessageHandler::handleMessage(QtMsgType type, const QString &description, const QUrl &identifier,
    const QSourceLocation &location)
{
63
	Q_UNUSED(type) //turn off compiler warnings
64
65
66
67
68
	Q_UNUSED(identifier)

	description_ = description;
	location_ = location;
}
69
70
71
72
73
74
75
76
77
78
79

/**
 * @brief Fill a list with directories to search for XMLs.
 * @details This function queries a couple of default folders on various systems, as well as
 * ones that can be set by the user. Duplicates (e. g. the same folder given by a relative
 * and an absolute path) are ignored.
 * @return A list of directories to search for XMLs.
 */
QStringList getSearchDirs(const bool &include_user_set, const bool &include_nonexistent_folders)
{

80
	/* hardcoded directories, the system specific paths are partly handled by Qt */
81
	QStringList locations;
82
	locations << "."; //the application's current directory
83
	os::getSystemLocations(locations); //update list with OS specific search paths
84

85
86
87
	//TODO: recursive search
	QStringList dirs;
	for (auto &tmp_dir : locations) {
88
		dirs << tmp_dir + "/inishell-apps";
89
		dirs << tmp_dir + "/simulations";
90
91
92
93
	}

	if (include_user_set) {
		/* user set paths */
94
		const QStringList user_xml_paths( getListSetting("user::xmlpaths", "path") );
95
		dirs.append(user_xml_paths);
96
97
98
99
100
	}

	//now, check that we don't have the same folder multiple times (e. g. via relative and absolute paths):
	QStringList filtered_dirs;
	for (auto &dir : dirs) {
101
		const QFileInfo dinfo( dir );
102
103
104
		if (!include_nonexistent_folders && !dinfo.exists())
			continue;
		bool found = false;
105
		for (const auto &filtered_dir : filtered_dirs) {
106
			if (dinfo.absoluteFilePath() == QFileInfo( filtered_dir ).absoluteFilePath()) {
107
108
109
110
111
112
113
114
115
116
				found = true;
				break;
			}
		}
		if (!found)
			filtered_dirs << dir;
	}

	return filtered_dirs;
}
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

/**
 * @brief Convert a key press event to a key sequence.
 * @details This way key sequences can also be used in key press event listeners.
 * @param[in] event Input key press event.
 * @return The key event converted to a QKeySequence.
 */
QKeySequence keyToSequence(QKeyEvent *event)
{ //this is a bit of a HACK: get key sequences in event listeners
	QString modifier;
	if (event->modifiers() & Qt::ShiftModifier)
		modifier += "Shift+";
	if (event->modifiers() & Qt::ControlModifier)
		modifier += "Ctrl+";
	if (event->modifiers() & Qt::AltModifier)
		modifier += "Alt+";
	if (event->modifiers() & Qt::MetaModifier)
		modifier += "Meta+";

136
	const QString key_string( QKeySequence(event->key()).toString() );
137
138
	return QKeySequence( modifier + key_string );
} //https://forum.qt.io/topic/73408/qt-reading-key-sequences-from-key-event