BornAgain  1.19.0
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/coregui/utils/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 void LayoutUtils::clearLayout(QLayout* layout, bool deleteWidgets)
22 {
23  if (!layout)
24  return;
25 
26  while (QLayoutItem* item = layout->takeAt(0)) {
27  if (deleteWidgets) {
28  if (QWidget* widget = item->widget())
29  delete widget;
30  }
31  if (QLayout* childLayout = item->layout())
32  LayoutUtils::clearLayout(childLayout, deleteWidgets);
33  delete item;
34  }
35 }
36 
37 //!
38 //! Solution was taken from
39 //! https://stackoverflow.com/questions/5395266/removing-widgets-from-qgridlayout
40 //!
41 //! Important: according to explanations given, grid layouts can only grow and never shrink.
42 //!
43 
44 namespace {
45 void remove(QGridLayout* layout, int row, int column, bool deleteWidgets);
46 void deleteChildWidgets(QLayoutItem* item);
47 } // namespace
48 
49 /**
50  * Removes all layout items on the given row from the given grid
51  * layout. If deleteWidgets is true, all concerned child widgets
52  * become not only removed from the layout, but also deleted. Note that
53  * this function doesn't actually remove the row itself from the grid
54  * layout, as this isn't possible (i.e. the rowCount() and row indices
55  * will stay the same after this function has been called).
56  */
57 
58 void LayoutUtils::removeRow(QGridLayout* layout, int row, bool deleteWidgets)
59 {
60  remove(layout, row, -1, deleteWidgets);
61  layout->setRowMinimumHeight(row, 0);
62  layout->setRowStretch(row, 0);
63 }
64 
65 /**
66  * Removes all layout items on the given column from the given grid
67  * layout. If deleteWidgets is true, all concerned child widgets
68  * become not only removed from the layout, but also deleted. Note that
69  * this function doesn't actually remove the column itself from the grid
70  * layout, as this isn't possible (i.e. the columnCount() and column
71  * indices will stay the same after this function has been called).
72  */
73 
74 void LayoutUtils::removeColumn(QGridLayout* layout, int column, bool deleteWidgets)
75 {
76  remove(layout, -1, column, deleteWidgets);
77  layout->setColumnMinimumWidth(column, 0);
78  layout->setColumnStretch(column, 0);
79 }
80 
81 void LayoutUtils::clearGridLayout(QGridLayout* layout, bool deleteWidgets)
82 {
83  for (int i_row = 0; i_row < layout->rowCount(); ++i_row) {
84  LayoutUtils::removeRow(layout, i_row, deleteWidgets);
85  }
86 }
87 
88 namespace {
89 
90 /**
91  * Helper function. Removes all layout items within the given layout
92  * which either span the given row or column. If deleteWidgets
93  * is true, all concerned child widgets become not only removed from the
94  * layout, but also deleted.
95  */
96 
97 void remove(QGridLayout* layout, int row, int column, bool deleteWidgets)
98 {
99  // We avoid usage of QGridLayout::itemAtPosition() here to improve performance.
100  for (int i = layout->count() - 1; i >= 0; i--) {
101  int r, c, rs, cs;
102  layout->getItemPosition(i, &r, &c, &rs, &cs);
103  if ((r <= row && r + rs - 1 >= row) || (c <= column && c + cs - 1 >= column)) {
104  // This layout item is subject to deletion.
105  QLayoutItem* item = layout->takeAt(i);
106  if (deleteWidgets) {
107  deleteChildWidgets(item);
108  }
109  delete item;
110  }
111  }
112 }
113 
114 /**
115  * Helper function. Deletes all child widgets of the given layout item.
116  */
117 
118 void deleteChildWidgets(QLayoutItem* item)
119 {
120  if (item->layout()) {
121  // Process all child items recursively.
122  for (int i = 0; i < item->layout()->count(); i++) {
123  deleteChildWidgets(item->layout()->itemAt(i));
124  }
125  }
126  delete item->widget();
127 }
128 
129 } // namespace
130 
132 {
133  auto result = new QWidget;
134  result->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
135  return result;
136 }
Defines LayoutUtils namespace.
void removeRow(QGridLayout *layout, int row, bool deleteWidgets=true)
Removes row from grid layout (important: doesn't change row count).
Definition: LayoutUtils.cpp:58
void removeColumn(QGridLayout *layout, int column, bool deleteWidgets=true)
Removes column from grid layout.
Definition: LayoutUtils.cpp:74
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Removes content from box layout.
Definition: LayoutUtils.cpp:21
QWidget * placeHolder()
Returns empty widget to occupy place in layout.
void clearGridLayout(QGridLayout *layout, bool deleteWidgets=true)
Clear layout completely.
Definition: LayoutUtils.cpp:81
MVVM_MODEL_EXPORT bool remove(const std::string &path)
Removes file or empty directory.
Definition: fileutils.cpp:57