Salome HOME
22fe270418649637395b719f8f9d8b17ddcbb629
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_DeleteDlg.cxx
1 // Copyright (C) 2007-2023  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 #include <iostream>
34
35 namespace
36 {
37   struct NaturalCompare
38   {
39     bool operator () (const std::string& s1, const std::string& s2) const
40     {
41       // hope the entries are never empty and never equal
42       int diff21 = 0;
43       const char* p1 = s1.c_str();
44       const char* p2 = s2.c_str();
45       for ( ; ; ++p1, ++p2 )
46       {
47         if ( *p1 == *p2 )
48         {
49           if ( diff21 && !*p1 ) // both numbers ends
50             return diff21 > 0;
51           continue;
52         }
53         // different chars
54         bool d1 = isdigit( *p1 );
55         bool d2 = isdigit( *p2 );
56         if ( d1 != d2 ) // one number is shorter then another
57           return d2; // is s1 shorter?
58         if ( !d1 && diff21 ) // both numbers ends
59           return diff21 > 0;
60         if ( !diff21 ) // remember the first difference
61           diff21 = *p2 - *p1;
62       }
63       return diff21 > 0;
64     }
65   };
66 }
67
68 static QStringList objectsToNames( const QMap<QString, QString>& objects )
69 {
70   typedef std::map< std::string, QString, NaturalCompare > TSortMap;
71   TSortMap sortedByEntry;
72   for ( QMap<QString, QString>::ConstIterator it = objects.begin(); it != objects.end(); ++it )
73     sortedByEntry.insert( sortedByEntry.end(), std::make_pair( it.key().toStdString(), it.value() ));
74
75   QStringList names;
76   for ( TSortMap::iterator it = sortedByEntry.begin(); it != sortedByEntry.end(); ++it )
77   {
78     const std::string& entry = it->first;
79     int level = std::count( entry.begin(), entry.end(), ':' ) - 3;
80     names.append( QString( level * 2, ' ' ) + it->second );
81   }
82   return names;
83 }
84
85 /*!
86   \brief Constructor.
87   \param parent parent widget
88   \param objects map of objects names/objects
89   \param deleteAll if True, delete all objects
90 */
91 GEOMToolsGUI_DeleteDlg::GEOMToolsGUI_DeleteDlg( QWidget* parent, 
92                                                 const QMap<QString, QString>& objects, 
93                                                 bool deleteAll )
94 : QDialog( parent )
95 {
96   setModal( true );
97   setObjectName( "GEOMToolsGUI_DeleteDlg" );
98
99   setWindowTitle( tr( "GEOM_DELETE_OBJECTS" ) );
100   setSizeGripEnabled( true );
101
102   QGridLayout* topLayout = new QGridLayout( this );
103
104   topLayout->setSpacing( 6 );
105   topLayout->setMargin( 11 );
106
107   QLabel* pix = new QLabel( this );
108   pix->setPixmap( SUIT_MessageBox::standardIcon( QMessageBox::Question ) );
109   pix->setScaledContents( false );
110   pix->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
111   topLayout->addWidget( pix, 0, 0, 1, 1 );
112
113   QLabel* lab = new QLabel( this );
114   lab->setAlignment( Qt::AlignCenter );
115   topLayout->addWidget( lab, 0, 1, 1, 1 );
116
117   if ( !deleteAll ) {
118     lab->setText( tr( "GEOM_REALLY_DELETE" ).arg( objects.count() ) );
119     QTextBrowser* viewer = new QTextBrowser( this );
120     viewer->setText( QString( " - %1" ).arg( objectsToNames( objects ).join( "\n - " ) ) );
121     topLayout->addWidget( viewer,    1, 0, 1, 2 );
122   }
123   else {
124     lab->setText( tr( "GEOM_REALLY_DELETE_ALL" ) );
125   }
126
127   QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Yes
128                                  | QDialogButtonBox::No);
129   int rc = topLayout->rowCount();
130   topLayout->addWidget( buttonBox, rc, 1, 1, 1 );
131
132   connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
133   connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
134 }
135
136 GEOMToolsGUI_DeleteDlg::~GEOMToolsGUI_DeleteDlg()
137 {
138 }