Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into HEAD
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Wizard.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_Wizard.h"
20 #include "HYDROGUI_Module.h"
21 #include <CAM_Application.h>
22 #include <SUIT_Desktop.h>
23 #include <QFrame>
24 #include <QVBoxLayout>
25 #include <QWidget>
26 #include <QPushButton>
27 #include <QAbstractButton>
28 #include <QStackedWidget>
29 #include <QWizardPage>
30
31 HYDROGUI_Wizard::HYDROGUI_Wizard( HYDROGUI_Module* theModule, const QString& theTitle )
32 : HYDROGUI_InputPanel( theModule, theTitle, false )
33 {
34
35   myWizard = new QStackedWidget( theModule->application()->desktop() );
36
37   myNext = new QPushButton( tr( "NEXT" ), buttonFrame() );
38   myBack = new QPushButton( tr( "BACK" ), buttonFrame() );
39   myFinish = new QPushButton( tr( "FINISH" ), buttonFrame() );
40
41   QHBoxLayout* aBtnsLayout = qobject_cast<QHBoxLayout*>(buttonFrame()->layout());
42   aBtnsLayout->addWidget( myBack, 0 );
43   aBtnsLayout->addWidget( myNext, 0 );
44   aBtnsLayout->addWidget( myFinish, 0 );
45   aBtnsLayout->addWidget( myCancel, 0 );
46   aBtnsLayout->addStretch( 1 );
47   aBtnsLayout->addWidget( myHelp, 0 );
48
49   myBack->setEnabled( false );
50   myNext->setVisible( false );
51
52   connect( myNext,  SIGNAL( clicked() ), SLOT( onNext() ) );
53   connect( myBack,  SIGNAL( clicked() ), SLOT( onBack() ) );
54   connect( myFinish, SIGNAL( clicked() ), SLOT( onApply() ) );
55
56   addWidget( myWizard );
57 }
58
59 HYDROGUI_Wizard::~HYDROGUI_Wizard()
60 {
61 }
62
63 QStackedWidget* HYDROGUI_Wizard::wizard() const
64 {
65   return myWizard;
66 }
67
68 int HYDROGUI_Wizard::addPage( QWizardPage* thePage )
69 {
70   if ( myWizard->count() > 0 )
71   {
72     SetButtonsState(false);
73     /*myNext->setVisible( true );
74     myFinish->setVisible( false );*/
75   }
76   return myWizard->addWidget( thePage );
77 }
78
79 void HYDROGUI_Wizard::onNext()
80 {
81   if ( !acceptCurrent() )
82     return;
83
84   if ( myWizard->count() <= 0 )
85     return;
86
87   myBack->setEnabled( true );
88   int aCurIdx = myWizard->currentIndex();
89   if ( IsLastPage() )
90   {
91     // Go to the last page
92     SetButtonsState( true );
93     /*myNext->setVisible( false );
94     myFinish->setVisible( true );*/
95   }
96   myWizard->setCurrentIndex( aCurIdx + 1 );
97
98   emit Next( myWizard->currentIndex() );
99 }
100
101 void HYDROGUI_Wizard::onBack()
102 {
103   if ( myWizard->count() <= 0 )
104     return;
105
106   if ( myWizard->count() > 1 )
107   {
108     /*myNext->setVisible( true );
109     myFinish->setVisible( false );*/
110     SetButtonsState( false );
111   }
112   else
113   {
114     // Wizard has a single page - show finish
115     SetButtonsState( true );
116     /*myNext->setVisible( false );
117     myFinish->setVisible( true );*/
118   }
119
120   int aCurIdx = myWizard->currentIndex();
121   myWizard->setCurrentIndex( aCurIdx - 1 );
122
123   aCurIdx = myWizard->currentIndex();
124   if ( aCurIdx <= 0 )
125   {
126     // Disable back if go to the first page
127     myBack->setEnabled( false );
128   }
129
130   emit Back( aCurIdx );
131 }
132
133 void HYDROGUI_Wizard::onFirstPage()
134 {
135   if ( myWizard->count() > 1 ) {
136     /*myNext->setVisible( true );
137     myFinish->setVisible( false );*/
138     SetButtonsState( false );
139     myWizard->setCurrentIndex( 0 );
140     myBack->setEnabled( false );
141     emit Back( myWizard->currentIndex() );
142   }
143 }
144
145 bool HYDROGUI_Wizard::acceptCurrent() const
146 {
147   return true;
148 }
149
150 QAbstractButton* HYDROGUI_Wizard::button( QWizard::WizardButton theBtnId ) const
151 {
152   QAbstractButton* aRes = 0;
153   switch( theBtnId )
154   {
155     case QWizard::BackButton:
156       aRes = myBack;
157       break;
158     case QWizard::NextButton:
159       aRes = myNext;
160       break;
161     case QWizard::FinishButton:
162       aRes = myFinish;
163       break;
164     case QWizard::CancelButton:
165       aRes = myCancel;
166       break;
167     case QWizard::HelpButton:
168       aRes = myHelp;
169   }
170   return aRes;
171 }
172
173 bool HYDROGUI_Wizard::IsLastPage()
174 {
175   if ( myWizard->currentIndex() == ( myWizard->count() - 2 ))
176     return true;
177   else
178     return false;
179 }
180
181 void HYDROGUI_Wizard::SetButtonsState (bool IsLastPage)
182 {
183   if (!IsLastPage)
184   {
185     myNext->setVisible( true );
186     myFinish->setVisible( false );
187   }
188   else
189   {
190     myNext->setVisible( false );
191     myFinish->setVisible( true );
192   }
193 }