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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ProfileDlg.h"
21 #include "HYDROGUI_Tool.h"
22 #include "HYDROGUI_AISTrihedron.h"
24 #include <HYDROGUI_Module.h>
25 #include "HYDROGUI_Tool2.h"
28 #include <CurveCreator_Widget.h>
29 #include <CurveCreator_ICurve.hxx>
30 #include <CurveCreator_Utils.hxx>
31 #include <HYDROGUI_CurveCreatorProfile.h>
32 #include <HYDROData_Tool.h>
33 #include <CurveCreator_Displayer.hxx>
35 #include <OCCViewer_ViewPort3d.h>
36 #include <OCCViewer_Utilities.h>
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewFrame.h>
41 #include <LightApp_Application.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ResourceMgr.h>
48 #include <QHBoxLayout>
51 #include <QMouseEvent>
54 #include <QListWidget>
55 #include <QPushButton>
56 #include <SUIT_MessageBox.h>
57 #include <QColorDialog>
59 const QString splitter_key = "HYDROGUI_ProfileDlg::splitter";
61 HYDROGUI_ProfileDlg::HYDROGUI_ProfileDlg( HYDROGUI_Module* theModule, const QString& theTitle,
63 : HYDROGUI_ViewerDlg( theModule, theTitle, true, false ),
64 myName( NULL ), myProfileNames (NULL), myProfilesPointer (NULL),
67 QFrame* name_frame = new QFrame( mainFrame() );
68 QHBoxLayout* name_layout = new QHBoxLayout( name_frame );
69 name_layout->setMargin( 0 );
70 QLabel* aNameLabel = new QLabel(tr("PROFILE_NAME_TLT"), this);
71 name_layout->addWidget(aNameLabel);
74 myName = new QLineEdit(this);
75 name_layout->addWidget(myName);
79 myProfileNames = new QListWidget(this);
80 myProfileNames->setSelectionMode(QAbstractItemView::SingleSelection);
81 name_layout->addWidget(myProfileNames);
83 QVBoxLayout* btn_layout = new QVBoxLayout( name_frame );
84 btn_layout->setAlignment(Qt::AlignTop);
86 myAddProfBtn = new QPushButton(tr("ADD_PROFILES"), this);
87 myRemProfBtn = new QPushButton(tr("REMOVE_PROFILE"), this);
88 mySetColorProfBtn = new QPushButton(tr("SETCOLOR_PROFILE"), this);
89 btn_layout->addWidget(myAddProfBtn);
90 btn_layout->addWidget(myRemProfBtn);
91 btn_layout->addWidget(mySetColorProfBtn);
93 name_layout->addLayout(btn_layout);
97 insertWidget( name_frame, 0, 0 );
100 CurveCreator_Widget::DisableNewSection | CurveCreator_Widget::DisableDetectionMode |
101 CurveCreator_Widget::DisableClosedSection | CurveCreator_Widget::DisableSetColor;
102 QStringList aCoordTitles;
103 aCoordTitles << tr( "U_TITLE" ) << tr( "Z_TITLE" );
104 myEditorWidget = new CurveCreator_Widget( this, NULL, anActionFlags, aCoordTitles );
105 insertWidget( myEditorWidget, 1, 1 );
107 myAddElementBox = new QGroupBox( tr( "ADD_ELEMENT" ), this );
108 insertWidget( myAddElementBox, 2, 1 );
110 QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
111 anAddElementLayout->setMargin( 0 );
112 anAddElementLayout->setSpacing( 5 );
114 myEditorWidget->setOCCViewer( viewer() );
116 connect( myEditorWidget, SIGNAL( selectionChanged() ), this, SIGNAL( selectionChanged() ) );
117 connect( myEditorWidget, SIGNAL( subOperationStarted(QWidget*, bool) ), this, SLOT( processStartedSubOperation(QWidget*, bool) ) );
118 connect( myEditorWidget, SIGNAL( subOperationFinished(QWidget*) ), this, SLOT( processFinishedSubOperation(QWidget*) ) );
121 connect( myProfileNames, SIGNAL( currentTextChanged(QString)), SLOT(ProfileNameChanged(QString)) );
122 connect( myProfileNames, SIGNAL( itemSelectionChanged()), this, SLOT( onProfileIndexChanged()));
123 connect( myProfileNames, SIGNAL( itemChanged(QListWidgetItem*)), this, SLOT( onProfileNameChanged(QListWidgetItem*)));
124 connect( myAddProfBtn, SIGNAL( clicked(bool)), this, SLOT( onAddBtnPressed(bool)));
125 connect( myRemProfBtn, SIGNAL( clicked(bool)), this, SLOT( onRemoveBtnPressed(bool)));
126 connect( mySetColorProfBtn, SIGNAL( clicked(bool)), this, SLOT( onSetColorBtnPressed(bool)));
128 myAddElementBox->hide();
136 splitter()->setSizes( sizes );
140 splitter()->restoreState( settings.value( splitter_key ).toByteArray() );
144 HYDROGUI_ProfileDlg::~HYDROGUI_ProfileDlg()
148 settings.setValue( splitter_key, splitter()->saveState() );
152 void HYDROGUI_ProfileDlg::reset()
154 myEditorWidget->reset();
155 myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
156 viewer()->setTrihedronShown( false ); // Issue #548
157 viewer()->setStaticTrihedronDisplayed(false);
159 myProfileNames->clear();
162 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
166 myName->setText(theName);
169 void HYDROGUI_ProfileDlg::eraseProfile( int index )
171 myProfileNames->takeItem(index);
172 if (myProfileNames->count() == 1)
173 myRemProfBtn->setEnabled(false);
176 void HYDROGUI_ProfileDlg::addProfileName( const QString& theName, const QColor& theColor )
180 myProfileNames->blockSignals(true);
181 myProfileNames->addItem(theName);
182 int count = myProfileNames->count();
183 QListWidgetItem* anItem = myProfileNames->item(count - 1);
184 anItem->setFlags (anItem->flags () | Qt::ItemIsEditable);
185 QPixmap SPixmap(16, 16);
186 SPixmap.fill(theColor);
187 QIcon SIcon(SPixmap);
188 anItem->setIcon( SIcon );
190 anItem->setSelected(true);
191 if (count == 1 && myIsEdit)
192 myRemProfBtn->setEnabled(false);
194 myRemProfBtn->setEnabled(true);
196 myProfileNames->blockSignals(false);
199 QStringList HYDROGUI_ProfileDlg::getProfileNames() const
201 QStringList aProfNames;
203 aProfNames << myName->text();
205 for (int i = 0; i < myProfileNames->count(); i++)
206 aProfNames << myProfileNames->item(i)->text();
210 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
212 myEditorWidget->setCurve( theProfile );
214 // select the single section by default
215 QList<int> aSections;
217 myEditorWidget->setSelectedSections( aSections );
220 void HYDROGUI_ProfileDlg::setProfilesPointer(std::vector<HYDROGUI_CurveCreatorProfile*>* theProfilesPointer)
222 myProfilesPointer = theProfilesPointer;
225 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
227 return myEditorWidget->getSelectedSections();
230 void HYDROGUI_ProfileDlg::switchToFirstProfile()
232 emit onProfileIndexChanged();
236 * Redirect the delete action to editor widget
238 void HYDROGUI_ProfileDlg::deleteSelected()
240 myEditorWidget->removeSelected();
244 * Checks whether there are some to delete
246 bool HYDROGUI_ProfileDlg::deleteEnabled()
248 return myEditorWidget->removeEnabled();
251 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
253 myEditorWidget->setEnabled( false );
255 myAddElementBox->setTitle( theIsEdit ? tr( "EDIT_ELEMENT" ) : tr( "ADD_ELEMENT" ) );
256 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
257 anAddElementLayout->addWidget( theWidget );
260 myAddElementBox->show();
263 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
265 myEditorWidget->setEnabled( true );
267 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
268 anAddElementLayout->removeWidget( theWidget );
271 myAddElementBox->hide();
274 void HYDROGUI_ProfileDlg::ProfileNameChanged(QString text)
276 myCurrentName = text;
279 void HYDROGUI_ProfileDlg::onProfileIndexChanged()
281 int theIndex = GetProfileSelectionIndex();
283 SwitchToProfile(theIndex);
286 int HYDROGUI_ProfileDlg::GetProfileSelectionIndex()
288 if (!myProfilesPointer)
290 QModelIndexList MI = myProfileNames->selectionModel()->selectedIndexes();
293 return MI.first().row();
296 void HYDROGUI_ProfileDlg::BlockProfileNameSignals(bool state)
298 myProfileNames->blockSignals(state);
301 void HYDROGUI_ProfileDlg::SwitchToProfile(int theIndex)
303 myEditorWidget->setCurve(NULL);
304 myEditorWidget->reset();
305 myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
306 myEditorWidget->setSelectedSections(QList<int>());
307 setProfile( (*myProfilesPointer)[theIndex] );
308 for (int i = 0; i < myProfilesPointer->size(); i++)
310 HYDROGUI_CurveCreatorProfile* aCurve = (*myProfilesPointer)[i];
313 aCurve->myLineWidth = 3;
314 Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
316 anAISObject->SetWidth(3);
320 aCurve->myLineWidth = 1;
321 Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
323 anAISObject->SetWidth(1);
327 if( myProfilesPointer &&
328 myProfilesPointer->size()>0 &&
329 myProfilesPointer->at(0) &&
330 myProfilesPointer->at(0)->getDisplayer() )
331 myProfilesPointer->at(0)->getDisplayer()->Update();
334 void HYDROGUI_ProfileDlg::onAddBtnPressed(bool)
339 void HYDROGUI_ProfileDlg::onRemoveBtnPressed(bool)
341 int theIndex = GetProfileSelectionIndex();
343 emit RemoveProfile(theIndex);
346 void HYDROGUI_ProfileDlg::onSetColorBtnPressed(bool)
348 int theIndex = GetProfileSelectionIndex();
352 HYDROGUI_CurveCreatorProfile* aCurve = (*myProfilesPointer)[theIndex];
354 QColor aCurrentColor = HYDROData_Tool::toQtColor(/*aCurve->myCurveColor*/aCurve->getColorSection(0));
355 QColor newQCurCol = QColorDialog::getColor( aCurrentColor, this );
356 if( !newQCurCol.isValid() )
359 Quantity_Color newOCCCol = HYDROData_Tool::toOccColor(newQCurCol);
360 aCurve->setColorSectionInternal(0, newOCCCol);
361 Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
364 //color points of shape in the same color as edges
365 Handle(Prs3d_PointAspect) anAspect = anAISObject->Attributes()->PointAspect();
366 anAspect->SetColor(newOCCCol);
367 anAISObject->Attributes()->SetPointAspect( anAspect );
370 QListWidgetItem* item = myProfileNames->item(theIndex);
371 QPixmap SPixmap(16, 16);
372 SPixmap.fill(newQCurCol);
373 QIcon SIcon(SPixmap);
374 item->setIcon( SIcon );
376 if( myProfilesPointer &&
377 myProfilesPointer->size()>0 &&
378 myProfilesPointer->at(0) &&
379 myProfilesPointer->at(0)->getDisplayer() )
380 myProfilesPointer->at(0)->getDisplayer()->Update();
383 void HYDROGUI_ProfileDlg::onProfileNameChanged(QListWidgetItem* item)
385 int ind = GetProfileSelectionIndex();
388 int count = myProfileNames->count();
390 for (int i = 0; i < count; i++)
392 QListWidgetItem* citem = myProfileNames->item(i);
394 names << citem->text();
396 QString curText = item->text();
397 myProfileNames->blockSignals(true);
398 if (names.contains(curText))
400 QString mes = tr( "PROFILE_ALREADY_EXISTS" );
401 QString title = tr( "PROFILEDLG_WARNING" );
403 SUIT_MessageBox::warning( module()->getApp()->desktop(), title, mes );
405 item->setText(myCurrentName);
407 myProfileNames->blockSignals(false);
411 void HYDROGUI_ProfileDlg::SetSingleProfileMode(bool SingleMode)
413 mySingleProfileMode = SingleMode;
416 bool HYDROGUI_ProfileDlg::GetSingleProfileMode() const
418 return mySingleProfileMode;
421 Handle(AIS_Trihedron) HYDROGUI_ProfileDlg::trihedron()
423 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
424 Handle(AIS_Trihedron) aTrihedron =
425 HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", viewer()->trihedronSize() ) );