gui_utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <QLayout>
4 #include <QLayoutItem>
5 #include <QSpinBox>
6 #include <QWidget>
7 
8 class QLayout;
9 class QSplitter;
10 class QTreeWidgetItem;
11 
12 
13 namespace armarx::gui
14 {
15  /**
16  * @brief Clear a layout.
17  *
18  * Recursively take and delete all items in `layout`.
19  *
20  * @param layout The layout.
21  */
22  void clearLayout(QLayout* layout);
23 
24  /**
25  * @brief Clear a tree widget item
26  *
27  * Take and delete all children of `item`.
28  *
29  * @param item The tree widget item.
30  */
31  void clearItem(QTreeWidgetItem* item);
32 
33 
34 
35  template <class WidgetT>
36  void replaceWidget(WidgetT*& old, QWidget* neu, QLayout* parentLayout)
37  {
38  QLayoutItem* oldItem = parentLayout->replaceWidget(old, neu);
39  if (oldItem)
40  {
41  delete oldItem;
42  delete old;
43  old = nullptr;
44  }
45  }
46 
47  /**
48  * @brief Let items in `layout` be children of a splitter.
49  *
50  * Items in `layout` are moved to a new `QSplitter`, which will
51  * be the only child of `layout`.
52  * If `layout` is a Q{H,V}BoxLayout, the respective orientation
53  * will be adopted.
54  *
55  * @param The parent layout.
56  * @return The splitter item.
57  */
58  QSplitter* useSplitter(QLayout* layout);
59 
60  // Source: https://stackoverflow.com/a/26538572
61  class LeadingZeroSpinBox : public QSpinBox
62  {
63  using QSpinBox::QSpinBox;
64 
65  public:
66  LeadingZeroSpinBox(int numDigits, int base);
67 
68  QString textFromValue(int value) const override;
69 
70  private:
71  int numDigits;
72  int base;
73  };
74 }
armarx::gui::replaceWidget
void replaceWidget(WidgetT *&old, QWidget *neu, QLayout *parentLayout)
Definition: gui_utils.h:36
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
armarx::gui::LeadingZeroSpinBox
Definition: gui_utils.h:61
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