Back to index

salome-gui  6.5.0
QtxDialog.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 // File:      QtxDialog.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxDialog.h"
00027 
00028 #include <QLabel>
00029 #include <QLayout>
00030 #include <QKeyEvent>
00031 #include <QFrame>
00032 #include <QTabWidget>
00033 #include <QPushButton>
00034 #include <QApplication>
00035 
00042 class QtxDialog::Area : public QFrame
00043 {
00044 public:
00045   Area( Qt::Orientation, QtxDialog*, QWidget* = 0 );
00046   virtual ~Area();
00047 
00048   bool                     isBorderEnabled() const;
00049   void                     setBorderEnabled( const bool );
00050 
00051   void                     setBorderWidget( QLabel* );
00052 
00053   void                     insertButton( QAbstractButton* );
00054   void                     removeButton( QAbstractButton* );
00055   bool                     contains( QAbstractButton* ) const;
00056 
00057   int                      policy() const;
00058   void                     setPolicy( const int );
00059 
00060   void                     layoutButtons();
00061 
00062   const QList<QAbstractButton*>& buttons() const;
00063 
00064 private:
00065   void                     updateBorder();
00066 
00067 private:
00068   QtxDialog*               myDlg;          
00069   QLabel*                  myLine;         
00070   bool                     myBorder;       
00071   int                      myPolicy;       
00072   QList<QAbstractButton*>  myButtons;      
00073   Qt::Orientation          myOrientation;  
00074 };
00075 
00082 QtxDialog::Area::Area( Qt::Orientation o, QtxDialog* dlg, QWidget* parent )
00083 : QFrame( parent ),
00084   myDlg( dlg ),
00085   myLine( 0 ),
00086   myBorder( false ),
00087   myPolicy( Position ),
00088   myOrientation( o )
00089 {
00090   if ( myOrientation == Qt::Horizontal )
00091     setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum ) );
00092   else
00093     setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00094 
00095   hide();
00096 }
00097 
00101 QtxDialog::Area::~Area()
00102 {
00103 }
00104 
00110 void QtxDialog::Area::insertButton( QAbstractButton* b )
00111 {
00112   if ( !b || myButtons.contains( b ) )
00113     return;
00114 
00115   QWidget* parent = b->parentWidget();
00116   if ( parent != this )
00117     b->setParent( this );
00118 
00119   myButtons.append( b );
00120 
00121   if ( myDlg )
00122     myDlg->adjustButtons();
00123   layoutButtons();
00124 
00125   show();
00126 
00127   updateBorder();
00128 }
00129 
00135 void QtxDialog::Area::removeButton( QAbstractButton* b )
00136 {
00137   if ( !b )
00138     return;
00139 
00140   myButtons.removeAll( b );
00141 
00142   if ( myButtons.isEmpty() )
00143     hide();
00144 
00145   updateBorder();
00146 
00147   if ( myDlg )
00148     myDlg->adjustButtons();
00149 
00150   layoutButtons();
00151 }
00152 
00158 bool QtxDialog::Area::contains( QAbstractButton* b ) const
00159 {
00160   return myButtons.contains( b );
00161 }
00162 
00168 int QtxDialog::Area::policy() const
00169 {
00170   return myPolicy;
00171 }
00172 
00177 void QtxDialog::Area::setPolicy( const int p )
00178 {
00179   if ( myPolicy == p )
00180     return;
00181 
00182   myPolicy = p;
00183   layoutButtons();
00184 }
00185 
00191 bool QtxDialog::Area::isBorderEnabled() const
00192 {
00193   return myLine && myBorder;
00194 }
00195 
00200 void QtxDialog::Area::setBorderEnabled( const bool on )
00201 {
00202   if ( !myLine || myBorder == on )
00203     return;
00204 
00205   myBorder = on;
00206   updateBorder();
00207 }
00208 
00213 void QtxDialog::Area::setBorderWidget( QLabel* line )
00214 {
00215   if ( myLine == line )
00216     return;
00217 
00218   delete myLine;
00219   myLine = line;
00220   updateBorder();
00221 }
00222 
00227 const QList<QAbstractButton*>& QtxDialog::Area::buttons() const
00228 {
00229   return myButtons;
00230 }
00231 
00235 void QtxDialog::Area::updateBorder()
00236 {
00237   if ( !myLine )
00238     return;
00239 
00240   bool isVis = isVisibleTo( parentWidget() );
00241   myLine->setVisible( isVis );
00242 
00243   myLine->setLineWidth( myBorder ? 1 : 0 );
00244 }
00245 
00249 void QtxDialog::Area::layoutButtons()
00250 {
00251   int aPolicy = policy();
00252 
00253   QMap<QAbstractButton*, int> buttonId;
00254   for ( QList<QAbstractButton*>::iterator it1 = myButtons.begin(); it1 != myButtons.end(); ++it1 )
00255     buttonId.insert( *it1, 0 );
00256 
00257   QList<QAbstractButton*> src;
00258   for ( ButtonMap::Iterator mit = myDlg->myButton.begin(); mit != myDlg->myButton.end(); ++mit )
00259   {
00260     if ( buttonId.contains( mit.value() ) )
00261     {
00262       buttonId[mit.value()] = mit.key();
00263       if ( mit.key() >= 0 )
00264         src.append( mit.value() );
00265     }
00266   }
00267 
00268   for ( QList<QAbstractButton*>::iterator it2 = myButtons.begin(); it2 != myButtons.end(); ++it2 )
00269   {
00270     if ( buttonId[*it2] < 0 )
00271       src.append( *it2 );
00272   }
00273 
00274   QList<QAbstractButton*> left, right, center, other;
00275   for ( QList<QAbstractButton*>::iterator it = src.begin(); it != src.end(); ++it )
00276   {
00277     if ( !(*it)->isVisibleTo( this ) )
00278       continue;
00279 
00280     int aPosition = myDlg->buttonPosition( *it );
00281     if ( aPosition == -1 )
00282       continue;
00283 
00284     if ( aPolicy != QtxDialog::Position )
00285       other.append( *it );
00286     else if ( aPosition == QtxDialog::Left )
00287       left.append( *it );
00288     else if ( aPosition == QtxDialog::Right )
00289       right.append( *it );
00290     else if ( aPosition == QtxDialog::Center )
00291       center.append( *it );
00292   }
00293 
00294   delete layout();
00295 
00296   QBoxLayout* buttonLayout = 0;
00297   if ( myOrientation == Qt::Vertical )
00298     buttonLayout = new QVBoxLayout( this );
00299   else
00300     buttonLayout = new QHBoxLayout( this );
00301 
00302   if ( !buttonLayout )
00303     return;
00304 
00305   buttonLayout->setMargin( 0 );
00306   buttonLayout->setSpacing( 5 );
00307 
00308   if ( aPolicy == QtxDialog::Position )
00309   {
00310     for ( QList<QAbstractButton*>::iterator lit = left.begin(); lit != left.end(); ++lit )
00311       buttonLayout->addWidget( *lit );
00312     buttonLayout->addStretch( 1 );
00313     for ( QList<QAbstractButton*>::iterator cit = center.begin(); cit != center.end(); ++cit )
00314       buttonLayout->addWidget( *cit );
00315     buttonLayout->addStretch( 1 );
00316     for ( QList<QAbstractButton*>::iterator rit = right.begin(); rit != right.end(); ++rit )
00317       buttonLayout->addWidget( *rit );
00318   }
00319   else
00320   {
00321     for ( int i = 0; i < (int)other.count(); i++ )
00322     {
00323       buttonLayout->addWidget( other[i] );
00324       if ( aPolicy == QtxDialog::Uniform && i < (int)other.count() - 1  )
00325         buttonLayout->addStretch( 1 );
00326     }
00327   }
00328 
00329   QWidgetList wids;
00330   if ( layout() )
00331   {
00332     for ( int i = 0; i < layout()->count(); i++ )
00333     {
00334       if ( !layout()->itemAt( i ) || layout()->itemAt( i )->widget() )
00335         continue;
00336 
00337       if ( QApplication::layoutDirection() == Qt::RightToLeft )
00338         wids.prepend( layout()->itemAt( i )->widget() );
00339       else
00340         wids.append( layout()->itemAt( i )->widget() );
00341     }
00342   }
00343   Qtx::setTabOrder( wids );
00344 }
00345 
00346 
00354 class QtxDialog::Border : public QLabel
00355 {
00356 public:
00357   Border( QWidget* = 0 );
00358   virtual ~Border();
00359 
00360   virtual void setLineWidth( int );
00361 
00362   virtual QSize sizeHint() const;
00363   virtual QSize minimumSizeHint() const;
00364 };
00365 
00370 QtxDialog::Border::Border( QWidget* parent )
00371 : QLabel( parent )
00372 {
00373   setAlignment( Qt::AlignCenter );
00374 }
00375 
00379 QtxDialog::Border::~Border()
00380 {
00381 }
00382 
00387 void QtxDialog::Border::setLineWidth( int lw )
00388 {
00389   bool isOn = lineWidth() > 0;
00390 
00391   QLabel::setLineWidth( lw );
00392     
00393   if ( isOn != ( lineWidth() > 0 ) )
00394     updateGeometry();
00395 }
00396 
00401 QSize QtxDialog::Border::sizeHint() const
00402 {
00403   QSize sz( 5, 5 );
00404 
00405   if ( lineWidth() > 0 )
00406   {
00407     if ( frameShape() == VLine )
00408       sz += QSize( 5 + lineWidth(), 0 );
00409     else if ( frameShape() == HLine )
00410       sz += QSize( 0, 5 + lineWidth() );
00411   }
00412 
00413   return sz;
00414 }
00415 
00420 QSize QtxDialog::Border::minimumSizeHint() const
00421 {
00422   return sizeHint();
00423 }
00424 
00443 QtxDialog::QtxDialog( QWidget* parent, bool modal, bool allowResize, const int f, Qt::WindowFlags wf )
00444 : QDialog( parent, (Qt::WindowFlags)( wf | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::Dialog |
00445 #ifdef WIN32
00446            ( allowResize ? 0 : Qt::FramelessWindowHint ) |
00447 #endif
00448            ( ( allowResize 
00449 #ifdef WIN32 
00450                // in qwidget_win.cpp flag WStyle_ContextHelp will be unset in WStyle_MinMax in switched ON
00451                && !( wf & Qt::WindowContextHelpButtonHint )
00452 #endif
00453                ) ? Qt::WindowMaximizeButtonHint : 0 ) ) ),
00454   myInited( false ),
00455   mySender( 0 ),
00456   myAlignment( 0 ),
00457   myDialogFlags( Accept | SetFocus )
00458 {
00459   setModal( modal );
00460 
00461   QVBoxLayout* base = new QVBoxLayout( this );
00462   base->setMargin( 5 );
00463   base->setSpacing( 0 );
00464 
00465   QWidget* main = new QWidget( this );
00466   base->addWidget( main );
00467 
00468   QVBoxLayout* lMain = new QVBoxLayout( main );
00469   lMain->setMargin( 0 );
00470   lMain->setSpacing( 0 );
00471 
00472   Area* topArea = new Area( Qt::Horizontal, this, main );
00473   QLabel* topLine = new Border( main );
00474   lMain->addWidget( topArea );
00475   lMain->addWidget( topLine );
00476 
00477   QWidget* midGroup = new QWidget( main );
00478   lMain->addWidget( midGroup );
00479 
00480   QVBoxLayout* midLyout = new QVBoxLayout( midGroup );
00481   midLyout->setMargin( 0 );
00482   midLyout->setSpacing( 0 );
00483 
00484   QLabel* botLine = new Border( main );
00485   Area* botArea = new Area( Qt::Horizontal, this, main );
00486   lMain->addWidget( botLine );
00487   lMain->addWidget( botArea );
00488 
00489   Area* leftArea = new Area( Qt::Vertical, this, midGroup );
00490   QLabel* leftLine = new Border( midGroup );
00491   midLyout->addWidget( leftArea );
00492   midLyout->addWidget( leftLine );
00493 
00494   myMainFrame = new QFrame( midGroup );
00495   midLyout->addWidget( myMainFrame );
00496 
00497   QLabel* rightLine = new Border( midGroup );
00498   Area* rightArea = new Area( Qt::Vertical, this, midGroup );
00499   midLyout->addWidget( rightLine );
00500   midLyout->addWidget( rightArea );
00501 
00502   myMainFrame->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00503 
00504   topLine->setFrameStyle( QFrame::Sunken | QFrame::HLine );
00505   botLine->setFrameStyle( QFrame::Sunken | QFrame::HLine );
00506   leftLine->setFrameStyle( QFrame::Sunken | QFrame::VLine );
00507   rightLine->setFrameStyle( QFrame::Sunken | QFrame::VLine );
00508 
00509   topArea->setBorderWidget( topLine );
00510   botArea->setBorderWidget( botLine );
00511   leftArea->setBorderWidget( leftLine );
00512   rightArea->setBorderWidget( rightLine );
00513 
00514   myArea.insert( TopArea,    topArea );
00515   myArea.insert( BottomArea, botArea );
00516   myArea.insert( LeftArea,   leftArea );
00517   myArea.insert( RightArea,  rightArea );
00518 
00519   for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++ itr )
00520     itr.value()->setBorderEnabled( false );
00521 
00522   myButton.insert( OK,     new QPushButton( tr( "&OK" ),     this ) );
00523   myButton.insert( Cancel, new QPushButton( tr( "&Cancel" ), this ) );
00524   myButton.insert( Close,  new QPushButton( tr( "C&lose" ),  this ) );
00525   myButton.insert( Help,   new QPushButton( tr( "&Help" ),   this ) );
00526   myButton.insert( Apply,  new QPushButton( tr( "&Apply" ),  this ) );
00527   myButton.insert( Yes,    new QPushButton( tr( "&Yes" ),    this ) );
00528   myButton.insert( No,     new QPushButton( tr( "&No" ),     this ) );
00529 
00530   for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
00531   {
00532     ((QPushButton*)it.value())->setAutoDefault( false );
00533     connect( it.value(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
00534   }
00535 
00536   setButtonPosition( Left,   OK | Cancel | Apply );
00537   setButtonPosition( Center, Yes | No | Close );
00538   setButtonPosition( Right,  Help );
00539   setButtonPlace( BottomArea, All );
00540 
00541   connect( myButton[Apply],  SIGNAL( clicked() ), this, SIGNAL( dlgApply() ) );
00542   connect( myButton[Help],   SIGNAL( clicked() ), this, SIGNAL( dlgHelp() ) );
00543 
00544   connect( myButton[OK],     SIGNAL( clicked() ), this, SLOT( onAccept() ) );
00545   connect( myButton[Cancel], SIGNAL( clicked() ), this, SLOT( onReject() ) );
00546   connect( myButton[Yes],    SIGNAL( clicked() ), this, SLOT( onAccept() ) );
00547   connect( myButton[No],     SIGNAL( clicked() ), this, SLOT( onReject() ) );
00548   connect( myButton[Close],  SIGNAL( clicked() ), this, SLOT( onReject() ) );
00549 
00550   QIcon icon;
00551   QWidget* p = parentWidget();
00552   while( p && p->parentWidget() )
00553     p = p->parentWidget();
00554 
00555   if ( p )
00556     setWindowIcon( p->windowIcon() );
00557 
00558   myButtonFlags = f;
00559 
00560 #ifndef WIN32
00561   if ( !allowResize )
00562     setMaximumSize( minimumSize() );
00563 #endif
00564 
00565   update();
00566 }
00567 
00571 QtxDialog::~QtxDialog()
00572 {
00573 }
00574 
00580 void QtxDialog::setButtonFlags( const int f )
00581 {
00582   int old = myButtonFlags;
00583   myButtonFlags = myButtonFlags | f;
00584   if ( old != myButtonFlags )
00585     update();
00586 }
00587 
00593 void QtxDialog::clearButtonFlags( const int f )
00594 {
00595   int old = myButtonFlags;
00596   myButtonFlags = myButtonFlags & ~f;
00597   if ( old != myButtonFlags )
00598     update();
00599 }
00600 
00606 bool QtxDialog::testButtonFlags( const int f ) const
00607 {
00608   return ( myButtonFlags & f ) == f;
00609 }
00610 
00616 void QtxDialog::setDialogFlags( const int f )
00617 {
00618   myDialogFlags = myDialogFlags | f;
00619 }
00620 
00626 void QtxDialog::clearDialogFlags( const int f )
00627 {
00628   myDialogFlags = myDialogFlags & ~f;
00629 }
00630 
00636 bool QtxDialog::testDialogFlags( const int f ) const
00637 {
00638   return ( myDialogFlags & f ) == f;
00639 }
00640 
00649 QFrame* QtxDialog::mainFrame() const
00650 {
00651   return myMainFrame;
00652 }
00653 
00660 int QtxDialog::buttonPosition( const int id ) const
00661 {
00662   int pos = -1;
00663   if ( myPosition.contains( id ) )
00664     pos = myPosition[id];
00665   return pos;
00666 }
00672 void QtxDialog::setButtonPosition( const int pos, const int id )
00673 {
00674   ButtonMap map = buttons( id );
00675 
00676   QMap<QObject*, int> changed;
00677   for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
00678   {
00679     if ( myPosition[it.key()] == pos )
00680       continue;
00681       
00682     myPosition[it.key()] = pos;
00683     if ( button( it.key() ) )
00684       changed.insert( button( it.key() )->parent(), 0 );
00685   }
00686   
00687   for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++itr )
00688   {
00689     if ( changed.contains( itr.value() ) )
00690       itr.value()->layoutButtons();
00691   }
00692 }
00693 
00700 void QtxDialog::setPlacePosition( const int pos, const int area )
00701 {
00702   if ( !myArea.contains( area ) )
00703     return;
00704 
00705   Area* anArea = myArea[area];
00706 
00707   bool changed = false;
00708   for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
00709   {
00710     if ( !anArea->contains( it.value() ) )
00711       continue;
00712 
00713     changed = changed &&  myPosition[it.key()] != pos;
00714 
00715     myPosition[it.key()] = pos;
00716   }
00717 
00718   if ( changed )
00719     anArea->layoutButtons();
00720 }
00721 
00727 int QtxDialog::placePolicy( const int area ) const
00728 {
00729   int res = -1;
00730   if ( myArea.contains( area ) )
00731     res = myArea[area]->policy();
00732   return res;
00733 }
00734 
00741 void QtxDialog::setPlacePolicy( const int policy, const int area )
00742 {
00743   if ( area < 0 )
00744   {
00745     for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++itr )
00746       itr.value()->setPolicy( policy );
00747   }
00748   else if ( myArea.contains( area ) )
00749     myArea[area]->setPolicy( policy );
00750 }
00751 
00757 void QtxDialog::setButtonPlace( const int area, const int id )
00758 {
00759   if ( !myArea.contains( area ) )
00760     return;
00761 
00762   Area* anArea = myArea[area];
00763   ButtonMap map = buttons( id );
00764   QMap<Area*, int> areaMap;
00765   for ( AreaMap::ConstIterator aIt = myArea.begin(); aIt != myArea.end(); ++aIt )
00766     areaMap.insert( aIt.value(), 0 );
00767 
00768   for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
00769   {
00770     Area* old = (Area*)it.value()->parent();
00771     if ( old == anArea )
00772       continue;
00773 
00774     if ( areaMap.contains( old ) )
00775       old->removeButton( it.value() );
00776     anArea->insertButton( it.value() );
00777   }
00778 }
00779 
00786 bool QtxDialog::isBorderEnabled( const int area ) const
00787 {
00788   bool res = false;
00789   if ( myArea.contains( area ) )
00790     res  = myArea[area]->isBorderEnabled();
00791   return res;
00792 }
00793 
00803 void QtxDialog::setBorderEnabled( const bool on, const int area )
00804 {
00805   if ( !myArea.contains( area ) )
00806     return;
00807 
00808   if ( myArea[area]->isBorderEnabled() == on )
00809     return;
00810 
00811   myArea[area]->setBorderEnabled( on );
00812 
00813   if ( isVisible() )
00814   {
00815     QApplication::sendPostedEvents();
00816     adjustSize();
00817   }
00818 }
00819 
00826 bool QtxDialog::isButtonEnabled( const int id ) const
00827 {
00828   ButtonMap map = buttons( id );
00829   bool result = !map.isEmpty();
00830   for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
00831     result = result && it.value()->isEnabled();
00832   return result;
00833 }
00834 
00841 void QtxDialog::setButtonEnabled( const bool on, const int id )
00842 {
00843   ButtonMap map = buttons( id );
00844   for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
00845     it.value()->setEnabled( on );
00846 }
00847 
00854 bool QtxDialog::hasButtonFocus( const int id ) const
00855 {
00856   bool res = false;
00857   QAbstractButton* pb = button( id );
00858   if ( pb )
00859     res = pb->hasFocus();
00860   return res;
00861 }
00862 
00868 void QtxDialog::setButtonFocus( const int id )
00869 {
00870   QAbstractButton* pb = button( id );
00871   if ( pb )
00872     pb->setFocus();
00873 }
00874 
00881 QString QtxDialog::buttonText( const int id )
00882 {
00883   QString retval;
00884   QAbstractButton* but = button( id );
00885   if ( but )
00886     retval = but->text();
00887   return retval;
00888 }
00889 
00896 void QtxDialog::setButtonText( const int id, const QString& text )
00897 {
00898   QAbstractButton* but = button( id );
00899   if ( but && but->text() != text )
00900   {
00901     but->setText( text );
00902     adjustButtons();
00903   }
00904 }
00905 
00920 uint QtxDialog::setAlignment( uint align )
00921 {
00922   uint oldAlign = myAlignment;
00923   myAlignment = align;
00924   return oldAlign;
00925 }
00926 
00930 void QtxDialog::update()
00931 {
00932   for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
00933     if ( it.key() >= 0 )
00934       it.value()->setVisible( testButtonFlags( it.key() ) );
00935 
00936   for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++itr )
00937     itr.value()->layoutButtons();
00938 
00939   adjustButtons();
00940 
00941   QDialog::update();
00942 }
00943 
00951 void QtxDialog::setVisible( bool on )
00952 {
00953   resize( sizeHint() );
00954 
00955   QDialog::setVisible( on );
00956 
00957   if ( on )
00958   {
00959     if ( testDialogFlags( SetFocus ) )
00960       setFocus();
00961     myInited = true;
00962   }
00963   else
00964     QApplication::instance()->processEvents();
00965 }
00966 
00973 QAbstractButton* QtxDialog::userButton( const int id ) const
00974 {
00975   QAbstractButton* b = 0;
00976   if ( id < -1 && myButton.contains( id ) )
00977     b = myButton[id];
00978   return b;
00979 }
00980 
00986 QIntList QtxDialog::userButtonIds() const
00987 {
00988   QIntList retlist;
00989   for ( ButtonMap::ConstIterator it = myButton.begin(); it != myButton.end(); ++it )
00990     if ( it.key() < 0 )
00991       retlist.append( it.key() );
00992   return retlist;
00993 }
00994 
01007 int QtxDialog::insertButton( const QString& text, const int area )
01008 {
01009   if ( !myArea.contains( area ) )
01010     return -1;
01011 
01012   int id = -2;
01013   while ( myButton.contains( id ) )
01014     id--;
01015 
01016   Area* anArea = myArea[area];
01017   QAbstractButton* b = createButton( this );
01018   if ( b )
01019   {
01020     b->setText( text );
01021     myButton.insert( id, b );
01022     myPosition.insert( id, Left );
01023     
01024     connect( b, SIGNAL( clicked() ), this, SLOT( onButton() ) );
01025     connect( b, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
01026 
01027     anArea->insertButton( b );
01028     update();
01029   }
01030   else
01031     id = -1;
01032 
01033   return id;
01034 }
01035 
01044 void QtxDialog::removeButton( const int id )
01045 {
01046   if ( id >= 0 )
01047     return;
01048 
01049   ButtonMap map;
01050   if ( id == -1 )
01051   {
01052     for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
01053     {
01054       if ( it.key() < 0 )
01055         map.insert( it.key(), it.value() );
01056     }
01057   }
01058   else if ( myButton.contains( id ) )
01059     map.insert( id, myButton[id] );
01060 
01061   for ( ButtonMap::Iterator itr = map.begin(); itr != map.end(); ++itr )
01062   {
01063     for ( AreaMap::Iterator it = myArea.begin(); it != myArea.end(); ++it )
01064       it.value()->removeButton( itr.value() );
01065 
01066     myButton.remove( itr.key() );
01067     myPosition.remove( itr.key() );
01068     
01069     delete itr.value();
01070   }
01071   update();
01072 }
01073 
01100 void QtxDialog::setUnits( QLabel* aLabel, const QString& aUnits )
01101 {
01102   QString label = aLabel->text();
01103 
01104   int begin;
01105   int end = label.lastIndexOf( ')' );
01106 
01107   QString startLabel = label;
01108   QString finalLabel;
01109 
01110   if ( end != -1 )
01111   {
01112     begin = label.left( end ).lastIndexOf( '(' );
01113     if ( begin != -1 )
01114     {
01115       startLabel = label.mid( 0, begin );
01116       finalLabel = label.mid( end + 1 );
01117     }
01118   }
01119   else
01120   {
01121     startLabel = startLabel.trimmed();
01122     if ( startLabel.at( startLabel.length() - 1 ) == ':' )
01123     {
01124       finalLabel = startLabel.mid( startLabel.length() - 1 );
01125       startLabel = startLabel.mid( 0, startLabel.length() - 1 );
01126     }
01127   }
01128   if ( aUnits.length() )
01129     label = startLabel.trimmed() + " (" + aUnits + ") " + finalLabel.trimmed();
01130   else
01131     label = startLabel.trimmed() + " " + finalLabel.trimmed();
01132   aLabel->setText( label );
01133 }
01134 
01148 bool QtxDialog::acceptData() const
01149 {
01150   return true;
01151 }
01152 
01166 bool QtxDialog::rejectData() const
01167 {
01168   return true;
01169 }
01170 
01179 QAbstractButton* QtxDialog::createButton( QWidget* parent )
01180 {
01181   QPushButton* pb = new QPushButton( parent );
01182   pb->setAutoDefault( false );
01183   return pb;
01184 }
01185 
01191 QAbstractButton* QtxDialog::button( const int f ) const
01192 {
01193   QAbstractButton* retval = 0;
01194   if ( myButton.contains( f ) )
01195     retval = myButton[f];
01196   return retval;
01197 }
01198 
01204 QtxDialog::ButtonMap QtxDialog::buttons( const int f ) const
01205 {
01206   ButtonMap retmap;
01207   if ( f < -1 )
01208   {
01209     if ( myButton.contains( f ) )
01210       retmap.insert( f, myButton[f] );
01211   }
01212   else
01213   {
01214     for ( ButtonMap::ConstIterator it = myButton.begin(); it != myButton.end(); ++it )
01215       if ( f == -1 || ( it.key() >= 0 && f & it.key() ) )
01216         retmap.insert( it.key(), it.value() );
01217   }
01218   return retmap;
01219 }
01220 
01226 int QtxDialog::buttonId( const QAbstractButton* b ) const
01227 {
01228   int id = -1;
01229   for ( ButtonMap::ConstIterator it = myButton.begin(); it != myButton.end() && id == -1; ++it )
01230     if ( it.value() == b )
01231       id = it.key();
01232   return id;
01233 }
01234 
01240 int QtxDialog::buttonPosition( QAbstractButton* b ) const
01241 {
01242   return buttonPosition( buttonId( b ) );
01243 }
01244 
01253 void QtxDialog::showEvent( QShowEvent* e )
01254 {
01255   if ( !testDialogFlags( AlignOnce ) || !myInited )
01256     Qtx::alignWidget( this, parentWidget(), myAlignment );
01257   QDialog::showEvent( e );
01258 }
01259 
01267 void QtxDialog::hideEvent( QHideEvent* e )
01268 {
01269   QApplication::instance()->processEvents();
01270   QDialog::hideEvent( e );
01271 }
01272 
01280 void QtxDialog::childEvent( QChildEvent* e )
01281 {
01282   QDialog::childEvent( e );
01283   if ( layout() && e->added() && e->child()->inherits( "QSizeGrip" ) )
01284   {
01285     layout()->setMargin( 12 );
01286     connect( e->child(), SIGNAL( destroyed() ), this, SLOT( onSizeGripDestroyed() ) );
01287   }
01288 }
01289 
01303 void QtxDialog::keyPressEvent( QKeyEvent* e )
01304 {
01305   QDialog::keyPressEvent( e );
01306 
01307   if ( e->isAccepted() )
01308     return;
01309 
01310   if ( !e->modifiers() && e->key() == Qt::Key_Escape )
01311     reject();
01312 
01313   if ( e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Return )
01314   {
01315     if ( testButtonFlags( OK ) || testButtonFlags( Yes ) )
01316       accept();
01317     else if ( testButtonFlags( Apply ) && isButtonEnabled( Apply ) )
01318       emit dlgApply();
01319     e->accept();
01320   }
01321 
01322   if ( e->key() == Qt::Key_F1 && testButtonFlags( Help ) && isButtonEnabled( Help ) )
01323   {
01324     e->accept();
01325     emit dlgHelp();
01326   }
01327 
01328   if ( e->key() == Qt::Key_Tab && e->modifiers() & Qt::ControlModifier )
01329   {
01330     QObject* tab = qFindChild<QTabWidget*>( this );
01331     if ( tab && !property( "in_tab_event" ).toBool() ) {
01332       setProperty( "in_tab_event", true );
01333       QApplication::sendEvent( tab, e );
01334       setProperty( "in_tab_event", false );
01335     }
01336   }
01337 }
01338 
01346 void QtxDialog::closeEvent( QCloseEvent* /*e*/ )
01347 {
01348   reject();
01349 }
01350 
01371 void QtxDialog::accept()
01372 {
01373   if ( !mySender )
01374   {
01375     if ( testButtonFlags( OK ) )
01376       mySender = button( OK );
01377     else if ( testButtonFlags( Yes ) )
01378       mySender = button( Yes );
01379     else
01380       mySender = button( Close );
01381   }
01382 
01383   if ( !mySender || !mySender->isWidgetType() ||
01384        !((QWidget*)mySender)->isEnabled() )
01385     return;
01386 
01387   if ( testDialogFlags( Accept ) && !acceptData() )
01388     return;
01389 
01390   QDialog::accept();
01391 
01392   emitSignal();
01393 }
01394 
01415 void QtxDialog::reject()
01416 {
01417   if ( testDialogFlags( Reject ) && !rejectData() )
01418     return;
01419 
01420   if ( !mySender )
01421   {
01422     if ( testButtonFlags( Cancel ) )
01423       mySender = button( Cancel );
01424     else if ( testButtonFlags( No ) )
01425       mySender = button( No );
01426     else
01427       mySender = button( Close );
01428   }
01429 
01430   if ( !mySender || !mySender->isWidgetType() ||
01431        !((QWidget*)mySender)->isEnabled() )
01432     return;
01433 
01434   QDialog::reject();
01435 
01436   emitSignal();
01437 }
01438 
01442 void QtxDialog::emitSignal()
01443 {
01444   QApplication::instance()->processEvents();
01445   QApplication::syncX();
01446 
01447   int id = buttonId( (QAbstractButton*)mySender );
01448   mySender = 0;
01449 
01450   switch ( id )
01451   {
01452   case OK:
01453     emit dlgOk();
01454     break;
01455   case Cancel:
01456     emit dlgCancel();
01457     break;
01458   case Close:
01459     emit dlgClose();
01460     break;
01461   case Yes:
01462     emit dlgYes();
01463     break;
01464   case No:
01465     emit dlgNo();
01466     break;
01467   }
01468 }
01469 
01476 void QtxDialog::onAccept()
01477 {
01478   const QObject* obj = sender();
01479   mySender = obj;
01480   accept();
01481 }
01482 
01490 void QtxDialog::onReject()
01491 {
01492   const QObject* obj = sender();
01493   mySender = obj;
01494   reject();
01495 }
01496 
01504 void QtxDialog::onButton()
01505 {
01506   int id = buttonId( (QAbstractButton*)sender() );
01507   if ( id != -1 )
01508     emit dlgButton( id );
01509 }
01510 
01515 void QtxDialog::onDestroyed( QObject* obj )
01516 {
01517   QAbstractButton* b = (QAbstractButton*)obj;
01518   int id = buttonId( b );
01519   if ( id == -1 )
01520     return;
01521 
01522   myButton.remove( id );
01523   myPosition.remove( id );
01524   for ( AreaMap::Iterator it = myArea.begin(); it != myArea.end(); ++it )
01525     it.value()->removeButton( b );
01526 }
01527 
01531 void QtxDialog::onSizeGripDestroyed()
01532 {
01533   if ( layout() )
01534     layout()->setMargin( 5 );
01535 }
01536 
01540 void QtxDialog::adjustButtons()
01541 {
01542   int minWidth = 0;
01543   for ( AreaMap::Iterator aIt = myArea.begin(); aIt != myArea.end(); ++aIt )
01544   {
01545     const QList<QAbstractButton*>& lst = aIt.value()->buttons();
01546     for ( QList<QAbstractButton*>::const_iterator bIt = lst.begin(); bIt != lst.end(); ++bIt )
01547       if ( (*bIt)->isVisibleTo( this ) )
01548         minWidth = qMax( minWidth, (*bIt)->sizeHint().width() );
01549   }
01550 
01551   for ( AreaMap::Iterator aItr = myArea.begin(); aItr != myArea.end(); ++aItr )
01552   {
01553     const QList<QAbstractButton*>& lst = aItr.value()->buttons();
01554     for ( QList<QAbstractButton*>::const_iterator bItr = lst.begin(); bItr != lst.end(); ++bItr )
01555       if ( (*bItr)->isVisibleTo( this ) )
01556         (*bItr)->setMinimumWidth( minWidth );
01557   }
01558 }
01559