Salome HOME
refs #568: Land Cover: a draft of data model and implementation of dialog box and...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TwoImagesDlg.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_TwoImagesDlg.h"
20
21 #include "HYDROGUI_ColorWidget.h"
22 #include "HYDROGUI_ObjSelector.h"
23
24 #include <QCheckBox>
25 #include <QGroupBox>
26 #include <QLabel>
27 #include <QLayout>
28 #include <QLineEdit>
29 #include <QRadioButton>
30
31 HYDROGUI_TwoImagesDlg::HYDROGUI_TwoImagesDlg( HYDROGUI_Module* theModule, const QString& theTitle )
32 : HYDROGUI_InputPanel( theModule, theTitle ),
33   myMode( TwoFuseImage ),
34   myIsEdit( false )
35 {
36   // Image name
37   QGroupBox* anImageNameGroup = new QGroupBox( tr( "IMAGE_NAME" ) );
38
39   QLabel* anImageNameLabel = new QLabel( tr( "NAME" ), anImageNameGroup );
40   myImageName = new QLineEdit( anImageNameGroup );
41
42   myModifySelectedImage = new QCheckBox( tr( "MODIFY_SELECTED_IMAGE" ) );
43
44   QGridLayout* anImageNameLayout = new QGridLayout( anImageNameGroup );
45   anImageNameLayout->setMargin( 5 );
46   anImageNameLayout->setSpacing( 5 );
47   anImageNameLayout->addWidget( anImageNameLabel,      0, 0 );
48   anImageNameLayout->addWidget( myImageName,           0, 1 );
49   anImageNameLayout->addWidget( myModifySelectedImage, 1, 0, 1, 2 );
50
51   // Parameters
52   QGroupBox* aParamGroup = new QGroupBox( tr( "PARAMETERS" ) );
53
54   myImage1Label = new QLabel( tr( "IMAGE_1" ), aParamGroup );
55   myImage1 = new HYDROGUI_ObjSelector( theModule, KIND_IMAGE, aParamGroup );
56
57   myImage2Label = new QLabel( tr( "IMAGE_2" ), aParamGroup );
58   myImage2 = new HYDROGUI_ObjSelector( theModule, KIND_IMAGE, aParamGroup );
59
60   myPolylineLabel = new QLabel( tr( "POLYLINE" ), aParamGroup );
61   myPolyline = new HYDROGUI_ObjSelector( theModule, KIND_POLYLINEXY, aParamGroup,
62     HYDROGUI_ObjSelector::ClosedPolyline);
63
64   myBackgroundFrame = new QFrame( aParamGroup );
65   QLabel* aBackgroundLabel = new QLabel( tr( "BACKGROUND" ), myBackgroundFrame );
66   myTransparent = new QRadioButton( tr( "TRANSPARENT" ), myBackgroundFrame );
67   myTransparent->setChecked( true );
68   myColor = new QRadioButton( tr( "COLOR" ), myBackgroundFrame );
69   myColorBox = new HYDROGUI_ColorWidget( myBackgroundFrame );
70
71   QGridLayout* aBackgroundLayout = new QGridLayout( myBackgroundFrame );
72   aBackgroundLayout->setMargin( 0 );
73   aBackgroundLayout->setSpacing( 5 );
74   aBackgroundLayout->addWidget( aBackgroundLabel, 0, 0, 2, 1 );
75   aBackgroundLayout->addWidget( myTransparent,    0, 1 );
76   aBackgroundLayout->addWidget( myColor,          1, 1 );
77   aBackgroundLayout->addWidget( myColorBox,       1, 2 );
78
79   QGridLayout* aParamLayout = new QGridLayout( aParamGroup );
80   aParamLayout->setMargin( 5 );
81   aParamLayout->setSpacing( 5 );
82   aParamLayout->addWidget( myImage1Label,    0, 0 );
83   aParamLayout->addWidget( myImage1,         0, 1 );
84   aParamLayout->addWidget( myImage2Label,    1, 0 );
85   aParamLayout->addWidget( myImage2,         1, 1 );
86   aParamLayout->addWidget( myPolylineLabel,  1, 0 );
87   aParamLayout->addWidget( myPolyline,       1, 1 );
88   aParamLayout->addWidget( myBackgroundFrame, 2, 0, 1, 2 );
89
90   // Common
91   addWidget( anImageNameGroup );
92   addWidget( aParamGroup );
93   addStretch();
94
95   connect( myModifySelectedImage, SIGNAL( toggled( bool ) ),
96            this, SLOT( onModifySelectedImage( bool ) ) );
97   connect( myTransparent, SIGNAL( toggled( bool ) ), 
98     myColorBox, SLOT( setDisabled( bool ) ) );
99   connect( myImage1, SIGNAL( alreadySelected( const QString& ) ),
100     this, SIGNAL( alreadySelected( const QString& ) ) );
101   connect( myImage2, SIGNAL( alreadySelected( const QString& ) ),
102     this, SIGNAL( alreadySelected( const QString& ) ) );
103
104   setMode( myMode, myIsEdit );
105 }
106
107 HYDROGUI_TwoImagesDlg::~HYDROGUI_TwoImagesDlg()
108 {
109 }
110
111 void HYDROGUI_TwoImagesDlg::reset()
112 {
113   myImageName->clear();
114   myModifySelectedImage->setChecked( false );
115   myImage1->Clear();
116   myImage2->Clear();
117   myPolyline->Clear();
118   myTransparent->setChecked( true );
119   myColorBox->resetColor();
120   myColorBox->setDisabled( true );
121 }
122
123 void HYDROGUI_TwoImagesDlg::setMode( const int theMode, const bool theIsEdit )
124 {
125   myMode = theMode;
126   myIsEdit = theIsEdit;
127
128   bool anIsTwoImages = isTwoImagesMode();
129
130   myBackgroundFrame->setVisible( theMode != TwoCutImage );
131
132   myModifySelectedImage->setVisible( !anIsTwoImages && !myIsEdit );
133
134   myImage1Label->setText( anIsTwoImages ? tr( "IMAGE_1" ) : tr( "IMAGE" ) );
135
136   myImage2Label->setVisible( anIsTwoImages );
137   myImage2->setVisible( anIsTwoImages );
138   myPolylineLabel->setVisible( !anIsTwoImages );
139   myPolyline->setVisible( !anIsTwoImages );
140 }
141
142 bool HYDROGUI_TwoImagesDlg::isModifySelected() const
143 {
144   return myModifySelectedImage->isChecked();
145 }
146
147 void HYDROGUI_TwoImagesDlg::setImageName( const QString& theName )
148 {
149   myImageName->setText( theName );
150 }
151
152 QString HYDROGUI_TwoImagesDlg::getImageName() const
153 {
154   return myImageName->text();
155 }
156
157 void HYDROGUI_TwoImagesDlg::setSelectedObjects( const QString& theName1,
158                                                 const QString& theName2 )
159 {
160   myImage1->SetName( theName1 );
161   if( isTwoImagesMode() )
162     myImage2->SetName( theName2 );
163   else
164     myPolyline->SetName( theName2 );
165 }
166
167 bool HYDROGUI_TwoImagesDlg::getSelectedObjects( QString& theName1,
168                                                 QString& theName2 ) const
169 {
170   theName1 = myImage1->GetName();
171   if( isTwoImagesMode() )
172     theName2 = myImage2->GetName();
173   else
174     theName2 = myPolyline->GetName();
175   return !theName1.isEmpty() && !theName2.isEmpty();
176 }
177
178 void HYDROGUI_TwoImagesDlg::setPreselectedObject( const QString& theName )
179 {
180   myImage1->SetName( theName );
181
182   HYDROGUI_ObjSelector* aSelector = isTwoImagesMode() ? myImage2 : myPolyline;
183   aSelector->SetChecked( true );
184   aSelector->SetName( QString() );
185 }
186
187 void HYDROGUI_TwoImagesDlg::setColor( const QColor& theColor )
188 {
189   if( theColor.alpha() == 0 ) { // transparent
190     myTransparent->setChecked( true );
191     myColorBox->resetColor();
192   }
193   else {
194     myColor->setChecked( true );
195     myColorBox->setColor( theColor );
196   }
197 }
198
199 QColor HYDROGUI_TwoImagesDlg::getColor() const
200 {
201   QColor aColor( 255, 255, 255, 0 ); // transparent
202   if( myColor->isChecked() )
203     aColor = myColorBox->color();
204   return aColor;
205 }
206
207 void HYDROGUI_TwoImagesDlg::onModifySelectedImage( bool theState )
208 {
209   myImageName->setEnabled( !theState );
210 }
211
212 bool HYDROGUI_TwoImagesDlg::isTwoImagesMode() const
213 {
214   return myMode == TwoFuseImage || myMode == TwoCutImage;
215 }