Back to index

salome-gui  6.5.0
SUIT_MessageBox.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SUIT_MessageBox.h"
00024 
00025 #include "SUIT_OverrideCursor.h"
00026 
00027 #include <QMessageBox>
00028 #include <QPushButton>
00029 #include <QApplication>
00030 #include <QString>
00031 
00032 #include <stdarg.h>
00033 
00103 SUIT_MessageBox::SUIT_MessageBox( QWidget* parent )
00104 : QMessageBox( parent )
00105 {
00106 }
00107 
00117 SUIT_MessageBox::SUIT_MessageBox( Icon icon, const QString& title, const QString& text,
00118                                   StandardButtons buttons, QWidget* parent, Qt::WindowFlags f )
00119 : QMessageBox( icon, title, text, buttons, parent, f )
00120 {
00121 }
00122 
00126 SUIT_MessageBox::~SUIT_MessageBox()
00127 {
00128 }
00129 
00135 QString SUIT_MessageBox::buttonText( StandardButton btn ) const
00136 {
00137   QString res;
00138   QAbstractButton* b = button( btn );
00139   if ( b )
00140     res = b->text();
00141   return res;
00142 }
00143 
00149 void SUIT_MessageBox::setButtonText( StandardButton btn, const QString& text )
00150 {
00151   QAbstractButton* b = button( btn );
00152   if ( b )
00153     b->setText( text );
00154 }
00155 
00165 SUIT_MessageBox::StandardButton SUIT_MessageBox::critical( QWidget* parent, const QString& title, const QString& text,
00166                                                            StandardButtons buttons, StandardButton defaultButton )
00167 {
00168   SUIT_OverrideCursor cw( parent ? parent->cursor() : Qt::ArrowCursor );
00169   return QMessageBox::critical( parent, title, text, buttons, defaultButton );
00170 }
00171 
00181 SUIT_MessageBox::StandardButton SUIT_MessageBox::warning( QWidget* parent, const QString& title, const QString& text,
00182                                                           SUIT_MessageBox::StandardButtons buttons, StandardButton defaultButton )
00183 {
00184   SUIT_OverrideCursor cw( parent ? parent->cursor() : Qt::ArrowCursor );
00185   return QMessageBox::warning( parent, title, text, buttons, defaultButton );
00186 }
00187 
00197 SUIT_MessageBox::StandardButton SUIT_MessageBox::information( QWidget* parent, const QString& title, const QString& text,
00198                                                               StandardButtons buttons, StandardButton defaultButton )
00199 {
00200   SUIT_OverrideCursor cw( parent ? parent->cursor() : Qt::ArrowCursor );
00201   return QMessageBox::information( parent, title, text, buttons, defaultButton );
00202 }
00203 
00213 SUIT_MessageBox::StandardButton SUIT_MessageBox::question( QWidget* parent, const QString& title, const QString& text,
00214                                                            StandardButtons buttons, StandardButton defaultButton )
00215 {
00216   SUIT_OverrideCursor cw( parent ? parent->cursor() : Qt::ArrowCursor );
00217   return QMessageBox::question( parent, title, text, buttons, defaultButton );
00218 }
00219 
00233 int SUIT_MessageBox::critical( QWidget* parent, const QString& title, const QString& text,
00234                                const QString& button )
00235 {
00236   ButtonInfos lst;
00237   lst.append( ButtonInfo( 0, button ) );
00238   return messageBox( SUIT_MessageBox::Critical, parent, title, text, lst );
00239 }
00240 
00254 int SUIT_MessageBox::warning( QWidget* parent, const QString& title, const QString& text,
00255                               const QString& button )
00256 {
00257   ButtonInfos lst;
00258   lst.append( ButtonInfo( 0, button ) );
00259   return messageBox( SUIT_MessageBox::Warning, parent, title, text, lst );
00260 }
00261 
00275 int SUIT_MessageBox::information( QWidget* parent, const QString& title, const QString& text,
00276                                   const QString& button )
00277 {
00278   ButtonInfos lst;
00279   lst.append( ButtonInfo( 0, button ) );
00280   return messageBox( SUIT_MessageBox::Information, parent, title, text, lst );
00281 }
00282 
00301 int SUIT_MessageBox::question( QWidget* parent, const QString& title, const QString& text,
00302                                const QString& button )
00303 {
00304   ButtonInfos lst;
00305   lst.append( ButtonInfo( 0, button ) );
00306   return messageBox( SUIT_MessageBox::Question, parent, title, text, lst );
00307 }
00308 
00332 int SUIT_MessageBox::critical( QWidget* parent, const QString& title, const QString& text,
00333                                const QString& button1, const QString& button2,
00334                                const int defaultButton, const int escapeButton )
00335 {
00336   ButtonInfos lst;
00337   int id = 0;
00338   lst.append( ButtonInfo( id++, button1 ) );
00339   lst.append( ButtonInfo( id++, button2 ) );
00340   return messageBox( SUIT_MessageBox::Critical, parent, title, text, lst, 
00341                      defaultButton, escapeButton );
00342 }
00343 
00367 int SUIT_MessageBox::warning( QWidget* parent, const QString& title, const QString& text,
00368                               const QString& button1, const QString& button2,
00369                               const int defaultButton, const int escapeButton )
00370 {
00371   ButtonInfos lst;
00372   int id = 0;
00373   lst.append( ButtonInfo( id++, button1 ) );
00374   lst.append( ButtonInfo( id++, button2 ) );
00375   return messageBox( SUIT_MessageBox::Warning, parent, title, text, lst, 
00376                      defaultButton, escapeButton );
00377 }
00378 
00402 int SUIT_MessageBox::information( QWidget* parent, const QString& title, const QString& text,
00403                                   const QString& button1, const QString& button2,
00404                                   const int defaultButton, const int escapeButton )
00405 {
00406   ButtonInfos lst;
00407   int id = 0;
00408   lst.append( ButtonInfo( id++, button1 ) );
00409   lst.append( ButtonInfo( id++, button2 ) );
00410   return messageBox( SUIT_MessageBox::Information, parent, title, text, lst, 
00411                      defaultButton, escapeButton );
00412 }
00413 
00437 int SUIT_MessageBox::question( QWidget* parent, const QString& title, const QString& text,
00438                                const QString& button1, const QString& button2,
00439                                const int defaultButton, const int escapeButton )
00440 {
00441   ButtonInfos lst;
00442   int id = 0;
00443   lst.append( ButtonInfo( id++, button1 ) );
00444   lst.append( ButtonInfo( id++, button2 ) );
00445   return messageBox( SUIT_MessageBox::Question, parent, title, text, lst, 
00446                      defaultButton, escapeButton );
00447 }
00448 
00473 int SUIT_MessageBox::critical( QWidget* parent, const QString& title, const QString& text,
00474                                const QString& button1, const QString& button2, 
00475                                const QString& button3,
00476                                const int defaultButton, const int escapeButton )
00477 {
00478   ButtonInfos lst;
00479   int id = 0;
00480   lst.append( ButtonInfo( id++, button1 ) );
00481   lst.append( ButtonInfo( id++, button2 ) );
00482   lst.append( ButtonInfo( id++, button3 ) );
00483   return messageBox( SUIT_MessageBox::Critical, parent, title, text, lst, 
00484                      defaultButton, escapeButton );
00485 }
00486 
00511 int SUIT_MessageBox::warning( QWidget* parent, const QString& title, const QString& text,
00512                               const QString& button1, const QString& button2,
00513                               const QString& button3,
00514                               const int defaultButton, const int escapeButton )
00515 {
00516   ButtonInfos lst;
00517   int id = 0;
00518   lst.append( ButtonInfo( id++, button1 ) );
00519   lst.append( ButtonInfo( id++, button2 ) );
00520   lst.append( ButtonInfo( id++, button3 ) );
00521   return messageBox( SUIT_MessageBox::Warning, parent, title, text, lst, 
00522                      defaultButton, escapeButton );
00523 }
00524 
00549 int SUIT_MessageBox::information( QWidget* parent, const QString& title, const QString& text,
00550                                   const QString& button1, const QString& button2,
00551                                   const QString& button3,
00552                                   const int defaultButton, const int escapeButton )
00553 {
00554   ButtonInfos lst;
00555   int id = 0;
00556   lst.append( ButtonInfo( id++, button1 ) );
00557   lst.append( ButtonInfo( id++, button2 ) );
00558   lst.append( ButtonInfo( id++, button3 ) );
00559   return messageBox( SUIT_MessageBox::Information, parent, title, text, lst, 
00560                      defaultButton, escapeButton );
00561 }
00562 
00587 int SUIT_MessageBox::question( QWidget* parent, const QString& title, const QString& text,
00588                                const QString& button1, const QString& button2,
00589                                const QString& button3,
00590                                const int defaultButton, const int escapeButton )
00591 {
00592   ButtonInfos lst;
00593   int id = 0;
00594   lst.append( ButtonInfo( id++, button1 ) );
00595   lst.append( ButtonInfo( id++, button2 ) );
00596   lst.append( ButtonInfo( id++, button3 ) );
00597   return messageBox( SUIT_MessageBox::Question, parent, title, text, lst, 
00598                      defaultButton, escapeButton );
00599 }
00600 
00627 int SUIT_MessageBox::critical( QWidget* parent, const QString& title, const QString& text,
00628                                const QString& button1, const QString& button2, 
00629                                const QString& button3, const QString& button4,
00630                                const int defaultButton, const int escapeButton )
00631 {
00632   ButtonInfos lst;
00633   int id = 0;
00634   lst.append( ButtonInfo( id++, button1 ) );
00635   lst.append( ButtonInfo( id++, button2 ) );
00636   lst.append( ButtonInfo( id++, button3 ) );
00637   lst.append( ButtonInfo( id++, button4 ) );
00638   return messageBox( SUIT_MessageBox::Critical, parent, title, text, lst, 
00639                      defaultButton, escapeButton );
00640 }
00641 
00668 int SUIT_MessageBox::warning( QWidget* parent, const QString& title, const QString& text,
00669                               const QString& button1, const QString& button2, 
00670                               const QString& button3, const QString& button4,
00671                               const int defaultButton, const int escapeButton )
00672 {
00673   ButtonInfos lst;
00674   int id = 0;
00675   lst.append( ButtonInfo( id++, button1 ) );
00676   lst.append( ButtonInfo( id++, button2 ) );
00677   lst.append( ButtonInfo( id++, button3 ) );
00678   lst.append( ButtonInfo( id++, button4 ) );
00679   return messageBox( SUIT_MessageBox::Warning, parent, title, text, lst, 
00680                      defaultButton, escapeButton );
00681 }
00682 
00709 int SUIT_MessageBox::information( QWidget* parent, const QString& title, const QString& text,
00710                                   const QString& button1, const QString& button2, 
00711                                   const QString& button3, const QString& button4,
00712                                   const int defaultButton, const int escapeButton )
00713 {
00714   ButtonInfos lst;
00715   int id = 0;
00716   lst.append( ButtonInfo( id++, button1 ) );
00717   lst.append( ButtonInfo( id++, button2 ) );
00718   lst.append( ButtonInfo( id++, button3 ) );
00719   lst.append( ButtonInfo( id++, button4 ) );
00720   return messageBox( SUIT_MessageBox::Information, parent, title, text, lst, 
00721                      defaultButton, escapeButton );
00722 }
00723 
00750 int SUIT_MessageBox::question( QWidget* parent, const QString& title, const QString& text,
00751                                const QString& button1, const QString& button2, 
00752                                const QString& button3, const QString& button4,
00753                                const int defaultButton, const int escapeButton )
00754 {
00755   ButtonInfos lst;
00756   int id = 0;
00757   lst.append( ButtonInfo( id++, button1 ) );
00758   lst.append( ButtonInfo( id++, button2 ) );
00759   lst.append( ButtonInfo( id++, button3 ) );
00760   lst.append( ButtonInfo( id++, button4 ) );
00761   return messageBox( SUIT_MessageBox::Question, parent, title, text, lst, 
00762                      defaultButton, escapeButton );
00763 }
00764 
00792 int SUIT_MessageBox::critical( QWidget* parent, const QString& title, const QString& text, 
00793                                const int defaultButton, const int escapeButton, 
00794                                char* btn, ... )
00795 {
00796   va_list args;
00797   va_start( args, btn );
00798   return messageBox( SUIT_MessageBox::Critical, parent, title, text,
00799                      messageList( btn, args ),
00800                      defaultButton, escapeButton );
00801 }
00802 
00830 int SUIT_MessageBox::warning( QWidget* parent, const QString& title, const QString& text, 
00831                               const int defaultButton, const int escapeButton, 
00832                               char* btn, ... )
00833 {
00834   va_list args;
00835   va_start( args, btn );
00836   return messageBox( SUIT_MessageBox::Warning, parent, title, text,
00837                      messageList( btn, args ),
00838                      defaultButton, escapeButton );
00839 }
00840 
00868 int SUIT_MessageBox::information( QWidget* parent, const QString& title, const QString& text, 
00869                                   const int defaultButton, const int escapeButton, 
00870                                   char* btn, ... )
00871 {
00872   va_list args;
00873   va_start( args, btn );
00874   return messageBox( SUIT_MessageBox::Information, parent, title, text,
00875                      messageList( btn, args ),
00876                      defaultButton, escapeButton );
00877 }
00878 
00906 int SUIT_MessageBox::question( QWidget* parent, const QString& title, const QString& text, 
00907                                const int defaultButton, const int escapeButton, 
00908                                char* btn, ... )
00909 {
00910   va_list args;
00911   va_start( args, btn );
00912   return messageBox( SUIT_MessageBox::Question, parent, title, text,
00913                      messageList( btn, args ),
00914                      defaultButton, escapeButton );
00915 }
00916 
00926 SUIT_MessageBox::ButtonInfos SUIT_MessageBox::messageList( char* txt, va_list& args )
00927 {
00928   int i = 0;
00929   ButtonInfos lst;
00930   char* cur = txt;
00931   while ( cur )
00932   {
00933     lst.append( ButtonInfo( i++, cur ) );
00934     cur = va_arg( args, char* );
00935   }
00936 
00937   va_end( args );
00938 
00939   return lst;
00940 }
00941 
00953 int SUIT_MessageBox::messageBox( Icon icon, QWidget* parent, 
00954                                  const QString& title, const QString& text, 
00955                                  const ButtonInfos& lst, 
00956                                  const int defaultButton, 
00957                                  const int escapeButton )
00958 {
00959   SUIT_MessageBox msgBox( icon, title, text, NoButton, parent );
00960 
00961   QMap<QAbstractButton*, int> bm;
00962   for ( int i = 0; i < lst.count(); i++ )
00963   {
00964     int btn         = lst[i].id();
00965     QString txt     = lst[i].text();
00966     ButtonRole role = lst[i].role();
00967 
00968     QPushButton* pb = msgBox.addButton( txt, role );
00969     bm.insert( pb, btn );
00970 
00971     if ( ( defaultButton == -1 && i == 0 ) || btn == defaultButton )
00972       msgBox.setDefaultButton( pb );
00973     if ( ( escapeButton == -1 && i == lst.count() - 1 ) || btn == escapeButton )
00974       msgBox.setEscapeButton( pb );
00975   }
00976     
00977   SUIT_OverrideCursor cw( parent ? parent->cursor() : Qt::ArrowCursor );
00978 
00979   int res = msgBox.exec();
00980   if ( res != -1 )
00981     res = bm[msgBox.clickedButton()];
00982 
00983   QApplication::processEvents();
00984 
00985   return res;
00986 }
00987