Salome HOME
Synchronize adm files
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_DeleteDlg.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // GEOM GEOMGUI : GUI for Geometry component
21 // File   : GEOMToolsGUI_DeleteDlg.cxx
22 // Author : Dmitry Matveitchev, Open CASCADE S.A.S.
23 //
24 #include "GEOMToolsGUI_DeleteDlg.h"
25
26 #include <QLabel>
27 #include <QDialogButtonBox>
28 #include <QTextBrowser>
29 #include <QStringList>
30 #include <QGridLayout>
31 #include <SUIT_MessageBox.h>
32  
33 static bool isEntryLess( const QString& e1, const QString& e2 )
34 {
35   QStringList el1 = e1.split(":");
36   QStringList el2 = e2.split(":");
37   int e1c = el1.count(), e2c = el2.count();
38   for ( int i = 0; i < e1c && i < e2c; i++ ) {
39     int id1 = el1[i].toInt();
40     int id2 = el2[i].toInt();
41     if ( id1 < id2 ) return true;
42     else if ( id2 < id1 ) return false;
43   }
44   return el1.count() < el2.count();
45 }
46
47 static QStringList objectsToNames( const QMap<QString, QString>& objects )
48 {
49   QStringList entries;
50   for ( QMap<QString, QString>::ConstIterator it = objects.begin(); it != objects.end(); ++it ) {
51     QString entry = it.key();
52     QStringList::Iterator iter;
53     bool added = false;
54     for ( iter = entries.begin(); iter != entries.end() && !added; ++iter ) {
55       if ( isEntryLess( entry, *iter ) ) {
56         entries.insert( iter, entry );
57         added = true;
58       }
59     }
60     if ( !added ) 
61       entries.append( entry );
62   }
63   QStringList names;
64   for ( int i = 0; i < entries.count(); i++ ) {
65     int level = entries[i].count(":")-3;
66     names.append( QString( level*2, ' ' ) + objects[ entries[i] ] );
67   }
68   return names;
69 }
70
71 /*!
72   \brief Constructor.
73   \param parent parent widget
74   \param objects map of objects names/objects
75   \param deleteAll if True, delete all objects
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   QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Yes
114                                  | QDialogButtonBox::No);
115   int rc = topLayout->rowCount();
116   topLayout->addWidget( buttonBox, rc, 1, 1, 1 );
117
118   connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
119   connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
120 }
121
122 GEOMToolsGUI_DeleteDlg::~GEOMToolsGUI_DeleteDlg()
123 {
124 }