| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qabstractbutton.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||
| 2 | - | |||||||
| 3 | - | |||||||
| 4 | - | |||||||
| 5 | - | |||||||
| 6 | - | |||||||
| 7 | - | |||||||
| 8 | - | |||||||
| 9 | - | |||||||
| 10 | - | |||||||
| 11 | - | |||||||
| 12 | __attribute__((visibility("default"))) extern bool qt_tab_all_widgets(); | - | ||||||
| 13 | QAbstractButtonPrivate::QAbstractButtonPrivate(QSizePolicy::ControlType type) | - | ||||||
| 14 | : | - | ||||||
| 15 | - | |||||||
| 16 | shortcutId(0), | - | ||||||
| 17 | - | |||||||
| 18 | checkable(false), checked(false), autoRepeat(false), autoExclusive(false), | - | ||||||
| 19 | down(false), blockRefresh(false), pressed(false), | - | ||||||
| 20 | - | |||||||
| 21 | group(0), | - | ||||||
| 22 | - | |||||||
| 23 | autoRepeatDelay(300), | - | ||||||
| 24 | autoRepeatInterval(100), | - | ||||||
| 25 | controlType(type) | - | ||||||
| 26 | {} | - | ||||||
| 27 | - | |||||||
| 28 | QList<QAbstractButton *>QAbstractButtonPrivate::queryButtonList() const | - | ||||||
| 29 | { | - | ||||||
| 30 | - | |||||||
| 31 | if (group
| 0 | ||||||
| 32 | return never executed: group->d_func()->buttonList;return group->d_func()->buttonList;never executed: return group->d_func()->buttonList; | 0 | ||||||
| 33 | - | |||||||
| 34 | - | |||||||
| 35 | QList<QAbstractButton*>candidates = parent->findChildren<QAbstractButton *>(); | - | ||||||
| 36 | if (autoExclusive
| 0 | ||||||
| 37 | for (int iauto isNoMemberOfMyAutoExclusiveGroup = candidates.count() - 1; i >= 0; --i) {[](QAbstractButton *candidate= candidates.at(i); | - | ||||||
| if (!) { | ||||||||
| 38 | return never executed: !return !candidate->autoExclusive() || candidate->group() ;never executed: return !candidate->autoExclusive() || candidate->group() ;never executed: candidate->autoExclusive()return !candidate->autoExclusive() || candidate->group() ;never executed: return !candidate->autoExclusive() || candidate->group() ; | 0 | ||||||
| 39 | 0 | |||||||
| 40 | || candidate->group() never executed: return !candidate->autoExclusive() || candidate->group() ; | 0 | ||||||
| 41 | 0 | |||||||
| 42 | ); never executed: return !candidate->autoExclusive() || candidate->group() ; | 0 | ||||||
| 43 | }; | - | ||||||
| 44 | candidates.removeAterase(i); | - | ||||||
| }std::remove_if(candidates.begin(), candidates.end(), | ||||||||
| 45 | isNoMemberOfMyAutoExclusiveGroup), | - | ||||||
| 46 | candidates.end()); | - | ||||||
| 47 | } never executed: end of block | 0 | ||||||
| 48 | return never executed: candidates;return candidates;never executed: return candidates; | 0 | ||||||
| 49 | } | - | ||||||
| 50 | - | |||||||
| 51 | QAbstractButton *QAbstractButtonPrivate::queryCheckedButton() const | - | ||||||
| 52 | { | - | ||||||
| 53 | - | |||||||
| 54 | if (group) | - | ||||||
| 55 | return group->d_func()->checkedButton; | - | ||||||
| 56 | - | |||||||
| 57 | - | |||||||
| 58 | const QAbstractButton * const q = q_func(); | - | ||||||
| 59 | QList<QAbstractButton *> buttonList = queryButtonList(); | - | ||||||
| 60 | if (!autoExclusive || buttonList.count() == 1) | - | ||||||
| 61 | return 0; | - | ||||||
| 62 | - | |||||||
| 63 | for (int i = 0; i < buttonList.count(); ++i) { | - | ||||||
| 64 | QAbstractButton *b = buttonList.at(i); | - | ||||||
| 65 | if (b->d_func()->checked && b != q) | - | ||||||
| 66 | return b; | - | ||||||
| 67 | } | - | ||||||
| 68 | return checked ? const_cast<QAbstractButton *>(q) : 0; | - | ||||||
| 69 | } | - | ||||||
| 70 | - | |||||||
| 71 | void QAbstractButtonPrivate::notifyChecked() | - | ||||||
| 72 | { | - | ||||||
| 73 | - | |||||||
| 74 | QAbstractButton * const q = q_func(); | - | ||||||
| 75 | if (group) { | - | ||||||
| 76 | QAbstractButton *previous = group->d_func()->checkedButton; | - | ||||||
| 77 | group->d_func()->checkedButton = q; | - | ||||||
| 78 | if (group->d_func()->exclusive && previous && previous != q) | - | ||||||
| 79 | previous->nextCheckState(); | - | ||||||
| 80 | } else | - | ||||||
| 81 | - | |||||||
| 82 | if (autoExclusive) { | - | ||||||
| 83 | if (QAbstractButton *b = queryCheckedButton()) | - | ||||||
| 84 | b->setChecked(false); | - | ||||||
| 85 | } | - | ||||||
| 86 | } | - | ||||||
| 87 | - | |||||||
| 88 | void QAbstractButtonPrivate::moveFocus(int key) | - | ||||||
| 89 | { | - | ||||||
| 90 | QList<QAbstractButton *> buttonList = queryButtonList();; | - | ||||||
| 91 | - | |||||||
| 92 | bool exclusive = group ? group->d_func()->exclusive : autoExclusive; | - | ||||||
| 93 | - | |||||||
| 94 | - | |||||||
| 95 | - | |||||||
| 96 | QWidget *f = QApplication::focusWidget(); | - | ||||||
| 97 | QAbstractButton *fb = qobject_cast<QAbstractButton *>(f); | - | ||||||
| 98 | if (!fb || !buttonList.contains(fb)) | - | ||||||
| 99 | return; | - | ||||||
| 100 | - | |||||||
| 101 | QAbstractButton *candidate = 0; | - | ||||||
| 102 | int bestScore = -1; | - | ||||||
| 103 | QRect target = f->rect().translated(f->mapToGlobal(QPoint(0,0))); | - | ||||||
| 104 | QPoint goal = target.center(); | - | ||||||
| 105 | uint focus_flag = qt_tab_all_widgets() ? Qt::TabFocus : Qt::StrongFocus; | - | ||||||
| 106 | - | |||||||
| 107 | for (int i = 0; i < buttonList.count(); ++i) { | - | ||||||
| 108 | QAbstractButton *button = buttonList.at(i); | - | ||||||
| 109 | if (button != f && button->window() == f->window() && button->isEnabled() && !button->isHidden() && | - | ||||||
| 110 | (autoExclusive || (button->focusPolicy() & focus_flag) == focus_flag)) { | - | ||||||
| 111 | QRect buttonRect = button->rect().translated(button->mapToGlobal(QPoint(0,0))); | - | ||||||
| 112 | QPoint p = buttonRect.center(); | - | ||||||
| 113 | - | |||||||
| 114 | - | |||||||
| 115 | - | |||||||
| 116 | - | |||||||
| 117 | int score; | - | ||||||
| 118 | if ((buttonRect.x() < target.right() && target.x() < buttonRect.right()) | - | ||||||
| 119 | && (key == Qt::Key_Up || key == Qt::Key_Down)) { | - | ||||||
| 120 | - | |||||||
| 121 | score = (qAbs(p.y() - goal.y()) << 16) + qAbs(p.x() - goal.x()); | - | ||||||
| 122 | } else if ((buttonRect.y() < target.bottom() && target.y() < buttonRect.bottom()) | - | ||||||
| 123 | && (key == Qt::Key_Left || key == Qt::Key_Right) ) { | - | ||||||
| 124 | - | |||||||
| 125 | score = (qAbs(p.x() - goal.x()) << 16) + qAbs(p.y() - goal.y()); | - | ||||||
| 126 | } else { | - | ||||||
| 127 | score = (1 << 30) + (p.y() - goal.y()) * (p.y() - goal.y()) + (p.x() - goal.x()) * (p.x() - goal.x()); | - | ||||||
| 128 | } | - | ||||||
| 129 | - | |||||||
| 130 | if (score > bestScore && candidate) | - | ||||||
| 131 | continue; | - | ||||||
| 132 | - | |||||||
| 133 | switch(key) { | - | ||||||
| 134 | case Qt::Key_Up: | - | ||||||
| 135 | if (p.y() < goal.y()) { | - | ||||||
| 136 | candidate = button; | - | ||||||
| 137 | bestScore = score; | - | ||||||
| 138 | } | - | ||||||
| 139 | break; | - | ||||||
| 140 | case Qt::Key_Down: | - | ||||||
| 141 | if (p.y() > goal.y()) { | - | ||||||
| 142 | candidate = button; | - | ||||||
| 143 | bestScore = score; | - | ||||||
| 144 | } | - | ||||||
| 145 | break; | - | ||||||
| 146 | case Qt::Key_Left: | - | ||||||
| 147 | if (p.x() < goal.x()) { | - | ||||||
| 148 | candidate = button; | - | ||||||
| 149 | bestScore = score; | - | ||||||
| 150 | } | - | ||||||
| 151 | break; | - | ||||||
| 152 | case Qt::Key_Right: | - | ||||||
| 153 | if (p.x() > goal.x()) { | - | ||||||
| 154 | candidate = button; | - | ||||||
| 155 | bestScore = score; | - | ||||||
| 156 | } | - | ||||||
| 157 | break; | - | ||||||
| 158 | } | - | ||||||
| 159 | } | - | ||||||
| 160 | } | - | ||||||
| 161 | - | |||||||
| 162 | if (exclusive | - | ||||||
| 163 | - | |||||||
| 164 | - | |||||||
| 165 | - | |||||||
| 166 | && candidate | - | ||||||
| 167 | && fb->d_func()->checked | - | ||||||
| 168 | && candidate->d_func()->checkable) | - | ||||||
| 169 | candidate->click(); | - | ||||||
| 170 | - | |||||||
| 171 | if (candidate) { | - | ||||||
| 172 | if (key == Qt::Key_Up || key == Qt::Key_Left) | - | ||||||
| 173 | candidate->setFocus(Qt::BacktabFocusReason); | - | ||||||
| 174 | else | - | ||||||
| 175 | candidate->setFocus(Qt::TabFocusReason); | - | ||||||
| 176 | } | - | ||||||
| 177 | } | - | ||||||
| 178 | - | |||||||
| 179 | void QAbstractButtonPrivate::fixFocusPolicy() | - | ||||||
| 180 | { | - | ||||||
| 181 | QAbstractButton * const q = q_func(); | - | ||||||
| 182 | - | |||||||
| 183 | if (!group && !autoExclusive) | - | ||||||
| 184 | - | |||||||
| 185 | - | |||||||
| 186 | - | |||||||
| 187 | return; | - | ||||||
| 188 | - | |||||||
| 189 | QList<QAbstractButton *> buttonList = queryButtonList(); | - | ||||||
| 190 | for (int i = 0; i < buttonList.count(); ++i) { | - | ||||||
| 191 | QAbstractButton *b = buttonList.at(i); | - | ||||||
| 192 | if (!b->isCheckable()) | - | ||||||
| 193 | continue; | - | ||||||
| 194 | b->setFocusPolicy((Qt::FocusPolicy) ((b == q || !q->isCheckable()) | - | ||||||
| 195 | ? (b->focusPolicy() | Qt::TabFocus) | - | ||||||
| 196 | : (b->focusPolicy() & ~Qt::TabFocus))); | - | ||||||
| 197 | } | - | ||||||
| 198 | } | - | ||||||
| 199 | - | |||||||
| 200 | void QAbstractButtonPrivate::init() | - | ||||||
| 201 | { | - | ||||||
| 202 | QAbstractButton * const q = q_func(); | - | ||||||
| 203 | - | |||||||
| 204 | q->setFocusPolicy(Qt::FocusPolicy(q->style()->styleHint(QStyle::SH_Button_FocusPolicy))); | - | ||||||
| 205 | q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, controlType)); | - | ||||||
| 206 | q->setAttribute(Qt::WA_WState_OwnSizePolicy, false); | - | ||||||
| 207 | q->setForegroundRole(QPalette::ButtonText); | - | ||||||
| 208 | q->setBackgroundRole(QPalette::Button); | - | ||||||
| 209 | } | - | ||||||
| 210 | - | |||||||
| 211 | void QAbstractButtonPrivate::refresh() | - | ||||||
| 212 | { | - | ||||||
| 213 | QAbstractButton * const q = q_func(); | - | ||||||
| 214 | - | |||||||
| 215 | if (blockRefresh) | - | ||||||
| 216 | return; | - | ||||||
| 217 | q->update(); | - | ||||||
| 218 | } | - | ||||||
| 219 | - | |||||||
| 220 | void QAbstractButtonPrivate::click() | - | ||||||
| 221 | { | - | ||||||
| 222 | QAbstractButton * const q = q_func(); | - | ||||||
| 223 | - | |||||||
| 224 | down = false; | - | ||||||
| 225 | blockRefresh = true; | - | ||||||
| 226 | bool changeState = true; | - | ||||||
| 227 | if (checked && queryCheckedButton() == q) { | - | ||||||
| 228 | - | |||||||
| 229 | - | |||||||
| 230 | if (group ? group->d_func()->exclusive : autoExclusive) | - | ||||||
| 231 | - | |||||||
| 232 | - | |||||||
| 233 | - | |||||||
| 234 | changeState = false; | - | ||||||
| 235 | } | - | ||||||
| 236 | - | |||||||
| 237 | QPointer<QAbstractButton> guard(q); | - | ||||||
| 238 | if (changeState) { | - | ||||||
| 239 | q->nextCheckState(); | - | ||||||
| 240 | if (!guard) | - | ||||||
| 241 | return; | - | ||||||
| 242 | } | - | ||||||
| 243 | blockRefresh = false; | - | ||||||
| 244 | refresh(); | - | ||||||
| 245 | q->repaint(); | - | ||||||
| 246 | QApplication::flush(); | - | ||||||
| 247 | if (guard) | - | ||||||
| 248 | emitReleased(); | - | ||||||
| 249 | if (guard) | - | ||||||
| 250 | emitClicked(); | - | ||||||
| 251 | } | - | ||||||
| 252 | - | |||||||
| 253 | void QAbstractButtonPrivate::emitClicked() | - | ||||||
| 254 | { | - | ||||||
| 255 | QAbstractButton * const q = q_func(); | - | ||||||
| 256 | QPointer<QAbstractButton> guard(q); | - | ||||||
| 257 | q->clicked(checked); | - | ||||||
| 258 | - | |||||||
| 259 | if (guard && group) { | - | ||||||
| 260 | group->buttonClicked(group->id(q)); | - | ||||||
| 261 | if (guard && group) | - | ||||||
| 262 | group->buttonClicked(q); | - | ||||||
| 263 | } | - | ||||||
| 264 | - | |||||||
| 265 | } | - | ||||||
| 266 | - | |||||||
| 267 | void QAbstractButtonPrivate::emitPressed() | - | ||||||
| 268 | { | - | ||||||
| 269 | QAbstractButton * const q = q_func(); | - | ||||||
| 270 | QPointer<QAbstractButton> guard(q); | - | ||||||
| 271 | q->pressed(); | - | ||||||
| 272 | - | |||||||
| 273 | if (guard && group) { | - | ||||||
| 274 | group->buttonPressed(group->id(q)); | - | ||||||
| 275 | if (guard && group) | - | ||||||
| 276 | group->buttonPressed(q); | - | ||||||
| 277 | } | - | ||||||
| 278 | - | |||||||
| 279 | } | - | ||||||
| 280 | - | |||||||
| 281 | void QAbstractButtonPrivate::emitReleased() | - | ||||||
| 282 | { | - | ||||||
| 283 | QAbstractButton * const q = q_func(); | - | ||||||
| 284 | QPointer<QAbstractButton> guard(q); | - | ||||||
| 285 | q->released(); | - | ||||||
| 286 | - | |||||||
| 287 | if (guard && group) { | - | ||||||
| 288 | group->buttonReleased(group->id(q)); | - | ||||||
| 289 | if (guard && group) | - | ||||||
| 290 | group->buttonReleased(q); | - | ||||||
| 291 | } | - | ||||||
| 292 | - | |||||||
| 293 | } | - | ||||||
| 294 | - | |||||||
| 295 | void QAbstractButtonPrivate::emitToggled(bool checked) | - | ||||||
| 296 | { | - | ||||||
| 297 | QAbstractButton * const q = q_func(); | - | ||||||
| 298 | QPointer<QAbstractButton> guard(q); | - | ||||||
| 299 | q->toggled(checked); | - | ||||||
| 300 | - | |||||||
| 301 | if (guard && group) { | - | ||||||
| 302 | group->buttonToggled(group->id(q), checked); | - | ||||||
| 303 | if (guard && group) | - | ||||||
| 304 | group->buttonToggled(q, checked); | - | ||||||
| 305 | } | - | ||||||
| 306 | - | |||||||
| 307 | } | - | ||||||
| 308 | - | |||||||
| 309 | - | |||||||
| 310 | - | |||||||
| 311 | - | |||||||
| 312 | QAbstractButton::QAbstractButton(QWidget *parent) | - | ||||||
| 313 | : QWidget(*new QAbstractButtonPrivate, parent, 0) | - | ||||||
| 314 | { | - | ||||||
| 315 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 316 | d->init(); | - | ||||||
| 317 | } | - | ||||||
| 318 | - | |||||||
| 319 | - | |||||||
| 320 | - | |||||||
| 321 | - | |||||||
| 322 | QAbstractButton::~QAbstractButton() | - | ||||||
| 323 | { | - | ||||||
| 324 | - | |||||||
| 325 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 326 | if (d->group) | - | ||||||
| 327 | d->group->removeButton(this); | - | ||||||
| 328 | - | |||||||
| 329 | } | - | ||||||
| 330 | - | |||||||
| 331 | - | |||||||
| 332 | - | |||||||
| 333 | - | |||||||
| 334 | QAbstractButton::QAbstractButton(QAbstractButtonPrivate &dd, QWidget *parent) | - | ||||||
| 335 | : QWidget(dd, parent, 0) | - | ||||||
| 336 | { | - | ||||||
| 337 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 338 | d->init(); | - | ||||||
| 339 | } | - | ||||||
| 340 | void QAbstractButton::setText(const QString &text) | - | ||||||
| 341 | { | - | ||||||
| 342 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 343 | if (d->text == text) | - | ||||||
| 344 | return; | - | ||||||
| 345 | d->text = text; | - | ||||||
| 346 | - | |||||||
| 347 | QKeySequence newMnemonic = QKeySequence::mnemonic(text); | - | ||||||
| 348 | setShortcut(newMnemonic); | - | ||||||
| 349 | - | |||||||
| 350 | d->sizeHint = QSize(); | - | ||||||
| 351 | update(); | - | ||||||
| 352 | updateGeometry(); | - | ||||||
| 353 | - | |||||||
| 354 | QAccessibleEvent event(this, QAccessible::NameChanged); | - | ||||||
| 355 | QAccessible::updateAccessibility(&event); | - | ||||||
| 356 | - | |||||||
| 357 | } | - | ||||||
| 358 | - | |||||||
| 359 | QString QAbstractButton::text() const | - | ||||||
| 360 | { | - | ||||||
| 361 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 362 | return d->text; | - | ||||||
| 363 | } | - | ||||||
| 364 | void QAbstractButton::setIcon(const QIcon &icon) | - | ||||||
| 365 | { | - | ||||||
| 366 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 367 | d->icon = icon; | - | ||||||
| 368 | d->sizeHint = QSize(); | - | ||||||
| 369 | update(); | - | ||||||
| 370 | updateGeometry(); | - | ||||||
| 371 | } | - | ||||||
| 372 | - | |||||||
| 373 | QIcon QAbstractButton::icon() const | - | ||||||
| 374 | { | - | ||||||
| 375 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 376 | return d->icon; | - | ||||||
| 377 | } | - | ||||||
| 378 | - | |||||||
| 379 | - | |||||||
| 380 | - | |||||||
| 381 | - | |||||||
| 382 | - | |||||||
| 383 | - | |||||||
| 384 | - | |||||||
| 385 | void QAbstractButton::setShortcut(const QKeySequence &key) | - | ||||||
| 386 | { | - | ||||||
| 387 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 388 | if (d->shortcutId != 0) | - | ||||||
| 389 | releaseShortcut(d->shortcutId); | - | ||||||
| 390 | d->shortcut = key; | - | ||||||
| 391 | d->shortcutId = grabShortcut(key); | - | ||||||
| 392 | } | - | ||||||
| 393 | - | |||||||
| 394 | QKeySequence QAbstractButton::shortcut() const | - | ||||||
| 395 | { | - | ||||||
| 396 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 397 | return d->shortcut; | - | ||||||
| 398 | } | - | ||||||
| 399 | void QAbstractButton::setCheckable(bool checkable) | - | ||||||
| 400 | { | - | ||||||
| 401 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 402 | if (d->checkable == checkable) | - | ||||||
| 403 | return; | - | ||||||
| 404 | - | |||||||
| 405 | d->checkable = checkable; | - | ||||||
| 406 | d->checked = false; | - | ||||||
| 407 | } | - | ||||||
| 408 | - | |||||||
| 409 | bool QAbstractButton::isCheckable() const | - | ||||||
| 410 | { | - | ||||||
| 411 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 412 | return d->checkable; | - | ||||||
| 413 | } | - | ||||||
| 414 | void QAbstractButton::setChecked(bool checked) | - | ||||||
| 415 | { | - | ||||||
| 416 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 417 | if (!d->checkable || d->checked == checked) { | - | ||||||
| 418 | if (!d->blockRefresh) | - | ||||||
| 419 | checkStateSet(); | - | ||||||
| 420 | return; | - | ||||||
| 421 | } | - | ||||||
| 422 | - | |||||||
| 423 | if (!checked && d->queryCheckedButton() == this) { | - | ||||||
| 424 | - | |||||||
| 425 | - | |||||||
| 426 | if (d->group ? d->group->d_func()->exclusive : d->autoExclusive) | - | ||||||
| 427 | return; | - | ||||||
| 428 | if (d->group) | - | ||||||
| 429 | d->group->d_func()->detectCheckedButton(); | - | ||||||
| 430 | - | |||||||
| 431 | - | |||||||
| 432 | - | |||||||
| 433 | - | |||||||
| 434 | } | - | ||||||
| 435 | - | |||||||
| 436 | QPointer<QAbstractButton> guard(this); | - | ||||||
| 437 | - | |||||||
| 438 | d->checked = checked; | - | ||||||
| 439 | if (!d->blockRefresh) | - | ||||||
| 440 | checkStateSet(); | - | ||||||
| 441 | d->refresh(); | - | ||||||
| 442 | - | |||||||
| 443 | if (guard && checked) | - | ||||||
| 444 | d->notifyChecked(); | - | ||||||
| 445 | if (guard) | - | ||||||
| 446 | d->emitToggled(checked); | - | ||||||
| 447 | - | |||||||
| 448 | - | |||||||
| 449 | - | |||||||
| 450 | QAccessible::State s; | - | ||||||
| 451 | s.checked = true; | - | ||||||
| 452 | QAccessibleStateChangeEvent event(this, s); | - | ||||||
| 453 | QAccessible::updateAccessibility(&event); | - | ||||||
| 454 | - | |||||||
| 455 | } | - | ||||||
| 456 | - | |||||||
| 457 | bool QAbstractButton::isChecked() const | - | ||||||
| 458 | { | - | ||||||
| 459 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 460 | return d->checked; | - | ||||||
| 461 | } | - | ||||||
| 462 | void QAbstractButton::setDown(bool down) | - | ||||||
| 463 | { | - | ||||||
| 464 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 465 | if (d->down == down) | - | ||||||
| 466 | return; | - | ||||||
| 467 | d->down = down; | - | ||||||
| 468 | d->refresh(); | - | ||||||
| 469 | if (d->autoRepeat && d->down) | - | ||||||
| 470 | d->repeatTimer.start(d->autoRepeatDelay, this); | - | ||||||
| 471 | else | - | ||||||
| 472 | d->repeatTimer.stop(); | - | ||||||
| 473 | } | - | ||||||
| 474 | - | |||||||
| 475 | bool QAbstractButton::isDown() const | - | ||||||
| 476 | { | - | ||||||
| 477 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 478 | return d->down; | - | ||||||
| 479 | } | - | ||||||
| 480 | void QAbstractButton::setAutoRepeat(bool autoRepeat) | - | ||||||
| 481 | { | - | ||||||
| 482 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 483 | if (d->autoRepeat == autoRepeat) | - | ||||||
| 484 | return; | - | ||||||
| 485 | d->autoRepeat = autoRepeat; | - | ||||||
| 486 | if (d->autoRepeat && d->down) | - | ||||||
| 487 | d->repeatTimer.start(d->autoRepeatDelay, this); | - | ||||||
| 488 | else | - | ||||||
| 489 | d->repeatTimer.stop(); | - | ||||||
| 490 | } | - | ||||||
| 491 | - | |||||||
| 492 | bool QAbstractButton::autoRepeat() const | - | ||||||
| 493 | { | - | ||||||
| 494 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 495 | return d->autoRepeat; | - | ||||||
| 496 | } | - | ||||||
| 497 | void QAbstractButton::setAutoRepeatDelay(int autoRepeatDelay) | - | ||||||
| 498 | { | - | ||||||
| 499 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 500 | d->autoRepeatDelay = autoRepeatDelay; | - | ||||||
| 501 | } | - | ||||||
| 502 | - | |||||||
| 503 | int QAbstractButton::autoRepeatDelay() const | - | ||||||
| 504 | { | - | ||||||
| 505 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 506 | return d->autoRepeatDelay; | - | ||||||
| 507 | } | - | ||||||
| 508 | void QAbstractButton::setAutoRepeatInterval(int autoRepeatInterval) | - | ||||||
| 509 | { | - | ||||||
| 510 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 511 | d->autoRepeatInterval = autoRepeatInterval; | - | ||||||
| 512 | } | - | ||||||
| 513 | - | |||||||
| 514 | int QAbstractButton::autoRepeatInterval() const | - | ||||||
| 515 | { | - | ||||||
| 516 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 517 | return d->autoRepeatInterval; | - | ||||||
| 518 | } | - | ||||||
| 519 | void QAbstractButton::setAutoExclusive(bool autoExclusive) | - | ||||||
| 520 | { | - | ||||||
| 521 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 522 | d->autoExclusive = autoExclusive; | - | ||||||
| 523 | } | - | ||||||
| 524 | - | |||||||
| 525 | bool QAbstractButton::autoExclusive() const | - | ||||||
| 526 | { | - | ||||||
| 527 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 528 | return d->autoExclusive; | - | ||||||
| 529 | } | - | ||||||
| 530 | QButtonGroup *QAbstractButton::group() const | - | ||||||
| 531 | { | - | ||||||
| 532 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 533 | return d->group; | - | ||||||
| 534 | } | - | ||||||
| 535 | void QAbstractButton::animateClick(int msec) | - | ||||||
| 536 | { | - | ||||||
| 537 | if (!isEnabled()) | - | ||||||
| 538 | return; | - | ||||||
| 539 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 540 | if (d->checkable && focusPolicy() & Qt::ClickFocus) | - | ||||||
| 541 | setFocus(); | - | ||||||
| 542 | setDown(true); | - | ||||||
| 543 | repaint(); | - | ||||||
| 544 | QApplication::flush(); | - | ||||||
| 545 | if (!d->animateTimer.isActive()) | - | ||||||
| 546 | d->emitPressed(); | - | ||||||
| 547 | d->animateTimer.start(msec, this); | - | ||||||
| 548 | } | - | ||||||
| 549 | void QAbstractButton::click() | - | ||||||
| 550 | { | - | ||||||
| 551 | if (!isEnabled()) | - | ||||||
| 552 | return; | - | ||||||
| 553 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 554 | QPointer<QAbstractButton> guard(this); | - | ||||||
| 555 | d->down = true; | - | ||||||
| 556 | d->emitPressed(); | - | ||||||
| 557 | if (guard) { | - | ||||||
| 558 | d->down = false; | - | ||||||
| 559 | nextCheckState(); | - | ||||||
| 560 | if (guard) | - | ||||||
| 561 | d->emitReleased(); | - | ||||||
| 562 | if (guard) | - | ||||||
| 563 | d->emitClicked(); | - | ||||||
| 564 | } | - | ||||||
| 565 | } | - | ||||||
| 566 | - | |||||||
| 567 | - | |||||||
| 568 | - | |||||||
| 569 | - | |||||||
| 570 | - | |||||||
| 571 | - | |||||||
| 572 | - | |||||||
| 573 | void QAbstractButton::toggle() | - | ||||||
| 574 | { | - | ||||||
| 575 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 576 | setChecked(!d->checked); | - | ||||||
| 577 | } | - | ||||||
| 578 | void QAbstractButton::checkStateSet() | - | ||||||
| 579 | { | - | ||||||
| 580 | } | - | ||||||
| 581 | void QAbstractButton::nextCheckState() | - | ||||||
| 582 | { | - | ||||||
| 583 | if (isCheckable()) | - | ||||||
| 584 | setChecked(!isChecked()); | - | ||||||
| 585 | } | - | ||||||
| 586 | bool QAbstractButton::hitButton(const QPoint &pos) const | - | ||||||
| 587 | { | - | ||||||
| 588 | return rect().contains(pos); | - | ||||||
| 589 | } | - | ||||||
| 590 | - | |||||||
| 591 | - | |||||||
| 592 | bool QAbstractButton::event(QEvent *e) | - | ||||||
| 593 | { | - | ||||||
| 594 | - | |||||||
| 595 | - | |||||||
| 596 | if (!isEnabled()) { | - | ||||||
| 597 | switch(e->type()) { | - | ||||||
| 598 | case QEvent::TabletPress: | - | ||||||
| 599 | case QEvent::TabletRelease: | - | ||||||
| 600 | case QEvent::TabletMove: | - | ||||||
| 601 | case QEvent::MouseButtonPress: | - | ||||||
| 602 | case QEvent::MouseButtonRelease: | - | ||||||
| 603 | case QEvent::MouseButtonDblClick: | - | ||||||
| 604 | case QEvent::MouseMove: | - | ||||||
| 605 | case QEvent::HoverMove: | - | ||||||
| 606 | case QEvent::HoverEnter: | - | ||||||
| 607 | case QEvent::HoverLeave: | - | ||||||
| 608 | case QEvent::ContextMenu: | - | ||||||
| 609 | - | |||||||
| 610 | case QEvent::Wheel: | - | ||||||
| 611 | - | |||||||
| 612 | return true; | - | ||||||
| 613 | default: | - | ||||||
| 614 | break; | - | ||||||
| 615 | } | - | ||||||
| 616 | } | - | ||||||
| 617 | - | |||||||
| 618 | - | |||||||
| 619 | if (e->type() == QEvent::Shortcut) { | - | ||||||
| 620 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 621 | QShortcutEvent *se = static_cast<QShortcutEvent *>(e); | - | ||||||
| 622 | if (d->shortcutId != se->shortcutId()) | - | ||||||
| 623 | return false; | - | ||||||
| 624 | if (!se->isAmbiguous()) { | - | ||||||
| 625 | if (!d->animateTimer.isActive()) | - | ||||||
| 626 | animateClick(); | - | ||||||
| 627 | } else { | - | ||||||
| 628 | if (focusPolicy() != Qt::NoFocus) | - | ||||||
| 629 | setFocus(Qt::ShortcutFocusReason); | - | ||||||
| 630 | window()->setAttribute(Qt::WA_KeyboardFocusChange); | - | ||||||
| 631 | } | - | ||||||
| 632 | return true; | - | ||||||
| 633 | } | - | ||||||
| 634 | - | |||||||
| 635 | return QWidget::event(e); | - | ||||||
| 636 | } | - | ||||||
| 637 | - | |||||||
| 638 | - | |||||||
| 639 | void QAbstractButton::mousePressEvent(QMouseEvent *e) | - | ||||||
| 640 | { | - | ||||||
| 641 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 642 | if (e->button() != Qt::LeftButton) { | - | ||||||
| 643 | e->ignore(); | - | ||||||
| 644 | return; | - | ||||||
| 645 | } | - | ||||||
| 646 | if (hitButton(e->pos())) { | - | ||||||
| 647 | setDown(true); | - | ||||||
| 648 | d->pressed = true; | - | ||||||
| 649 | repaint(); | - | ||||||
| 650 | QApplication::flush(); | - | ||||||
| 651 | d->emitPressed(); | - | ||||||
| 652 | e->accept(); | - | ||||||
| 653 | } else { | - | ||||||
| 654 | e->ignore(); | - | ||||||
| 655 | } | - | ||||||
| 656 | } | - | ||||||
| 657 | - | |||||||
| 658 | - | |||||||
| 659 | void QAbstractButton::mouseReleaseEvent(QMouseEvent *e) | - | ||||||
| 660 | { | - | ||||||
| 661 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 662 | d->pressed = false; | - | ||||||
| 663 | - | |||||||
| 664 | if (e->button() != Qt::LeftButton) { | - | ||||||
| 665 | e->ignore(); | - | ||||||
| 666 | return; | - | ||||||
| 667 | } | - | ||||||
| 668 | - | |||||||
| 669 | if (!d->down) { | - | ||||||
| 670 | - | |||||||
| 671 | d->refresh(); | - | ||||||
| 672 | e->ignore(); | - | ||||||
| 673 | return; | - | ||||||
| 674 | } | - | ||||||
| 675 | - | |||||||
| 676 | if (hitButton(e->pos())) { | - | ||||||
| 677 | d->repeatTimer.stop(); | - | ||||||
| 678 | d->click(); | - | ||||||
| 679 | e->accept(); | - | ||||||
| 680 | } else { | - | ||||||
| 681 | setDown(false); | - | ||||||
| 682 | e->ignore(); | - | ||||||
| 683 | } | - | ||||||
| 684 | } | - | ||||||
| 685 | - | |||||||
| 686 | - | |||||||
| 687 | void QAbstractButton::mouseMoveEvent(QMouseEvent *e) | - | ||||||
| 688 | { | - | ||||||
| 689 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 690 | if (!(e->buttons() & Qt::LeftButton) || !d->pressed) { | - | ||||||
| 691 | e->ignore(); | - | ||||||
| 692 | return; | - | ||||||
| 693 | } | - | ||||||
| 694 | - | |||||||
| 695 | if (hitButton(e->pos()) != d->down) { | - | ||||||
| 696 | setDown(!d->down); | - | ||||||
| 697 | repaint(); | - | ||||||
| 698 | QApplication::flush(); | - | ||||||
| 699 | if (d->down) | - | ||||||
| 700 | d->emitPressed(); | - | ||||||
| 701 | else | - | ||||||
| 702 | d->emitReleased(); | - | ||||||
| 703 | e->accept(); | - | ||||||
| 704 | } else if (!hitButton(e->pos())) { | - | ||||||
| 705 | e->ignore(); | - | ||||||
| 706 | } | - | ||||||
| 707 | } | - | ||||||
| 708 | - | |||||||
| 709 | - | |||||||
| 710 | void QAbstractButton::keyPressEvent(QKeyEvent *e) | - | ||||||
| 711 | { | - | ||||||
| 712 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 713 | bool next = true; | - | ||||||
| 714 | switch (e->key()) { | - | ||||||
| 715 | case Qt::Key_Enter: | - | ||||||
| 716 | case Qt::Key_Return: | - | ||||||
| 717 | e->ignore(); | - | ||||||
| 718 | break; | - | ||||||
| 719 | case Qt::Key_Select: | - | ||||||
| 720 | case Qt::Key_Space: | - | ||||||
| 721 | if (!e->isAutoRepeat()) { | - | ||||||
| 722 | setDown(true); | - | ||||||
| 723 | repaint(); | - | ||||||
| 724 | QApplication::flush(); | - | ||||||
| 725 | d->emitPressed(); | - | ||||||
| 726 | } | - | ||||||
| 727 | break; | - | ||||||
| 728 | case Qt::Key_Up: | - | ||||||
| 729 | next = false; | - | ||||||
| 730 | - | |||||||
| 731 | case Qt::Key_Left: | - | ||||||
| 732 | case Qt::Key_Right: | - | ||||||
| 733 | case Qt::Key_Down: { | - | ||||||
| 734 | QWidget *pw = parentWidget(); | - | ||||||
| 735 | if (d->autoExclusive | - | ||||||
| 736 | - | |||||||
| 737 | || d->group | - | ||||||
| 738 | - | |||||||
| 739 | - | |||||||
| 740 | || (pw && qobject_cast<QAbstractItemView *>(pw->parentWidget())) | - | ||||||
| 741 | - | |||||||
| 742 | ) { | - | ||||||
| 743 | - | |||||||
| 744 | - | |||||||
| 745 | - | |||||||
| 746 | - | |||||||
| 747 | d->moveFocus(e->key()); | - | ||||||
| 748 | if (hasFocus()) | - | ||||||
| 749 | e->ignore(); | - | ||||||
| 750 | } else { | - | ||||||
| 751 | - | |||||||
| 752 | QWidget *w = pw ? pw : this; | - | ||||||
| 753 | bool reverse = (w->layoutDirection() == Qt::RightToLeft); | - | ||||||
| 754 | if ((e->key() == Qt::Key_Left && !reverse) | - | ||||||
| 755 | || (e->key() == Qt::Key_Right && reverse)) { | - | ||||||
| 756 | next = false; | - | ||||||
| 757 | } | - | ||||||
| 758 | focusNextPrevChild(next); | - | ||||||
| 759 | } | - | ||||||
| 760 | break; | - | ||||||
| 761 | } | - | ||||||
| 762 | default: | - | ||||||
| 763 | if (e->matches(QKeySequence::Cancel) && d->down) { | - | ||||||
| 764 | setDown(false); | - | ||||||
| 765 | repaint(); | - | ||||||
| 766 | QApplication::flush(); | - | ||||||
| 767 | d->emitReleased(); | - | ||||||
| 768 | return; | - | ||||||
| 769 | } | - | ||||||
| 770 | e->ignore(); | - | ||||||
| 771 | } | - | ||||||
| 772 | } | - | ||||||
| 773 | - | |||||||
| 774 | - | |||||||
| 775 | void QAbstractButton::keyReleaseEvent(QKeyEvent *e) | - | ||||||
| 776 | { | - | ||||||
| 777 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 778 | - | |||||||
| 779 | if (!e->isAutoRepeat()) | - | ||||||
| 780 | d->repeatTimer.stop(); | - | ||||||
| 781 | - | |||||||
| 782 | switch (e->key()) { | - | ||||||
| 783 | case Qt::Key_Select: | - | ||||||
| 784 | case Qt::Key_Space: | - | ||||||
| 785 | if (!e->isAutoRepeat() && d->down) | - | ||||||
| 786 | d->click(); | - | ||||||
| 787 | break; | - | ||||||
| 788 | default: | - | ||||||
| 789 | e->ignore(); | - | ||||||
| 790 | } | - | ||||||
| 791 | } | - | ||||||
| 792 | - | |||||||
| 793 | - | |||||||
| 794 | - | |||||||
| 795 | void QAbstractButton::timerEvent(QTimerEvent *e) | - | ||||||
| 796 | { | - | ||||||
| 797 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 798 | if (e->timerId() == d->repeatTimer.timerId()) { | - | ||||||
| 799 | d->repeatTimer.start(d->autoRepeatInterval, this); | - | ||||||
| 800 | if (d->down) { | - | ||||||
| 801 | QPointer<QAbstractButton> guard(this); | - | ||||||
| 802 | nextCheckState(); | - | ||||||
| 803 | if (guard) | - | ||||||
| 804 | d->emitReleased(); | - | ||||||
| 805 | if (guard) | - | ||||||
| 806 | d->emitClicked(); | - | ||||||
| 807 | if (guard) | - | ||||||
| 808 | d->emitPressed(); | - | ||||||
| 809 | } | - | ||||||
| 810 | } else if (e->timerId() == d->animateTimer.timerId()) { | - | ||||||
| 811 | d->animateTimer.stop(); | - | ||||||
| 812 | d->click(); | - | ||||||
| 813 | } | - | ||||||
| 814 | } | - | ||||||
| 815 | - | |||||||
| 816 | - | |||||||
| 817 | void QAbstractButton::focusInEvent(QFocusEvent *e) | - | ||||||
| 818 | { | - | ||||||
| 819 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 820 | - | |||||||
| 821 | - | |||||||
| 822 | - | |||||||
| 823 | d->fixFocusPolicy(); | - | ||||||
| 824 | QWidget::focusInEvent(e); | - | ||||||
| 825 | } | - | ||||||
| 826 | - | |||||||
| 827 | - | |||||||
| 828 | void QAbstractButton::focusOutEvent(QFocusEvent *e) | - | ||||||
| 829 | { | - | ||||||
| 830 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 831 | if (e->reason() != Qt::PopupFocusReason && d->down) { | - | ||||||
| 832 | d->down = false; | - | ||||||
| 833 | d->emitReleased(); | - | ||||||
| 834 | } | - | ||||||
| 835 | QWidget::focusOutEvent(e); | - | ||||||
| 836 | } | - | ||||||
| 837 | - | |||||||
| 838 | - | |||||||
| 839 | void QAbstractButton::changeEvent(QEvent *e) | - | ||||||
| 840 | { | - | ||||||
| 841 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 842 | switch (e->type()) { | - | ||||||
| 843 | case QEvent::EnabledChange: | - | ||||||
| 844 | if (!isEnabled() && d->down) { | - | ||||||
| 845 | d->down = false; | - | ||||||
| 846 | d->emitReleased(); | - | ||||||
| 847 | } | - | ||||||
| 848 | break; | - | ||||||
| 849 | default: | - | ||||||
| 850 | d->sizeHint = QSize(); | - | ||||||
| 851 | break; | - | ||||||
| 852 | } | - | ||||||
| 853 | QWidget::changeEvent(e); | - | ||||||
| 854 | } | - | ||||||
| 855 | QSize QAbstractButton::iconSize() const | - | ||||||
| 856 | { | - | ||||||
| 857 | const QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 858 | if (d->iconSize.isValid()) | - | ||||||
| 859 | return d->iconSize; | - | ||||||
| 860 | int e = style()->pixelMetric(QStyle::PM_ButtonIconSize, 0, this); | - | ||||||
| 861 | return QSize(e, e); | - | ||||||
| 862 | } | - | ||||||
| 863 | - | |||||||
| 864 | void QAbstractButton::setIconSize(const QSize &size) | - | ||||||
| 865 | { | - | ||||||
| 866 | QAbstractButtonPrivate * const d = d_func(); | - | ||||||
| 867 | if (d->iconSize == size) | - | ||||||
| 868 | return; | - | ||||||
| 869 | - | |||||||
| 870 | d->iconSize = size; | - | ||||||
| 871 | d->sizeHint = QSize(); | - | ||||||
| 872 | updateGeometry(); | - | ||||||
| 873 | if (isVisible()) { | - | ||||||
| 874 | update(); | - | ||||||
| 875 | } | - | ||||||
| 876 | } | - | ||||||
| 877 | - | |||||||
| 878 | - | |||||||
| 879 | - | |||||||
| 880 | - | |||||||
| 881 | - | |||||||
| Switch to Source code | Preprocessed file |