From 3fe66ea62824070cf49fdf75947edd2fc67d2893 Mon Sep 17 00:00:00 2001
From: hjk <qtc-committer@nokia.com>
Date: Mon, 30 Mar 2009 16:54:25 +0200
Subject: [PATCH] fakevim: make Alt-V Alt-V a global toggle

---
 src/plugins/fakevim/fakevimactions.cpp      |  7 +-
 src/plugins/fakevim/fakevimhandler.cpp      | 42 +++++----
 src/plugins/fakevim/fakevimhandler.h        |  6 +-
 src/plugins/fakevim/fakevimplugin.cpp       | 98 ++++++++++-----------
 tests/manual/gdbdebugger/simple/app/app.pro |  2 +-
 5 files changed, 76 insertions(+), 79 deletions(-)

diff --git a/src/plugins/fakevim/fakevimactions.cpp b/src/plugins/fakevim/fakevimactions.cpp
index df76b88689b..fff5a008ce7 100644
--- a/src/plugins/fakevim/fakevimactions.cpp
+++ b/src/plugins/fakevim/fakevimactions.cpp
@@ -114,13 +114,15 @@ FakeVimSettings *theFakeVimSettings()
     SavedAction *item = 0;
 
     item = new SavedAction(instance);
-    item->setText(QObject::tr("Use vim-style editing"));
+    item->setText(QObject::tr("Toggle vim-style editing"));
     item->setSettingsKey("FakeVim", "UseFakeVim");
+    item->setCheckable(true);
     instance->insertItem(ConfigUseFakeVim, item);
 
     item = new SavedAction(instance);
     item->setDefaultValue(false);
     item->setSettingsKey("FakeVim", "StartOfLine");
+    item->setCheckable(true);
     instance->insertItem(ConfigStartOfLine, item, "startofline", "sol");
 
     item = new SavedAction(instance);
@@ -136,6 +138,7 @@ FakeVimSettings *theFakeVimSettings()
     item = new SavedAction(instance);
     item->setDefaultValue(true);
     item->setSettingsKey("FakeVim", "HlSearch");
+    item->setCheckable(true);
     instance->insertItem(ConfigHlSearch, item, "hlsearch", "hls");
 
     item = new SavedAction(instance);
@@ -146,11 +149,13 @@ FakeVimSettings *theFakeVimSettings()
     item = new SavedAction(instance);
     item->setDefaultValue(false);
     item->setSettingsKey("FakeVim", "ExpandTab");
+    item->setCheckable(true);
     instance->insertItem(ConfigExpandTab, item, "expandtab", "et");
 
     item = new SavedAction(instance);
     item->setDefaultValue(false);
     item->setSettingsKey("FakeVim", "AutoIndent");
+    item->setCheckable(true);
     instance->insertItem(ConfigAutoIndent, item, "autoindent", "ai");
 
     item = new SavedAction(instance);
diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp
index 8dfc9ec9224..4d7927e8c6d 100644
--- a/src/plugins/fakevim/fakevimhandler.cpp
+++ b/src/plugins/fakevim/fakevimhandler.cpp
@@ -211,6 +211,7 @@ public:
     bool wantsOverride(QKeyEvent *ev);
     void handleExCommand(const QString &cmd);
 
+    void installEventFilter();
     void setupWidget();
     void restoreWidget();
 
@@ -393,7 +394,6 @@ public:
     int m_savedYankPosition;
     int m_desiredColumn;
 
-    QPointer<QObject> m_extraData;
     int m_cursorWidth;
 
     void recordJump();
@@ -521,10 +521,14 @@ EventResult FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
     return result;
 }
 
+void FakeVimHandler::Private::installEventFilter()
+{
+    EDITOR(installEventFilter(q));
+}
+
 void FakeVimHandler::Private::setupWidget()
 {
     enterCommandMode();
-    EDITOR(installEventFilter(q));
     //EDITOR(setCursorWidth(QFontMetrics(ed->font()).width(QChar('x')));
     if (m_textedit) {
         m_textedit->setLineWrapMode(QTextEdit::NoWrap);
@@ -548,7 +552,7 @@ void FakeVimHandler::Private::setupWidget()
         updateSelection();
     }
 
-    showBlackMessage("vi emulation mode. Type :q to leave. Use , Ctrl-R to trigger run.");
+    //showBlackMessage("vi emulation mode. Type :q to leave. Use , Ctrl-R to trigger run.");
     updateMiniBuffer();
 }
 
@@ -556,8 +560,10 @@ void FakeVimHandler::Private::restoreWidget()
 {
     //showBlackMessage(QString());
     //updateMiniBuffer();
-    EDITOR(removeEventFilter(q));
+    //EDITOR(removeEventFilter(q));
     EDITOR(setReadOnly(m_wasReadOnly));
+    EDITOR(setCursorWidth(m_cursorWidth));
+    EDITOR(setOverwriteMode(false));
     
     if (m_visualMode == VisualLineMode) {
         m_tc = EDITOR(textCursor());
@@ -2453,7 +2459,6 @@ void FakeVimHandler::Private::quit()
     q->quitRequested();
 }
 
-
 void FakeVimHandler::Private::recordJump()
 {
     m_jumpListUndo.append(position());
@@ -2461,6 +2466,7 @@ void FakeVimHandler::Private::recordJump()
     UNDO_DEBUG("jumps: " << m_jumpListUndo);
 }
 
+
 ///////////////////////////////////////////////////////////////////////
 //
 // FakeVimHandler
@@ -2478,7 +2484,9 @@ FakeVimHandler::~FakeVimHandler()
 
 bool FakeVimHandler::eventFilter(QObject *ob, QEvent *ev)
 {
-    if (ev->type() == QEvent::KeyPress && ob == d->editor()) {
+    bool active = theFakeVimSetting(ConfigUseFakeVim)->value().toBool();
+
+    if (active && ev->type() == QEvent::KeyPress && ob == d->editor()) {
         QKeyEvent *kev = static_cast<QKeyEvent *>(ev);
         KEY_DEBUG("KEYPRESS" << kev->key());
         EventResult res = d->handleEvent(kev);
@@ -2489,7 +2497,7 @@ bool FakeVimHandler::eventFilter(QObject *ob, QEvent *ev)
         return res == EventHandled;
     }
 
-    if (ev->type() == QEvent::ShortcutOverride && ob == d->editor()) {
+    if (active && ev->type() == QEvent::ShortcutOverride && ob == d->editor()) {
         QKeyEvent *kev = static_cast<QKeyEvent *>(ev);
         if (d->wantsOverride(kev)) {
             KEY_DEBUG("OVERRIDING SHORTCUT" << kev->key());
@@ -2503,6 +2511,11 @@ bool FakeVimHandler::eventFilter(QObject *ob, QEvent *ev)
     return QObject::eventFilter(ob, ev);
 }
 
+void FakeVimHandler::installEventFilter()
+{
+    d->installEventFilter();
+}
+
 void FakeVimHandler::setupWidget()
 {
     d->setupWidget();
@@ -2518,11 +2531,6 @@ void FakeVimHandler::handleCommand(const QString &cmd)
     d->handleExCommand(cmd);
 }
 
-void FakeVimHandler::setConfigValue(int code, const QVariant &value)
-{
-    theFakeVimSetting(code)->setValue(value);
-}
-
 void FakeVimHandler::quit()
 {
     d->quit();
@@ -2538,15 +2546,5 @@ QWidget *FakeVimHandler::widget()
     return d->editor();
 }
 
-void FakeVimHandler::setExtraData(QObject *data)
-{
-    d->m_extraData = data;
-}
-
-QObject *FakeVimHandler::extraData() const
-{
-    return d->m_extraData;
-}
-
 } // namespace Internal
 } // namespace FakeVim
diff --git a/src/plugins/fakevim/fakevimhandler.h b/src/plugins/fakevim/fakevimhandler.h
index f2c9e0d934f..8ec2702c146 100644
--- a/src/plugins/fakevim/fakevimhandler.h
+++ b/src/plugins/fakevim/fakevimhandler.h
@@ -48,18 +48,16 @@ public:
 
     QWidget *widget();
 
-    void setExtraData(QObject *data);
-    QObject *extraData() const;
-
 public slots:
     void setCurrentFileName(const QString &fileName);
 
     // This executes an "ex" style command taking context
     // information from widget;
     void handleCommand(const QString &cmd);
-    void setConfigValue(int code, const QVariant &value);
     void quit();
 
+    void installEventFilter();
+
     // Convenience
     void setupWidget();
     void restoreWidget();
diff --git a/src/plugins/fakevim/fakevimplugin.cpp b/src/plugins/fakevim/fakevimplugin.cpp
index 67399150c8b..b0a5539560c 100644
--- a/src/plugins/fakevim/fakevimplugin.cpp
+++ b/src/plugins/fakevim/fakevimplugin.cpp
@@ -52,7 +52,6 @@
 #include <texteditor/completionsupport.h>
 #include <texteditor/itexteditor.h>
 #include <texteditor/texteditorconstants.h>
-#include <texteditor/interactionsettings.h>
 #include <texteditor/tabsettings.h>
 #include <texteditor/texteditorsettings.h>
 #include <texteditor/textblockiterator.h>
@@ -229,9 +228,8 @@ private slots:
     void editorOpened(Core::IEditor *);
     void editorAboutToClose(Core::IEditor *);
 
-    void installHandlerOnCurrentEditor();
-    void installHandler(Core::IEditor *editor);
-    void removeHandler();
+    void setUseFakeVim(const QVariant &value);
+    void quit();
     void triggerCompletions();
     void showSettingsDialog();
 
@@ -244,8 +242,8 @@ private slots:
 
 private:
     FakeVimPlugin *q;
-    QAction *m_installHandlerAction; 
     FakeVimOptionPage *m_fakeVimOptionsPage;
+    QHash<Core::IEditor *, FakeVimHandler *> m_editorToHandler;
 };
 
 } // namespace Internal
@@ -254,7 +252,6 @@ private:
 FakeVimPluginPrivate::FakeVimPluginPrivate(FakeVimPlugin *plugin)
 {       
     q = plugin;
-    m_installHandlerAction = 0;
     m_fakeVimOptionsPage = 0;
 }
 
@@ -278,15 +275,12 @@ bool FakeVimPluginPrivate::initialize()
     QList<int> globalcontext;
     globalcontext << Core::Constants::C_GLOBAL_ID;
 
-    m_installHandlerAction = new QAction(this);
-    m_installHandlerAction->setText(tr("Set vi-Style Keyboard Action Handler"));
-
     m_fakeVimOptionsPage = new FakeVimOptionPage;
     q->addObject(m_fakeVimOptionsPage);
     theFakeVimSettings()->readSettings(Core::ICore::instance()->settings());
     
     Core::Command *cmd = 0;
-    cmd = actionManager->registerAction(m_installHandlerAction,
+    cmd = actionManager->registerAction(theFakeVimSetting(ConfigUseFakeVim),
         Constants::INSTALL_HANDLER, globalcontext);
     cmd->setDefaultKeySequence(QKeySequence(Constants::INSTALL_KEY));
 
@@ -294,9 +288,6 @@ bool FakeVimPluginPrivate::initialize()
         actionManager->actionContainer(Core::Constants::M_EDIT_ADVANCED);
     advancedMenu->addAction(cmd, Core::Constants::G_EDIT_EDITOR);
 
-    connect(m_installHandlerAction, SIGNAL(triggered()),
-        this, SLOT(installHandlerOnCurrentEditor()));
-
     // EditorManager
     QObject *editorManager = Core::ICore::instance()->editorManager();
     connect(editorManager, SIGNAL(editorAboutToClose(Core::IEditor*)),
@@ -306,6 +297,8 @@ bool FakeVimPluginPrivate::initialize()
 
     connect(theFakeVimSetting(SettingsDialog), SIGNAL(triggered()),
         this, SLOT(showSettingsDialog()));
+    connect(theFakeVimSetting(ConfigUseFakeVim), SIGNAL(valueChanged(QVariant)),
+        this, SLOT(setUseFakeVim(QVariant)));
 
     return true;
 }
@@ -315,7 +308,7 @@ void FakeVimPluginPrivate::showSettingsDialog()
     Core::ICore::instance()->showOptionsDialog("FakeVim", "General");
 }
 
-void FakeVimPluginPrivate::installHandler(Core::IEditor *editor)
+void FakeVimPluginPrivate::editorOpened(Core::IEditor *editor)
 {
     if (!editor)
         return;
@@ -328,14 +321,18 @@ void FakeVimPluginPrivate::installHandler(Core::IEditor *editor)
     if (!qobject_cast<QTextEdit *>(widget) && !qobject_cast<QPlainTextEdit *>(widget))
         return;
     
+    //qDebug() << "OPENING: " << editor << editor->widget()
+    //    << "MODE: " << theFakeVimSetting(ConfigUseFakeVim)->value();
+
     FakeVimHandler *handler = new FakeVimHandler(widget, widget);
+    m_editorToHandler[editor] = handler;
 
     connect(handler, SIGNAL(extraInformationChanged(QString)),
         this, SLOT(showExtraInformation(QString)));
     connect(handler, SIGNAL(commandBufferChanged(QString)),
         this, SLOT(showCommandBuffer(QString)));
     connect(handler, SIGNAL(quitRequested()),
-        this, SLOT(removeHandler()), Qt::QueuedConnection);
+        this, SLOT(quit()), Qt::QueuedConnection);
     connect(handler, SIGNAL(writeFileRequested(bool*,QString,QString)),
         this, SLOT(writeFile(bool*,QString,QString)));
     connect(handler, SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)),
@@ -347,14 +344,34 @@ void FakeVimPluginPrivate::installHandler(Core::IEditor *editor)
     connect(handler, SIGNAL(completionRequested()),
         this, SLOT(triggerCompletions()));
 
-    handler->setupWidget();
-    handler->setExtraData(editor);
     handler->setCurrentFileName(editor->file()->fileName());
+    handler->installEventFilter();
 }
 
-void FakeVimPluginPrivate::installHandlerOnCurrentEditor()
+void FakeVimPluginPrivate::editorAboutToClose(Core::IEditor *editor)
 {
-    installHandler(EditorManager::instance()->currentEditor());
+    //qDebug() << "CLOSING: " << editor << editor->widget();
+    m_editorToHandler.remove(editor);
+}
+
+void FakeVimPluginPrivate::setUseFakeVim(const QVariant &value)
+{
+    bool on = value.toBool();
+    if (on) {
+        Core::EditorManager::instance()->showEditorStatusBar( 
+            QLatin1String(Constants::MINI_BUFFER), 
+            "vi emulation mode. Type :q to leave. Use , Ctrl-R to trigger run.",
+            tr("Quit FakeVim"), this, SLOT(quit()));
+        foreach (Core::IEditor *editor, m_editorToHandler.keys())
+            m_editorToHandler[editor]->setupWidget();
+        //qDebug() << "SETTING" << m_editorToHandler.keys();
+    } else {
+        Core::EditorManager::instance()->hideEditorStatusBar(
+            QLatin1String(Constants::MINI_BUFFER));
+        foreach (Core::IEditor *editor, m_editorToHandler.keys())
+            m_editorToHandler[editor]->restoreWidget();
+        //qDebug() << "REMOVING" << m_editorToHandler.keys();
+    }
 }
 
 void FakeVimPluginPrivate::triggerCompletions()
@@ -377,7 +394,7 @@ void FakeVimPluginPrivate::writeFile(bool *handled,
     if (!handler)
         return;
 
-    Core::IEditor *editor = qobject_cast<Core::IEditor *>(handler->extraData());
+    Core::IEditor *editor = m_editorToHandler.key(handler);
     if (editor && editor->file()->fileName() == fileName) {
         // Handle that as a special case for nicer interaction with core
         Core::IFile *file = editor->file();
@@ -467,42 +484,21 @@ void FakeVimPluginPrivate::indentRegion(int *amount, int beginLine, int endLine,
     } while (cur != end);
 }
 
-void FakeVimPluginPrivate::removeHandler()
-{
-    if (FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender())) {
-        handler->restoreWidget();
-        handler->deleteLater();
-    }
-    Core::EditorManager::instance()->hideEditorStatusBar(
-        QLatin1String(Constants::MINI_BUFFER));
-}
-
-void FakeVimPluginPrivate::editorOpened(Core::IEditor *editor)
+void FakeVimPluginPrivate::quit()
 {
-    //qDebug() << "OPENING: " << editor << editor->widget();
-    QSettings *s = ICore::instance()->settings();
-    bool automatic = s->value("textInteractionSettings/UseVim").toBool();
-    if (automatic)
-       installHandler(editor);
-}
-
-void FakeVimPluginPrivate::editorAboutToClose(Core::IEditor *editor)
-{
-    //qDebug() << "CLOSING: " << editor << editor->widget();
-    Q_UNUSED(editor);
+    //if (FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender())) {
+    //    handler->restoreWidget();
+    //    handler->deleteLater();
+    //}
+    setUseFakeVim(false);
 }
 
 void FakeVimPluginPrivate::showCommandBuffer(const QString &contents)
 {
-    FakeVimHandler *handler = qobject_cast<FakeVimHandler *>(sender());
-    if (handler) {
-        //qDebug() << "SHOW COMMAND BUFFER" << contents;
-        Core::EditorManager::instance()->showEditorStatusBar( 
-            QLatin1String(Constants::MINI_BUFFER), contents,
-            tr("Quit FakeVim"), handler, SLOT(quit()));
-    } else {
-        qDebug() << "\nNO HANDLER\n";
-    }
+    //qDebug() << "SHOW COMMAND BUFFER" << contents;
+    Core::EditorManager::instance()->showEditorStatusBar( 
+        QLatin1String(Constants::MINI_BUFFER), contents,
+        tr("Quit FakeVim"), this, SLOT(quit()));
 }
 
 void FakeVimPluginPrivate::showExtraInformation(const QString &text)
diff --git a/tests/manual/gdbdebugger/simple/app/app.pro b/tests/manual/gdbdebugger/simple/app/app.pro
index 2210857b6a2..ab98e906f48 100644
--- a/tests/manual/gdbdebugger/simple/app/app.pro
+++ b/tests/manual/gdbdebugger/simple/app/app.pro
@@ -8,4 +8,4 @@ DESTDIR = ..
 SOURCES += ../app.cpp
 QT += network
 
-message("this says <foo & bar>")
+mesage("this says <foo & bar>")
-- 
GitLab