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