BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
LayoutUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Tool/LayoutUtils.cpp
6 //! @brief Implements LayoutUtils namespace
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <QBoxLayout>
17 #include <QGridLayout>
18 #include <QLayoutItem>
19 #include <QWidget>
20 
21 //! Solution was taken from
22 //! https://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout
23 //!
24 //! Important: according to explanations given, grid layouts can only grow and never shrink.
25 
26 namespace {
27 
28 /**
29  * Helper function. Deletes all child widgets of the given layout item.
30  */
31 
32 void deleteChildWidgets(QLayoutItem* item)
33 {
34  if (item->layout()) {
35  // Process all child items recursively.
36  for (int i = 0; i < item->layout()->count(); i++)
37  deleteChildWidgets(item->layout()->itemAt(i));
38  }
39  delete item->widget();
40 }
41 
42 /**
43  * Helper function. Removes all layout items within the given layout
44  * which either span the given row or column. If deleteWidgets
45  * is true, all concerned child widgets become not only removed from the
46  * layout, but also deleted.
47  */
48 
49 void remove(QGridLayout* layout, int row, int column, bool deleteWidgets)
50 {
51  // We avoid usage of QGridLayout::itemAtPosition() here to improve performance.
52  for (int i = layout->count() - 1; i >= 0; i--) {
53  int r, c, rs, cs;
54  layout->getItemPosition(i, &r, &c, &rs, &cs);
55  if ((r <= row && r + rs - 1 >= row) || (c <= column && c + cs - 1 >= column)) {
56  // This layout item is subject to deletion.
57  QLayoutItem* item = layout->takeAt(i);
58  if (deleteWidgets)
59  deleteChildWidgets(item);
60  delete item;
61  }
62  }
63 }
64 
65 } // namespace
66 
67 
68 void GUI::Util::Layout::clearLayout(QLayout* layout, bool deleteWidgets)
69 {
70  if (!layout)
71  return;
72 
73  while (layout->count() > 0) {
74  QLayoutItem* item = layout->takeAt(0);
75  if (deleteWidgets)
76  delete item->widget();
77  if (QLayout* childLayout = item->layout())
78  GUI::Util::Layout::clearLayout(childLayout, deleteWidgets);
79  delete item;
80  }
81 }
82 
83 /**
84  * Removes all layout items on the given row from the given grid
85  * layout. If deleteWidgets is true, all concerned child widgets
86  * become not only removed from the layout, but also deleted. Note that
87  * this function doesn't actually remove the row itself from the grid
88  * layout, as this isn't possible (i.e. the rowCount() and row indices
89  * will stay the same after this function has been called).
90  */
91 
92 void GUI::Util::Layout::removeRow(QGridLayout* layout, int row, bool deleteWidgets)
93 {
94  remove(layout, row, -1, deleteWidgets);
95  layout->setRowMinimumHeight(row, 0);
96  layout->setRowStretch(row, 0);
97 }
98 
99 /**
100  * Removes all layout items on the given column from the given grid
101  * layout. If deleteWidgets is true, all concerned child widgets
102  * become not only removed from the layout, but also deleted. Note that
103  * this function doesn't actually remove the column itself from the grid
104  * layout, as this isn't possible (i.e. the columnCount() and column
105  * indices will stay the same after this function has been called).
106  */
107 
108 void GUI::Util::Layout::removeColumn(QGridLayout* layout, int column, bool deleteWidgets)
109 {
110  remove(layout, -1, column, deleteWidgets);
111  layout->setColumnMinimumWidth(column, 0);
112  layout->setColumnStretch(column, 0);
113 }
114 
115 void GUI::Util::Layout::clearGridLayout(QGridLayout* layout, bool deleteWidgets)
116 {
117  for (int i_row = 0; i_row < layout->rowCount(); ++i_row)
118  GUI::Util::Layout::removeRow(layout, i_row, deleteWidgets);
119 }
120 
122 {
123  auto* result = new QWidget;
124  result->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
125  return result;
126 }
Defines namespace GUI::Util::Layout.
void removeColumn(QGridLayout *layout, int column, bool deleteWidgets=true)
Removes column from grid layout.
void removeRow(QGridLayout *layout, int row, bool deleteWidgets=true)
Removes row from grid layout (important: doesn't change row count).
Definition: LayoutUtils.cpp:92
QWidget * placeHolder()
Returns empty widget to occupy place in layout.
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Removes content from box layout.
Definition: LayoutUtils.cpp:68
void clearGridLayout(QGridLayout *layout, bool deleteWidgets=true)
Clear layout completely.