1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: QtxDialog.cxx
24 // Author: Sergey TELKOV
26 #include "QtxDialog.h"
33 #include <QPushButton>
34 #include <QApplication>
37 \class QtxDialog::Area
39 \brief Area containing dialog box buttons.
42 class QtxDialog::Area : public QFrame
45 Area( Qt::Orientation, QtxDialog*, QWidget* = 0 );
48 bool isBorderEnabled() const;
49 void setBorderEnabled( const bool );
51 void setBorderWidget( QLabel* );
53 void insertButton( QAbstractButton* );
54 void removeButton( QAbstractButton* );
55 bool contains( QAbstractButton* ) const;
58 void setPolicy( const int );
62 const QList<QAbstractButton*>& buttons() const;
68 QtxDialog* myDlg; //!< parent dialog box
69 QLabel* myLine; //!< border widget
70 bool myBorder; //!< "has border" flag
71 int myPolicy; //!< button layout type (QtxDialog::PlacePolicy)
72 QList<QAbstractButton*> myButtons; //!< buttons list
73 Qt::Orientation myOrientation; //!< buttons orientation (Qt::Orientation)
78 \param o buttons orientation
79 \param dlg dialog box owning this area
80 \param parent parent widget
82 QtxDialog::Area::Area( Qt::Orientation o, QtxDialog* dlg, QWidget* parent )
90 if ( myOrientation == Qt::Horizontal )
91 setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum ) );
93 setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
101 QtxDialog::Area::~Area()
106 \brief Insert button to the area.
107 \param b button to be added
110 void QtxDialog::Area::insertButton( QAbstractButton* b )
112 if ( !b || myButtons.contains( b ) )
115 QWidget* parent = b->parentWidget();
116 if ( parent != this )
117 b->setParent( this );
119 myButtons.append( b );
122 myDlg->adjustButtons();
131 \brief Remove button from the area.
132 \param b button to be removed
135 void QtxDialog::Area::removeButton( QAbstractButton* b )
140 myButtons.removeAll( b );
142 if ( myButtons.isEmpty() )
148 myDlg->adjustButtons();
154 \brief Check if area owns the button specified.
155 \param b button to be checked
156 \return \c true if area contains button
158 bool QtxDialog::Area::contains( QAbstractButton* b ) const
160 return myButtons.contains( b );
164 \brief Get buttons layout policy.
165 \return policy of button layouting (Qtx::PlacePolicy)
168 int QtxDialog::Area::policy() const
174 \brief Set buttons layout policy.
177 void QtxDialog::Area::setPolicy( const int p )
187 \brief Check of the border is enabled.
188 \return \c true if border is enabled
189 \sa setBorderEnabled(), setBorderWidget()
191 bool QtxDialog::Area::isBorderEnabled() const
193 return myLine && myBorder;
197 \brief Enable/disable border (separator between main frame and button frame)
200 void QtxDialog::Area::setBorderEnabled( const bool on )
202 if ( !myLine || myBorder == on )
210 \brief Set border widget (separator between main frame and button frame).
211 \param line new separator widget
213 void QtxDialog::Area::setBorderWidget( QLabel* line )
215 if ( myLine == line )
224 \brief Get all area buttons.
225 \return const reference to the list of buttons
227 const QList<QAbstractButton*>& QtxDialog::Area::buttons() const
233 \brief Update border visibility.
235 void QtxDialog::Area::updateBorder()
240 bool isVis = isVisibleTo( parentWidget() );
241 myLine->setVisible( isVis );
243 myLine->setLineWidth( myBorder ? 1 : 0 );
247 \brief Layout buttons in the area.
249 void QtxDialog::Area::layoutButtons()
251 int aPolicy = policy();
253 QMap<QAbstractButton*, int> buttonId;
254 for ( QList<QAbstractButton*>::iterator it1 = myButtons.begin(); it1 != myButtons.end(); ++it1 )
255 buttonId.insert( *it1, 0 );
257 QList<QAbstractButton*> src;
258 for ( ButtonMap::Iterator mit = myDlg->myButton.begin(); mit != myDlg->myButton.end(); ++mit )
260 if ( buttonId.contains( mit.value() ) )
262 buttonId[mit.value()] = mit.key();
263 if ( mit.key() >= 0 )
264 src.append( mit.value() );
268 for ( QList<QAbstractButton*>::iterator it2 = myButtons.begin(); it2 != myButtons.end(); ++it2 )
270 if ( buttonId[*it2] < 0 )
274 QList<QAbstractButton*> left, right, center, other;
275 for ( QList<QAbstractButton*>::iterator it = src.begin(); it != src.end(); ++it )
277 if ( !(*it)->isVisibleTo( this ) )
280 int aPosition = myDlg->buttonPosition( *it );
281 if ( aPosition == -1 )
284 if ( aPolicy != QtxDialog::Position )
286 else if ( aPosition == QtxDialog::Left )
288 else if ( aPosition == QtxDialog::Right )
290 else if ( aPosition == QtxDialog::Center )
291 center.append( *it );
296 QBoxLayout* buttonLayout = 0;
297 if ( myOrientation == Qt::Vertical )
298 buttonLayout = new QVBoxLayout( this );
300 buttonLayout = new QHBoxLayout( this );
305 buttonLayout->setMargin( 0 );
306 buttonLayout->setSpacing( 5 );
308 if ( aPolicy == QtxDialog::Position )
310 for ( QList<QAbstractButton*>::iterator lit = left.begin(); lit != left.end(); ++lit )
311 buttonLayout->addWidget( *lit );
312 buttonLayout->addStretch( 1 );
313 for ( QList<QAbstractButton*>::iterator cit = center.begin(); cit != center.end(); ++cit )
314 buttonLayout->addWidget( *cit );
315 buttonLayout->addStretch( 1 );
316 for ( QList<QAbstractButton*>::iterator rit = right.begin(); rit != right.end(); ++rit )
317 buttonLayout->addWidget( *rit );
321 for ( int i = 0; i < (int)other.count(); i++ )
323 buttonLayout->addWidget( other[i] );
324 if ( aPolicy == QtxDialog::Uniform && i < (int)other.count() - 1 )
325 buttonLayout->addStretch( 1 );
332 for ( int i = 0; i < layout()->count(); i++ )
334 if ( !layout()->itemAt( i ) || layout()->itemAt( i )->widget() )
337 if ( QApplication::layoutDirection() == Qt::RightToLeft )
338 wids.prepend( layout()->itemAt( i )->widget() );
340 wids.append( layout()->itemAt( i )->widget() );
343 Qtx::setTabOrder( wids );
348 \class QtxDialog::Border
350 \brief Special label used as border widget (separator
351 between main frame and button frame).
354 class QtxDialog::Border : public QLabel
357 Border( QWidget* = 0 );
360 virtual void setLineWidth( int );
362 virtual QSize sizeHint() const;
363 virtual QSize minimumSizeHint() const;
368 \param parent parent widget
370 QtxDialog::Border::Border( QWidget* parent )
373 setAlignment( Qt::AlignCenter );
379 QtxDialog::Border::~Border()
384 \brief Set separator line width.
385 \param lw new line width
387 void QtxDialog::Border::setLineWidth( int lw )
389 bool isOn = lineWidth() > 0;
391 QLabel::setLineWidth( lw );
393 if ( isOn != ( lineWidth() > 0 ) )
398 \brief Get recommended size for the widget.
399 \return recommended size for the widget
401 QSize QtxDialog::Border::sizeHint() const
405 if ( lineWidth() > 0 )
407 if ( frameShape() == VLine )
408 sz += QSize( 5 + lineWidth(), 0 );
409 else if ( frameShape() == HLine )
410 sz += QSize( 0, 5 + lineWidth() );
417 \brief Get recommended minimum size for the widget.
418 \return recommended minimum size for the widget
420 QSize QtxDialog::Border::minimumSizeHint() const
427 \brief Generic dialog box class.
433 Construct a dialog with specified parent and name.
434 By default non-modal, non-resizable with the OK, Cancel and Help buttons
435 dialog box is created.
437 \param parent parent widget
438 \param modal if \c true dialog box is modal
439 \param allowResize if \c true then dialog can be resized by user
440 \param f specified control buttons for dialog box (QtxDialog::ButtonFlags)
441 \param wf dialog box flags (Qt::WindowFlags)
443 QtxDialog::QtxDialog( QWidget* parent, bool modal, bool allowResize, const int f, Qt::WindowFlags wf )
444 : QDialog( parent, (Qt::WindowFlags)( wf | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::Dialog |
446 ( allowResize ? 0 : Qt::FramelessWindowHint ) |
450 // in qwidget_win.cpp flag WStyle_ContextHelp will be unset in WStyle_MinMax in switched ON
451 && !( wf & Qt::WindowContextHelpButtonHint )
453 ) ? Qt::WindowMaximizeButtonHint : 0 ) ) ),
457 myDialogFlags( Accept | SetFocus )
461 QVBoxLayout* base = new QVBoxLayout( this );
462 base->setMargin( 5 );
463 base->setSpacing( 0 );
465 QWidget* main = new QWidget( this );
466 base->addWidget( main );
468 QVBoxLayout* lMain = new QVBoxLayout( main );
469 lMain->setMargin( 0 );
470 lMain->setSpacing( 0 );
472 Area* topArea = new Area( Qt::Horizontal, this, main );
473 QLabel* topLine = new Border( main );
474 lMain->addWidget( topArea );
475 lMain->addWidget( topLine );
477 QWidget* midGroup = new QWidget( main );
478 lMain->addWidget( midGroup );
480 QVBoxLayout* midLyout = new QVBoxLayout( midGroup );
481 midLyout->setMargin( 0 );
482 midLyout->setSpacing( 0 );
484 QLabel* botLine = new Border( main );
485 Area* botArea = new Area( Qt::Horizontal, this, main );
486 lMain->addWidget( botLine );
487 lMain->addWidget( botArea );
489 Area* leftArea = new Area( Qt::Vertical, this, midGroup );
490 QLabel* leftLine = new Border( midGroup );
491 midLyout->addWidget( leftArea );
492 midLyout->addWidget( leftLine );
494 myMainFrame = new QFrame( midGroup );
495 midLyout->addWidget( myMainFrame );
497 QLabel* rightLine = new Border( midGroup );
498 Area* rightArea = new Area( Qt::Vertical, this, midGroup );
499 midLyout->addWidget( rightLine );
500 midLyout->addWidget( rightArea );
502 myMainFrame->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
504 topLine->setFrameStyle( QFrame::Sunken | QFrame::HLine );
505 botLine->setFrameStyle( QFrame::Sunken | QFrame::HLine );
506 leftLine->setFrameStyle( QFrame::Sunken | QFrame::VLine );
507 rightLine->setFrameStyle( QFrame::Sunken | QFrame::VLine );
509 topArea->setBorderWidget( topLine );
510 botArea->setBorderWidget( botLine );
511 leftArea->setBorderWidget( leftLine );
512 rightArea->setBorderWidget( rightLine );
514 myArea.insert( TopArea, topArea );
515 myArea.insert( BottomArea, botArea );
516 myArea.insert( LeftArea, leftArea );
517 myArea.insert( RightArea, rightArea );
519 for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++ itr )
520 itr.value()->setBorderEnabled( false );
522 myButton.insert( OK, new QPushButton( tr( "&OK" ), this ) );
523 myButton.insert( Cancel, new QPushButton( tr( "&Cancel" ), this ) );
524 myButton.insert( Close, new QPushButton( tr( "C&lose" ), this ) );
525 myButton.insert( Help, new QPushButton( tr( "&Help" ), this ) );
526 myButton.insert( Apply, new QPushButton( tr( "&Apply" ), this ) );
527 myButton.insert( Yes, new QPushButton( tr( "&Yes" ), this ) );
528 myButton.insert( No, new QPushButton( tr( "&No" ), this ) );
530 for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
532 ((QPushButton*)it.value())->setAutoDefault( false );
533 connect( it.value(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
536 setButtonPosition( Left, OK | Cancel | Apply );
537 setButtonPosition( Center, Yes | No | Close );
538 setButtonPosition( Right, Help );
539 setButtonPlace( BottomArea, All );
541 connect( myButton[Apply], SIGNAL( clicked() ), this, SIGNAL( dlgApply() ) );
542 connect( myButton[Help], SIGNAL( clicked() ), this, SIGNAL( dlgHelp() ) );
544 connect( myButton[OK], SIGNAL( clicked() ), this, SLOT( onAccept() ) );
545 connect( myButton[Cancel], SIGNAL( clicked() ), this, SLOT( onReject() ) );
546 connect( myButton[Yes], SIGNAL( clicked() ), this, SLOT( onAccept() ) );
547 connect( myButton[No], SIGNAL( clicked() ), this, SLOT( onReject() ) );
548 connect( myButton[Close], SIGNAL( clicked() ), this, SLOT( onReject() ) );
551 QWidget* p = parentWidget();
552 while( p && p->parentWidget() )
553 p = p->parentWidget();
556 setWindowIcon( p->windowIcon() );
562 setMaximumSize( minimumSize() );
571 QtxDialog::~QtxDialog()
576 \brief Add specified control button(s) to the dialog box.
577 \param f ORed buttons flags (Qtx::ButtonFlags)
578 \sa clearButtonFlags(), testButtonFlags()
580 void QtxDialog::setButtonFlags( const int f )
582 int old = myButtonFlags;
583 myButtonFlags = myButtonFlags | f;
584 if ( old != myButtonFlags )
589 \brief Remove specified control button(s) from the dialog box.
590 \param f ORed buttons flags (Qtx::ButtonFlags)
591 \sa setButtonFlags(), testButtonFlags()
593 void QtxDialog::clearButtonFlags( const int f )
595 int old = myButtonFlags;
596 myButtonFlags = myButtonFlags & ~f;
597 if ( old != myButtonFlags )
602 \brief Test specified buttons.
603 \return \c true if specified control buttons are used in the dialog box
604 \sa setButtonFlags(), clearButtonFlags()
606 bool QtxDialog::testButtonFlags( const int f ) const
608 return ( myButtonFlags & f ) == f;
612 \brief Set specified dialog box flags.
613 \param f dialog box flags (QtxDialog::DialogFlags)
614 \sa clearDialogFlags(), testDialogFlags(), acceptData(), rejectData()
616 void QtxDialog::setDialogFlags( const int f )
618 myDialogFlags = myDialogFlags | f;
622 \brief Clear specified the dialog flags.
623 \param f dialog box flags (QtxDialog::DialogFlags)
624 \sa setDialogFlags(), testDialogFlags()
626 void QtxDialog::clearDialogFlags( const int f )
628 myDialogFlags = myDialogFlags & ~f;
632 \brief Test specified dialog flags.
633 \return \c true if specified dialog box falgs are set
634 \sa setDialogFlags(), clearDialogFlags()
636 bool QtxDialog::testDialogFlags( const int f ) const
638 return ( myDialogFlags & f ) == f;
642 \brief Get dialog box main frame widget.
644 Main frame is an internal widget which should contains all
645 elements of dialog box except control buttons.
647 \return main frame widget
649 QFrame* QtxDialog::mainFrame() const
655 \brief Get specified control button position
656 \param id control button ID (QtxDialog::ButtonFlags)
657 \return button's position (QtxDialog::ButtonPosition) or -1 if it is not set or invalid \a id is given
658 \sa setButtonPosition()
660 int QtxDialog::buttonPosition( const int id ) const
663 if ( myPosition.contains( id ) )
664 pos = myPosition[id];
668 \brief Set the specified control button(s) position.
669 \param id control button(s) ID (QtxDialog::ButtonFlags)
670 \param pos button(s) position (QtxDialog::ButtonPosition)
672 void QtxDialog::setButtonPosition( const int pos, const int id )
674 ButtonMap map = buttons( id );
676 QMap<QObject*, int> changed;
677 for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
679 if ( myPosition[it.key()] == pos )
682 myPosition[it.key()] = pos;
683 if ( button( it.key() ) )
684 changed.insert( button( it.key() )->parent(), 0 );
687 for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++itr )
689 if ( changed.contains( itr.value() ) )
690 itr.value()->layoutButtons();
695 \brief Set button position for all buttons in specified \a area.
696 \param pos button(s) position (QtxDialog::ButtonPosition)
697 \param area buttons area (QtxDialog::ButtonArea)
698 \sa setButtonPosition()
700 void QtxDialog::setPlacePosition( const int pos, const int area )
702 if ( !myArea.contains( area ) )
705 Area* anArea = myArea[area];
707 bool changed = false;
708 for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
710 if ( !anArea->contains( it.value() ) )
713 changed = changed && myPosition[it.key()] != pos;
715 myPosition[it.key()] = pos;
719 anArea->layoutButtons();
723 \brief Get buttons layouting policy for the specified \a area.
724 \param area buttons area (QtxDialog::ButtonArea)
727 int QtxDialog::placePolicy( const int area ) const
730 if ( myArea.contains( area ) )
731 res = myArea[area]->policy();
736 \brief set buttons layouting policy for the specified \a area.
737 \param policy buttons layouting policy (QtxDialog::PlacePolicy)
738 \param area buttons area (QtxDialog::ButtonArea)
741 void QtxDialog::setPlacePolicy( const int policy, const int area )
745 for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++itr )
746 itr.value()->setPolicy( policy );
748 else if ( myArea.contains( area ) )
749 myArea[area]->setPolicy( policy );
753 \brief Move specified button(s) into specified area.
754 \param area buttons area (QtxDialog::ButtonArea)
755 \param id control button(s) ID (QtxDialog::ButtonFlags)
757 void QtxDialog::setButtonPlace( const int area, const int id )
759 if ( !myArea.contains( area ) )
762 Area* anArea = myArea[area];
763 ButtonMap map = buttons( id );
764 QMap<Area*, int> areaMap;
765 for ( AreaMap::ConstIterator aIt = myArea.begin(); aIt != myArea.end(); ++aIt )
766 areaMap.insert( aIt.value(), 0 );
768 for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
770 Area* old = (Area*)it.value()->parent();
774 if ( areaMap.contains( old ) )
775 old->removeButton( it.value() );
776 anArea->insertButton( it.value() );
781 \brief Check if border is enabled.
782 \param area buttons area (QtxDialog::ButtonArea)
783 \return \c true if border is enabled for specified button area.
784 \sa setBorderEnabled()
786 bool QtxDialog::isBorderEnabled( const int area ) const
789 if ( myArea.contains( area ) )
790 res = myArea[area]->isBorderEnabled();
795 \brief Show/hide border for the specified button area.
797 Border is a line which separate main frame and control buttons.
799 \param area buttons area (QtxDialog::ButtonArea)
800 \param on enable border flag
801 \sa isBorderEnabled()
803 void QtxDialog::setBorderEnabled( const bool on, const int area )
805 if ( !myArea.contains( area ) )
808 if ( myArea[area]->isBorderEnabled() == on )
811 myArea[area]->setBorderEnabled( on );
815 QApplication::sendPostedEvents();
821 \brief Get "enabled" status of the specified button(s).
822 \param id control button(s) ID (QtxDialog::ButtonFlags)
823 \return \c true if all specified buttons are enabled.
824 \sa setButtonEnabled()
826 bool QtxDialog::isButtonEnabled( const int id ) const
828 ButtonMap map = buttons( id );
829 bool result = !map.isEmpty();
830 for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
831 result = result && it.value()->isEnabled();
836 \brief Enable/disable specified button(s).
837 \param on enable button(s) flag
838 \param id control button(s) ID (QtxDialog::ButtonFlags)
839 \sa isButtonEnabled()
841 void QtxDialog::setButtonEnabled( const bool on, const int id )
843 ButtonMap map = buttons( id );
844 for ( ButtonMap::Iterator it = map.begin(); it != map.end(); ++it )
845 it.value()->setEnabled( on );
849 \brief Check if specified button has keyboard focus.
850 \param id control button ID (QtxDialog::ButtonFlags)
851 \return \c true if specified button has keyboard focus
854 bool QtxDialog::hasButtonFocus( const int id ) const
857 QAbstractButton* pb = button( id );
859 res = pb->hasFocus();
864 \brief Sets the keyboard focus to the specified button.
865 \param id control button ID (QtxDialog::ButtonFlags)
868 void QtxDialog::setButtonFocus( const int id )
870 QAbstractButton* pb = button( id );
876 \brief Get specified button's text.
877 \param id control button ID (QtxDialog::ButtonFlags)
878 \return button's text
881 QString QtxDialog::buttonText( const int id )
884 QAbstractButton* but = button( id );
886 retval = but->text();
891 \brief Set specified button's text.
892 \param id control button ID (QtxDialog::ButtonFlags)
893 \param text button's text
896 void QtxDialog::setButtonText( const int id, const QString& text )
898 QAbstractButton* but = button( id );
899 if ( but && but->text() != text )
901 but->setText( text );
907 \brief Sets alignment policy.
909 Use the function before the the dialog is first time shown.
910 If dialog flag AlignOnce is set then alignment is performed
911 only once, otherwise the dialog is aligned each time when it
913 Dialog box is aligned relatively to its parent.
914 By default, dialog box is aligned to the center of the parent
915 widget (usually desktop or another dialog box).
917 \param align alignment flag(s) (Qtx::AlignmentFlags)
918 \return previous alignment policy
920 uint QtxDialog::setAlignment( uint align )
922 uint oldAlign = myAlignment;
928 \brief Update dialog box.
930 void QtxDialog::update()
932 for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
934 it.value()->setVisible( testButtonFlags( it.key() ) );
936 for ( AreaMap::Iterator itr = myArea.begin(); itr != myArea.end(); ++itr )
937 itr.value()->layoutButtons();
945 \brief Show/hide dialog box, set keyboard focus to the dialog.
947 Re-implemented from Qt.
949 \param on show/hide flag
951 void QtxDialog::setVisible( bool on )
953 resize( sizeHint() );
955 QDialog::setVisible( on );
959 if ( testDialogFlags( SetFocus ) )
964 QApplication::instance()->processEvents();
968 \brief Get user button by the specified \a id.
969 \param id user button ID
970 \return user button or 0 if it is not found
971 \sa insertButton(), removeButton(), userButtonIds()
973 QAbstractButton* QtxDialog::userButton( const int id ) const
975 QAbstractButton* b = 0;
976 if ( id < -1 && myButton.contains( id ) )
982 \brief Get all user button IDs.
983 \return list of user buttons identificators
984 \sa insertButton(), removeButton(), userButton()
986 QIntList QtxDialog::userButtonIds() const
989 for ( ButtonMap::ConstIterator it = myButton.begin(); it != myButton.end(); ++it )
991 retlist.append( it.key() );
996 \brief Add user button to the dialog box.
998 The button is inserted to the specified dialog box area.
999 if the button is added successfully, the unique identificator of
1000 the added button is returned, otherwise -1 is returned.
1002 \param text text of the added button
1003 \param area buttons area (QtxDialog::ButtonArea)
1005 \sa removeButton(), userButton(), userButtonIds()
1007 int QtxDialog::insertButton( const QString& text, const int area )
1009 if ( !myArea.contains( area ) )
1013 while ( myButton.contains( id ) )
1016 Area* anArea = myArea[area];
1017 QAbstractButton* b = createButton( this );
1021 myButton.insert( id, b );
1022 myPosition.insert( id, Left );
1024 connect( b, SIGNAL( clicked() ), this, SLOT( onButton() ) );
1025 connect( b, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
1027 anArea->insertButton( b );
1037 \brief Remove user button.
1039 If \c id is -1, all user buttons are removed.
1041 \param id user button ID
1042 \sa insertButton(), userButton(), userButtonIds()
1044 void QtxDialog::removeButton( const int id )
1052 for ( ButtonMap::Iterator it = myButton.begin(); it != myButton.end(); ++it )
1055 map.insert( it.key(), it.value() );
1058 else if ( myButton.contains( id ) )
1059 map.insert( id, myButton[id] );
1061 for ( ButtonMap::Iterator itr = map.begin(); itr != map.end(); ++itr )
1063 for ( AreaMap::Iterator it = myArea.begin(); it != myArea.end(); ++it )
1064 it.value()->removeButton( itr.value() );
1066 myButton.remove( itr.key() );
1067 myPosition.remove( itr.key() );
1075 \brief Set measure units to the specified label.
1077 In the dialog box label the measure units are closed in braces.
1078 If measure units do not exist they will be added.
1082 // create label "Radius"
1083 QLabel* aLabel = new QLabel( "Radius", mainFrame() );
1084 // set measure units to "mm"
1085 setUnits( aLabel, "mm" ) // => aLabel contains 'Radius (mm)'
1086 // set measure units to "cm"
1087 setUnits( aLabel, "cm" ) // => aLabel contains 'Radius (cm)'
1089 // create label "Radius" with initially not set measure units
1090 QLabel* aLabel = new QLabel( "Radius ():", mainFrame() );
1091 // set measure units to "mm"
1092 setUnits( aLabel, "mm" ) // => aLabel contains 'Radius (mm):'
1093 // set measure units to "cm"
1094 setUnits( aLabel, "cm" ) // => aLabel contains 'Radius (cm):'
1097 \param aLabel label widget
1098 \param aUnits measure units
1100 void QtxDialog::setUnits( QLabel* aLabel, const QString& aUnits )
1102 QString label = aLabel->text();
1105 int end = label.lastIndexOf( ')' );
1107 QString startLabel = label;
1112 begin = label.left( end ).lastIndexOf( '(' );
1115 startLabel = label.mid( 0, begin );
1116 finalLabel = label.mid( end + 1 );
1121 startLabel = startLabel.trimmed();
1122 if ( startLabel.at( startLabel.length() - 1 ) == ':' )
1124 finalLabel = startLabel.mid( startLabel.length() - 1 );
1125 startLabel = startLabel.mid( 0, startLabel.length() - 1 );
1128 if ( aUnits.length() )
1129 label = startLabel.trimmed() + " (" + aUnits + ") " + finalLabel.trimmed();
1131 label = startLabel.trimmed() + " " + finalLabel.trimmed();
1132 aLabel->setText( label );
1136 \brief Check if data entered by the user is valid.
1138 This method can be re-implemented in the successor class if it
1139 requires to check user input consistency.
1140 Default implementation returns \c true.
1142 This method is called if dialog flag QtxDialog::Accept is set.
1143 If this method returns \c true, then dialog will be accepted and closed.
1145 \return \c true if user input is valid
1148 bool QtxDialog::acceptData() const
1154 \brief Check if dialog box can be cancelled.
1156 This method can be re-implemented in the successor class if it
1157 requires to check possibility to cancel dialog box safely.
1158 Default implementation returns \c true.
1160 This method is called if dialog flag QtxDialog::Reject is set.
1161 If this method returns \c true, then dialog will be rejected and closed.
1163 \return \c true if dialog box can be cancelled
1166 bool QtxDialog::rejectData() const
1172 \brief Create new user button.
1174 This method is invoked from method insertButton().
1176 \param parent parent widget
1177 \return new user button
1179 QAbstractButton* QtxDialog::createButton( QWidget* parent )
1181 QPushButton* pb = new QPushButton( parent );
1182 pb->setAutoDefault( false );
1187 \brief Get button by the specified ID.
1188 \param f control button ID (QtxDialog::ButtonFlags)
1189 \return button or 0 if \a id is invalid
1191 QAbstractButton* QtxDialog::button( const int f ) const
1193 QAbstractButton* retval = 0;
1194 if ( myButton.contains( f ) )
1195 retval = myButton[f];
1200 \brief Get buttons by the specified IDs.
1201 \param f control button(s) ID(s) (QtxDialog::ButtonFlags)
1204 QtxDialog::ButtonMap QtxDialog::buttons( const int f ) const
1209 if ( myButton.contains( f ) )
1210 retmap.insert( f, myButton[f] );
1214 for ( ButtonMap::ConstIterator it = myButton.begin(); it != myButton.end(); ++it )
1215 if ( f == -1 || ( it.key() >= 0 && f & it.key() ) )
1216 retmap.insert( it.key(), it.value() );
1222 \brief Get specified button's identifier.
1226 int QtxDialog::buttonId( const QAbstractButton* b ) const
1229 for ( ButtonMap::ConstIterator it = myButton.begin(); it != myButton.end() && id == -1; ++it )
1230 if ( it.value() == b )
1236 \brief Get position of specified button.
1238 \return button position (QtxDialog::ButtonPosition)
1240 int QtxDialog::buttonPosition( QAbstractButton* b ) const
1242 return buttonPosition( buttonId( b ) );
1246 \brief Align this dialog according to the parent widget and alignment
1247 policy before the dialog box is shown.
1249 Re-implemented from Qt.
1253 void QtxDialog::showEvent( QShowEvent* e )
1255 if ( !testDialogFlags( AlignOnce ) || !myInited )
1256 Qtx::alignWidget( this, parentWidget(), myAlignment );
1257 QDialog::showEvent( e );
1261 \brief Process all existing events when dialog box is hidden.
1263 Re-implemented from Qt.
1267 void QtxDialog::hideEvent( QHideEvent* e )
1269 QApplication::instance()->processEvents();
1270 QDialog::hideEvent( e );
1274 \brief Update dialog box layout when the size grip is added.
1276 Re-implemented from Qt.
1278 \param e child event
1280 void QtxDialog::childEvent( QChildEvent* e )
1282 QDialog::childEvent( e );
1283 if ( layout() && e->added() && e->child()->inherits( "QSizeGrip" ) )
1285 layout()->setMargin( 12 );
1286 connect( e->child(), SIGNAL( destroyed() ), this, SLOT( onSizeGripDestroyed() ) );
1291 \brief Process key pressing event.
1293 Re-implemented from Qt.
1295 Call reject() if "Escape" key is pressed.
1296 Call accept() if "Ctrl+Enter" key-sequence is pressed.
1297 Process "F1" key and emit signal dlgHelp().
1298 Transfer "Ctrl+(Shift+)Tab" key-sequence press event
1299 to the child Tab widget (if there is any).
1301 \param e key press event
1303 void QtxDialog::keyPressEvent( QKeyEvent* e )
1305 QDialog::keyPressEvent( e );
1307 if ( e->isAccepted() )
1310 if ( !e->modifiers() && e->key() == Qt::Key_Escape )
1313 if ( e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Return )
1315 if ( testButtonFlags( OK ) || testButtonFlags( Yes ) )
1317 else if ( testButtonFlags( Apply ) && isButtonEnabled( Apply ) )
1322 if ( e->key() == Qt::Key_F1 && testButtonFlags( Help ) && isButtonEnabled( Help ) )
1328 if ( e->key() == Qt::Key_Tab && e->modifiers() & Qt::ControlModifier )
1330 QObject* tab = this->findChild<QTabWidget*>();
1331 if ( tab && !property( "in_tab_event" ).toBool() ) {
1332 setProperty( "in_tab_event", true );
1333 QApplication::sendEvent( tab, e );
1334 setProperty( "in_tab_event", false );
1340 \brief Called when user closes dialog box.
1342 Call reject() method.
1344 \param e close event (not used)
1346 void QtxDialog::closeEvent( QCloseEvent* /*e*/ )
1352 \brief Accept the dialog box.
1354 This method is used when any accept button is pressed (usually
1357 If dialog flag QtxDialog::Accept is set, this function invokes
1358 acceptData() method, which should in this case return \c true to
1359 allow further processing.
1361 If acceptData() returns \c false, this function just returns.
1363 If acceptData() returns \c true, the Accepted result is set
1364 and signal according to the pressed control button is emitted.
1365 Then the default implementation of accept() method is called
1366 (which hides the dialog box and, depending on the dialog box flags,
1367 can close and destroy it).
1371 void QtxDialog::accept()
1375 if ( testButtonFlags( OK ) )
1376 mySender = button( OK );
1377 else if ( testButtonFlags( Yes ) )
1378 mySender = button( Yes );
1380 mySender = button( Close );
1383 if ( !mySender || !mySender->isWidgetType() ||
1384 !((QWidget*)mySender)->isEnabled() )
1387 if ( testDialogFlags( Accept ) && !acceptData() )
1396 \brief Reject the dialog box.
1398 This method is used when any reject button is pressed (usually
1399 "Close", "Cancel", "No", etc).
1401 If dialog flag QtxDialog::Reject is set, this function invokes
1402 rejectData() method, which should in this case return \c true to
1403 allow further processing.
1405 If rejectData() returns \c false, this function just returns.
1407 If rejectData() returns \c true, the Rejected result is set
1408 and signal according to the pressed control button is emitted.
1409 Then the default implementation of reject() method is called
1410 (which hides the dialog box and, depending on the dialog box flags,
1411 can close and destroy it).
1415 void QtxDialog::reject()
1417 if ( testDialogFlags( Reject ) && !rejectData() )
1422 if ( testButtonFlags( Cancel ) )
1423 mySender = button( Cancel );
1424 else if ( testButtonFlags( No ) )
1425 mySender = button( No );
1427 mySender = button( Close );
1430 if ( !mySender || !mySender->isWidgetType() ||
1431 !((QWidget*)mySender)->isEnabled() )
1440 \brief Emit signal correspondingly to the control button.
1442 void QtxDialog::emitSignal()
1444 QApplication::instance()->processEvents();
1445 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
1446 QApplication::syncX();
1448 int id = buttonId( (QAbstractButton*)mySender );
1472 \brief This slot is called when user presses on of the buttons
1475 Call accept() method.
1477 void QtxDialog::onAccept()
1479 const QObject* obj = sender();
1485 \brief This slot is called when user presses on of the buttons
1486 "Cancel", "No", "Close".
1488 Call reject() method.
1491 void QtxDialog::onReject()
1493 const QObject* obj = sender();
1499 \brief Process user button click event.
1501 This method is called when user presses one of custom user buttons.
1502 Emits signal dlgButton(int) with identificator of the clicked user
1503 button passed as parameter.
1505 void QtxDialog::onButton()
1507 int id = buttonId( (QAbstractButton*)sender() );
1509 emit dlgButton( id );
1513 \brief Watch for the user button destroying.
1514 \param obj button being destroyed
1516 void QtxDialog::onDestroyed( QObject* obj )
1518 QAbstractButton* b = (QAbstractButton*)obj;
1519 int id = buttonId( b );
1523 myButton.remove( id );
1524 myPosition.remove( id );
1525 for ( AreaMap::Iterator it = myArea.begin(); it != myArea.end(); ++it )
1526 it.value()->removeButton( b );
1530 \brief Update dialog box layout when the size grip is destroyed.
1532 void QtxDialog::onSizeGripDestroyed()
1535 layout()->setMargin( 5 );
1539 \brief Adjust buttons (set equal size for all buttons).
1541 void QtxDialog::adjustButtons()
1544 for ( AreaMap::Iterator aIt = myArea.begin(); aIt != myArea.end(); ++aIt )
1546 const QList<QAbstractButton*>& lst = aIt.value()->buttons();
1547 for ( QList<QAbstractButton*>::const_iterator bIt = lst.begin(); bIt != lst.end(); ++bIt )
1548 if ( (*bIt)->isVisibleTo( this ) )
1549 minWidth = qMax( minWidth, (*bIt)->sizeHint().width() );
1552 for ( AreaMap::Iterator aItr = myArea.begin(); aItr != myArea.end(); ++aItr )
1554 const QList<QAbstractButton*>& lst = aItr.value()->buttons();
1555 for ( QList<QAbstractButton*>::const_iterator bItr = lst.begin(); bItr != lst.end(); ++bItr )
1556 if ( (*bItr)->isVisibleTo( this ) )
1557 (*bItr)->setMinimumWidth( minWidth );
1562 \fn void QtxDialog::dlgButton( int id )
1563 \brief Emitted when the user button is clicked.
1564 \param id user button identificator
1567 \fn void QtxDialog::dlgParamChanged()
1568 \brief This signal can be used in successor classes to signalize about
1569 some dialog parameter changing.
1572 \fn void QtxDialog::dlgHelp()
1573 \brief Emitted when the "Help" button is clicked.
1576 \fn void QtxDialog::dlgApply()
1577 \brief Emitted when the "Apply" button is clicked.
1580 \fn void QtxDialog::dlgOk()
1581 \brief Emitted when the "OK" button is clicked.
1584 \fn void QtxDialog::dlgNo()
1585 \brief Emitted when the "No" button is clicked.
1588 \fn void QtxDialog::dlgYes()
1589 \brief Emitted when the "Yes" button is clicked.
1592 \fn void QtxDialog::dlgClose()
1593 \brief Emitted when the "Close" button is clicked.
1596 \fn void QtxDialog::dlgCancel()
1597 \brief Emitted when the "Cancel" button is clicked.