Salome HOME
refs #568: use ordered list view with selection synchronized with object browser...
[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     myNext->setVisible( true );
73     myFinish->setVisible( false );
74   }
75   return myWizard->addWidget( thePage );
76 }
77
78 void HYDROGUI_Wizard::onNext()
79 {
80   if ( !acceptCurrent() )
81     return;
82
83   if ( myWizard->count() <= 0 )
84     return;
85
86   myBack->setEnabled( true );
87   int aCurIdx = myWizard->currentIndex();
88   if ( aCurIdx == ( myWizard->count() - 2 ) )
89   {
90     // Go to the last page
91     myNext->setVisible( false );
92     myFinish->setVisible( true );
93   }
94   myWizard->setCurrentIndex( aCurIdx + 1 );
95
96   emit Next( myWizard->currentIndex() );
97 }
98
99 void HYDROGUI_Wizard::onBack()
100 {
101   if ( myWizard->count() <= 0 )
102     return;
103
104   if ( myWizard->count() > 1 )
105   {
106     myNext->setVisible( true );
107     myFinish->setVisible( false );
108   }
109   else
110   {
111     // Wizard has a single page - show finish
112     myNext->setVisible( false );
113     myFinish->setVisible( true );
114   }
115
116   int aCurIdx = myWizard->currentIndex();
117   myWizard->setCurrentIndex( aCurIdx - 1 );
118
119   aCurIdx = myWizard->currentIndex();
120   if ( aCurIdx <= 0 )
121   {
122     // Disable back if go to the first page
123     myBack->setEnabled( false );
124   }
125
126   emit Back( aCurIdx );
127 }
128
129 void HYDROGUI_Wizard::onFirstPage()
130 {
131   if ( myWizard->count() > 1 ) {
132     myNext->setVisible( true );
133     myFinish->setVisible( false );
134     myWizard->setCurrentIndex( 0 );
135     myBack->setEnabled( false );
136     emit Back( myWizard->currentIndex() );
137   }
138 }
139
140 bool HYDROGUI_Wizard::acceptCurrent() const
141 {
142   return true;
143 }
144
145 QAbstractButton* HYDROGUI_Wizard::button( QWizard::WizardButton theBtnId ) const
146 {
147   QAbstractButton* aRes = 0;
148   switch( theBtnId )
149   {
150     case QWizard::BackButton:
151       aRes = myBack;
152       break;
153     case QWizard::NextButton:
154       aRes = myNext;
155       break;
156     case QWizard::FinishButton:
157       aRes = myFinish;
158       break;
159     case QWizard::CancelButton:
160       aRes = myCancel;
161       break;
162     case QWizard::HelpButton:
163       aRes = myHelp;
164   }
165   return aRes;
166 }