Add autocomplete to novacoin-qt's console window
authorsvost <ya.nowa@yandex.ru>
Mon, 6 Feb 2017 09:03:42 +0000 (12:03 +0300)
committersvost <ya.nowa@yandex.ru>
Mon, 6 Feb 2017 09:10:41 +0000 (12:10 +0300)
Origin: https://github.com/bitcoin/bitcoin/pull/7613

src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/qt/rpcconsole.cpp
src/qt/rpcconsole.h

index 0c7e278..e1d38ea 100644 (file)
@@ -1109,6 +1109,16 @@ json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_s
     }
 }
 
+std::vector<std::string> CRPCTable::listCommands() const
+{
+    std::vector<std::string> commandList;
+    typedef std::map<std::string, const CRPCCommand*> commandMap;
+
+    std::transform( mapCommands.begin(), mapCommands.end(),
+                   std::back_inserter(commandList),
+                   boost::bind(&commandMap::value_type::first,_1) );
+    return commandList;
+}
 
 Object CallRPC(const string& strMethod, const Array& params)
 {
index a5888a9..d304686 100644 (file)
@@ -119,6 +119,11 @@ public:
      * @throws an exception (json_spirit::Value) when an error happens.
      */
     json_spirit::Value execute(const std::string &method, const json_spirit::Array &params) const;
+    /**
+    * Returns a list of registered commands
+    * @returns List of registered commands.
+    */
+    std::vector<std::string> listCommands() const;
 };
 
 extern const CRPCTable tableRPC;
index 1c86ddd..e9c0987 100644 (file)
@@ -13,6 +13,7 @@
 #include <QKeyEvent>
 #include <QUrl>
 #include <QScrollBar>
+#include <QStringList>
 
 #include <openssl/crypto.h>
 #include <db_cxx.h>
@@ -243,6 +244,14 @@ bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
                 return true;
             }
             break;
+        case Qt::Key_Return:
+        case Qt::Key_Enter:
+            // forward these events to lineEdit
+            if(obj == autoCompleter->popup()) {
+                QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
+                return true;
+            }
+            break;
         default:
             // Typing in messages widget brings focus to line edit, and redirects key there
             // Exclude most combinations and keys that emit no text, except paste shortcuts
@@ -282,6 +291,20 @@ void RPCConsole::setClientModel(ClientModel *model)
         ui->isTestNet->setChecked(model->isTestNet());
 
         setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
+
+        //Setup autocomplete and attach it
+        QStringList wordList;
+        std::vector<std::string> commandList = tableRPC.listCommands();
+        for (size_t i = 0; i < commandList.size(); ++i)
+        {
+            wordList << commandList[i].c_str();
+        }
+
+        autoCompleter = new QCompleter(wordList, this);
+        ui->lineEdit->setCompleter(autoCompleter);
+
+        // clear the lineEdit after activating from QCompleter
+        autoCompleter->popup()->installEventFilter(this);
     }
 }
 
index aa8c8f3..d5f9b69 100644 (file)
@@ -2,6 +2,7 @@
 #define RPCCONSOLE_H
 
 #include <QWidget>
+#include <QCompleter>
 
 namespace Ui {
     class RPCConsole;
@@ -82,6 +83,7 @@ private:
     int historyPtr;
 
     void startExecutor();
+    QCompleter *autoCompleter;
 };
 
 #endif // RPCCONSOLE_H