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_GUI_QT_TOOL_H
10#define MCRL2_GUI_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
25#include "mcrl2/utilities/logger.h"
26#include "mcrl2/utilities/platform.h"
27#include "mcrl2/utilities/toolset_version.h"
28
29#ifdef MCRL2_PLATFORM_WINDOWS
30#include <windows.h>
31#endif // MCRL2_PLATFORM_WINDOWS
32
33namespace mcrl2::gui::qt
34{
35
36class HelpMenu : public QMenu
37{
39
40 private:
46
49
50 public:
51 HelpMenu(QMainWindow* window, const std::string& name, const std::string& author, const std::string& description, const std::string& manualUrl):
60 {
61 m_actionContents.setText("&Contents");
62 m_actionContents.setShortcut(QString("F1"));
63 connect(&m_actionContents, SIGNAL(triggered()), this, SLOT(showContents()));
64
65 m_actionAbout.setText("&About");
66 connect(&m_actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
67
68 setTitle("&Help");
69 addAction(&m_actionContents);
70 window->addAction(&m_actionContents);
71 addSeparator();
72 addAction(&m_actionAbout);
73 }
74
75 protected slots:
77 {
79 }
80
81 void showAbout()
82 {
85 description.replace("\n", "<br>");
87 message += "<h1>" + m_name + "</h1>";
88 message += "<p>" + description + "</p>";
89 message += "<p>Written by " + m_author + "</p>";
90 message += "<\br>";
91 message += "<p>Version: " + version + "</p>";
93 }
94};
95
96inline
97void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
98{
99 QByteArray localMsg = msg.toLocal8Bit();
100 switch (type) {
101 case QtDebugMsg:
102 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) Maurice: waiting for std::format to be supported.
103 fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
104 break;
105 case QtInfoMsg:
106 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) Maurice: waiting for std::format to be supported.
107 fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
108 break;
109 case QtWarningMsg:
110 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) Maurice: waiting for std::format to be supported.
111 fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
112 break;
113 case QtCriticalMsg:
114 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) Maurice: waiting for std::format to be supported.
115 fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
116 break;
117 case QtFatalMsg:
118 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) Maurice: waiting for std::format to be supported.
119 fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
120 abort();
121 }
122}
123
124template <typename Tool>
125class qt_tool: public Tool
126{
127 protected:
133
134 public:
135 qt_tool(const std::string& name,
136 const std::string& author,
137 const std::string& what_is,
138 const std::string& tool_description,
139 const std::string& about_description,
140 const std::string& manual_url,
141 std::string known_issues = ""
142 )
144 m_name(name),
148 {
149 }
150
151 int execute(int& argc, char** argv)
152 {
153#ifdef MCRL2_PLATFORM_WINDOWS
154 if (GetConsoleWindow() == 0)
155 {
156 if (AttachConsole(ATTACH_PARENT_PROCESS) == 0)
157 {
158 // Failed to attach a console, we could spawn one or just ignore it.
159 }
160 }
161#endif // MCRL2_PLATFORM_WINDOWS
162
163
164 return Tool::execute(argc, argv);
165 }
166
167 bool pre_run(int& argc, char** argv) override
168 {
169#ifdef MCRL2_PLATFORM_WINDOWS
170 // Disable the dark mode on Windows 11 until the icons have been adapted
171 qputenv("QT_QPA_PLATFORM", "windows:darkmode=0");
172#endif // MCRL2_PLATFORM_WINDOWS
173
174 // The instantiation of QApplication has been postponed
175 // to here, since creating it in execute would make it
176 // impossible to view the help text in an environment
177 // without display server
178 try
179 {
180 qInstallMessageHandler(myMessageOutput); // Install the handler
181 m_application = std::make_unique<QApplication>(argc, argv);
182#ifdef MCRL2_PLATFORM_WINDOWS
183 m_application->setStyle("windowsvista");
184#endif // MCRL2_PLATFORM_WINDOWS
185 }
186 catch (...)
187 {
188 mCRL2log(mcrl2::log::log_level_t::error) << "Creating QApplication failed." << std::endl;
189 return false;
190 }
191 return true;
192 }
193
194 bool show_main_window(QMainWindow& window)
195 {
196 HelpMenu menu(&window, m_name, m_author, m_about_description, m_manual_url);
197 window.menuBar()->addAction(menu.menuAction());
198 window.menuBar()->setNativeMenuBar(true);
199 window.show();
200 return m_application->exec() == 0;
201 }
202
203 ~qt_tool() override = default;
204};
205
206} // namespace mcrl2::gui::qt
207
208#endif // MCRL2_GUI_QT_TOOL_H
HelpMenu(QMainWindow *window, const std::string &name, const std::string &author, const std::string &description, const std::string &manualUrl)
Definition qt_tool.h:51
bool pre_run(int &argc, char **argv) override
Definition qt_tool.h:167
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:135
int execute(int &argc, char **argv)
Definition qt_tool.h:151
std::string m_manual_url
Definition qt_tool.h:132
std::string m_about_description
Definition qt_tool.h:131
bool show_main_window(QMainWindow &window)
Definition qt_tool.h:194
std::string m_author
Definition qt_tool.h:130
~qt_tool() override=default
std::unique_ptr< QApplication > m_application
Definition qt_tool.h:128
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
Definition qt_tool.h:97