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