RTXI  3.0.0
The Real-Time eXperiment Interface Reference Manual
widgets.hpp
Go to the documentation of this file.
1 #ifndef WIDGET_HPP
2 #define WIDGET_HPP
3 
4 #include <QGridLayout>
5 #include <QGroupBox>
6 #include <QLabel>
7 #include <QLineEdit>
8 #include <QMainWindow>
9 #include <QMdiSubWindow>
10 #include <QPushButton>
11 #include <QValidator>
12 #include <limits>
13 #include <map>
14 #include <memory>
15 #include <optional>
16 #include <string>
17 #include <unordered_map>
18 #include <variant>
19 #include <vector>
20 
21 #include "dlplugin.hpp"
22 #include "event.hpp"
23 #include "io.hpp"
24 #include "rt.hpp"
25 
26 // These metatype declarations are needed by qt to store
27 // the types in QVariant, which is very convenient and reduces
28 // unnecessary repetiotion in panel construction
29 Q_DECLARE_METATYPE(IO::Block*)
30 Q_DECLARE_METATYPE(IO::flags_t)
31 Q_DECLARE_METATYPE(IO::endpoint)
32 Q_DECLARE_METATYPE(RT::block_connection_t)
33 Q_DECLARE_METATYPE(RT::State::state_t)
34 
35 
38 namespace Widgets
39 {
40 
44 namespace Variable
45 {
46 
47 typedef size_t Id;
48 constexpr Id INVALID_ID = static_cast<Id>(std::numeric_limits<size_t>::max());
49 
53 enum variable_t : size_t
54 {
60  UNKNOWN
61 };
62 
70 std::string state2string(RT::State::state_t state);
71 
80 std::string vartype2string(variable_t type);
81 
95 struct Info
96 {
97  size_t id = 0;
98  std::string name;
99  std::string description;
101  std::variant<int64_t, double, uint64_t, std::string, RT::State::state_t>
103 };
104 
105 } // namespace Variable
106 
107 class DefaultGUILineEdit : public QLineEdit
108 {
109  Q_OBJECT
110 
111 public:
112  explicit DefaultGUILineEdit(QWidget* parent);
113 
114  void blacken();
115  QPalette palette;
116 
117 public slots:
118  void redden();
119 }; // class DefaultGUILineEdi
120 
121 // Forward declare plugin class for the component and panel private pointers
122 class Plugin;
123 class Panel;
124 
135 {
145  std::unique_ptr<Widgets::Plugin> (*createPlugin)(Event::Manager*) = nullptr;
146 
156  std::unique_ptr<Widgets::Component> (*createComponent)(Widgets::Plugin*) =
157  nullptr;
158 
170  Widgets::Panel* (*createPanel)(QMainWindow*, Event::Manager*) = nullptr;
171 };
172 
183 class Component : public RT::Thread
184 {
185 public:
186  Component(Widgets::Plugin* hplugin,
187  const std::string& mod_name,
188  const std::vector<IO::channel_t>& channels,
189  const std::vector<Widgets::Variable::Info>& variables);
190 
199  template<typename T>
200  T getValue(const size_t& var_id)
201  {
202  return std::get<T>(this->parameters.at(var_id).value);
203  }
204 
211  template<typename T>
212  void setValue(const size_t& var_id, T value)
213  {
214  this->parameters.at(var_id).value = value;
215  }
216 
224  std::string getDescription(const size_t& var_id);
225 
234  std::string getValueString(const size_t& var_id);
235 
242  RT::State::state_t getState() const { return this->component_state; }
243 
249  void setState(RT::State::state_t state) { this->component_state = state; }
250 
257  std::vector<Widgets::Variable::Info> getParametersInfo()
258  {
259  return this->parameters;
260  }
261 
267  Widgets::Plugin* getHostPlugin() { return this->hostPlugin; }
268 
269 private:
270  std::vector<Widgets::Variable::Info> parameters;
271  Widgets::Plugin* hostPlugin;
272  RT::State::state_t component_state = RT::State::INIT;
273 };
274 
278 class Panel : public QWidget
279 {
280  Q_OBJECT
281 public:
282  Panel(const std::string& mod_name,
283  QMainWindow* mw,
284  Event::Manager* ev_manager);
285 
286  QMdiSubWindow* getMdiWindow() { return this->m_subwindow; }
287 
301  virtual void createGUI(const std::vector<Widgets::Variable::Info>& vars,
302  const std::vector<Widgets::Variable::Id>& skip_ids);
303 
310  void setHostPlugin(Widgets::Plugin* hplugin) { this->hostPlugin = hplugin; }
311 
312 signals:
313 
318 
319 public slots:
320 
324  void resizeMe();
325 
329  virtual void exit();
330 
334  virtual void refresh();
335 
339  virtual void modify();
340 
346  virtual void pause(bool p);
347 
353  virtual void update_state(RT::State::state_t flag);
354 
355 protected:
363  QString getParameter(const QString& var_name);
364 
372  void setParameter(const QString& var_name, double value);
373 
381  void setParameter(const QString& var_name, uint64_t value);
382 
390  void setParameter(const QString& var_name, int value);
391 
399  QString getComment(const QString& name);
400 
407  void setComment(const QString& var_name, const QString& comment);
408 
415  void closeEvent(QCloseEvent* event) override;
416 
422  std::string getName() { return this->m_name; }
423 
429  Widgets::Plugin* getHostPlugin() { return this->hostPlugin; }
430 
436  QMainWindow* getQMainWindowPtr() { return this->main_window; }
437 
443  Event::Manager* getRTXIEventManager() { return this->event_manager; }
444 
445 private:
446  QMainWindow* main_window = nullptr;
447  std::string m_name;
448  QMdiSubWindow* m_subwindow = nullptr;
449  Widgets::Plugin* hostPlugin = nullptr;
450  Event::Manager* event_manager = nullptr;
451 
452  // Default buttons
453  QPushButton* pauseButton = nullptr;
454  QPushButton* modifyButton = nullptr;
455  QPushButton* unloadButton = nullptr;
456 
457  struct param_t
458  {
459  QLabel* label = nullptr;
460  QString str_value;
461  DefaultGUILineEdit* edit = nullptr;
464  };
465  std::unordered_map<std::string, param_t> parameter;
466  QPalette palette;
467 };
468 
479 class Plugin : public Event::Handler
480 {
481 public:
482  Plugin(Event::Manager* ev_manager, std::string mod_name);
483  Plugin(const Plugin& plugin) = delete; // copy constructor
484  Plugin& operator=(const Plugin& plugin) =
485  delete; // copy assignment noperator
486  Plugin(Plugin&&) = delete; // move constructor
487  Plugin& operator=(Plugin&&) = delete; // move assignment operator
488  ~Plugin() override;
489 
495  size_t getID();
496 
502  void attachComponent(std::unique_ptr<Widgets::Component> component);
503 
509  void attachPanel(Widgets::Panel* panel);
510 
519  int64_t getComponentIntParameter(const Variable::Id& parameter_id);
520 
529  uint64_t getComponentUIntParameter(const Variable::Id& parameter_id);
530 
539  double getComponentDoubleParameter(const Variable::Id& parameter_id);
540 
553  template<typename T>
554  int setComponentParameter(const Variable::Id& parameter_id, T value)
555  {
556  const int result = 0;
558  if (typeid(T) == typeid(int64_t)) {
560  } else if (typeid(T) == typeid(double)) {
562  } else if (typeid(T) == typeid(uint64_t)) {
564  } else if (typeid(T) == typeid(std::string)) {
565  param_type = Widgets::Variable::COMMENT;
566  } else {
567  ERROR_MSG(
568  "Widgets::Plugin::setComponentParameter : Parameter type not "
569  "supported");
570  return -1;
571  }
573  event.setParam("paramID", std::any(parameter_id));
574  event.setParam("paramType", std::any(param_type));
575  event.setParam("paramValue", std::any(value));
576  event.setParam("paramWidget", std::any(this->plugin_component.get()));
577  this->event_manager->postEvent(&event);
578  return result;
579  }
580 
586  std::string getName() const { return this->name; }
587 
593  bool getActive();
594 
602  int setActive(bool state);
603 
609  void receiveEvent(Event::Object* event) override;
610 
616  std::string getLibrary() const { return this->library; }
617 
623  void setLibrary(const std::string& lib) { this->library = lib; }
624 
628  void registerComponent();
629 
636 
643  virtual std::vector<Widgets::Variable::Info> getComponentParametersInfo();
644 
645 protected:
648  QMainWindow* getQMainWindow();
650 
651 private:
652  // owned pointers
653  std::unique_ptr<Widgets::Component> plugin_component;
654 
655  // not owned pointers (managed by external objects)
656  Event::Manager* event_manager = nullptr;
657  QMainWindow* main_window = nullptr; // Qt handles this lifetime
658  Widgets::Panel* widget_panel = nullptr; // Qt handles this lifetime
659 
660  std::string library;
661  std::string name;
662 };
663 
664 } // namespace Widgets
665 
666 #endif
void postEvent(Object *event)
Definition: event.cpp:299
Definition: io.hpp:79
std::string getDescription(const size_t &var_id)
Definition: widgets.cpp:120
std::vector< Widgets::Variable::Info > getParametersInfo()
Definition: widgets.hpp:257
RT::State::state_t getState() const
Definition: widgets.hpp:242
void setState(RT::State::state_t state)
Definition: widgets.hpp:249
Component(Widgets::Plugin *hplugin, const std::string &mod_name, const std::vector< IO::channel_t > &channels, const std::vector< Widgets::Variable::Info > &variables)
Definition: widgets.cpp:98
Widgets::Plugin * getHostPlugin()
Definition: widgets.hpp:267
void setValue(const size_t &var_id, T value)
Definition: widgets.hpp:212
std::string getValueString(const size_t &var_id)
Definition: widgets.cpp:125
T getValue(const size_t &var_id)
Definition: widgets.hpp:200
DefaultGUILineEdit(QWidget *parent)
Definition: widgets.cpp:75
QMainWindow * getQMainWindowPtr()
Definition: widgets.hpp:436
void setHostPlugin(Widgets::Plugin *hplugin)
Definition: widgets.hpp:310
void signal_state_change(RT::State::state_t state)
Panel(const std::string &mod_name, QMainWindow *mw, Event::Manager *ev_manager)
Definition: widgets.cpp:155
virtual void createGUI(const std::vector< Widgets::Variable::Info > &vars, const std::vector< Widgets::Variable::Id > &skip_ids)
Definition: widgets.cpp:185
QString getParameter(const QString &var_name)
virtual void refresh()
Definition: widgets.cpp:296
void setComment(const QString &var_name, const QString &comment)
Definition: widgets.cpp:386
Event::Manager * getRTXIEventManager()
Definition: widgets.hpp:443
virtual void pause(bool p)
Definition: widgets.cpp:440
QMdiSubWindow * getMdiWindow()
Definition: widgets.hpp:286
Widgets::Plugin * getHostPlugin()
Definition: widgets.hpp:429
void closeEvent(QCloseEvent *event) override
Definition: widgets.cpp:179
virtual void exit()
Definition: widgets.cpp:286
virtual void update_state(RT::State::state_t flag)
Definition: widgets.cpp:275
void resizeMe()
Definition: widgets.cpp:281
std::string getName()
Definition: widgets.hpp:422
void setParameter(const QString &var_name, double value)
Definition: widgets.cpp:398
QString getComment(const QString &name)
virtual void modify()
Definition: widgets.cpp:340
int64_t getComponentIntParameter(const Variable::Id &parameter_id)
Definition: widgets.cpp:525
bool getActive()
Definition: widgets.cpp:557
virtual std::vector< Widgets::Variable::Info > getComponentParametersInfo()
Definition: widgets.cpp:499
std::string getName() const
Definition: widgets.hpp:586
void setLibrary(const std::string &lib)
Definition: widgets.hpp:623
double getComponentDoubleParameter(const Variable::Id &parameter_id)
Definition: widgets.cpp:537
Plugin(Plugin &&)=delete
~Plugin() override
Definition: widgets.cpp:463
void attachComponent(std::unique_ptr< Widgets::Component > component)
Definition: widgets.cpp:504
void setComponentState(RT::State::state_t state)
Definition: widgets.cpp:485
Plugin & operator=(Plugin &&)=delete
QMainWindow * getQMainWindow()
Plugin(Event::Manager *ev_manager, std::string mod_name)
Definition: widgets.cpp:457
std::string getLibrary() const
Definition: widgets.hpp:616
int setComponentParameter(const Variable::Id &parameter_id, T value)
Definition: widgets.hpp:554
void receiveEvent(Event::Object *event) override
Definition: widgets.cpp:543
uint64_t getComponentUIntParameter(const Variable::Id &parameter_id)
Definition: widgets.cpp:531
Widgets::Panel * getPanel()
Definition: widgets.cpp:594
void attachPanel(Widgets::Panel *panel)
Definition: widgets.cpp:519
Plugin & operator=(const Plugin &plugin)=delete
int setActive(bool state)
Definition: widgets.cpp:567
Plugin(const Plugin &plugin)=delete
Event::Manager * getEventManager()
Definition: widgets.cpp:589
Widgets::Component * getComponent()
Definition: widgets.cpp:584
void registerComponent()
Definition: widgets.cpp:474
void ERROR_MSG(const std::string &errmsg, Args... args)
Definition: debug.hpp:36
@ RT_WIDGET_PARAMETER_CHANGE_EVENT
Definition: event.hpp:68
flags_t
Definition: io.hpp:52
state_t
Definition: rt.hpp:53
@ INIT
Definition: rt.hpp:54
std::string vartype2string(variable_t type)
Definition: widgets.cpp:51
constexpr Id INVALID_ID
Definition: widgets.hpp:48
std::string state2string(RT::State::state_t state)
Definition: widgets.cpp:22
Definition: rt.hpp:35
std::unique_ptr< Widgets::Plugin >(* createPlugin)(Event::Manager *)
Function that returns a smart pointer to plugin object.
Definition: widgets.hpp:145
std::unique_ptr< Widgets::Component >(* createComponent)(Widgets::Plugin *)
Definition: widgets.hpp:156
std::string description
Definition: widgets.hpp:99
std::variant< int64_t, double, uint64_t, std::string, RT::State::state_t > value
Definition: widgets.hpp:102
Widgets::Variable::variable_t vartype
Definition: widgets.hpp:100