Qt 4.8
qabstractbutton.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractbutton.h"
43 #include "qabstractitemview.h"
44 #include "qbuttongroup.h"
45 #include "qabstractbutton_p.h"
46 #include "qevent.h"
47 #include "qpainter.h"
48 #include "qapplication.h"
49 #include "qstyle.h"
50 #include "qaction.h"
51 #ifndef QT_NO_ACCESSIBILITY
52 #include "qaccessible.h"
53 #endif
54 
56 
57 #define AUTO_REPEAT_DELAY 300
58 #define AUTO_REPEAT_INTERVAL 100
59 
61 
168  :
169 #ifndef QT_NO_SHORTCUT
170  shortcutId(0),
171 #endif
172  checkable(false), checked(false), autoRepeat(false), autoExclusive(false),
173  down(false), blockRefresh(false), pressed(false),
174 #ifndef QT_NO_BUTTONGROUP
175  group(0),
176 #endif
177  autoRepeatDelay(AUTO_REPEAT_DELAY),
178  autoRepeatInterval(AUTO_REPEAT_INTERVAL),
179  controlType(type)
180 {}
181 
182 #ifndef QT_NO_BUTTONGROUP
183 
185 {
187 
188 public:
189  QButtonGroupPrivate():exclusive(true){}
192  void detectCheckedButton();
193  void notifyChecked(QAbstractButton *button);
194  bool exclusive;
196 };
197 
199  : QObject(*new QButtonGroupPrivate, parent)
200 {
201 }
202 
204 {
205  Q_D(QButtonGroup);
206  for (int i = 0; i < d->buttonList.count(); ++i)
207  d->buttonList.at(i)->d_func()->group = 0;
208 }
209 
210 
211 bool QButtonGroup::exclusive() const
212 {
213  Q_D(const QButtonGroup);
214  return d->exclusive;
215 }
216 
218 {
219  Q_D(QButtonGroup);
220  d->exclusive = exclusive;
221 }
222 
223 
224 // TODO: Qt 5: Merge with addButton(QAbstractButton *button, int id)
226 {
227  addButton(button, -1);
228 }
229 
231 {
232  Q_D(QButtonGroup);
233  if (QButtonGroup *previous = button->d_func()->group)
234  previous->removeButton(button);
235  button->d_func()->group = this;
236  d->buttonList.append(button);
237  if (id == -1) {
238  QList<int> ids = d->mapping.values();
239  if (ids.isEmpty())
240  d->mapping[button] = -2;
241  else {
242  qSort(ids);
243  d->mapping[button] = ids.first()-1;
244  }
245  } else {
246  d->mapping[button] = id;
247  }
248 
249  if (d->exclusive && button->isChecked())
250  button->d_func()->notifyChecked();
251 }
252 
254 {
255  Q_D(QButtonGroup);
256  if (d->checkedButton == button) {
257  d->detectCheckedButton();
258  }
259  if (button->d_func()->group == this) {
260  button->d_func()->group = 0;
261  d->buttonList.removeAll(button);
262  d->mapping.remove(button);
263  }
264 }
265 
267 {
268  Q_D(const QButtonGroup);
269  return d->buttonList;
270 }
271 
273 {
274  Q_D(const QButtonGroup);
275  return d->checkedButton;
276 }
277 
279 {
280  Q_D(const QButtonGroup);
281  return d->mapping.key(id);
282 }
283 
285 {
286  Q_D(QButtonGroup);
287  if (button && id != -1)
288  d->mapping[button] = id;
289 }
290 
292 {
293  Q_D(const QButtonGroup);
294  return d->mapping.value(button, -1);
295 }
296 
298 {
299  Q_D(const QButtonGroup);
300  return d->mapping.value(d->checkedButton, -1);
301 }
302 
303 // detect a checked button other than the current one
305 {
306  QAbstractButton *previous = checkedButton;
307  checkedButton = 0;
308  if (exclusive)
309  return;
310  for (int i = 0; i < buttonList.count(); i++) {
311  if (buttonList.at(i) != previous && buttonList.at(i)->isChecked()) {
312  checkedButton = buttonList.at(i);
313  return;
314  }
315  }
316 }
317 
318 #endif // QT_NO_BUTTONGROUP
319 
321 {
322 #ifndef QT_NO_BUTTONGROUP
323  if (group)
324  return group->d_func()->buttonList;
325 #endif
326 
328  if (autoExclusive) {
329  for (int i = candidates.count() - 1; i >= 0; --i) {
330  QAbstractButton *candidate = candidates.at(i);
331  if (!candidate->autoExclusive()
332 #ifndef QT_NO_BUTTONGROUP
333  || candidate->group()
334 #endif
335  )
336  candidates.removeAt(i);
337  }
338  }
339  return candidates;
340 }
341 
343 {
344 #ifndef QT_NO_BUTTONGROUP
345  if (group)
346  return group->d_func()->checkedButton;
347 #endif
348 
349  Q_Q(const QAbstractButton);
350  QList<QAbstractButton *> buttonList = queryButtonList();
351  if (!autoExclusive || buttonList.count() == 1) // no group
352  return 0;
353 
354  for (int i = 0; i < buttonList.count(); ++i) {
355  QAbstractButton *b = buttonList.at(i);
356  if (b->d_func()->checked && b != q)
357  return b;
358  }
359  return checked ? const_cast<QAbstractButton *>(q) : 0;
360 }
361 
363 {
364 #ifndef QT_NO_BUTTONGROUP
366  if (group) {
367  QAbstractButton *previous = group->d_func()->checkedButton;
368  group->d_func()->checkedButton = q;
369  if (group->d_func()->exclusive && previous && previous != q)
370  previous->nextCheckState();
371  } else
372 #endif
373  if (autoExclusive) {
374  if (QAbstractButton *b = queryCheckedButton())
375  b->setChecked(false);
376  }
377 }
378 
380 {
381  QList<QAbstractButton *> buttonList = queryButtonList();;
382 #ifndef QT_NO_BUTTONGROUP
383  bool exclusive = group ? group->d_func()->exclusive : autoExclusive;
384 #else
385  bool exclusive = autoExclusive;
386 #endif
389  if (!fb || !buttonList.contains(fb))
390  return;
391 
392  QAbstractButton *candidate = 0;
393  int bestScore = -1;
394  QRect target = f->rect().translated(f->mapToGlobal(QPoint(0,0)));
395  QPoint goal = target.center();
397 
398  for (int i = 0; i < buttonList.count(); ++i) {
399  QAbstractButton *button = buttonList.at(i);
400  if (button != f && button->window() == f->window() && button->isEnabled() && !button->isHidden() &&
401  (autoExclusive || (button->focusPolicy() & focus_flag) == focus_flag)) {
402  QRect buttonRect = button->rect().translated(button->mapToGlobal(QPoint(0,0)));
403  QPoint p = buttonRect.center();
404 
405  //Priority to widgets that overlap on the same coordinate.
406  //In that case, the distance in the direction will be used as significant score,
407  //take also in account orthogonal distance in case two widget are in the same distance.
408  int score;
409  if ((buttonRect.x() < target.right() && target.x() < buttonRect.right())
410  && (key == Qt::Key_Up || key == Qt::Key_Down)) {
411  //one item's is at the vertical of the other
412  score = (qAbs(p.y() - goal.y()) << 16) + qAbs(p.x() - goal.x());
413  } else if ((buttonRect.y() < target.bottom() && target.y() < buttonRect.bottom())
414  && (key == Qt::Key_Left || key == Qt::Key_Right) ) {
415  //one item's is at the horizontal of the other
416  score = (qAbs(p.x() - goal.x()) << 16) + qAbs(p.y() - goal.y());
417  } else {
418  score = (1 << 30) + (p.y() - goal.y()) * (p.y() - goal.y()) + (p.x() - goal.x()) * (p.x() - goal.x());
419  }
420 
421  if (score > bestScore && candidate)
422  continue;
423 
424  switch(key) {
425  case Qt::Key_Up:
426  if (p.y() < goal.y()) {
427  candidate = button;
428  bestScore = score;
429  }
430  break;
431  case Qt::Key_Down:
432  if (p.y() > goal.y()) {
433  candidate = button;
434  bestScore = score;
435  }
436  break;
437  case Qt::Key_Left:
438  if (p.x() < goal.x()) {
439  candidate = button;
440  bestScore = score;
441  }
442  break;
443  case Qt::Key_Right:
444  if (p.x() > goal.x()) {
445  candidate = button;
446  bestScore = score;
447  }
448  break;
449  }
450  }
451  }
452 
453  if (exclusive
454 #ifdef QT_KEYPAD_NAVIGATION
455  && !QApplication::keypadNavigationEnabled()
456 #endif
457  && candidate
458  && fb->d_func()->checked
459  && candidate->d_func()->checkable)
460  candidate->click();
461 
462  if (candidate) {
463  if (key == Qt::Key_Up || key == Qt::Key_Left)
464  candidate->setFocus(Qt::BacktabFocusReason);
465  else
466  candidate->setFocus(Qt::TabFocusReason);
467  }
468 }
469 
471 {
473 #ifndef QT_NO_BUTTONGROUP
474  if (!group && !autoExclusive)
475 #else
476  if (!autoExclusive)
477 #endif
478  return;
479 
480  QList<QAbstractButton *> buttonList = queryButtonList();
481  for (int i = 0; i < buttonList.count(); ++i) {
482  QAbstractButton *b = buttonList.at(i);
483  if (!b->isCheckable())
484  continue;
485  b->setFocusPolicy((Qt::FocusPolicy) ((b == q || !q->isCheckable())
486  ? (b->focusPolicy() | Qt::TabFocus)
487  : (b->focusPolicy() & ~Qt::TabFocus)));
488  }
489 }
490 
492 {
494 
495  q->setFocusPolicy(Qt::FocusPolicy(q->style()->styleHint(QStyle::SH_Button_FocusPolicy)));
496  q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, controlType));
497  q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
498  q->setForegroundRole(QPalette::ButtonText);
499  q->setBackgroundRole(QPalette::Button);
500 }
501 
503 {
505 
506  if (blockRefresh)
507  return;
508  q->update();
509 #ifndef QT_NO_ACCESSIBILITY
511 #endif
512 }
513 
515 {
517 
518  down = false;
519  blockRefresh = true;
520  bool changeState = true;
521  if (checked && queryCheckedButton() == q) {
522  // the checked button of an exclusive or autoexclusive group cannot be unchecked
523 #ifndef QT_NO_BUTTONGROUP
524  if (group ? group->d_func()->exclusive : autoExclusive)
525 #else
526  if (autoExclusive)
527 #endif
528  changeState = false;
529  }
530 
531  QPointer<QAbstractButton> guard(q);
532  if (changeState) {
533  q->nextCheckState();
534  if (!guard)
535  return;
536  }
537  blockRefresh = false;
538  refresh();
539  q->repaint(); //flush paint event before invoking potentially expensive operation
541  if (guard)
542  emitReleased();
543  if (guard)
544  emitClicked();
545 }
546 
548 {
550  QPointer<QAbstractButton> guard(q);
551  emit q->clicked(checked);
552 #ifndef QT_NO_BUTTONGROUP
553  if (guard && group) {
554  emit group->buttonClicked(group->id(q));
555  if (guard && group)
556  emit group->buttonClicked(q);
557  }
558 #endif
559 }
560 
562 {
564  QPointer<QAbstractButton> guard(q);
565  emit q->pressed();
566 #ifndef QT_NO_BUTTONGROUP
567  if (guard && group) {
568  emit group->buttonPressed(group->id(q));
569  if (guard && group)
570  emit group->buttonPressed(q);
571  }
572 #endif
573 }
574 
576 {
578  QPointer<QAbstractButton> guard(q);
579  emit q->released();
580 #ifndef QT_NO_BUTTONGROUP
581  if (guard && group) {
582  emit group->buttonReleased(group->id(q));
583  if (guard && group)
584  emit group->buttonReleased(q);
585  }
586 #endif
587 }
588 
593  : QWidget(*new QAbstractButtonPrivate, parent, 0)
594 {
596  d->init();
597 }
598 
603 {
604 #ifndef QT_NO_BUTTONGROUP
606  if (d->group)
607  d->group->removeButton(this);
608 #endif
609 }
610 
611 
615  : QWidget(dd, parent, 0)
616 {
618  d->init();
619 }
620 
642 {
644  if (d->text == text)
645  return;
646  d->text = text;
647 #ifndef QT_NO_SHORTCUT
648  QKeySequence newMnemonic = QKeySequence::mnemonic(text);
649  setShortcut(newMnemonic);
650 #endif
651  d->sizeHint = QSize();
652  update();
653  updateGeometry();
654 #ifndef QT_NO_ACCESSIBILITY
656 #endif
657 }
658 
660 {
661  Q_D(const QAbstractButton);
662  return d->text;
663 }
664 
665 
677 {
679  d->icon = icon;
680  d->sizeHint = QSize();
681  update();
682  updateGeometry();
683 }
684 
686 {
687  Q_D(const QAbstractButton);
688  return d->icon;
689 }
690 
691 #ifndef QT_NO_SHORTCUT
692 
701 {
703  if (d->shortcutId != 0)
704  releaseShortcut(d->shortcutId);
705  d->shortcut = key;
706  d->shortcutId = grabShortcut(key);
707 }
708 
710 {
711  Q_D(const QAbstractButton);
712  return d->shortcut;
713 }
714 #endif // QT_NO_SHORTCUT
715 
728 {
730  if (d->checkable == checkable)
731  return;
732 
733  d->checkable = checkable;
734  d->checked = false;
735 }
736 
738 {
739  Q_D(const QAbstractButton);
740  return d->checkable;
741 }
742 
755 {
757  if (!d->checkable || d->checked == checked) {
758  if (!d->blockRefresh)
759  checkStateSet();
760  return;
761  }
762 
763  if (!checked && d->queryCheckedButton() == this) {
764  // the checked button of an exclusive or autoexclusive group cannot be unchecked
765 #ifndef QT_NO_BUTTONGROUP
766  if (d->group ? d->group->d_func()->exclusive : d->autoExclusive)
767  return;
768  if (d->group)
769  d->group->d_func()->detectCheckedButton();
770 #else
771  if (d->autoExclusive)
772  return;
773 #endif
774  }
775 
776  QPointer<QAbstractButton> guard(this);
777 
778  d->checked = checked;
779  if (!d->blockRefresh)
780  checkStateSet();
781  d->refresh();
782 
783  if (guard && checked)
784  d->notifyChecked();
785  if (guard)
786  emit toggled(checked);
787 }
788 
790 {
791  Q_D(const QAbstractButton);
792  return d->checked;
793 }
794 
808 {
810  if (d->down == down)
811  return;
812  d->down = down;
813  d->refresh();
814  if (d->autoRepeat && d->down)
815  d->repeatTimer.start(d->autoRepeatDelay, this);
816  else
817  d->repeatTimer.stop();
818 }
819 
821 {
822  Q_D(const QAbstractButton);
823  return d->down;
824 }
825 
843 {
845  if (d->autoRepeat == autoRepeat)
846  return;
847  d->autoRepeat = autoRepeat;
848  if (d->autoRepeat && d->down)
849  d->repeatTimer.start(d->autoRepeatDelay, this);
850  else
851  d->repeatTimer.stop();
852 }
853 
854 bool QAbstractButton::autoRepeat() const
855 {
856  Q_D(const QAbstractButton);
857  return d->autoRepeat;
858 }
859 
875 {
877  d->autoRepeatDelay = autoRepeatDelay;
878 }
879 
881 {
882  Q_D(const QAbstractButton);
883  return d->autoRepeatDelay;
884 }
885 
901 {
903  d->autoRepeatInterval = autoRepeatInterval;
904 }
905 
907 {
908  Q_D(const QAbstractButton);
909  return d->autoRepeatInterval;
910 }
911 
912 
913 
935 {
937  d->autoExclusive = autoExclusive;
938 }
939 
941 {
942  Q_D(const QAbstractButton);
943  return d->autoExclusive;
944 }
945 
946 #ifndef QT_NO_BUTTONGROUP
947 
956 {
957  Q_D(const QAbstractButton);
958  return d->group;
959 }
960 #endif // QT_NO_BUTTONGROUP
961 
977 {
978  if (!isEnabled())
979  return;
981  if (d->checkable && focusPolicy() & Qt::ClickFocus)
982  setFocus();
983  setDown(true);
984  repaint(); //flush paint event before invoking potentially expensive operation
986  if (!d->animateTimer.isActive())
987  d->emitPressed();
988  d->animateTimer.start(msec, this);
989 }
990 
1004 {
1005  if (!isEnabled())
1006  return;
1008  QPointer<QAbstractButton> guard(this);
1009  d->down = true;
1010  d->emitPressed();
1011  if (guard) {
1012  d->down = false;
1013  nextCheckState();
1014  if (guard)
1015  d->emitReleased();
1016  if (guard)
1017  d->emitClicked();
1018  }
1019 }
1020 
1031 {
1033  setChecked(!d->checked);
1034 }
1035 
1036 
1044 {
1045 }
1046 
1055 {
1056  if (isCheckable())
1057  setChecked(!isChecked());
1058 }
1059 
1069 {
1070  return rect().contains(pos);
1071 }
1072 
1075 {
1076  // as opposed to other widgets, disabled buttons accept mouse
1077  // events. This avoids surprising click-through scenarios
1078  if (!isEnabled()) {
1079  switch(e->type()) {
1080  case QEvent::TabletPress:
1081  case QEvent::TabletRelease:
1082  case QEvent::TabletMove:
1086  case QEvent::MouseMove:
1087  case QEvent::HoverMove:
1088  case QEvent::HoverEnter:
1089  case QEvent::HoverLeave:
1090  case QEvent::ContextMenu:
1091 #ifndef QT_NO_WHEELEVENT
1092  case QEvent::Wheel:
1093 #endif
1094  return true;
1095  default:
1096  break;
1097  }
1098  }
1099 
1100 #ifndef QT_NO_SHORTCUT
1101  if (e->type() == QEvent::Shortcut) {
1103  QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
1104  if (d->shortcutId != se->shortcutId())
1105  return false;
1106  if (!se->isAmbiguous()) {
1107  if (!d->animateTimer.isActive())
1108  animateClick();
1109  } else {
1110  if (focusPolicy() != Qt::NoFocus)
1113  }
1114  return true;
1115  }
1116 #endif
1117  return QWidget::event(e);
1118 }
1119 
1122 {
1124  if (e->button() != Qt::LeftButton) {
1125  e->ignore();
1126  return;
1127  }
1128  if (hitButton(e->pos())) {
1129  setDown(true);
1130  d->pressed = true;
1131  repaint(); //flush paint event before invoking potentially expensive operation
1133  d->emitPressed();
1134  e->accept();
1135  } else {
1136  e->ignore();
1137  }
1138 }
1139 
1142 {
1144  d->pressed = false;
1145 
1146  if (e->button() != Qt::LeftButton) {
1147  e->ignore();
1148  return;
1149  }
1150 
1151  if (!d->down) {
1152  e->ignore();
1153  return;
1154  }
1155 
1156  if (hitButton(e->pos())) {
1157  d->repeatTimer.stop();
1158  d->click();
1159  e->accept();
1160  } else {
1161  setDown(false);
1162  e->ignore();
1163  }
1164 }
1165 
1168 {
1170  if (!(e->buttons() & Qt::LeftButton) || !d->pressed) {
1171  e->ignore();
1172  return;
1173  }
1174 
1175  if (hitButton(e->pos()) != d->down) {
1176  setDown(!d->down);
1177  repaint(); //flush paint event before invoking potentially expensive operation
1179  if (d->down)
1180  d->emitPressed();
1181  else
1182  d->emitReleased();
1183  e->accept();
1184  } else if (!hitButton(e->pos())) {
1185  e->ignore();
1186  }
1187 }
1188 
1191 {
1193  bool next = true;
1194  switch (e->key()) {
1195  case Qt::Key_Enter:
1196  case Qt::Key_Return:
1197  e->ignore();
1198  break;
1199  case Qt::Key_Select:
1200  case Qt::Key_Space:
1201  if (!e->isAutoRepeat()) {
1202  setDown(true);
1203  repaint(); //flush paint event before invoking potentially expensive operation
1205  d->emitPressed();
1206  }
1207  break;
1208  case Qt::Key_Up:
1209  case Qt::Key_Left:
1210  next = false;
1211  // fall through
1212  case Qt::Key_Right:
1213  case Qt::Key_Down:
1214 #ifdef QT_KEYPAD_NAVIGATION
1215  if ((QApplication::keypadNavigationEnabled()
1216  && (e->key() == Qt::Key_Left || e->key() == Qt::Key_Right))
1217  || (!QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional
1218  || (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))) {
1219  e->ignore();
1220  return;
1221  }
1222 #endif
1223  QWidget *pw;
1224  if (d->autoExclusive
1225 #ifndef QT_NO_BUTTONGROUP
1226  || d->group
1227 #endif
1228 #ifndef QT_NO_ITEMVIEWS
1229  || ((pw = parentWidget()) && qobject_cast<QAbstractItemView *>(pw->parentWidget()))
1230 #endif
1231  ) {
1232  // ### Using qobject_cast to check if the parent is a viewport of
1233  // QAbstractItemView is a crude hack, and should be revisited and
1234  // cleaned up when fixing task 194373. It's here to ensure that we
1235  // keep compatibility outside QAbstractItemView.
1236  d->moveFocus(e->key());
1237  if (hasFocus()) // nothing happend, propagate
1238  e->ignore();
1239  } else {
1240  focusNextPrevChild(next);
1241  }
1242  break;
1243  case Qt::Key_Escape:
1244  if (d->down) {
1245  setDown(false);
1246  repaint(); //flush paint event before invoking potentially expensive operation
1248  d->emitReleased();
1249  break;
1250  }
1251  // fall through
1252  default:
1253  e->ignore();
1254  }
1255 }
1256 
1259 {
1261 
1262  if (!e->isAutoRepeat())
1263  d->repeatTimer.stop();
1264 
1265  switch (e->key()) {
1266  case Qt::Key_Select:
1267  case Qt::Key_Space:
1268  if (!e->isAutoRepeat() && d->down)
1269  d->click();
1270  break;
1271  default:
1272  e->ignore();
1273  }
1274 }
1275 
1279 {
1281  if (e->timerId() == d->repeatTimer.timerId()) {
1282  d->repeatTimer.start(d->autoRepeatInterval, this);
1283  if (d->down) {
1284  QPointer<QAbstractButton> guard(this);
1285  nextCheckState();
1286  if (guard)
1287  d->emitReleased();
1288  if (guard)
1289  d->emitClicked();
1290  if (guard)
1291  d->emitPressed();
1292  }
1293  } else if (e->timerId() == d->animateTimer.timerId()) {
1294  d->animateTimer.stop();
1295  d->click();
1296  }
1297 }
1298 
1301 {
1303 #ifdef QT_KEYPAD_NAVIGATION
1304  if (!QApplication::keypadNavigationEnabled())
1305 #endif
1306  d->fixFocusPolicy();
1308 }
1309 
1312 {
1314  if (e->reason() != Qt::PopupFocusReason)
1315  d->down = false;
1317 }
1318 
1321 {
1323  switch (e->type()) {
1324  case QEvent::EnabledChange:
1325  if (!isEnabled())
1326  setDown(false);
1327  break;
1328  default:
1329  d->sizeHint = QSize();
1330  break;
1331  }
1333 }
1334 
1417 {
1418  Q_D(const QAbstractButton);
1419  if (d->iconSize.isValid())
1420  return d->iconSize;
1421  int e = style()->pixelMetric(QStyle::PM_ButtonIconSize, 0, this);
1422  return QSize(e, e);
1423 }
1424 
1426 {
1428  if (d->iconSize == size)
1429  return;
1430 
1431  d->iconSize = size;
1432  d->sizeHint = QSize();
1433  updateGeometry();
1434  if (isVisible()) {
1435  update();
1436  }
1437 }
1438 
1439 
1440 #ifdef QT3_SUPPORT
1441 
1444 QIcon *QAbstractButton::iconSet() const
1445 {
1446  Q_D(const QAbstractButton);
1447  if (!d->icon.isNull())
1448  return const_cast<QIcon *>(&d->icon);
1449  return 0;
1450 }
1451 
1458 QAbstractButton::QAbstractButton(QWidget *parent, const char *name, Qt::WindowFlags f)
1459  : QWidget(*new QAbstractButtonPrivate, parent, f)
1460 {
1463  d->init();
1464 }
1465 
1538 #endif
1539 
The QAbstractButton class is the abstract base class of button widgets, providing functionality commo...
QPoint pos() const
double d
Definition: qnumeric_p.h:62
~QAbstractButton()
Destroys the button.
static void updateAccessibility(QObject *, int who, Event reason)
Notifies accessibility clients about a change in object&#39;s accessibility information.
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QList< QAbstractButton * > buttons() const
Returns the list of this groups&#39;s buttons.
int type
Definition: qmetatype.cpp:239
bool isChecked() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString text() const
void setText(const QString &text)
QSize size() const
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
void setAutoRepeatDelay(int)
QAbstractButton * checkedButton() const
Returns the button group&#39;s checked button, or 0 if no buttons are checked.
void setShortcut(const QKeySequence &key)
bool isVisible() const
Definition: qwidget.h:1005
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
void removeButton(QAbstractButton *)
Removes the given button from the button group.
static QKeySequence mnemonic(const QString &text)
Returns the shortcut key sequence for the mnemonic in text, or an empty key sequence if no mnemonics ...
void timerEvent(QTimerEvent *e)
This event handler can be reimplemented in a subclass to receive timer events for the object...
#define QT_NO_BUTTONGROUP
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
bool isDown() const
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
Definition: qevent.h:675
QRect translated(int dx, int dy) const
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis...
Definition: qrect.h:328
void keyReleaseEvent(QKeyEvent *e)
Reimplemented Function
virtual void checkStateSet()
This virtual handler is called when setChecked() was called, unless it was called from within nextChe...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
void addButton(QAbstractButton *)
Adds the given button to the end of the group&#39;s internal list of buttons.
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool hasFocus() const
Definition: qwidget.cpp:6583
T * qobject_cast(QObject *object)
Definition: qobject.h:375
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
void setCheckable(bool)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
void focusOutEvent(QFocusEvent *e)
Reimplemented Function
void setId(QAbstractButton *button, int id)
Qt::FocusPolicy focusPolicy() const
void releaseShortcut(int id)
Removes the shortcut with the given id from Qt&#39;s shortcut system.
Definition: qwidget.cpp:11771
virtual void focusOutEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
Definition: qwidget.cpp:9457
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
QStyle * style() const
Definition: qwidget.cpp:2742
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
bool autoExclusive() const
#define Q_Q(Class)
Definition: qglobal.h:2483
void setAutoRepeat(bool)
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition: qwidget.h:1008
QAbstractButton(QWidget *parent=0)
Constructs an abstract button with a parent.
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
int key() const
Returns the code of the key that was pressed or released.
Definition: qevent.h:231
bool event(QEvent *e)
Reimplemented Function
void mouseMoveEvent(QMouseEvent *e)
Reimplemented Function
bool checked
whether the button is checked
int checkedId() const
Returns the id of the checkedButton(), or -1 if no button is checked.
void setExclusive(bool)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int id(QAbstractButton *button) const
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
void keyPressEvent(QKeyEvent *e)
Reimplemented Function
QAbstractButton * button(int id) const
virtual void nextCheckState()
This virtual handler is called when a button is clicked.
QWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a widget which is a child of parent, with widget flags set to f.
Definition: qwidget.cpp:1189
const char * name
~QButtonGroup()
Destroys the button group.
QList< QAbstractButton * > queryButtonList() const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define emit
Definition: qobjectdefs.h:76
virtual void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qwidget.cpp:9170
void focusInEvent(QFocusEvent *e)
Reimplemented Function
int timerId() const
Returns the unique timer identifier, which is the same identifier as returned from QObject::startTime...
Definition: qcoreevent.h:346
QButtonGroup * group() const
Returns the group that this button belongs to.
QAbstractButton * queryCheckedButton() const
unsigned int uint
Definition: qglobal.h:996
bool isCheckable() const
#define AUTO_REPEAT_DELAY
Qt::MouseButton button() const
Returns the button that caused the event.
Definition: qevent.h:101
QRect rect() const
void qSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:177
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
bool isEnabled() const
Definition: qwidget.h:948
QButtonGroup(QObject *parent=0)
Constructs a new, empty button group with the given parent.
bool exclusive() const
#define QT_NO_SHORTCUT
bool checkable
whether the button is checkable
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
void repaint()
Repaints the widget directly by calling paintEvent() immediately, unless updates are disabled or the ...
Definition: qwidget.cpp:10761
int grabShortcut(const QKeySequence &key, Qt::ShortcutContext context=Qt::WindowShortcut)
Adds a shortcut to Qt&#39;s shortcut system that watches for the given key sequence in the given context...
Definition: qwidget.cpp:11747
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
QPointer< QAbstractButton > checkedButton
QPoint center() const
Returns the center point of the rectangle.
Definition: qrect.h:300
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432
bool autoRepeat() const
int shortcutId()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qevent.h:683
QSize iconSize() const
bool down
whether the button is pressed down
void mouseReleaseEvent(QMouseEvent *e)
Reimplemented Function
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void setAutoExclusive(bool)
QAbstractButtonPrivate(QSizePolicy::ControlType type=QSizePolicy::DefaultType)
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
void changeEvent(QEvent *e)
Reimplemented Function
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition: qevent.h:102
QMap< QAbstractButton *, int > mapping
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
int autoRepeatInterval() const
QRect rect
the internal geometry of the widget excluding any window frame
Definition: qwidget.h:168
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
bool autoExclusive
whether auto-exclusivity is enabled
void toggle()
Toggles the state of a checkable button.
void setIconSize(const QSize &size)
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void setAutoRepeatInterval(int)
#define AUTO_REPEAT_INTERVAL
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
virtual bool focusNextPrevChild(bool next)
Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it can&#39;t.
Definition: qwidget.cpp:6836
FocusPolicy
Definition: qnamespace.h:181
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
bool isAutoRepeat() const
Returns true if this event comes from an auto-repeating key; returns false if it comes from an initia...
Definition: qevent.h:237
Qt::FocusReason reason()
Definition: qevent.cpp:1197
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QKeySequence shortcut() const
Q_GUI_EXPORT bool qt_tab_all_widgets
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
QObject * parent
Definition: qobject.h:92
void ignore()
Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
Definition: qcoreevent.h:310
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
Definition: qcoreevent.h:309
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isAmbiguous()
Definition: qevent.h:685
void click()
Performs a click.
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
void updateGeometry()
Notifies the layout system that this widget has changed and may need to change geometry.
Definition: qwidget.cpp:10372
bool event(QEvent *)
This is the main event handler; it handles event event.
Definition: qwidget.cpp:8636
virtual bool hitButton(const QPoint &pos) const
Returns true if pos is inside the clickable button rectangle; otherwise returns false.
void setIcon(const QIcon &icon)
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or 0 if no widget in this applicati...
The QButtonGroup class provides a container to organize groups of button widgets. ...
Definition: qbuttongroup.h:59
#define QT_NO_ITEMVIEWS
virtual void focusInEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
Definition: qwidget.cpp:9431
QList< T > findChildren(const QString &aName=QString()) const
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
Definition: qobject.h:162
QPoint mapToGlobal(const QPoint &) const
Translates the widget coordinate pos to global screen coordinates.
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
QList< QAbstractButton * > buttonList
static void flush()
Flushes the platform specific event queues.
void mousePressEvent(QMouseEvent *e)
Reimplemented Function
void toggled(bool checked)
This signal is emitted whenever a checkable button changes its state.
QIcon icon() const
void setFocusPolicy(Qt::FocusPolicy policy)
Definition: qwidget.cpp:7631
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void animateClick(int msec=100)
Performs an animated click: the button is pressed immediately, and released msec milliseconds later (...
int autoRepeatDelay() const
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480