mCRL2
Loading...
Searching...
No Matches
qt_tool.h
Go to the documentation of this file.
1// Author: Ruud Koolen
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9#ifndef MCRL2_UTILITIES_QT_TOOL_H
10#define MCRL2_UTILITIES_QT_TOOL_H
11
12#include <memory>
13#include <QAction>
14#include <QApplication>
15#include <QDateTime>
16#include <QDesktopServices>
17#include <QMainWindow>
18#include <QMenu>
19#include <QMenuBar>
20#include <QMessageBox>
21#include <QString>
22#include <QUrl>
23#include <QtGlobal>
24
28
29#ifdef MCRL2_PLATFORM_WINDOWS
30#include <windows.h>
31#endif // MCRL2_PLATFORM_WINDOWS
32
33namespace mcrl2
34{
35namespace gui
36{
37namespace qt
38{
39
40class HelpMenu : public QMenu
41{
42 Q_OBJECT
43
44 private:
45 QMainWindow* m_window;
46 QString m_name;
47 QString m_author;
49 QString m_manualUrl;
50
53
54 public:
55 HelpMenu(QMainWindow* window, const std::string& name, const std::string& author, const std::string& description, const std::string& manualUrl):
56 QMenu(window->menuBar()),
57 m_window(window),
58 m_name(QString::fromStdString(name)),
59 m_author(QString::fromStdString(author)),
60 m_description(QString::fromStdString(description)),
61 m_manualUrl(QString::fromStdString(manualUrl)),
62 m_actionContents(window),
63 m_actionAbout(window)
64 {
65 m_actionContents.setText("&Contents");
66 m_actionContents.setShortcut(QString("F1"));
67 connect(&m_actionContents, SIGNAL(triggered()), this, SLOT(showContents()));
68
69 m_actionAbout.setText("&About");
70 connect(&m_actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
71
72 setTitle("&Help");
73 addAction(&m_actionContents);
74 window->addAction(&m_actionContents);
75 addSeparator();
76 addAction(&m_actionAbout);
77 }
78
79 protected slots:
81 {
82 QDesktopServices::openUrl(QUrl(m_manualUrl));
83 }
84
85 void showAbout()
86 {
87 QString version = QString(mcrl2::utilities::get_toolset_version().c_str());
88 QString description = m_description;
89 description.replace("\n", "<br>");
90 QString message;
91 message += "<h1>" + m_name + "</h1>";
92 message += "<p>" + description + "</p>";
93 message += "<p>Written by " + m_author + "</p>";
94 message += "<\br>";
95 message += "<p>Version: " + version + "</p>";
96 QMessageBox::about(m_window, QString("About ") + m_name, message);
97 }
98};
99
100inline
101void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
102{
103 QByteArray localMsg = msg.toLocal8Bit();
104 switch (type) {
105 case QtDebugMsg:
106 fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
107 break;
108 case QtInfoMsg:
109 fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
110 break;
111 case QtWarningMsg:
112 fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
113 break;
114 case QtCriticalMsg:
115 fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
116 break;
117 case QtFatalMsg:
118 fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
119 abort();
120 }
121}
122
123template <typename Tool>
124class qt_tool: public Tool
125{
126 protected:
127 std::unique_ptr<QApplication> m_application;
128 std::string m_name;
129 std::string m_author;
131 std::string m_manual_url;
132
133 public:
134 qt_tool(const std::string& name,
135 const std::string& author,
136 const std::string& what_is,
137 const std::string& tool_description,
138 const std::string& about_description,
139 const std::string& manual_url,
140 std::string known_issues = ""
141 )
142 : Tool(name, author, what_is, tool_description, known_issues),
143 m_name(name),
144 m_author(author),
145 m_about_description(about_description),
146 m_manual_url(manual_url)
147 {
148 }
149
150 int execute(int& argc, char** argv)
151 {
152#ifdef MCRL2_PLATFORM_WINDOWS
153 if (GetConsoleWindow() == 0)
154 {
155 if (AttachConsole(ATTACH_PARENT_PROCESS) == 0)
156 {
157 // Failed to attach a console, we could spawn one or just ignore it.
158 }
159 }
160#endif // MCRL2_PLATFORM_WINDOWS
161
162
163 return Tool::execute(argc, argv);
164 }
165
166 bool pre_run(int& argc, char** argv)
167 {
168#ifdef MCRL2_PLATFORM_WINDOWS
169 // Disable the dark mode on Windows 11 until the icons have been adapted
170 qputenv("QT_QPA_PLATFORM", "windows:darkmode=0");
171#endif // MCRL2_PLATFORM_WINDOWS
172
173 // The instantiation of QApplication has been postponed
174 // to here, since creating it in execute would make it
175 // impossible to view the help text in an environment
176 // without display server
177 try
178 {
179 qInstallMessageHandler(myMessageOutput); // Install the handler
180 m_application = std::unique_ptr<QApplication>(new QApplication(argc, argv));
181#ifdef MCRL2_PLATFORM_WINDOWS
182 m_application->setStyle("windowsvista");
183#endif // MCRL2_PLATFORM_WINDOWS
184 }
185 catch (...)
186 {
187 mCRL2log(mcrl2::log::debug) << "Creating QApplication failed." << std::endl;
188 }
189 return true;
190 }
191
192 bool show_main_window(QMainWindow& window)
193 {
195 window.menuBar()->addAction(menu.menuAction());
196 window.menuBar()->setNativeMenuBar(true);
197 window.show();
198 return m_application->exec() == 0;
199 }
200
201 virtual ~qt_tool() {}
202};
203
204} // namespace qt
205} // namespace gui
206} // namespace mcrl2
207
208#endif // MCRL2_UTILITIES_QT_TOOL_H
QMainWindow * m_window
Definition qt_tool.h:45
HelpMenu(QMainWindow *window, const std::string &name, const std::string &author, const std::string &description, const std::string &manualUrl)
Definition qt_tool.h:55
bool pre_run(int &argc, char **argv)
Definition qt_tool.h:166
qt_tool(const std::string &name, const std::string &author, const std::string &what_is, const std::string &tool_description, const std::string &about_description, const std::string &manual_url, std::string known_issues="")
Definition qt_tool.h:134
int execute(int &argc, char **argv)
Definition qt_tool.h:150
std::string m_manual_url
Definition qt_tool.h:131
std::string m_about_description
Definition qt_tool.h:130
bool show_main_window(QMainWindow &window)
Definition qt_tool.h:192
std::string m_author
Definition qt_tool.h:129
std::unique_ptr< QApplication > m_application
Definition qt_tool.h:127
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:391
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Definition qt_tool.h:101
std::string get_toolset_version()
Get the toolset revision.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
Get the toolset revision.