QCodeEditor: Update to current cpeditor/QCodeEditor fork, commit ed1196a
[smuview.git] / src / python / uiproxy.hpp
blob21534174eea4769a2676f8de6896e1c02743dd66
1 /*
2 * This file is part of the SmuView project.
4 * Copyright (C) 2019-2021 Frank Stettner <frank-stettner@gmx.net>
6 * This program 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 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef PYTHON_UIPROXY_HPP
21 #define PYTHON_UIPROXY_HPP
23 #include <memory>
24 #include <string>
25 #include <tuple>
26 #include <vector>
27 #include <pybind11/pybind11.h>
29 #include <QDockWidget>
30 #include <QEventLoop>
31 #include <QMetaObject>
32 #include <QObject>
33 #include <QTimer>
34 #include <QVariant>
36 using std::shared_ptr;
37 using std::string;
38 using std::tuple;
39 using std::vector;
41 namespace py = pybind11;
43 namespace sv {
45 class Session;
47 namespace channels {
48 class BaseChannel;
50 namespace data {
51 class AnalogTimeSignal;
53 namespace devices {
54 class BaseDevice;
55 class Configurable;
58 namespace python {
60 class UiHelper;
62 /**
63 * The python interpreter is executed in its own thread, therefore calling any
64 * methods that will manipulate Qt widgets directly won't work.
65 * So we are using UiProxy which is communicating with the Qt main loop via
66 * signals and slots.
68 class UiProxy : public QObject
70 Q_OBJECT
72 public:
73 UiProxy(Session &session, shared_ptr<UiHelper> ui_helper);
75 string ui_add_device_tab(shared_ptr<devices::BaseDevice> device);
77 string ui_add_data_view(const string &tab_id, Qt::DockWidgetArea area,
78 shared_ptr<data::AnalogTimeSignal> signal);
79 vector<string> ui_add_control_views(const string &tab_id,
80 Qt::DockWidgetArea area,
81 shared_ptr<devices::Configurable> configurable);
82 string ui_add_time_plot_view(const string &tab_id, Qt::DockWidgetArea area);
83 string ui_add_xy_plot_view(const string &tab_id, Qt::DockWidgetArea area);
84 string ui_add_power_panel_view(const string &tab_id,
85 Qt::DockWidgetArea area,
86 shared_ptr<data::AnalogTimeSignal> voltage_signal,
87 shared_ptr<data::AnalogTimeSignal> current_signal);
88 string ui_add_value_panel_view(const string &tab_id,
89 Qt::DockWidgetArea area,
90 shared_ptr<channels::BaseChannel> channel);
91 string ui_add_value_panel_view(const string &tab_id,
92 Qt::DockWidgetArea area,
93 shared_ptr<data::AnalogTimeSignal> signal);
95 void ui_add_signal_to_data_view(const string &tab_id, const string &view_id,
96 shared_ptr<data::AnalogTimeSignal> signal);
98 void ui_set_channel_to_time_plot_view(const string &tab_id,
99 const string &view_id,
100 shared_ptr<channels::BaseChannel> channel);
101 string ui_add_curve_to_time_plot_view(const string &tab_id,
102 const string &view_id,
103 shared_ptr<data::AnalogTimeSignal> signal);
104 string ui_add_curve_to_xy_plot_view(const string &tab_id,
105 const string &view_id,
106 shared_ptr<data::AnalogTimeSignal> x_signal,
107 shared_ptr<data::AnalogTimeSignal> y_signal);
108 void ui_set_curve_name(const string &tab_id, const string &view_id,
109 const string &curve_id, const string &name);
110 void ui_set_curve_color(const string &tab_id, const string &view_id,
111 const string &curve_id, tuple<int, int, int> color);
113 bool ui_show_message_box(const std::string &title, const std::string &text);
114 py::object ui_show_string_input_dialog(const string &title,
115 const string &label, const string &value);
116 py::object ui_show_double_input_dialog(const string &title,
117 const string &label, double value, int decimals, double step,
118 double min, double max);
119 py::object ui_show_int_input_dialog(const string &title,
120 const string &label, int value, int step, int min, int max);
122 private:
123 void init_wait_for_tab_added(string &id, int timeout = 1000);
124 void init_wait_for_view_added(string &id, int timeout = 1000);
125 void init_wait_for_views_added(vector<string> &ids, int timeout = 1000);
126 void init_wait_for_curve_added(string &id, int timeout = 1000);
127 void init_wait_for_message_box(bool &ok, int timeout = 0);
128 void init_wait_for_input_dialog(bool &ok, QVariant &qvar, int timeout = 0);
129 void finish_wait_for_signal();
131 Session &session_;
132 shared_ptr<UiHelper> ui_helper_;
133 QEventLoop event_loop_;
134 QTimer timer_;
135 QMetaObject::Connection event_loop_finished_conn_;
136 QMetaObject::Connection event_loop_canceled_conn_;
137 QMetaObject::Connection timer_conn_;
139 Q_SIGNALS:
140 void add_device_tab(shared_ptr<sv::devices::BaseDevice> device);
142 void add_data_view(const std::string &tab_id, Qt::DockWidgetArea area,
143 shared_ptr<sv::data::AnalogTimeSignal> signal);
144 void add_control_views(const std::string &tab_id, Qt::DockWidgetArea area,
145 shared_ptr<sv::devices::Configurable> configurable);
146 void add_time_plot_view(const std::string &tab_id, Qt::DockWidgetArea area);
147 void add_xy_plot_view(const std::string &tab_id, Qt::DockWidgetArea area);
148 void add_power_panel_view(const std::string &tab_id,
149 Qt::DockWidgetArea area,
150 shared_ptr<sv::data::AnalogTimeSignal> voltage_signal,
151 shared_ptr<sv::data::AnalogTimeSignal> current_signal);
152 void add_value_panel_view(const std::string &tab_id,
153 Qt::DockWidgetArea area,
154 shared_ptr<sv::channels::BaseChannel> channel);
155 void add_value_panel_view(const std::string &tab_id,
156 Qt::DockWidgetArea area,
157 shared_ptr<sv::data::AnalogTimeSignal> signal);
159 void add_signal_to_data_view(const std::string &tab_id,
160 const std::string &view_id,
161 shared_ptr<sv::data::AnalogTimeSignal> signal);
163 void set_channel_to_time_plot_view(const std::string &tab_id,
164 const std::string &view_id,
165 shared_ptr<sv::channels::BaseChannel> channel);
166 void add_curve_to_time_plot_view(const std::string &tab_id,
167 const std::string &view_id,
168 shared_ptr<sv::data::AnalogTimeSignal> signal);
169 void add_curve_to_xy_plot_view(const std::string &tab_id,
170 const std::string &view_id,
171 shared_ptr<sv::data::AnalogTimeSignal> x_signal,
172 shared_ptr<sv::data::AnalogTimeSignal> y_signal);
173 void set_curve_name(const std::string &tab_id, const std::string &view_id,
174 const std::string &curve_id, const std::string &name);
175 void set_curve_color(const std::string &tab_id, const std::string &view_id,
176 const std::string &curve_id, std::tuple<int, int, int> color);
178 void show_message_box(const std::string &title, const std::string &text);
179 void show_string_input_dialog(const std::string &title,
180 const std::string &label, const std::string &value);
181 void show_double_input_dialog(const std::string &title,
182 const std::string &label, double value, int decimals, double step,
183 double min, double max);
184 void show_int_input_dialog(const std::string &title,
185 const std::string &label, int value, int step, int min, int max);
188 } // namespace python
189 } // namespace sv
191 #endif // PYTHON_UIPROXY_HPP