Salome HOME
IPAL21354 Deleting of referenced father object doesn't work.
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_DeleteDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : GEOMToolsGUI_DeleteDlg.cxx
24 // Author : Dmitry Matveitchev, Open CASCADE S.A.S.
25 //
26 #include "GEOMToolsGUI_DeleteDlg.h"
27
28 #include <QLabel>
29 #include <QPushButton>
30 #include <QTextBrowser>
31 #include <QStringList>
32 #include <QGridLayout>
33 #include <SUIT_MessageBox.h>
34  
35 static bool isEntryLess( const QString& e1, const QString& e2 )
36 {
37   QStringList el1 = e1.split(":");
38   QStringList el2 = e2.split(":");
39   int e1c = el1.count(), e2c = el2.count();
40   for ( int i = 0; i < e1c && i < e2c; i++ ) {
41     int id1 = el1[i].toInt();
42     int id2 = el2[i].toInt();
43     if ( id1 < id2 ) return true;
44     else if ( id2 < id1 ) return false;
45   }
46   return el1.count() < el2.count();
47 }
48
49 static QStringList objectsToNames( const QMap<QString, QString>& objects )
50 {
51   QStringList entries;
52   for ( QMap<QString, QString>::ConstIterator it = objects.begin(); it != objects.end(); ++it ) {
53     QString entry = it.key();
54     QStringList::Iterator iter;
55     bool added = false;
56     for ( iter = entries.begin(); iter != entries.end() && !added; ++iter ) {
57       if ( isEntryLess( entry, *iter ) ) {
58         entries.insert( iter, entry );
59         added = true;
60       }
61     }
62     if ( !added ) 
63       entries.append( entry );
64   }
65   QStringList names;
66   for ( int i = 0; i < entries.count(); i++ ) {
67     int level = entries[i].count(":")-3;
68     names.append( QString( level*2, ' ' ) + objects[ entries[i] ] );
69   }
70   return names;
71 }
72
73 /*!
74   \brief Constructor.
75   \param parent parent widget
76 */
77 GEOMToolsGUI_DeleteDlg::GEOMToolsGUI_DeleteDlg( QWidget* parent, 
78                                                 const QMap<QString, QString>& objects, 
79                                                 bool deleteAll )
80 : QDialog( parent )
81 {
82   setModal( true );
83   setObjectName( "GEOMToolsGUI_DeleteDlg" );
84
85   setWindowTitle( tr( "GEOM_DELETE_OBJECTS" ) );
86   setSizeGripEnabled( true );
87
88   QGridLayout* topLayout = new QGridLayout( this );
89
90   topLayout->setSpacing( 6 );
91   topLayout->setMargin( 11 );
92
93   QLabel* pix = new QLabel( this );
94   pix->setPixmap( SUIT_MessageBox::standardIcon( QMessageBox::Question ) );
95   pix->setScaledContents( false );
96   pix->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
97   topLayout->addWidget( pix, 0, 0, 1, 1 );
98
99   QLabel* lab = new QLabel( this );
100   lab->setAlignment( Qt::AlignCenter );
101   topLayout->addWidget( lab, 0, 1, 1, 1 );
102
103   if ( !deleteAll ) {
104     lab->setText( tr( "GEOM_REALLY_DELETE" ).arg( objects.count() ) );
105     QTextBrowser* viewer = new QTextBrowser( this );
106     viewer->setText( QString( " - %1" ).arg( objectsToNames( objects ).join( "\n - " ) ) );
107     topLayout->addWidget( viewer,    1, 0, 1, 2 );
108   }
109   else {
110     lab->setText( tr( "GEOM_REALLY_DELETE_ALL" ) );
111   }
112
113   QPushButton* buttonYes = new QPushButton( tr( "GEOM_BUT_YES" ), this );
114   QPushButton* buttonNo  = new QPushButton( tr( "GEOM_BUT_NO" ),  this );
115   QHBoxLayout* btnLayout = new QHBoxLayout;
116   btnLayout->setMargin( 0 );
117   btnLayout->setSpacing( 6 );
118   btnLayout->addWidget( buttonYes );
119   btnLayout->addSpacing( 10 );
120   btnLayout->addStretch();
121   btnLayout->addWidget( buttonNo );
122   int rc = topLayout->rowCount();
123   topLayout->addLayout( btnLayout, rc, 0, 1, 2 );
124
125   /* signals and slots connections */
126   connect( buttonYes, SIGNAL( clicked() ), this, SLOT( accept() ) );
127   connect( buttonNo,  SIGNAL( clicked() ), this, SLOT( reject() ) );
128 }
129
130 GEOMToolsGUI_DeleteDlg::~GEOMToolsGUI_DeleteDlg()
131 {
132 }