Salome HOME
debug of profileOp
[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 ),
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   if (myProfileNames)
146     myProfileNames->clear();
147 }
148
149 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
150 {
151   if (myIsEdit)
152     return;
153   myName->setText(theName);
154 }
155
156 void HYDROGUI_ProfileDlg::eraseProfile( int index )
157 {
158   myProfileNames->takeItem(index);
159   if (myProfileNames->count() == 1)
160     myRemProfBtn->setEnabled(false);
161 }
162
163 void HYDROGUI_ProfileDlg::addProfileName( const QString& theName, const QColor& theColor )
164 {
165   if (!myIsEdit)
166     return;
167   myProfileNames->blockSignals(true);
168   myProfileNames->addItem(theName);
169   int count = myProfileNames->count();
170   QListWidgetItem* anItem = myProfileNames->item(count - 1);
171   anItem->setFlags (anItem->flags () | Qt::ItemIsEditable);
172   QPixmap SPixmap(16, 16);
173   SPixmap.fill(theColor);
174   QIcon SIcon(SPixmap);
175   anItem->setIcon( SIcon );
176   if (count == 1)
177     anItem->setSelected(true);
178   if (count == 1 && myIsEdit)
179     myRemProfBtn->setEnabled(false);
180   if (count > 1)
181     myRemProfBtn->setEnabled(true);
182
183   myProfileNames->blockSignals(false);
184 }
185
186 QStringList HYDROGUI_ProfileDlg::getProfileNames() const
187 {
188   QStringList aProfNames;
189   if (!myIsEdit)
190     aProfNames << myName->text();
191   else
192     for (int i = 0; i < myProfileNames->count(); i++)
193       aProfNames <<  myProfileNames->item(i)->text();
194   return aProfNames;
195 }
196
197 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
198 {
199   myEditorWidget->setCurve( theProfile );
200
201   // select the single section by default
202   QList<int> aSections;
203   aSections << 0;
204   myEditorWidget->setSelectedSections( aSections );
205 }
206
207 void HYDROGUI_ProfileDlg::setProfilesPointer(std::vector<HYDROGUI_CurveCreatorProfile*>* theProfilesPointer)
208 {
209   myProfilesPointer = theProfilesPointer;
210 }
211
212 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
213 {
214   return myEditorWidget->getSelectedSections();
215 }
216
217 void HYDROGUI_ProfileDlg::switchToFirstProfile()
218 {
219   emit onProfileIndexChanged();
220 }
221
222 /**
223  * Redirect the delete action to editor widget
224  */
225 void HYDROGUI_ProfileDlg::deleteSelected()
226 {
227   myEditorWidget->removeSelected();
228 }
229
230 /**
231  * Checks whether there are some to delete
232  */
233 bool HYDROGUI_ProfileDlg::deleteEnabled()
234 {
235   return myEditorWidget->removeEnabled();
236 }
237
238 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
239 {
240   myEditorWidget->setEnabled( false );
241
242   myAddElementBox->setTitle( theIsEdit ? tr( "EDIT_ELEMENT" ) : tr( "ADD_ELEMENT" ) );
243   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
244   anAddElementLayout->addWidget( theWidget );
245
246   theWidget->show();
247   myAddElementBox->show();
248 }
249
250 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
251 {
252   myEditorWidget->setEnabled( true );
253
254   QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
255   anAddElementLayout->removeWidget( theWidget );
256
257   theWidget->hide();
258   myAddElementBox->hide();
259 }
260
261 void HYDROGUI_ProfileDlg::ProfileNameChanged(QString text)
262 {
263   myCurrentName = text;
264 }
265
266 void HYDROGUI_ProfileDlg::onProfileIndexChanged()
267 {
268   int theIndex = GetProfileSelectionIndex();
269   if (theIndex > -1)
270     SwitchToProfile(theIndex);
271 }
272
273 int HYDROGUI_ProfileDlg::GetProfileSelectionIndex()
274 {
275   if (!myProfilesPointer)
276     return -1;
277   QModelIndexList MI = myProfileNames->selectionModel()->selectedIndexes();
278   if (MI.size() != 1)
279     return -1;
280   return MI.first().row();
281 }
282
283 void HYDROGUI_ProfileDlg::BlockProfileNameSignals(bool state)
284 {
285   myProfileNames->blockSignals(state);
286 }
287
288 void HYDROGUI_ProfileDlg::SwitchToProfile(int theIndex)
289 {
290   myEditorWidget->setCurve(NULL);
291   myEditorWidget->reset();
292   myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
293   myEditorWidget->setSelectedSections(QList<int>());
294   setProfile( (*myProfilesPointer)[theIndex] );
295   for (int i = 0; i < myProfilesPointer->size(); i++)
296   {
297     HYDROGUI_CurveCreatorProfile* aCurve = (*myProfilesPointer)[i];
298     if (i == theIndex)
299     {
300       aCurve->myLineWidth = 3;
301       Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
302       if (anAISObject)
303         anAISObject->SetWidth(3);
304     }
305     else
306     {
307       aCurve->myLineWidth = 1;
308       Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
309       if (anAISObject)
310         anAISObject->SetWidth(1);
311     }
312   }
313
314   if( myProfilesPointer && 
315       myProfilesPointer->size()>0 && 
316       myProfilesPointer->at(0) && 
317       myProfilesPointer->at(0)->getDisplayer() )
318     myProfilesPointer->at(0)->getDisplayer()->Update();
319 }
320
321 void HYDROGUI_ProfileDlg::onAddBtnPressed(bool)
322
323   emit AddProfiles();
324 }
325
326 void HYDROGUI_ProfileDlg::onRemoveBtnPressed(bool)
327 {
328   int theIndex = GetProfileSelectionIndex();
329   if (theIndex > -1)
330     emit RemoveProfile(theIndex);
331 }
332
333 void HYDROGUI_ProfileDlg::onProfileNameChanged(QListWidgetItem* item)
334 {
335   int ind = GetProfileSelectionIndex();
336   if (ind > -1)
337   {
338     int count = myProfileNames->count();
339     QSet<QString> names;
340     for (int i = 0; i < count; i++)
341     {
342       QListWidgetItem* citem = myProfileNames->item(i);
343       if (item!=citem)
344         names << citem->text();
345     }
346     QString curText = item->text();
347     myProfileNames->blockSignals(true);
348     if (names.contains(curText))
349     {
350       QString mes = tr( "PROFILE_ALREADY_EXISTS" );
351       QString title = tr( "PROFILEDLG_WARNING" );
352 #ifndef TEST_MODE
353       SUIT_MessageBox::warning( module()->getApp()->desktop(), title, mes );
354 #endif
355       item->setText(myCurrentName);
356     }
357     myProfileNames->blockSignals(false);
358   }
359 }
360 /*
361 void HYDROGUI_ProfileDlg::SetSingleProfileMode(bool SingleMode)
362 {
363   mySingleProfileMode = SingleMode;
364 }
365
366 bool HYDROGUI_ProfileDlg::GetSingleProfileMode() const
367 {
368   return mySingleProfileMode;
369 }*/
370
371 Handle(AIS_Trihedron) HYDROGUI_ProfileDlg::trihedron()
372 {
373   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
374   Handle(AIS_Trihedron) aTrihedron =
375     HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", viewer()->trihedronSize() ) );
376   return aTrihedron;
377 }