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 <CurveCreator_Displayer.hxx>
34 #include <OCCViewer_ViewPort3d.h>
35 #include <OCCViewer_Utilities.h>
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewFrame.h>
40 #include <LightApp_Application.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_ResourceMgr.h>
47 #include <QHBoxLayout>
50 #include <QMouseEvent>
53 #include <QListWidget>
54 #include <QPushButton>
55 #include <SUIT_MessageBox.h>
57 const QString splitter_key = "HYDROGUI_ProfileDlg::splitter";
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)
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)
72 myName = new QLineEdit(this);
73 name_layout->addWidget(myName);
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);
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);
89 insertWidget( name_frame, 0, 0 );
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 );
99 myAddElementBox = new QGroupBox( tr( "ADD_ELEMENT" ), this );
100 insertWidget( myAddElementBox, 2, 1 );
102 QBoxLayout* anAddElementLayout = new QVBoxLayout( myAddElementBox );
103 anAddElementLayout->setMargin( 0 );
104 anAddElementLayout->setSpacing( 5 );
106 myEditorWidget->setOCCViewer( viewer() );
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)
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)));
119 myAddElementBox->hide();
127 splitter()->setSizes( sizes );
131 splitter()->restoreState( settings.value( splitter_key ).toByteArray() );
135 HYDROGUI_ProfileDlg::~HYDROGUI_ProfileDlg()
139 settings.setValue( splitter_key, splitter()->saveState() );
143 void HYDROGUI_ProfileDlg::reset()
145 myEditorWidget->reset();
146 myEditorWidget->setActionMode( CurveCreator_Widget::AdditionMode );
147 viewer()->setTrihedronShown( false ); // Issue #548
149 myProfileNames->clear();
152 void HYDROGUI_ProfileDlg::setProfileName( const QString& theName )
154 if (!mySingleProfileMode)
156 myName->setText(theName);
157 //myProfileNames->setItemText(theInd, theName); //should used TODO check this
158 //myName->setText(theName);
161 void HYDROGUI_ProfileDlg::eraseProfile( int index )
163 myProfileNames->takeItem(index);
164 if (myProfileNames->count() == 1)
165 myRemProfBtn->setEnabled(false);
168 void HYDROGUI_ProfileDlg::addProfileName( const QString& theName, const QColor& theColor )
170 if (mySingleProfileMode)
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));
183 anItem->setSelected(true);
185 myRemProfBtn->setEnabled(true);
186 myProfileNames->blockSignals(false);
189 QStringList HYDROGUI_ProfileDlg::getProfileNames() const
191 QStringList aProfNames;
192 if (mySingleProfileMode)
193 aProfNames << myName->text();
195 for (int i = 0; i < myProfileNames->count(); i++)
196 aProfNames << myProfileNames->item(i)->text();
200 void HYDROGUI_ProfileDlg::setProfile( CurveCreator_ICurve* theProfile )
202 myEditorWidget->setCurve( theProfile );
204 // select the single section by default
205 QList<int> aSections;
207 myEditorWidget->setSelectedSections( aSections );
210 void HYDROGUI_ProfileDlg::setProfilesPointer(std::vector<HYDROGUI_CurveCreatorProfile*>* theProfilesPointer)
212 myProfilesPointer = theProfilesPointer;
215 QList<int> HYDROGUI_ProfileDlg::getSelectedSections()
217 return myEditorWidget->getSelectedSections();
220 void HYDROGUI_ProfileDlg::switchToFirstProfile()
222 emit onProfileIndexChanged();
226 * Redirect the delete action to editor widget
228 void HYDROGUI_ProfileDlg::deleteSelected()
230 myEditorWidget->removeSelected();
234 * Checks whether there are some to delete
236 bool HYDROGUI_ProfileDlg::deleteEnabled()
238 return myEditorWidget->removeEnabled();
241 void HYDROGUI_ProfileDlg::processStartedSubOperation( QWidget* theWidget, bool theIsEdit )
243 myEditorWidget->setEnabled( false );
245 myAddElementBox->setTitle( theIsEdit ? tr( "EDIT_ELEMENT" ) : tr( "ADD_ELEMENT" ) );
246 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
247 anAddElementLayout->addWidget( theWidget );
250 myAddElementBox->show();
253 void HYDROGUI_ProfileDlg::processFinishedSubOperation( QWidget* theWidget )
255 myEditorWidget->setEnabled( true );
257 QBoxLayout* anAddElementLayout = dynamic_cast<QBoxLayout*>( myAddElementBox->layout() );
258 anAddElementLayout->removeWidget( theWidget );
261 myAddElementBox->hide();
264 void HYDROGUI_ProfileDlg::ProfileNameChanged(QString text)
266 myCurrentName = text;
269 void HYDROGUI_ProfileDlg::onProfileIndexChanged()
271 int theIndex = GetProfileSelectionIndex();
273 SwitchToProfile(theIndex);
276 int HYDROGUI_ProfileDlg::GetProfileSelectionIndex()
278 if (!myProfilesPointer)
280 QModelIndexList MI = myProfileNames->selectionModel()->selectedIndexes();
283 return MI.first().row();
286 void HYDROGUI_ProfileDlg::BlockProfileNameSignals(bool state)
288 myProfileNames->blockSignals(state);
291 void HYDROGUI_ProfileDlg::SwitchToProfile(int theIndex)
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++)
300 HYDROGUI_CurveCreatorProfile* aCurve = (*myProfilesPointer)[i];
303 aCurve->myLineWidth = 3;
304 Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
306 anAISObject->SetWidth(3);
310 aCurve->myLineWidth = 1;
311 Handle(AIS_InteractiveObject) anAISObject = aCurve->getAISObject();
313 anAISObject->SetWidth(1);
316 (*myProfilesPointer)[0]->getDisplayer()->Update();
319 void HYDROGUI_ProfileDlg::onAddBtnPressed(bool)
324 void HYDROGUI_ProfileDlg::onRemoveBtnPressed(bool)
326 int theIndex = GetProfileSelectionIndex();
328 emit RemoveProfile(theIndex);
331 void HYDROGUI_ProfileDlg::onProfileNameChanged(QListWidgetItem* item)
333 int ind = GetProfileSelectionIndex();
336 int count = myProfileNames->count();
338 for (int i = 0; i < count; i++)
340 QListWidgetItem* citem = myProfileNames->item(i);
342 names << citem->text();
344 QString curText = item->text();
345 myProfileNames->blockSignals(true);
346 if (names.contains(curText))
348 QString mes = tr( "PROFILE_ALREADY_EXISTS" );
349 QString title = tr( "PROFILEDLG_WARNING" );
351 SUIT_MessageBox::warning( module()->getApp()->desktop(), title, mes );
353 item->setText(myCurrentName);
355 myProfileNames->blockSignals(false);
359 void HYDROGUI_ProfileDlg::SetSingleProfileMode(bool SingleMode)
361 mySingleProfileMode = SingleMode;
364 bool HYDROGUI_ProfileDlg::GetSingleProfileMode() const
366 return mySingleProfileMode;
369 Handle(AIS_Trihedron) HYDROGUI_ProfileDlg::trihedron()
371 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
372 Handle(AIS_Trihedron) aTrihedron =
373 HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", viewer()->trihedronSize() ) );