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