Salome HOME
#1659 New widget for supporting optional inputs
[modules/shaper.git] / src / ModuleBase / ModuleBase_PageBase.cpp
1 /*
2  * ModuleBase_PageBase.cpp
3  *
4  *  Created on: Mar 4, 2015
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_PageBase.h>
9 #include <ModuleBase_ModelWidget.h>
10
11 #include <QLayout>
12
13 class QWidget;
14
15 ModuleBase_PageBase::ModuleBase_PageBase()
16 {
17
18 }
19
20 ModuleBase_PageBase::~ModuleBase_PageBase()
21 {
22
23 }
24
25 QWidget* ModuleBase_PageBase::pageWidget()
26 {
27   return dynamic_cast<QWidget*>(this);
28 }
29
30 void ModuleBase_PageBase::addModelWidget(ModuleBase_ModelWidget* theWidget)
31 {
32   placeModelWidget(theWidget);
33   myWidgetList.append(theWidget);
34 }
35
36 void ModuleBase_PageBase::addPageWidget(ModuleBase_PageBase* thePage)
37 {
38   placePageWidget(thePage);
39 }
40
41 void ModuleBase_PageBase::addWidget(QWidget* theWidget)
42 {
43   placeWidget(theWidget);
44 }
45
46 void ModuleBase_PageBase::clearPage()
47 {
48   myWidgetList.clear();
49
50   QLayoutItem *aChild;
51   while ((aChild = pageLayout()->takeAt(0)) != 0) {
52     if(aChild->widget()) {
53       delete aChild->widget();
54     } else {
55       delete aChild;
56     }
57   }
58
59   // Issue #460: this patch is necessary since the row stretch information
60   // is stored independently on the items: when the items are removed
61   // from the layout the stretch information is kept, so in the next 
62   // filling of the layout some "additional" (old) rows may be stretched
63   // without necessity. 
64   // In this patch we clear the stretch information specifying the default value: 0.
65   QGridLayout* aLayout = dynamic_cast<QGridLayout*>( pageLayout() );
66   if( aLayout )
67   {
68     int r = aLayout->rowCount();
69     for( int i=0; i<r; i++ )
70       aLayout->setRowStretch( i, 0 );
71   }
72 }
73
74
75 bool ModuleBase_PageBase::takeFocus()
76 {
77   if(myWidgetList.isEmpty())
78     return false;
79
80   return myWidgetList.first()->focusTo();
81 }
82
83 QList<ModuleBase_ModelWidget*> ModuleBase_PageBase::modelWidgets() const
84 {
85   return myWidgetList;
86 }
87
88 void ModuleBase_PageBase::alignToTop()
89 {
90   bool hasExpanding = false;
91   QList<QWidget *> aListToCheck;
92   ModuleBase_ModelWidget* aModelWidget;
93   foreach(aModelWidget, myWidgetList) {
94     aListToCheck << aModelWidget->getControls();
95   }
96   foreach(QWidget* eachWidget, aListToCheck) {
97     QSizePolicy::Policy aVPolicy = eachWidget->sizePolicy().verticalPolicy();
98     if(aVPolicy & QSizePolicy::ExpandFlag) {
99       hasExpanding = true;
100       break;
101     }
102   }
103   if(!hasExpanding) {
104     addPageStretch();
105   }
106 }
107
108 void ModuleBase_PageBase::placePageWidget(ModuleBase_PageBase* theWidget)
109 {
110   QWidget* aWidget = dynamic_cast<QWidget*>(theWidget);
111   placeWidget(aWidget);
112 }