1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_octave_qobject_h)
27 #define octave_octave_qobject_h 1
28 
29 #include <memory>
30 
31 #include <QApplication>
32 #include <QList>
33 #include <QObject>
34 #include <QString>
35 #include <QStringList>
36 
37 #include "interpreter-qobject.h"
38 #include "resource-manager.h"
39 #include "shortcut-manager.h"
40 
41 namespace octave
42 {
43   class main_window;
44   class qt_application;
45   class qt_interpreter_events;
46 
47   //! This class is a simple wrapper around QApplication so that we can
48   //! reimplement QApplication::notify.  The octave_qapplication object
49   //! should behave identically to a QApplication object except that it
50   //! overrides the notify method so we can handle forward Octave
51   //! octave::execution_exception exceptions from the GUI thread to the
52   //! interpreter thread.
53 
54   class octave_qapplication : public QApplication
55   {
56     Q_OBJECT
57 
58   public:
59 
octave_qapplication(int & argc,char ** argv)60     octave_qapplication (int& argc, char **argv)
61       : QApplication (argc, argv)
62     { }
63 
64     virtual bool notify (QObject *receiver, QEvent *e) override;
65 
~octave_qapplication(void)66     ~octave_qapplication (void) { };
67 
68   signals:
69 
70     void interpreter_event (const fcn_callback& fcn);
71     void interpreter_event (const meth_callback& meth);
72   };
73 
74   //! Base class for Octave interfaces that use Qt.  There are two
75   //! classes derived from this one.  One provides a command-line
76   //! interface that may use Qt graphics and another provides the
77   //! full GUI experience.
78 
79   class base_qobject : public QObject
80   {
81     Q_OBJECT
82 
83   public:
84 
85     base_qobject (qt_application& app_context);
86 
87     ~base_qobject (void);
88 
89     void config_translators (void);
90 
91     void start_main_thread (void);
92 
93     int exec (void);
94 
95     // The Octave application context.
app_context(void)96     qt_application& app_context (void) { return m_app_context; }
97 
98     // The Qt QApplication.
qapplication(void)99     QApplication * qapplication (void) { return m_qapplication; };
100 
get_resource_manager(void)101     resource_manager& get_resource_manager (void)
102     {
103       return m_resource_manager;
104     }
105 
get_shortcut_manager(void)106     shortcut_manager& get_shortcut_manager (void)
107     {
108       return m_shortcut_manager;
109     }
110 
get_qt_interpreter_events(void)111     std::shared_ptr<qt_interpreter_events> get_qt_interpreter_events (void)
112     {
113       return m_qt_interpreter_events;
114     }
115 
qt_link(void)116     qt_interpreter_events * qt_link (void)
117     {
118       return m_qt_interpreter_events.get ();
119     }
120 
interpreter_qobj(void)121     interpreter_qobject * interpreter_qobj (void)
122     {
123       return m_interpreter_qobj;
124     }
125 
main_thread(void)126     QThread *main_thread (void) { return m_main_thread; }
127 
128     virtual bool confirm_shutdown (void);
129 
130   signals:
131 
132     void request_interpreter_shutdown (int);
133 
134   public slots:
135 
136     void handle_interpreter_execution_finished (int);
137 
138     void handle_interpreter_shutdown_finished (int);
139 
140     void interpreter_event (const fcn_callback& fcn);
141 
142     void interpreter_event (const meth_callback& meth);
143 
144     void copy_image_to_clipboard (const QString& file, bool remove_file);
145 
146   protected:
147 
148     qt_application& m_app_context;
149 
150     // Use these to ensure that argc and argv exist for as long as the
151     // QApplication object.
152 
153     int m_argc;
154     char **m_argv;
155 
156     octave_qapplication *m_qapplication;
157 
158     resource_manager m_resource_manager;
159 
160     shortcut_manager m_shortcut_manager;
161 
162     QTranslator *m_qt_tr;
163     QTranslator *m_gui_tr;
164     QTranslator *m_qsci_tr;
165 
166     bool m_translators_installed;
167 
168     std::shared_ptr<qt_interpreter_events> m_qt_interpreter_events;
169 
170     interpreter_qobject *m_interpreter_qobj;
171 
172     QThread *m_main_thread;
173   };
174 
175   //! This object provides a command-line interface to Octave that may
176   //! use Qt graphics.
177 
178   class cli_qobject : public base_qobject
179   {
180     Q_OBJECT
181 
182   public:
183 
184     cli_qobject (qt_application& app_context);
185 
186     ~cli_qobject (void) = default;
187   };
188 
189   //! This object provides a full GUI interface to Octave that is
190   //! implemented Qt.
191 
192   class gui_qobject : public base_qobject
193   {
194     Q_OBJECT
195 
196   public:
197 
198     gui_qobject (qt_application& app_context);
199 
200     ~gui_qobject (void);
201 
202     bool confirm_shutdown (void);
203 
204   private:
205 
206     main_window *m_main_window;
207   };
208 }
209 
210 #endif
211