gui_utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <mutex>
5 
6 #include <QLayout>
7 #include <QLayoutItem>
8 #include <QSpinBox>
9 #include <QSplitter>
10 #include <QTreeWidgetItem>
11 #include <QWidget>
12 
14 
15 namespace armarx::gui
16 {
17  /**
18  * @brief Clear a layout.
19  *
20  * Recursively take and delete all items in `layout`.
21  *
22  * @param layout The layout.
23  */
24  void clearLayout(QLayout* layout);
25 
26  void clearSplitter(QSplitter* splitter);
27 
28  /**
29  * @brief Clear a tree widget item
30  *
31  * Take and delete all children of `item`.
32  *
33  * @param item The tree widget item.
34  */
35  void clearItem(QTreeWidgetItem* item);
36 
37  template <class WidgetT>
38  void
39  replaceWidget(WidgetT*& old, QWidget* neu, QLayout* parentLayout)
40  {
41  ARMARX_CHECK(old);
42  ARMARX_CHECK(neu);
43  ARMARX_CHECK(parentLayout);
44  QLayoutItem* oldItem = parentLayout->replaceWidget(old, neu);
45  if (oldItem)
46  {
47  delete oldItem;
48  delete old;
49  old = nullptr;
50  }
51  }
52 
53  template <class WidgetT>
54  void
55  replaceWidget(WidgetT*& old, QWidget* neu, QSplitter*& parentLayout)
56  {
57  ARMARX_CHECK(old);
58  ARMARX_CHECK(neu);
59  ARMARX_CHECK(parentLayout);
60  int index = parentLayout->indexOf(old);
61  ARMARX_CHECK(index >= 0) << "Could not find widget in splitter";
62  QWidget* oldItem = parentLayout->replaceWidget(index, neu);
63  if (oldItem)
64  {
65  // deleting the widget(s) directly will segfault for some reason
66  oldItem->deleteLater();
67  old->deleteLater();
68  old = nullptr;
69  }
70  }
71 
72  /**
73  * @brief Let items in `layout` be children of a splitter.
74  *
75  * Items in `layout` are moved to a new `QSplitter`, which will
76  * be the only child of `layout`.
77  * If `layout` is a Q{H,V}BoxLayout, the respective orientation
78  * will be adopted.
79  *
80  * @param The parent layout.
81  * @return The splitter item.
82  */
83  QSplitter* useSplitter(QLayout* layout);
84 
85  // Source: https://stackoverflow.com/a/26538572
86  class LeadingZeroSpinBox : public QSpinBox
87  {
88  using QSpinBox::QSpinBox;
89 
90  public:
91  LeadingZeroSpinBox(int numDigits, int base);
92 
93  QString textFromValue(int value) const override;
94 
95  private:
96  int numDigits;
97  int base;
98  };
99 } // namespace armarx::gui
armarx::splitter
std::vector< std::string > splitter(std::string const &propertyValue)
Definition: ResultImageFuser.h:44
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::gui::replaceWidget
void replaceWidget(WidgetT *&old, QWidget *neu, QLayout *parentLayout)
Definition: gui_utils.h:34
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::gui::clearItem
void clearItem(QTreeWidgetItem *item)
Clear a tree widget item.
Definition: gui_utils.cpp:35
armarx::gui::clearLayout
void clearLayout(QLayout *layout)
Clear a layout.
Definition: gui_utils.cpp:12
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::gui::LeadingZeroSpinBox::textFromValue
QString textFromValue(int value) const override
Definition: gui_utils.cpp:97
ExpressionException.h
armarx::gui::clearSplitter
void clearSplitter(QSplitter *splitter)
Definition: gui_utils.cpp:103
armarx::gui
Definition: gui_utils.h:12
armarx::gui::LeadingZeroSpinBox::LeadingZeroSpinBox
LeadingZeroSpinBox(int numDigits, int base)
Definition: gui_utils.cpp:91
armarx::gui::useSplitter
QSplitter * useSplitter(QLayout *layout)
Let items in layout be children of a splitter.
Definition: gui_utils.cpp:44