Make tabcompletion key configurable via shortcuts. fixes 1018
[quassel.git] / src / uisupport / tabcompleter.cpp
index 751385a..527eb79 100644 (file)
 #include "network.h"
 #include "networkmodel.h"
 #include "uisettings.h"
+#include "action.h"
+#include "actioncollection.h"
+#include "qtui.h"
 
 #include <QRegExp>
 
 const Network *TabCompleter::_currentNetwork;
 BufferId TabCompleter::_currentBufferId;
 QString TabCompleter::_currentBufferName;
+TabCompleter::Type TabCompleter::_completionType;
 
 TabCompleter::TabCompleter(MultiLineEdit *_lineEdit)
   : QObject(_lineEdit),
@@ -41,7 +45,16 @@ TabCompleter::TabCompleter(MultiLineEdit *_lineEdit)
     _enabled(false),
     _nickSuffix(": ")
 {
+  // use both an Action and generic eventFilter, to make the shortcut configurable
+  // yet still be able to reset() when required
   _lineEdit->installEventFilter(this);
+  ActionCollection *coll = QtUi::actionCollection("General");
+  coll->addAction("TabCompletionKey", new Action(tr("Tab completion"), coll,
+                                              this, SLOT(onTabCompletionKey()), QKeySequence(Qt::Key_Tab)));
+}
+
+void TabCompleter::onTabCompletionKey() {
+  complete();
 }
 
 void TabCompleter::buildCompletionList() {
@@ -70,7 +83,7 @@ void TabCompleter::buildCompletionList() {
     _completionType = ChannelTab;
     foreach(IrcChannel *ircChannel, _currentNetwork->ircChannels()) {
       if(regex.indexIn(ircChannel->name()) > -1)
-        _completionMap[CompletionKey(ircChannel->name(), ChannelTab)] = ircChannel->name();
+        _completionMap[ircChannel->name()] = ircChannel->name();
     }
   } else {
     // user completion
@@ -83,16 +96,16 @@ void TabCompleter::buildCompletionList() {
           return;
         foreach(IrcUser *ircUser, channel->ircUsers()) {
           if(regex.indexIn(ircUser->nick()) > -1)
-            _completionMap[CompletionKey(ircUser->nick().toLower(), UserTab)] = ircUser->nick();
+            _completionMap[ircUser->nick().toLower()] = ircUser->nick();
         }
       }
       break;
     case BufferInfo::QueryBuffer:
       if(regex.indexIn(_currentBufferName) > -1)
-        _completionMap[CompletionKey(_currentBufferName.toLower(), UserTab)] = _currentBufferName;
+        _completionMap[_currentBufferName.toLower()] = _currentBufferName;
     case BufferInfo::StatusBuffer:
       if(!_currentNetwork->myNick().isEmpty() && regex.indexIn(_currentNetwork->myNick()) > -1)
-        _completionMap[CompletionKey(_currentNetwork->myNick().toLower(), UserTab)] = _currentNetwork->myNick();
+        _completionMap[_currentNetwork->myNick().toLower()] = _currentNetwork->myNick();
       break;
     default:
       return;
@@ -129,6 +142,8 @@ void TabCompleter::complete() {
     if(_completionType == UserTab && _lineEdit->cursorPosition() == _lastCompletionLength) {
       _lineEdit->insert(_nickSuffix);
       _lastCompletionLength += _nickSuffix.length();
+    } else if (s.addSpaceMidSentence()) {
+      _lineEdit->insert(" ");
     }
 
   // we're at the end of the list -> start over again
@@ -150,18 +165,15 @@ bool TabCompleter::eventFilter(QObject *obj, QEvent *event) {
 
   QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
 
-  if(keyEvent->key() == Qt::Key_Tab) {
-    complete();
-    return true;
-  } else {
+  if(keyEvent->key() != QtUi::actionCollection("General")->action("TabCompletionKey")->shortcut()) {
     reset();
-    return false;
   }
+  return false;
 }
 
 // this determines the sort order
 bool TabCompleter::CompletionKey::operator<(const CompletionKey &other) const {
-  switch(this->type) {
+  switch(_completionType) {
     case UserTab:
       {
         IrcUser *thisUser = _currentNetwork->ircUser(this->contents);