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