Salome HOME
lots 3,8
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileDlg.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_ProfileDlg.h"
20
21 #include "HYDROGUI_Tool.h"
22 #include "HYDROGUI_AISTrihedron.h"
23 #ifndef TEST_MODE
24 #include <HYDROGUI_Module.h>
25 #include "HYDROGUI_Tool2.h"
26 #endif
27
28 #include <CurveCreator_Widget.h>
29 #include <CurveCreator_ICurve.hxx>
30 #include <CurveCreator_Utils.hxx>
31 #include <HYDROGUI_CurveCreatorProfile.h>
32 #include <HYDROData_Tool.h>
33 #include <CurveCreator_Displayer.hxx>
34
35 #include <OCCViewer_ViewPort3d.h>
36 #include <OCCViewer_Utilities.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewFrame.h>
39
40 #ifndef TEST_MODE
41 #include <LightApp_Application.h>
42 #endif
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ResourceMgr.h>
46
47 #include <QGroupBox>
48 #include <QHBoxLayout>
49 #include <QLabel>
50 #include <QLineEdit>
51 #include <QMouseEvent>
52 #include <QSplitter>
53 #include <QSettings>
54 #include <QListWidget>
55 #include <QPushButton>
56 #include <SUIT_MessageBox.h>
57 #include <QColorDialog>
58
59 const QString splitter_key = "HYDROGUI_ProfileDlg::splitter";
60
61 HYDROGUI_ProfileDlg::HYDROGUI_ProfileDlg( HYDROGUI_Module* theModule, const QString& theTitle, 
62   bool theIsEdit )
63 : HYDROGUI_ViewerDlg( theModule, theTitle, true, false ),
64   myName( NULL ), myProfileNames (NULL), myProfilesPointer (NULL),
65   myIsEdit (theIsEdit)
66 {
67   QFrame* name_frame = new QFrame( mainFrame() );
68   QHBoxLayout* name_layout = new QHBoxLayout( name_frame );
69   name_layout->setMargin( 0 );
70   QLabel* aNameLabel = new QLabel(tr("PROFILE_NAME_TLT"), this);
71   name_layout->addWidget(aNameLabel);
72   if (!theIsEdit)
73   {
74     myName = new QLineEdit(this);
75     name_layout->addWidget(myName);
76   }
77   else
78   {
79     myProfileNames = new QListWidget(this);
80     myProfileNames->setSelectionMode(QAbstractItemView::SingleSelection);
81     name_layout->addWidget(myProfileNames);
82
83     QVBoxLayout* btn_layout = new QVBoxLayout( name_frame );
84     btn_layout->setAlignment(Qt::AlignTop);
85
86     myAddProfBtn = new QPushButton(tr("ADD_PROFILES"), this);    
87     myRemProfBtn = new QPushButton(tr("REMOVE_PROFILE"), this);
88     mySetColorProfBtn = new QPushButton(tr("SETCOLOR_PROFILE"), this);
89     btn_layout->addWidget(myAddProfBtn);
90     btn_layout->addWidget(myRemProfBtn);
91     btn_layout->addWidget(mySetColorProfBtn);
92
93     name_layout->addLayout(btn_layout);
94
95   }
96
97   insertWidget( name_frame, 0, 0 );
98
99   int anActionFlags = 
100     CurveCreator_Widget::DisableNewSection | CurveCreator_Widget::DisableDetectionMode |
101     CurveCreator_Widget::DisableClosedSection | CurveCreator_Widget::DisableSetColor;
102   QStringList aCoordTitles;
103   aCoordTitles << tr( "U_TITLE" ) << tr( "Z_TITLE" );
104   myEditorWidget = new CurveCreator_Widget( this, NULL, anActionFlags, aCoordTitles );
105   insertWidget( myEditorWidget, 1, 1 );
106
107   myAddElementBox = new QGroupBox( tr( "ADD_ELEMENT" ), this );
108   insertWidget( myAddElementBox, 2, 1 );
109
110   QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
111   anAddElementLayout->setMargin( 0 );
112   anAddElementLayout->setSpacing( 5 );
113
114   myEditorWidget->setOCCViewer( viewer() );
115
116   connect( myEditorWidget, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
117   connect( myEditorWidget, SIGNAL( subOperationStarted(QWidget*, bool) ), this, SLOT( processStartedSubOperation(QWidget*, bool) ) );
118   connect( myEditorWidget, SIGNAL( subOperationFinished(QWidget*) ), this, SLOT( processFinishedSubOperation(QWidget*) ) );
119   if (myIsEdit)
120   {
121     connect( myProfileNames, SIGNAL( currentTextChanged(QString)), SLOT(ProfileNameChanged(QString)) );
122     connect( myProfileNames, SIGNAL( itemSelectionChanged()), this, SLOT( onProfileIndexChanged()));
123     connect( myProfileNames, SIGNAL( itemChanged(QListWidgetItem*)), this, SLOT( onProfileNameChanged(QListWidgetItem*)));
124     connect( myAddProfBtn, SIGNAL( clicked(bool)), this, SLOT( onAddBtnPressed(bool)));
125     connect( myRemProfBtn, SIGNAL( clicked(bool)), this, SLOT( onRemoveBtnPressed(bool)));
126     connect( mySetColorProfBtn, SIGNAL( clicked(bool)), this, SLOT( onSetColorBtnPressed(bool)));
127   }
128   myAddElementBox->hide();
129
130   QList<int> sizes;
131   sizes.append( 25 );
132   sizes.append( 100 );
133   sizes.append( 100 );
134   sizes.append( 200 );
135   sizes.append( 25 );
136   splitter()->setSizes( sizes );
137
138 #ifndef TEST_MODE
139   QSettings settings;
140   splitter()->restoreState( settings.value( splitter_key ).toByteArray() );
141 #endif
142 }
143
144 HYDROGUI_ProfileDlg::~HYDROGUI_ProfileDlg()
145 {
146 #ifndef TEST_MODE
147   QSettings settings;
148   settings.setValue( splitter_key, splitter()->saveState() );
149 #endif
150 }
151
152 void HYDROGUI_ProfileDlg::reset()
153 {
154   myEditorWidget->reset();
155   myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
156   viewer()->setTrihedronShown( false ); // Issue #548
157   viewer()->setStaticTrihedronDisplayed(false);
158   if (myProfileNames)
159     myProfileNames->clear();
160 }
161
162 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
163 {
164   if (myIsEdit)
165     return;
166   myName->setText(theName);
167 }
168
169 void HYDROGUI_ProfileDlg::eraseProfile( int index )
170 {
171   myProfileNames->takeItem(index);
172   if (myProfileNames->count() == 1)
173     myRemProfBtn->setEnabled(false);
174 }
175
176 void HYDROGUI_ProfileDlg::addProfileName( const QString& theName, const QColor& theColor )
177 {
178   if (!myIsEdit)
179     return;
180   myProfileNames->blockSignals(true);
181   myProfileNames->addItem(theName);
182   int count = myProfileNames->count();
183   QListWidgetItem* anItem = myProfileNames->item(count - 1);
184   anItem->setFlags (anItem->flags () | Qt::ItemIsEditable);
185   QPixmap SPixmap(16, 16);
186   SPixmap.fill(theColor);
187   QIcon SIcon(SPixmap);
188   anItem->setIcon( SIcon );
189   if (count == 1)
190     anItem->setSelected(true);
191   if (count == 1 && myIsEdit)
192     myRemProfBtn->setEnabled(false);
193   if (count > 1)
194     myRemProfBtn->setEnabled(true);
195
196   myProfileNames->blockSignals(false);
197 }
198
199 QStringList HYDROGUI_ProfileDlg::getProfileNames() const
200 {
201   QStringList aProfNames;
202   if (!myIsEdit)
203     aProfNames << myName->text();
204   else
205     for (int i = 0; i < myProfileNames->count(); i++)
206       aProfNames <<  myProfileNames->item(i)->text();
207   return aProfNames;
208 }
209
210 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
211 {
212   myEditorWidget->setCurve( theProfile );
213
214   // select the single section by default
215   QList<int> aSections;
216   aSections << 0;
217   myEditorWidget->setSelectedSections( aSections );
218 }
219
220 void HYDROGUI_ProfileDlg::setProfilesPointer(std::vector<HYDROGUI_CurveCreatorProfile*>* theProfilesPointer)
221 {
222   myProfilesPointer = theProfilesPointer;
223 }
224
225 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
226 {
227   return myEditorWidget->getSelectedSections();
228 }
229
230 void HYDROGUI_ProfileDlg::switchToFirstProfile()
231 {
232   emit onProfileIndexChanged();
233 }
234
235 /**
236  * Redirect the delete action to editor widget
237  */
238 void HYDROGUI_ProfileDlg::deleteSelected()
239 {
240   myEditorWidget->removeSelected();
241 }
242
243 /**
244  * Checks whether there are some to delete
245  */
246 bool HYDROGUI_ProfileDlg::deleteEnabled()
247 {
248   return myEditorWidget->removeEnabled();
249 }
250
251 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
252 {
253   myEditorWidget->setEnabled( false );
254
255   myAddElementBox->setTitle( theIsEdit ? tr( "EDIT_ELEMENT" ) : tr( "ADD_ELEMENT" ) );
256   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
257   anAddElementLayout->addWidget( theWidget );
258
259   theWidget->show();
260   myAddElementBox->show();
261 }
262
263 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
264 {
265   myEditorWidget->setEnabled( true );
266
267   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
268   anAddElementLayout->removeWidget( theWidget );
269
270   theWidget->hide();
271   myAddElementBox->hide();
272 }
273
274 void HYDROGUI_ProfileDlg::ProfileNameChanged(QString text)
275 {
276   myCurrentName = text;
277 }
278
279 void HYDROGUI_ProfileDlg::onProfileIndexChanged()
280 {
281   int theIndex = GetProfileSelectionIndex();
282   if (theIndex > -1)
283     SwitchToProfile(theIndex);
284 }
285
286 int HYDROGUI_ProfileDlg::GetProfileSelectionIndex()
287 {
288   if (!myProfilesPointer)
289     return -1;
290   QModelIndexList MI = myProfileNames->selectionModel()->selectedIndexes();
291   if (MI.size() != 1)
292     return -1;
293   return MI.first().row();
294 }
295
296 void HYDROGUI_ProfileDlg::BlockProfileNameSignals(bool state)
297 {
298   myProfileNames->blockSignals(state);
299 }
300
301 void HYDROGUI_ProfileDlg::SwitchToProfile(int theIndex)
302 {
303   myEditorWidget->setCurve(NULL);
304   myEditorWidget->reset();
305   myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
306   myEditorWidget->setSelectedSections(QList<int>());
307   setProfile( (*myProfilesPointer)[theIndex] );
308   for (int i = 0; i < myProfilesPointer->size(); i++)
309   {
310     HYDROGUI_CurveCreatorProfile* aCurve = (*myProfilesPointer)[i];
311     if (i == theIndex)
312     {
313       aCurve->myLineWidth = 3;
314       Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
315       if (anAISObject)
316         anAISObject->SetWidth(3);
317     }
318     else
319     {
320       aCurve->myLineWidth = 1;
321       Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
322       if (anAISObject)
323         anAISObject->SetWidth(1);
324     }
325   }
326
327   if( myProfilesPointer && 
328       myProfilesPointer->size()>0 && 
329       myProfilesPointer->at(0) && 
330       myProfilesPointer->at(0)->getDisplayer() )
331     myProfilesPointer->at(0)->getDisplayer()->Update();
332 }
333
334 void HYDROGUI_ProfileDlg::onAddBtnPressed(bool)
335
336   emit AddProfiles();
337 }
338
339 void HYDROGUI_ProfileDlg::onRemoveBtnPressed(bool)
340 {
341   int theIndex = GetProfileSelectionIndex();
342   if (theIndex > -1)
343     emit RemoveProfile(theIndex);
344 }
345
346 void HYDROGUI_ProfileDlg::onSetColorBtnPressed(bool)
347 {
348   int theIndex = GetProfileSelectionIndex();
349   if (theIndex < 0)
350     return;
351
352   HYDROGUI_CurveCreatorProfile* aCurve = (*myProfilesPointer)[theIndex];
353   
354   QColor aCurrentColor = HYDROData_Tool::toQtColor(/*aCurve->myCurveColor*/aCurve->getColorSection(0));
355   QColor newQCurCol = QColorDialog::getColor( aCurrentColor, this );
356   if( !newQCurCol.isValid() )
357     return;
358
359   Quantity_Color newOCCCol = HYDROData_Tool::toOccColor(newQCurCol);
360   aCurve->setColorSectionInternal(0, newOCCCol);
361   //Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
362   //if (anAISObject)
363   //  anAISObject->SetColor(newOCCCol);
364
365   QListWidgetItem* item = myProfileNames->item(theIndex);
366   QPixmap SPixmap(16, 16);
367   SPixmap.fill(newQCurCol);
368   QIcon SIcon(SPixmap);
369   item->setIcon( SIcon );
370
371   if( myProfilesPointer && 
372     myProfilesPointer->size()>0 && 
373     myProfilesPointer->at(0) && 
374     myProfilesPointer->at(0)->getDisplayer() )
375     myProfilesPointer->at(0)->getDisplayer()->Update();
376 }
377
378 void HYDROGUI_ProfileDlg::onProfileNameChanged(QListWidgetItem* item)
379 {
380   int ind = GetProfileSelectionIndex();
381   if (ind > -1)
382   {
383     int count = myProfileNames->count();
384     QSet<QString> names;
385     for (int i = 0; i < count; i++)
386     {
387       QListWidgetItem* citem = myProfileNames->item(i);
388       if (item!=citem)
389         names << citem->text();
390     }
391     QString curText = item->text();
392     myProfileNames->blockSignals(true);
393     if (names.contains(curText))
394     {
395       QString mes = tr( "PROFILE_ALREADY_EXISTS" );
396       QString title = tr( "PROFILEDLG_WARNING" );
397 #ifndef TEST_MODE
398       SUIT_MessageBox::warning( module()->getApp()->desktop(), title, mes );
399 #endif
400       item->setText(myCurrentName);
401     }
402     myProfileNames->blockSignals(false);
403   }
404 }
405 /*
406 void HYDROGUI_ProfileDlg::SetSingleProfileMode(bool SingleMode)
407 {
408   mySingleProfileMode = SingleMode;
409 }
410
411 bool HYDROGUI_ProfileDlg::GetSingleProfileMode() const
412 {
413   return mySingleProfileMode;
414 }*/
415
416 Handle(AIS_Trihedron) HYDROGUI_ProfileDlg::trihedron()
417 {
418   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
419   Handle(AIS_Trihedron) aTrihedron =
420     HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", viewer()->trihedronSize() ) );
421   return aTrihedron;
422 }