src/gui/widgets/qdialogbuttonbox.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    44 #include <QtGui/qstyle.h>
    44 #include <QtGui/qstyle.h>
    45 #include <QtGui/qlayout.h>
    45 #include <QtGui/qlayout.h>
    46 #include <QtGui/qdialog.h>
    46 #include <QtGui/qdialog.h>
    47 #include <QtGui/qapplication.h>
    47 #include <QtGui/qapplication.h>
    48 #include <QtGui/private/qwidget_p.h>
    48 #include <QtGui/private/qwidget_p.h>
       
    49 #include <QtGui/qaction.h>
    49 
    50 
    50 #include "qdialogbuttonbox.h"
    51 #include "qdialogbuttonbox.h"
    51 
    52 
    52 #ifdef QT_SOFTKEYS_ENABLED
    53 #ifdef QT_SOFTKEYS_ENABLED
    53 #include <QtGui/qaction.h>
    54 #include <QtGui/qaction.h>
   256 
   257 
   257         // Mac modeless
   258         // Mac modeless
   258         { ActionRole, ApplyRole, ResetRole, Stretch, HelpRole, EOL, EOL, EOL, EOL, EOL, EOL, EOL, EOL, EOL }
   259         { ActionRole, ApplyRole, ResetRole, Stretch, HelpRole, EOL, EOL, EOL, EOL, EOL, EOL, EOL, EOL, EOL }
   259     }
   260     }
   260 };
   261 };
       
   262 
       
   263 class QDialogButtonEnabledProxy : public QObject
       
   264 {
       
   265 public:
       
   266     QDialogButtonEnabledProxy(QObject *parent, QWidget *src, QAction *trg) : QObject(parent), source(src), target(trg)
       
   267     {
       
   268         source->installEventFilter(this);
       
   269         target->setEnabled(source->isEnabled());
       
   270     }
       
   271     ~QDialogButtonEnabledProxy()
       
   272     {
       
   273         source->removeEventFilter(this);
       
   274     }
       
   275     bool eventFilter(QObject *object, QEvent *event)
       
   276     {
       
   277         if (object == source && event->type() == QEvent::EnabledChange) {
       
   278             target->setEnabled(source->isEnabled());
       
   279         }
       
   280         return false;
       
   281     };
       
   282 private:
       
   283     QWidget *source;
       
   284     QAction *target;
       
   285 };
       
   286 
   261 
   287 
   262 class QDialogButtonBoxPrivate : public QWidgetPrivate
   288 class QDialogButtonBoxPrivate : public QWidgetPrivate
   263 {
   289 {
   264     Q_DECLARE_PUBLIC(QDialogButtonBox)
   290     Q_DECLARE_PUBLIC(QDialogButtonBox)
   265 
   291 
   313             buttonLayout = new QHBoxLayout(q);
   339             buttonLayout = new QHBoxLayout(q);
   314         else
   340         else
   315             buttonLayout = new QVBoxLayout(q);
   341             buttonLayout = new QVBoxLayout(q);
   316     }
   342     }
   317 
   343 
   318 	int left, top, right, bottom;
   344     int left, top, right, bottom;
   319     setLayoutItemMargins(QStyle::SE_PushButtonLayoutItem);
   345     setLayoutItemMargins(QStyle::SE_PushButtonLayoutItem);
   320 	getLayoutItemMargins(&left, &top, &right, &bottom);
   346     getLayoutItemMargins(&left, &top, &right, &bottom);
   321     buttonLayout->setContentsMargins(-left, -top, -right, -bottom);
   347     buttonLayout->setContentsMargins(-left, -top, -right, -bottom);
   322 
   348 
   323     if (!q->testAttribute(Qt::WA_WState_OwnSizePolicy)) {
   349     if (!q->testAttribute(Qt::WA_WState_OwnSizePolicy)) {
   324         QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::ButtonBox);
   350         QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::ButtonBox);
   325         if (orientation == Qt::Vertical)
   351         if (orientation == Qt::Vertical)
   354 }
   380 }
   355 
   381 
   356 void QDialogButtonBoxPrivate::layoutButtons()
   382 void QDialogButtonBoxPrivate::layoutButtons()
   357 {
   383 {
   358     Q_Q(QDialogButtonBox);
   384     Q_Q(QDialogButtonBox);
   359     const int MacGap = 36 - 8;	// 8 is the default gap between a widget and a spacer item
   385     const int MacGap = 36 - 8;    // 8 is the default gap between a widget and a spacer item
   360 
   386 
   361     for (int i = buttonLayout->count() - 1; i >= 0; --i) {
   387     for (int i = buttonLayout->count() - 1; i >= 0; --i) {
   362         QLayoutItem *item = buttonLayout->takeAt(i);
   388         QLayoutItem *item = buttonLayout->takeAt(i);
   363         if (QWidget *widget = item->widget())
   389         if (QWidget *widget = item->widget())
   364             widget->hide();
   390             widget->hide();
   546     Q_Q(QDialogButtonBox);
   572     Q_Q(QDialogButtonBox);
   547     QObject::connect(button, SIGNAL(clicked()), q, SLOT(_q_handleButtonClicked()));
   573     QObject::connect(button, SIGNAL(clicked()), q, SLOT(_q_handleButtonClicked()));
   548     QObject::connect(button, SIGNAL(destroyed()), q, SLOT(_q_handleButtonDestroyed()));
   574     QObject::connect(button, SIGNAL(destroyed()), q, SLOT(_q_handleButtonDestroyed()));
   549     buttonLists[role].append(button);
   575     buttonLists[role].append(button);
   550 #ifdef QT_SOFTKEYS_ENABLED
   576 #ifdef QT_SOFTKEYS_ENABLED
   551     softKeyActions.insert(button, createSoftKey(button, role));
   577     QAction *action = createSoftKey(button, role);
       
   578     softKeyActions.insert(button, action);
       
   579     new QDialogButtonEnabledProxy(action, button, action);
   552 #endif
   580 #endif
   553     if (doLayout)
   581     if (doLayout)
   554         layoutButtons();
   582         layoutButtons();
   555 }
   583 }
   556 
   584 
   579     default:
   607     default:
   580         break;
   608         break;
   581     }
   609     }
   582     QObject::connect(action, SIGNAL(triggered()), button, SIGNAL(clicked()));
   610     QObject::connect(action, SIGNAL(triggered()), button, SIGNAL(clicked()));
   583     action->setSoftKeyRole(softkeyRole);
   611     action->setSoftKeyRole(softkeyRole);
       
   612 
       
   613 
       
   614     QWidget *dialog = 0;
       
   615     QWidget *p = q;
       
   616     while (p && !p->isWindow()) {
       
   617         p = p->parentWidget();
       
   618         if ((dialog = qobject_cast<QDialog *>(p)))
       
   619             break;
       
   620     }
       
   621 
       
   622     if (dialog) {
       
   623         dialog->addAction(action);
       
   624     } else {
       
   625         q->addAction(action);
       
   626     }
       
   627 
   584     return action;
   628     return action;
   585 }
   629 }
   586 #endif
   630 #endif
   587 
   631 
   588 void QDialogButtonBoxPrivate::createStandardButtons(QDialogButtonBox::StandardButtons buttons)
   632 void QDialogButtonBoxPrivate::createStandardButtons(QDialogButtonBox::StandardButtons buttons)
  1191             }
  1235             }
  1192         }
  1236         }
  1193         if (!hasDefault && firstAcceptButton)
  1237         if (!hasDefault && firstAcceptButton)
  1194             firstAcceptButton->setDefault(true);
  1238             firstAcceptButton->setDefault(true);
  1195 #ifdef QT_SOFTKEYS_ENABLED
  1239 #ifdef QT_SOFTKEYS_ENABLED
  1196         if (dialog) {
  1240         if (dialog)
  1197             setFixedSize(0,0);
  1241             setFixedSize(0,0);
  1198             dialog->addActions(d->softKeyActions.values());
       
  1199         } else {
       
  1200             addActions(d->softKeyActions.values());
       
  1201         }
       
  1202 #endif
  1242 #endif
  1203     }else if (event->type() == QEvent::LanguageChange) {
  1243     }else if (event->type() == QEvent::LanguageChange) {
  1204         d->retranslateStrings();
  1244         d->retranslateStrings();
  1205     }
  1245     }
       
  1246 #ifdef QT_SOFTKEYS_ENABLED
       
  1247     else if (event->type() == QEvent::ParentChange) {
       
  1248         QWidget *dialog = 0;
       
  1249         QWidget *p = this;
       
  1250         while (p && !p->isWindow()) {
       
  1251             p = p->parentWidget();
       
  1252             if ((dialog = qobject_cast<QDialog *>(p)))
       
  1253                 break;
       
  1254         }
       
  1255 
       
  1256         // If the parent changes, then move the softkeys
       
  1257         for (QHash<QAbstractButton *, QAction *>::const_iterator it = d->softKeyActions.constBegin();
       
  1258             it != d->softKeyActions.constEnd(); ++it) {
       
  1259             QAction *current = it.value();
       
  1260             QList<QWidget *> widgets = current->associatedWidgets();
       
  1261             foreach (QWidget *w, widgets)
       
  1262                 w->removeAction(current);
       
  1263             if (dialog)
       
  1264                 dialog->addAction(current);
       
  1265             else
       
  1266                 addAction(current);
       
  1267         }
       
  1268     }
       
  1269 #endif
  1206 
  1270 
  1207     return QWidget::event(event);
  1271     return QWidget::event(event);
  1208 }
  1272 }
  1209 
  1273 
  1210 QT_END_NAMESPACE
  1274 QT_END_NAMESPACE