Index: trunk/src/gui/widgets/qabstractbutton.cpp
===================================================================
--- trunk/src/gui/widgets/qabstractbutton.cpp	(revision 5)
+++ trunk/src/gui/widgets/qabstractbutton.cpp	(revision 561)
@@ -2,5 +2,6 @@
 **
 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
-** Contact: Qt Software Information (qt-info@nokia.com)
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the QtGui module of the Qt Toolkit.
@@ -21,8 +22,7 @@
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
-** In addition, as a special exception, Nokia gives you certain
-** additional rights. These rights are described in the Nokia Qt LGPL
-** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
-** package.
+** In addition, as a special exception, Nokia gives you certain additional
+** rights.  These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
@@ -34,6 +34,6 @@
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at qt-sales@nokia.com.
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
@@ -166,5 +166,5 @@
 #endif
     checkable(false), checked(false), autoRepeat(false), autoExclusive(false),
-    down(false), blockRefresh(false),
+    down(false), blockRefresh(false), pressed(false),
 #ifndef QT_NO_BUTTONGROUP
     group(0),
@@ -216,9 +216,6 @@
 }
 
-/*!
-    Adds the given \a button to the end of the group's internal list of buttons.
-
-    \sa removeButton()
-*/
+
+// TODO: Qt 5: Merge with addButton(QAbstractButton *button, int id)
 void QButtonGroup::addButton(QAbstractButton *button)
 {
@@ -233,6 +230,16 @@
     button->d_func()->group = this;
     d->buttonList.append(button);
-    if (id != -1)
+    if (id == -1) {
+        QList<int> ids = d->mapping.values();
+        if (ids.isEmpty())
+           d->mapping[button] = -2;
+        else {
+            qSort(ids);
+            d->mapping[button] = ids.first()-1;
+        }
+    } else {
         d->mapping[button] = id;
+    }
+
     if (d->exclusive && button->isChecked())
         button->d_func()->notifyChecked();
@@ -313,18 +320,14 @@
 #endif
 
-    Q_Q(const QAbstractButton);
-    QList<QAbstractButton*>candidates;
-    if (q->parentWidget()) {
-        candidates =  qFindChildren<QAbstractButton *>(q->parentWidget());
-        if (autoExclusive) {
-            for (int i = candidates.count() - 1; i >= 0; --i) {
-                QAbstractButton *candidate = candidates.at(i);
-                if (!candidate->autoExclusive()
-#ifndef QT_NO_BUTTONGROUP
-                    || candidate->group()
-#endif
-                    )
-                    candidates.removeAt(i);
-            }
+    QList<QAbstractButton*>candidates = qFindChildren<QAbstractButton *>(parent);
+    if (autoExclusive) {
+        for (int i = candidates.count() - 1; i >= 0; --i) {
+            QAbstractButton *candidate = candidates.at(i);
+            if (!candidate->autoExclusive()
+#ifndef QT_NO_BUTTONGROUP
+                || candidate->group()
+#endif
+                )
+                candidates.removeAt(i);
         }
     }
@@ -377,5 +380,5 @@
     bool exclusive = autoExclusive;
 #endif
-    QWidget *f = qApp->focusWidget();
+    QWidget *f = QApplication::focusWidget();
     QAbstractButton *fb = qobject_cast<QAbstractButton *>(f);
     if (!fb || !buttonList.contains(fb))
@@ -1088,4 +1091,5 @@
     if (hitButton(e->pos())) {
         setDown(true);
+        d->pressed = true;
         repaint(); //flush paint event before invoking potentially expensive operation
         QApplication::flush();
@@ -1101,4 +1105,6 @@
 {
     Q_D(QAbstractButton);
+    d->pressed = false;
+
     if (e->button() != Qt::LeftButton) {
         e->ignore();
@@ -1125,5 +1131,5 @@
 {
     Q_D(QAbstractButton);
-    if (!(e->buttons() & Qt::LeftButton)) {
+    if (!(e->buttons() & Qt::LeftButton) || !d->pressed) {
         e->ignore();
         return;
@@ -1170,5 +1176,8 @@
     case Qt::Key_Down:
 #ifdef QT_KEYPAD_NAVIGATION
-        if (QApplication::keypadNavigationEnabled() && (e->key() == Qt::Key_Left || e->key() == Qt::Key_Right)) {
+        if ((QApplication::keypadNavigationEnabled()
+                && (e->key() == Qt::Key_Left || e->key() == Qt::Key_Right))
+                || (!QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional
+                || (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))) {
             e->ignore();
             return;
@@ -1237,5 +1246,7 @@
         if (d->down) {
             QPointer<QAbstractButton> guard(this);
-            d->emitReleased();
+            nextCheckState();
+            if (guard)
+                d->emitReleased();
             if (guard)
                 d->emitClicked();
