Salome HOME
Merge remote-tracking branch 'origin/BR_IMPROVEMENTS' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_SetColorOp.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_SetColorOp.h"
20
21 #include "HYDROGUI_ColorDlg.h"
22 #include "HYDROGUI_DataModel.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_UpdateFlags.h"
26
27 #include <HYDROData_Object.h>
28 #include <HYDROData_IPolyline.h>
29
30 #include <LightApp_Application.h>
31 #include <LightApp_UpdateFlags.h>
32
33 #include <SUIT_Desktop.h>
34
35 HYDROGUI_SetColorOp::HYDROGUI_SetColorOp( HYDROGUI_Module* theModule )
36 : HYDROGUI_Operation( theModule ),
37   myColorDlg( 0 )
38 {
39   setName( tr( "SET_COLOR" ) );
40 }
41
42 HYDROGUI_SetColorOp::~HYDROGUI_SetColorOp()
43 {
44 }
45
46 bool HYDROGUI_SetColorOp::CanObjectBeColored( const Handle(HYDROData_Entity)& theObject )
47 {
48   if ( theObject.IsNull() )
49     return false;
50
51   return theObject->IsKind( STANDARD_TYPE(HYDROData_Object) ) ||
52          theObject->IsKind( STANDARD_TYPE(HYDROData_IPolyline) );
53 }
54
55 void HYDROGUI_SetColorOp::startOperation()
56 {
57   HYDROGUI_Operation::startOperation();
58
59   // Get the selected object
60   //myEditedObject = Handle(HYDROData_Object)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
61   myEditedObject = HYDROGUI_Tool::GetSelectedObject( module() );
62   if ( !CanObjectBeColored( myEditedObject ) )
63   {
64     abort();
65     return;
66   }
67
68   bool anIsOneColor = false;
69
70   // Get colors from the object
71   QColor aFirstColor, aSecondColor;
72   if ( myEditedObject->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
73   {
74     Handle(HYDROData_Object) aGeomObject =
75       Handle(HYDROData_Object)::DownCast( myEditedObject );
76
77     if ( myEditedObject->GetKind() == KIND_POLYLINE ||
78          myEditedObject->GetKind() == KIND_PROFILE )
79     {
80       aFirstColor = aGeomObject->GetBorderColor();
81       anIsOneColor = true;
82     }
83     else
84     {
85       aFirstColor = aGeomObject->GetFillingColor();
86       aSecondColor = aGeomObject->GetBorderColor();
87     }
88   }
89   else if ( myEditedObject->IsKind( STANDARD_TYPE(HYDROData_IPolyline) ) )
90   {
91     Handle(HYDROData_IPolyline) aPolyObject =
92       Handle(HYDROData_IPolyline)::DownCast( myEditedObject );
93
94     aFirstColor = aPolyObject->GetWireColor();
95
96     anIsOneColor = true;
97   }
98
99   // Create color dialog
100   myColorDlg = new HYDROGUI_ColorDlg( module()->getApp()->desktop(), anIsOneColor );
101   myColorDlg->setModal( true );
102   myColorDlg->setWindowTitle( getName() );
103
104   // Set colors from the object
105   myColorDlg->setFirstColor( aFirstColor );
106   myColorDlg->setSecondColor( aSecondColor );
107
108   // Connect the dialog to operation slots
109   connect( myColorDlg, SIGNAL( accepted() ), this, SLOT( onApply()  ) );
110   connect( myColorDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
111
112   // Show the dialog
113   myColorDlg->exec();
114 }
115
116 bool HYDROGUI_SetColorOp::processApply( int& theUpdateFlags,
117                                         QString& theErrorMsg,
118                                         QStringList& theBrowseObjectsEntries )
119 {
120   if ( !myColorDlg || myEditedObject.IsNull() )
121     return false;
122
123   QColor aFirstColor = myColorDlg->getFirstColor();
124   QColor aSecondColor = myColorDlg->getSecondColor();
125
126   if ( myEditedObject->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
127   {
128     Handle(HYDROData_Object) aGeomObject =
129       Handle(HYDROData_Object)::DownCast( myEditedObject );
130
131     if ( myEditedObject->GetKind() == KIND_POLYLINE ||
132          myEditedObject->GetKind() == KIND_PROFILE  )
133     {
134       aGeomObject->SetBorderColor( aFirstColor );
135     }
136     else
137     {
138       aGeomObject->SetFillingColor( aFirstColor );
139       aGeomObject->SetBorderColor( aSecondColor );
140     }
141   }
142   else if ( myEditedObject->IsKind( STANDARD_TYPE(HYDROData_IPolyline) ) )
143   {
144     Handle(HYDROData_IPolyline) aPolyObject =
145       Handle(HYDROData_IPolyline)::DownCast( myEditedObject );
146
147     aPolyObject->SetWireColor( aFirstColor );
148   }
149
150   module()->setIsToUpdate( myEditedObject );
151
152   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
153   
154   return true;
155 }