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