Line data Source code
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 :
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 :
33 : namespace mcrl2
34 : {
35 : namespace gui
36 : {
37 : namespace qt
38 : {
39 :
40 : class HelpMenu : public QMenu
41 : {
42 : Q_OBJECT
43 :
44 : private:
45 : QMainWindow* m_window;
46 : QString m_name;
47 : QString m_author;
48 : QString m_description;
49 : QString m_manualUrl;
50 :
51 : QAction m_actionContents;
52 : QAction m_actionAbout;
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:
80 0 : void showContents()
81 : {
82 0 : QDesktopServices::openUrl(QUrl(m_manualUrl));
83 0 : }
84 :
85 0 : void showAbout()
86 : {
87 0 : QString version = QString(mcrl2::utilities::get_toolset_version().c_str());
88 0 : QString description = m_description;
89 0 : description.replace("\n", "<br>");
90 0 : QString message;
91 0 : message += "<h1>" + m_name + "</h1>";
92 0 : message += "<p>" + description + "</p>";
93 0 : message += "<p>Written by " + m_author + "</p>";
94 0 : message += "<\br>";
95 0 : message += "<p>Version: " + version + "</p>";
96 0 : QMessageBox::about(m_window, QString("About ") + m_name, message);
97 0 : }
98 : };
99 :
100 : template <typename Tool>
101 : class qt_tool: public Tool
102 : {
103 : protected:
104 : std::unique_ptr<QApplication> m_application;
105 : std::string m_name;
106 : std::string m_author;
107 : std::string m_about_description;
108 : std::string m_manual_url;
109 :
110 : public:
111 : qt_tool(const std::string& name,
112 : const std::string& author,
113 : const std::string& what_is,
114 : const std::string& tool_description,
115 : const std::string& about_description,
116 : const std::string& manual_url,
117 : std::string known_issues = ""
118 : )
119 : : Tool(name, author, what_is, tool_description, known_issues),
120 : m_name(name),
121 : m_author(author),
122 : m_about_description(about_description),
123 : m_manual_url(manual_url)
124 : {
125 : }
126 :
127 : int execute(int& argc, char** argv)
128 : {
129 : #ifdef MCRL2_PLATFORM_WINDOWS
130 : if (GetConsoleWindow() == 0)
131 : {
132 : if (AttachConsole(ATTACH_PARENT_PROCESS) == 0)
133 : {
134 : // Failed to attach a console, we could spawn one or just ignore it.
135 : }
136 : }
137 : #endif // MCRL2_PLATFORM_WINDOWS
138 :
139 :
140 : return Tool::execute(argc, argv);
141 : }
142 :
143 : bool pre_run(int& argc, char** argv)
144 : {
145 : // The instantiation of QApplication has been postponed
146 : // to here, since creating it in execute would make it
147 : // impossible to view the help text in an environment
148 : // without display server
149 : try
150 : {
151 : m_application = std::unique_ptr<QApplication>(new QApplication(argc, argv));
152 : }
153 : catch (...)
154 : {
155 : mCRL2log(mcrl2::log::debug) << "Creating QApplication failed." << std::endl;
156 : }
157 : return true;
158 : }
159 :
160 : bool show_main_window(QMainWindow& window)
161 : {
162 : HelpMenu menu(&window, m_name, m_author, m_about_description, m_manual_url);
163 : window.menuBar()->addAction(menu.menuAction());
164 : window.menuBar()->setNativeMenuBar(true);
165 : window.show();
166 : return m_application->exec() == 0;
167 : }
168 :
169 : virtual ~qt_tool() {}
170 : };
171 :
172 : } // namespace qt
173 : } // namespace gui
174 : } // namespace mcrl2
175 :
176 : #endif // MCRL2_UTILITIES_QT_TOOL_H
|