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 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/view/mvvm/widgets/layoututils.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, 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 namespace {
22 void remove(QGridLayout* layout, int row, int column, bool deleteWidgets);
23 void deleteChildWidgets(QLayoutItem* item);
24 } // namespace
25 
26 void LayoutUtils::clearLayout(QLayout* layout, bool deleteWidgets)
27 {
28  if (!layout)
29  return;
30 
31  while (QLayoutItem* item = layout->takeAt(0)) {
32  if (deleteWidgets)
33  delete item->widget();
34  if (QLayout* childLayout = item->layout())
35  LayoutUtils::clearLayout(childLayout, deleteWidgets);
36  delete item;
37  }
38 }
39 
40 /**
41  * Removes all layout items on the given row from the given grid
42  * layout. If deleteWidgets is true, all concerned child widgets
43  * become not only removed from the layout, but also deleted. Note that
44  * this function doesn't actually remove the row itself from the grid
45  * layout, as this isn't possible (i.e. the rowCount() and row indices
46  * will stay the same after this function has been called).
47  */
48 
49 void LayoutUtils::removeRow(QGridLayout* layout, int row, bool deleteWidgets)
50 {
51  remove(layout, row, -1, deleteWidgets);
52  layout->setRowMinimumHeight(row, 0);
53  layout->setRowStretch(row, 0);
54 }
55 
56 /**
57  * Removes all layout items on the given column from the given grid
58  * layout. If deleteWidgets is true, all concerned child widgets
59  * become not only removed from the layout, but also deleted. Note that
60  * this function doesn't actually remove the column itself from the grid
61  * layout, as this isn't possible (i.e. the columnCount() and column
62  * indices will stay the same after this function has been called).
63  */
64 
65 void LayoutUtils::removeColumn(QGridLayout* layout, int column, bool deleteWidgets)
66 {
67  remove(layout, -1, column, deleteWidgets);
68  layout->setColumnMinimumWidth(column, 0);
69  layout->setColumnStretch(column, 0);
70 }
71 
72 void LayoutUtils::clearGridLayout(QGridLayout* layout, bool deleteWidgets)
73 {
74  for (int i_row = 0; i_row < layout->rowCount(); ++i_row) {
75  LayoutUtils::removeRow(layout, i_row, deleteWidgets);
76  }
77 }
78 
79 namespace {
80 
81 /**
82  * Helper function. Removes all layout items within the given layout
83  * which either span the given row or column. If deleteWidgets
84  * is true, all concerned child widgets become not only removed from the
85  * layout, but also deleted.
86  */
87 
88 void remove(QGridLayout* layout, int row, int column, bool deleteWidgets)
89 {
90  // We avoid usage of QGridLayout::itemAtPosition() here to improve performance.
91  for (int i = layout->count() - 1; i >= 0; i--) {
92  int r, c, rs, cs;
93  layout->getItemPosition(i, &r, &c, &rs, &cs);
94  if ((r <= row && r + rs - 1 >= row) || (c <= column && c + cs - 1 >= column)) {
95  // This layout item is subject to deletion.
96  QLayoutItem* item = layout->takeAt(i);
97  if (deleteWidgets)
98  deleteChildWidgets(item);
99  delete item;
100  }
101  }
102 }
103 
104 /**
105  * Helper function. Deletes all child widgets of the given layout item.
106  */
107 
108 void deleteChildWidgets(QLayoutItem* item)
109 {
110  if (item->layout()) {
111  // Process all child items recursively.
112  for (int i = 0; i < item->layout()->count(); i++)
113  deleteChildWidgets(item->layout()->itemAt(i));
114  }
115  item->widget()->deleteLater();
116 }
117 
118 } // namespace
119 
120 QWidget* LayoutUtils::placeHolder()
121 {
122  auto result = new QWidget;
123  result->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
124  return result;
125 }
Defines class CLASS?
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