smesh/src/SMESHGUI/SMESHGUI_SingleEditDlg.cxx

566 lines
17 KiB
C++
Raw Normal View History

2004-12-01 15:48:31 +05:00
// SMESH SMESHGUI : GUI for SMESH component
//
// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
2004-12-01 15:48:31 +05:00
//
//
//
// File : SMESHGUI_SingleEditDlg.cxx
// Author : Sergey LITONIN
// Module : SMESH
#include "SMESHGUI_SingleEditDlg.h"
#include "SMESHGUI.h"
#include "SMESHGUI_Utils.h"
#include "SMESHGUI_VTKUtils.h"
#include "SMESHGUI_MeshUtils.h"
#include "SMESHGUI_SpinBox.h"
#include "SMESH_Actor.h"
#include "SMDS_Mesh.hxx"
#include "LightApp_SelectionMgr.h"
#include "SUIT_ResourceMgr.h"
#include "SUIT_MessageBox.h"
#include "SUIT_Desktop.h"
#include "SVTK_Selector.h"
#include "SVTK_ViewWindow.h"
#include "SALOME_ListIO.hxx"
#include "utilities.h"
// OCCT Includes
#include <TColStd_MapOfInteger.hxx>
#include <TColStd_IndexedMapOfInteger.hxx>
// QT Includes
2004-12-01 15:48:31 +05:00
#include <qframe.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qmessagebox.h>
#include <qvalidator.h>
#define SPACING 5
#define MARGIN 10
/*!
* Class : SMESHGUI_DiagValidator
* Description : validate munual input of edge like "id1-id2"
*/
2004-12-01 15:48:31 +05:00
class SMESHGUI_DiagValidator: public QValidator
{
public:
SMESHGUI_DiagValidator (QWidget * parent, const char * name = 0):
2004-12-01 15:48:31 +05:00
QValidator(parent,name) {}
State validate (QString & text, int & pos) const
2004-12-01 15:48:31 +05:00
{
text.stripWhiteSpace();
text.replace(QRegExp("[^0-9]+"), "-");
if (text == "-")
2004-12-01 15:48:31 +05:00
text = "";
int ind = text.find(QRegExp("-[0-9]+-"));
if (ind > 0) { // leave only two ids
ind = text.find('-', ind + 1);
if (ind > 0)
text.truncate(ind);
2004-12-01 15:48:31 +05:00
}
if (pos > text.length())
2004-12-01 15:48:31 +05:00
pos = text.length();
return Acceptable;
}
};
/*!
* Class : SMESHGUI_SingleEditDlg
* Description : Inversion of the diagonal of a pseudo-quadrangle formed by
* 2 neighboring triangles with 1 common edge
*/
2004-12-01 15:48:31 +05:00
//=======================================================================
// name : SMESHGUI_SingleEditDlg()
2004-12-01 15:48:31 +05:00
// Purpose : Constructor
//=======================================================================
SMESHGUI_SingleEditDlg
::SMESHGUI_SingleEditDlg(SMESHGUI* theModule,
const char* theName):
QDialog(SMESH::GetDesktop(theModule),
theName,
false,
WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
mySMESHGUI(theModule)
2004-12-01 15:48:31 +05:00
{
QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
2004-12-01 15:48:31 +05:00
QFrame* aMainFrame = createMainFrame (this);
QFrame* aBtnFrame = createButtonFrame(this);
2004-12-01 15:48:31 +05:00
aDlgLay->addWidget(aMainFrame);
aDlgLay->addWidget(aBtnFrame);
2004-12-01 15:48:31 +05:00
aDlgLay->setStretchFactor(aMainFrame, 1);
2004-12-01 15:48:31 +05:00
Init();
2004-12-01 15:48:31 +05:00
}
//=======================================================================
// name : createMainFrame()
2004-12-01 15:48:31 +05:00
// Purpose : Create frame containing dialog's input fields
//=======================================================================
QFrame* SMESHGUI_SingleEditDlg::createMainFrame (QWidget* theParent)
2004-12-01 15:48:31 +05:00
{
QGroupBox* aMainGrp = new QGroupBox(1, Qt::Vertical, tr("EDGE_BETWEEN"), theParent);
2004-12-01 15:48:31 +05:00
2005-06-08 16:45:19 +06:00
QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
new QLabel(tr("SMESH_EDGE"), aMainGrp);
(new QPushButton(aMainGrp))->setPixmap(aPix);
myEdge = new QLineEdit(aMainGrp);
myEdge->setValidator(new SMESHGUI_DiagValidator(this, "validator"));
2004-12-01 15:48:31 +05:00
return aMainGrp;
}
//=======================================================================
// name : createButtonFrame()
2004-12-01 15:48:31 +05:00
// Purpose : Create frame containing buttons
//=======================================================================
QFrame* SMESHGUI_SingleEditDlg::createButtonFrame (QWidget* theParent)
2004-12-01 15:48:31 +05:00
{
QFrame* aFrame = new QFrame(theParent);
aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
2004-12-01 15:48:31 +05:00
myOkBtn = new QPushButton(tr("SMESH_BUT_OK" ), aFrame);
myApplyBtn = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
myCloseBtn = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
2004-12-01 15:48:31 +05:00
QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
2004-12-01 15:48:31 +05:00
QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
2004-12-01 15:48:31 +05:00
aLay->addWidget(myOkBtn);
aLay->addWidget(myApplyBtn);
aLay->addItem(aSpacer);
aLay->addWidget(myCloseBtn);
2004-12-01 15:48:31 +05:00
return aFrame;
}
//=======================================================================
// name : isValid()
2004-12-01 15:48:31 +05:00
// Purpose : Verify validity of input data
//=======================================================================
bool SMESHGUI_SingleEditDlg::isValid (const bool theMess) const
2004-12-01 15:48:31 +05:00
{
int id1, id2;
return getNodeIds(myEdge->text(), id1, id2);
2004-12-01 15:48:31 +05:00
}
//=======================================================================
// name : getNodeIds()
2004-12-01 15:48:31 +05:00
// Purpose : Retrieve node ids from string
//=======================================================================
bool SMESHGUI_SingleEditDlg::getNodeIds (const QString& theStr,
int& theId1, int& theId2) const
2004-12-01 15:48:31 +05:00
{
if (!theStr.contains('-'))
2004-12-01 15:48:31 +05:00
return false;
bool ok1, ok2;
QString str1 = theStr.section('-', 0, 0, QString::SectionSkipEmpty);
QString str2 = theStr.section('-', 1, 1, QString::SectionSkipEmpty);
theId1 = str1.toInt(&ok1);
theId2 = str2.toInt(&ok2);
2004-12-01 15:48:31 +05:00
return ok1 & ok2;
}
//=======================================================================
// name : ~SMESHGUI_SingleEditDlg()
2004-12-01 15:48:31 +05:00
// Purpose : Destructor
//=======================================================================
SMESHGUI_SingleEditDlg::~SMESHGUI_SingleEditDlg()
{
}
//=======================================================================
// name : Init()
2004-12-01 15:48:31 +05:00
// Purpose : Init dialog fields, connect signals and slots, show dialog
//=======================================================================
void SMESHGUI_SingleEditDlg::Init()
2004-12-01 15:48:31 +05:00
{
mySMESHGUI->SetActiveDialogBox((QDialog*)this);
2004-12-01 15:48:31 +05:00
myBusy = false;
myActor = 0;
2004-12-01 15:48:31 +05:00
// main buttons
connect(myOkBtn, SIGNAL(clicked()), SLOT(onOk()));
connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
2004-12-01 15:48:31 +05:00
// selection and SMESHGUI
connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
connect(myEdge, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
2004-12-01 15:48:31 +05:00
myOkBtn->setEnabled(false);
myApplyBtn->setEnabled(false);
setEnabled(true);
2004-12-01 15:48:31 +05:00
int x, y;
mySMESHGUI->DefineDlgPosition(this, x, y);
this->move(x, y);
this->show();
2004-12-01 15:48:31 +05:00
// set selection mode
if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
aViewWindow->SetSelectionMode(EdgeOfCellSelection);
2004-12-01 15:48:31 +05:00
onSelectionDone();
}
//=======================================================================
// name : onOk()
// Purpose : SLOT called when "Ok" button pressed.
2004-12-01 15:48:31 +05:00
// Assign filters VTK viewer and close dialog
//=======================================================================
void SMESHGUI_SingleEditDlg::onOk()
{
if (onApply())
2004-12-01 15:48:31 +05:00
onClose();
}
//=======================================================================
// name : onClose()
2004-12-01 15:48:31 +05:00
// Purpose : SLOT called when "Close" button pressed. Close dialog
//=======================================================================
void SMESHGUI_SingleEditDlg::onClose()
{
if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
aViewWindow->SetSelectionMode(ActorSelection);
mySelectionMgr->clearSelected();
disconnect(mySelectionMgr, 0, this, 0);
disconnect(mySMESHGUI, 0, this, 0);
mySMESHGUI->ResetState();
2004-12-01 15:48:31 +05:00
reject();
}
//=======================================================================
//function : findTriangles()
2004-12-01 15:48:31 +05:00
//purpose : find triangles sharing theNode1-theNode2 link
// THIS IS A PIECE OF SMESH_MeshEditor.cxx
// TO DO: make it available in SMDS for ex.
2004-12-01 15:48:31 +05:00
//=======================================================================
static bool findTriangles (const SMDS_MeshNode * theNode1,
const SMDS_MeshNode * theNode2,
const SMDS_MeshElement*& theTria1,
const SMDS_MeshElement*& theTria2)
2004-12-01 15:48:31 +05:00
{
if (!theNode1 || !theNode2) return false;
2004-12-01 15:48:31 +05:00
theTria1 = theTria2 = 0;
set< const SMDS_MeshElement* > emap;
SMDS_ElemIteratorPtr it = theNode1->GetInverseElementIterator();
while (it->more()) {
const SMDS_MeshElement* elem = it->next();
if (elem->GetType() == SMDSAbs_Face && elem->NbNodes() == 3)
emap.insert(elem);
2004-12-01 15:48:31 +05:00
}
it = theNode2->GetInverseElementIterator();
while (it->more()) {
const SMDS_MeshElement* elem = it->next();
if (elem->GetType() == SMDSAbs_Face &&
emap.find(elem) != emap.end())
if (theTria1) {
2004-12-01 15:48:31 +05:00
theTria2 = elem;
break;
} else {
theTria1 = elem;
}
}
return (theTria1 && theTria2);
2004-12-01 15:48:31 +05:00
}
//=======================================================================
//function : onTextChange()
//purpose :
2004-12-01 15:48:31 +05:00
//=======================================================================
void SMESHGUI_SingleEditDlg::onTextChange (const QString& theNewText)
2004-12-01 15:48:31 +05:00
{
if (myBusy) return;
2004-12-01 15:48:31 +05:00
myOkBtn->setEnabled(false);
myApplyBtn->setEnabled(false);
2004-12-01 15:48:31 +05:00
// hilight entered edge
if(myActor){
if(SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh()){
2004-12-01 15:48:31 +05:00
myBusy = true; // block onSelectionDone()
Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
SALOME_ListIO aList;
aList.Append(anIO);
mySelectionMgr->setSelectedObjects(aList,false);
TColStd_IndexedMapOfInteger selectedIndices;
TColStd_MapOfInteger newIndices;
mySelector->GetIndex(anIO,selectedIndices);
2004-12-01 15:48:31 +05:00
myBusy = false;
QStringList aListId = QStringList::split("-", theNewText, false);
if (aListId.count() != 2)
return;
int i;
bool allOk = true;
const SMDS_MeshNode* a2Nodes[2];
for (i = 0; i < aListId.count(); i++) {
if(const SMDS_MeshNode *aNode = aMesh->FindNode(aListId[ i ].toInt()))
a2Nodes[ i ] = aNode;
else
allOk = false;
}
// find a triangle and an edge nb
const SMDS_MeshElement* tria[2];
allOk &= a2Nodes[0] != a2Nodes[1] && findTriangles(a2Nodes[0],a2Nodes[1],tria[0],tria[1]);
myBusy = true; // block onSelectionDone()
if(allOk)
{
newIndices.Add(tria[0]->GetID());
const SMDS_MeshNode* a3Nodes [3];
SMDS_ElemIteratorPtr it;
int edgeInd = 2;
for (i = 0, it = tria[0]->nodesIterator(); it->more(); i++) {
a3Nodes[ i ] = static_cast<const SMDS_MeshNode*>(it->next());
if (i > 0) {
allOk = (a3Nodes[ i ] == a2Nodes[ 0 ] && a3Nodes[ i - 1] == a2Nodes[ 1 ]) ||
(a3Nodes[ i ] == a2Nodes[ 1 ] && a3Nodes[ i - 1] == a2Nodes[ 0 ]);
if (allOk) {
edgeInd = i - 1;
break;
}
}
}
newIndices.Add(-edgeInd-1);
myOkBtn->setEnabled(true);
myApplyBtn->setEnabled(true);
}
mySelector->AddOrRemoveIndex(anIO,newIndices, false);
SMESH::GetViewWindow(mySMESHGUI)->highlight( anIO, true, true );
myBusy = false;
2004-12-01 15:48:31 +05:00
}
}
}
2004-12-01 15:48:31 +05:00
//=======================================================================
// name : onSelectionDone()
2004-12-01 15:48:31 +05:00
// Purpose : SLOT called when selection changed
//=======================================================================
void SMESHGUI_SingleEditDlg::onSelectionDone()
{
if (myBusy) return;
2004-12-01 15:48:31 +05:00
int anId1 = 0, anId2 = 0;
myOkBtn->setEnabled(false);
myApplyBtn->setEnabled(false);
2004-12-01 15:48:31 +05:00
SALOME_ListIO aList;
mySelectionMgr->selectedObjects(aList);
if (aList.Extent() != 1) {
2004-12-01 15:48:31 +05:00
myEdge->clear();
return;
}
Handle(SALOME_InteractiveObject) anIO = aList.First();
myActor = SMESH::FindActorByEntry(anIO->getEntry());
if(myActor){
TVisualObjPtr aVisualObj = myActor->GetObject();
if(SMDS_Mesh* aMesh = aVisualObj->GetMesh())
{
const SMDS_MeshElement* tria[2];
if( SMESH::GetEdgeNodes( mySelector, aVisualObj, anId1, anId2 ) >= 1 &&
findTriangles( aMesh->FindNode( anId1 ), aMesh->FindNode( anId2 ), tria[0],tria[1] ) )
{
QString aText = QString("%1-%2").arg(anId1).arg(anId2);
myBusy = true;
myEdge->setText(aText);
myBusy = false;
myOkBtn->setEnabled(true);
myApplyBtn->setEnabled(true);
}
else
{
myEdge->clear();
}
2004-12-01 15:48:31 +05:00
}
}
}
//=======================================================================
// name : onDeactivate()
2004-12-01 15:48:31 +05:00
// Purpose : SLOT called when dialog must be deativated
//=======================================================================
void SMESHGUI_SingleEditDlg::onDeactivate()
{
setEnabled(false);
2004-12-01 15:48:31 +05:00
}
//=======================================================================
// name : enterEvent()
2004-12-01 15:48:31 +05:00
// Purpose : Event filter
//=======================================================================
void SMESHGUI_SingleEditDlg::enterEvent (QEvent*)
2004-12-01 15:48:31 +05:00
{
if (!isEnabled()) {
mySMESHGUI->EmitSignalDeactivateDialog();
if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
aViewWindow->SetSelectionMode(EdgeOfCellSelection);
setEnabled(true);
2004-12-01 15:48:31 +05:00
}
}
//=================================================================================
// function : closeEvent()
// purpose :
//=================================================================================
void SMESHGUI_SingleEditDlg::closeEvent (QCloseEvent*)
2004-12-01 15:48:31 +05:00
{
onClose();
2004-12-01 15:48:31 +05:00
}
2004-12-01 15:48:31 +05:00
//=======================================================================
//function : hideEvent()
2004-12-01 15:48:31 +05:00
//purpose : caused by ESC key
//=======================================================================
void SMESHGUI_SingleEditDlg::hideEvent (QHideEvent*)
2004-12-01 15:48:31 +05:00
{
if (!isMinimized())
2004-12-01 15:48:31 +05:00
onClose();
}
//=================================================================================
// function : onApply()
2004-12-01 15:48:31 +05:00
// purpose : SLOT. Called when apply button is pressed
//=================================================================================
bool SMESHGUI_SingleEditDlg::onApply()
{
if (mySMESHGUI->isActiveStudyLocked())
2004-12-01 15:48:31 +05:00
return false;
// verify validity of input data
if (!isValid(true))
2004-12-01 15:48:31 +05:00
return false;
// get mesh, actor and nodes
SALOME_ListIO aList;
mySelectionMgr->selectedObjects(aList);
2004-12-01 15:48:31 +05:00
SMESH::SMESH_Mesh_var aMesh = SMESH::GetMeshByIO(aList.First());
if (aMesh->_is_nil()) {
SUIT_MessageBox::info1(SMESH::GetDesktop(mySMESHGUI),
tr("SMESH_ERROR"),
tr("SMESHG_NO_MESH"),
tr("SMESH_BUT_OK"));
2004-12-01 15:48:31 +05:00
return false;
}
SMESH::SMESH_MeshEditor_var aMeshEditor = aMesh->GetMeshEditor();
int anId1= 0, anId2 = 0;
if (aMeshEditor->_is_nil() || !getNodeIds(myEdge->text(), anId1, anId2))
2004-12-01 15:48:31 +05:00
return false;
// perform operation
bool aResult = process(aMeshEditor.in(), anId1, anId2);
2004-12-01 15:48:31 +05:00
// update actor
if (aResult) {
mySelector->ClearIndex();
mySelectionMgr->setSelectedObjects(aList, false);
onSelectionDone();
2004-12-01 15:48:31 +05:00
SMESH::UpdateView();
}
return aResult;
}
/*!
* Class : SMESHGUI_TrianglesInversionDlg
* Description : Inversion of the diagonal of a pseudo-quadrangle formed by
* 2 neighboring triangles with 1 common edge
*/
2004-12-01 15:48:31 +05:00
SMESHGUI_TrianglesInversionDlg
::SMESHGUI_TrianglesInversionDlg(SMESHGUI* theModule,
const char* theName)
: SMESHGUI_SingleEditDlg(theModule,theName)
2004-12-01 15:48:31 +05:00
{
setCaption(tr("CAPTION"));
2004-12-01 15:48:31 +05:00
}
SMESHGUI_TrianglesInversionDlg::~SMESHGUI_TrianglesInversionDlg()
{
}
bool SMESHGUI_TrianglesInversionDlg::process (SMESH::SMESH_MeshEditor_ptr theMeshEditor,
const int theId1, const int theId2)
2004-12-01 15:48:31 +05:00
{
return theMeshEditor->InverseDiag(theId1, theId2);
2004-12-01 15:48:31 +05:00
}
/*!
* Class : SMESHGUI_UnionOfTwoTrianglesDlg
* Description : Construction of a quadrangle by deletion of the
* common border of 2 neighboring triangles
*/
2004-12-01 15:48:31 +05:00
SMESHGUI_UnionOfTwoTrianglesDlg
::SMESHGUI_UnionOfTwoTrianglesDlg(SMESHGUI* theModule,
const char* theName)
: SMESHGUI_SingleEditDlg(theModule,theName)
2004-12-01 15:48:31 +05:00
{
setCaption(tr("CAPTION"));
2004-12-01 15:48:31 +05:00
}
SMESHGUI_UnionOfTwoTrianglesDlg::~SMESHGUI_UnionOfTwoTrianglesDlg()
{
}
bool SMESHGUI_UnionOfTwoTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theMeshEditor,
const int theId1, const int theId2)
2004-12-01 15:48:31 +05:00
{
return theMeshEditor->DeleteDiag(theId1, theId2);
2004-12-01 15:48:31 +05:00
}