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