From e8b01efe445cc97dce1a2da42ca455da3037a398 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20L=C3=BCbking?= <thomas.luebking@gmail.com>
Date: Wed, 10 Nov 2010 22:28:15 +0000
Subject: [PATCH] improve keyboard navigation of the window shortcut dialog,
 add a conflict warning kkeysequencewidget only needs the pushbutton as
 focusproxy BUG: 182873 BUG: 251297

svn path=/trunk/KDE/kdebase/workspace/; revision=1195365
---
 utils.cpp | 40 +++++++++++++++++++++++++++++++++-------
 utils.h   |  3 +++
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/utils.cpp b/utils.cpp
index ee3520d8b5..3d1e626063 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -30,13 +30,16 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <unistd.h>
 
 #ifndef KCMRULES
-
+#include <QLabel>
+#include <QVBoxLayout>
 #include <kxerrorhandler.h>
 #include <assert.h>
 #include <kdebug.h>
 #include <kglobalaccel.h>
+#include <klocale.h>
 #include <kshortcut.h>
 #include <kkeyserver.h>
+#include <KPushButton>
 
 #include <X11/Xlib.h>
 #include <X11/extensions/shape.h>
@@ -438,9 +441,13 @@ bool isLocalMachine( const QByteArray& host )
 
 #ifndef KCMRULES
 ShortcutDialog::ShortcutDialog( const QKeySequence& cut )
-    : widget( new KKeySequenceWidget( this ))
-     ,_shortcut(cut)
+    : _shortcut(cut)
     {
+    QWidget *vBoxContainer = new QWidget( this );
+    vBoxContainer->setLayout( new QVBoxLayout( vBoxContainer ) );
+    vBoxContainer->layout()->addWidget( widget = new KKeySequenceWidget( vBoxContainer ) );
+    vBoxContainer->layout()->addWidget( warning = new QLabel( vBoxContainer ) );
+    warning->hide();
     widget->setKeySequence( cut );
 
     // To not check for conflicting shortcuts. The widget would use a message
@@ -454,7 +461,8 @@ ShortcutDialog::ShortcutDialog( const QKeySequence& cut )
         widget, SIGNAL(keySequenceChanged(const QKeySequence&)),
         SLOT(keySequenceChanged(const QKeySequence&)));
 
-    setMainWidget( widget );
+    setMainWidget( vBoxContainer );
+    widget->setFocus();
 
     // make it a popup, so that it has the grab
     XSetWindowAttributes attrs;
@@ -492,13 +500,31 @@ void ShortcutDialog::done( int r )
 
 void ShortcutDialog::keySequenceChanged(const QKeySequence &seq)
     {
+    activateWindow(); // where is the kbd focus lost? cause of popup state?
+    if (_shortcut == seq)
+        return; // don't try to update the same
+
     // Check if the key sequence is used currently
+    QString sc = seq.toString();
+    // NOTICE - seq.toString() & the entries in "conflicting" randomly get invalidated after the next call (if no sc has been set & conflicting isn't empty?!)
     QList<KGlobalShortcutInfo> conflicting = KGlobalAccel::getGlobalShortcutsByKey(seq);
-    if (!conflicting.isEmpty()) {
-        kDebug(1212) << "TODO: Display conflicting shortcuts to user";
-        // TODO: Inform the user somehow instead of just ignoring his wish
+    if (!conflicting.isEmpty()) 
+        {
+        const KGlobalShortcutInfo &conflict = conflicting.at(0);
+        warning->setText(i18nc("'%1' is a keyboard shortcut like 'ctrl+w'",
+                               "<b>%1</b> is already in use", sc));
+        warning->setToolTip(i18nc("keyboard shortcut '%1' is used by action '%2' in application '%3'",
+                                  "<b>%1</b> is used by %2 in %3", sc, conflict.friendlyName(), conflict.componentFriendlyName()));
+        warning->show();
         widget->setKeySequence(shortcut());
         }
+    else if (seq != _shortcut)
+        {
+        warning->hide();
+        if ( KPushButton *ok = button( KDialog::Ok ) )
+            ok->setFocus();
+        }
+    
     _shortcut = seq;
     }
 
diff --git a/utils.h b/utils.h
index b0a06f982a..14dd429e3e 100644
--- a/utils.h
+++ b/utils.h
@@ -22,6 +22,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef KWIN_UTILS_H
 #define KWIN_UTILS_H
 
+class QLabel;
+
 #include <config-workspace.h>
 #include <config-X11.h>
 #include <config-kwin.h>
@@ -364,6 +366,7 @@ class ShortcutDialog
     private:
         KKeySequenceWidget* widget;
         QKeySequence _shortcut;
+        QLabel *warning;
     };
 
 #endif //KCMRULES