Salome HOME
Bug 0020413: Dump file has many GetMainShape instructions.
[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.h>
29 #include <qpushbutton.h>
30 #include <qtextbrowser.h>
31 #include <qstringlist.h>
32 #include <qlayout.h>
33 #include <qmessagebox.h>
34 #include <SUIT_MessageBox.h>
35
36 static bool isEntryLess( const QString& e1, const QString& e2 )
37 {
38   QStringList el1 = QStringList::split(":", e1);
39   QStringList el2 = QStringList::split(":", e2);
40   int e1c = el1.count(), e2c = el2.count();
41   for ( int i = 0; i < e1c && i < e2c; i++ ) {
42     int id1 = el1[i].toInt();
43     int id2 = el2[i].toInt();
44     if ( id1 < id2 ) return true;
45     else if ( id2 < id1 ) return false;
46   }
47   return el1.count() < el2.count();
48 }
49
50 static QStringList objectsToNames( const QMap<QString, QString>& objects )
51 {
52   QStringList entries;
53   for ( QMap<QString, QString>::ConstIterator cit = objects.begin(); cit != objects.end(); ++cit ) {
54     QString entry = cit.key();
55     QStringList::Iterator it;
56     bool added = false;
57     for ( it = entries.begin(); it != entries.end() && !added; ++it ) {
58       if ( isEntryLess( entry, *it ) ) {
59         entries.insert( it, entry );
60         added = true;
61       }
62     }
63     if ( !added ) 
64       entries.append( entry );
65   }
66   QStringList names;
67   for ( int i = 0; i < entries.count(); i++ ) {
68     int level = entries[i].contains(":")-3;
69     QString prefix; prefix.fill( ' ', level*2 );
70     names.append( prefix + objects[ entries[i] ] );
71   }
72   return names;
73 }
74
75 /*!
76 \brief Constructor.
77 \param parent parent widget
78 */
79 GEOMToolsGUI_DeleteDlg::GEOMToolsGUI_DeleteDlg( QWidget* parent, 
80                                                const QMap<QString, QString>& objects, 
81                                                bool deleteAll )
82                                                : QDialog( parent, "GEOMToolsGUI_DeleteDlg", true )
83 {
84   setCaption( tr( "GEOM_DELETE_OBJECTS" ) );
85   setSizeGripEnabled( true );
86
87   QGridLayout* topLayout = new QGridLayout( this );
88
89   topLayout->setSpacing( 6 );
90   topLayout->setMargin( 11 );
91
92   QLabel* pix = new QLabel( this );
93   pix->setPixmap( QMessageBox::standardIcon( QMessageBox::Question ) );
94   pix->setScaledContents( false );
95   pix->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
96   topLayout->addWidget( pix, 0, 0 );
97
98   QLabel* lab = new QLabel( this );
99   lab->setAlignment( Qt::AlignCenter );
100   topLayout->addWidget( lab, 0, 1 );
101
102   if ( !deleteAll ) {
103     lab->setText( tr( "GEOM_REALLY_DELETE" ).arg( objects.count() ) );
104     QTextBrowser* viewer = new QTextBrowser( this );
105     viewer->setText( QString( " - %1" ).arg( objectsToNames( objects ).join( "\n - " ) ) );
106     topLayout->addMultiCellWidget( viewer,    1, 1, 0, 1 );
107   }
108   else {
109     lab->setText( tr( "GEOM_REALLY_DELETE_ALL" ) );
110   }
111
112   QPushButton* buttonYes = new QPushButton( tr( "GEOM_BUT_YES" ), this );
113   QPushButton* buttonNo  = new QPushButton( tr( "GEOM_BUT_NO" ),  this );
114   QHBoxLayout* btnLayout = new QHBoxLayout;
115   btnLayout->setMargin( 0 );
116   btnLayout->setSpacing( 6 );
117   btnLayout->addWidget( buttonYes );
118   btnLayout->addSpacing( 10 );
119   btnLayout->addStretch();
120   btnLayout->addWidget( buttonNo );
121   int rc = topLayout->numRows();
122   topLayout->addMultiCellLayout( btnLayout, rc, rc, 0, 1 );
123
124   /* signals and slots connections */
125   connect( buttonYes, SIGNAL( clicked() ), this, SLOT( accept() ) );
126   connect( buttonNo,  SIGNAL( clicked() ), this, SLOT( reject() ) );
127 }
128
129 GEOMToolsGUI_DeleteDlg::~GEOMToolsGUI_DeleteDlg()
130 {
131 }