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