WSL/SLF GitLab Repository

common.h 4.21 KB
Newer Older
Mathias Bavay's avatar
Mathias Bavay committed
1
//SPDX-License-Identifier: GPL-3.0-or-later
2
3
4
5
6
/*****************************************************************************/
/*  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
7
   it under the terms of the GNU General Public License as published by
8
9
10
11
12
13
   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
14
   GNU General Public License for more details.
15

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

/*
 * This file provides some common mostly light-weight functionalty across INIshell.
 * 2019-11
 */

25
26
27
#ifndef COMMON_H
#define COMMON_H

28
//this is a cmake generated file
29
30
#include <src/main/config.h>

31
#include <QIcon>
32
33
#include <QKeyEvent>
#include <QKeySequence>
34
#include <QString>
35
#include <QStringList>
36
#include <QtXml>
Michael Reisecker's avatar
Michael Reisecker committed
37
38
#include <QtXmlPatterns/QAbstractMessageHandler>

39
namespace html {
Michael Reisecker's avatar
Michael Reisecker committed
40

41
42
43
44
QString bold(const QString &text);
QString color(const QString &text, const QString &color);

} //namespace html
45

46
47
48
/**
 * @struct CaseInsensitiveCompare
 * @brief A weak ordered comparison struct for case insensitive key-value mapping.
49
 * @details This struct can be used in containers for key lookup.
50
51
52
53
54
55
56
57
 */
struct CaseInsensitiveCompare {
	bool operator() (const QString &first_str, const QString &second_str) const
	{ //<0: less than; =0: equal; >0: greater than; STL comparison is done via a less-type operator
		return (QString::compare(first_str, second_str, Qt::CaseInsensitive) < 0);
	}
};

58
59
60
61
62
/**
 * @class MessageHandler
 * @brief Message handler to conveniently retrieve Qt internal messages.
 * @details This class is used for example to get XML schema validation errors.
 */
63
64
class MessageHandler : public QAbstractMessageHandler {
	public:
65
		MessageHandler() : QAbstractMessageHandler(nullptr), description_(), location_() {}
66
67
68
69
70
		QString status() const { return description_; }
		int line() const { return static_cast<int>(location_.line()); }
		int column() const { return static_cast<int>(location_.column()); }

	protected:
Michael Reisecker's avatar
Michael Reisecker committed
71
72
		void handleMessage(QtMsgType type, const QString &description,
		    const QUrl &identifier, const QSourceLocation &location) override;
73
74
75
76
77
78

	private:
		QString description_;
		QSourceLocation location_;
};

79
80
81
82
83
84
/**
 * @brief Check if an XML node has a certain INI section associated with i.
 * @param[in] section Check if this section is present.
 * @param[in] options XML node to check for the section.
 * @return True if the section is available.
 */
85
86
inline bool hasSectionSpecified(const QString &section, const QDomElement &options)
{
87
88
89
90
91
92
93
94
95
	/*
	 * Sections can be specified in an attribute and also as a separate element. This function
	 * checks if either is true for a given section and panel and tells the element factory that
	 * the panel should be constructed. This is useful if multiple sections are given, but not
	 * for every element that follows.
	 * (Furthermore, they could be in a dedicated <section>...</section> node,
	 * but then the section is fixed to a single one.)
	 */
	if (!options.attribute("section").isNull()) //<section name="name"/>
96
97
98
99
		return (QString::compare(options.attribute("section"), section, Qt::CaseInsensitive) == 0);
	int counter = 0;
	for (QDomElement section_element = options.firstChildElement("section"); !section_element.isNull();
	    section_element = section_element.nextSiblingElement("section")) { //read all <section> tags
100
		counter++; //"<parameter key=... section="name">
101
102
103
		if (QString::compare(section_element.attribute("name"), section, Qt::CaseInsensitive) == 0)
			return true;
	}
Michael Reisecker's avatar
Michael Reisecker committed
104
	return (counter == 0); //no section specified means all sections are good to go
105
106
}

107
QIcon getIcon(const QString& icon_name);
108

109
110
111
QStringList getSearchDirs(const bool &include_user_set = true, const bool &include_nonexistent_folders = true);
QKeySequence keyToSequence(QKeyEvent *event);

112
#endif //COMMON_H