gui_utils.cpp
Go to the documentation of this file.
1 #include "gui_utils.h"
2 
3 #include <QLayout>
4 #include <QLayoutItem>
5 #include <QSplitter>
6 #include <QTreeWidgetItem>
7 #include <QWidget>
8 
10 
11 void
12 armarx::gui::clearLayout(QLayout* layout)
13 {
14  // Source: https://stackoverflow.com/a/4857631
15 
16  ARMARX_CHECK(layout);
17 
18  QLayoutItem* item;
19  while ((item = layout->takeAt(0)))
20  {
21  if (item->layout())
22  {
23  clearLayout(item->layout());
24  delete item->layout();
25  }
26  if (item->widget())
27  {
28  delete item->widget();
29  }
30  delete item;
31  }
32 }
33 
34 void
35 armarx::gui::clearItem(QTreeWidgetItem* item)
36 {
37  while (item->childCount() > 0)
38  {
39  delete item->takeChild(0);
40  }
41 }
42 
43 QSplitter*
44 armarx::gui::useSplitter(QLayout* layout)
45 {
46  ARMARX_CHECK(layout);
47 
48  // Check all items
49  for (int i = 0; i < layout->count(); ++i)
50  {
51  ARMARX_CHECK_NOT_NULL(layout->itemAt(i)->widget())
52  << "QSplitter only supports widgets, but layout item #" << i << " is not a widget.";
53  }
54 
55  QSplitter* splitter;
56  if (dynamic_cast<QHBoxLayout*>(layout))
57  {
58  splitter = new QSplitter(Qt::Orientation::Horizontal);
59  }
60  else if (dynamic_cast<QVBoxLayout*>(layout))
61  {
62  splitter = new QSplitter(Qt::Orientation::Vertical);
63  }
64  else
65  {
66  splitter = new QSplitter();
67  }
68 
69  while (layout->count() > 0)
70  {
71  const int index = 0;
72  if (layout->itemAt(index))
73  {
74  QLayoutItem* item = layout->takeAt(index);
75 
76  ARMARX_CHECK(item->widget());
77  splitter->addWidget(item->widget());
78 
79  delete item;
80  }
81  }
82  ARMARX_CHECK_EQUAL(layout->count(), 0);
83 
84  layout->addWidget(splitter);
85  ARMARX_CHECK_EQUAL(layout->count(), 1);
86  ARMARX_CHECK_EQUAL(layout->itemAt(0)->widget(), splitter);
87 
88  return splitter;
89 }
90 
92  numDigits(numDigits), base(base)
93 {
94 }
95 
96 QString
98 {
99  return QString("%1").arg(value, numDigits, base, QChar('0'));
100 }
101 
102 void
104 {
105  for (int i = 0; i < splitter->count(); ++i)
106  {
107  splitter->widget(i)->deleteLater();
108  }
109 }
armarx::splitter
std::vector< std::string > splitter(std::string const &propertyValue)
Definition: ResultImageFuser.h:44
index
uint8_t index
Definition: EtherCATFrame.h:59
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
clearLayout
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Definition: SimoxCSpaceVisualizerWidgetController.cpp:59
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_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::gui::LeadingZeroSpinBox::LeadingZeroSpinBox
LeadingZeroSpinBox(int numDigits, int base)
Definition: gui_utils.cpp:90
gui_utils.h
armarx::gui::useSplitter
QSplitter * useSplitter(QLayout *layout)
Let items in layout be children of a splitter.
Definition: gui_utils.cpp:43