diff options
author | hjk <hjk@qt.io> | 2018-02-09 18:49:18 +0100 |
---|---|---|
committer | hjk <hjk@qt.io> | 2018-03-02 09:07:33 +0000 |
commit | 3ea4b0156c7a0567656eecabd67d801ad14c7eb5 (patch) | |
tree | 8cb3174f574137590b3847ff0f83602aacca821c /tests/manual/fakevim | |
parent | 33f2c240490b02ecae00a296022cdb8061a69a3d (diff) |
FakeVim: Experiment with hand-written signals
Change-Id: If76c68d3abb42240a279c15cf4b3d4b9e04460bd
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'tests/manual/fakevim')
-rw-r--r-- | tests/manual/fakevim/main.cpp | 193 |
1 files changed, 87 insertions, 106 deletions
diff --git a/tests/manual/fakevim/main.cpp b/tests/manual/fakevim/main.cpp index 7c364c09ad9..12d933e6934 100644 --- a/tests/manual/fakevim/main.cpp +++ b/tests/manual/fakevim/main.cpp @@ -74,105 +74,69 @@ public: } }; -class Proxy : public QObject +static void highlightMatches(QWidget *widget, const QString &pattern) { - Q_OBJECT - -public: - Proxy(QMainWindow *mw) - : m_mainWindow(mw) - {} - - void changeSelection(FakeVimHandler *handler, const QList<QTextEdit::ExtraSelection> &s) - { - QWidget *widget = handler->widget(); - if (auto ed = qobject_cast<QPlainTextEdit *>(widget)) - ed->setExtraSelections(s); - else if (auto ed = qobject_cast<QTextEdit *>(widget)) - ed->setExtraSelections(s); - } - - void changeStatusData(FakeVimHandler *, const QString &info) - { - m_statusData = info; - updateStatusBar(); - } - - void highlightMatches(FakeVimHandler *handler, const QString &pattern) - { - QWidget *widget = handler->widget(); - auto ed = qobject_cast<QTextEdit *>(widget); - if (!ed) - return; - - // Clear previous highlights. - ed->selectAll(); - QTextCursor cur = ed->textCursor(); - QTextCharFormat fmt = cur.charFormat(); - fmt.setBackground(Qt::transparent); - cur.setCharFormat(fmt); - - // Highlight matches. - QTextDocument *doc = ed->document(); - QRegExp re(pattern); - cur = doc->find(re); - - int a = cur.position(); - while ( !cur.isNull() ) { - if ( cur.hasSelection() ) { - fmt.setBackground(Qt::yellow); - cur.setCharFormat(fmt); - } else { - cur.movePosition(QTextCursor::NextCharacter); - } + auto ed = qobject_cast<QTextEdit *>(widget); + if (!ed) + return; + + // Clear previous highlights. + ed->selectAll(); + QTextCursor cur = ed->textCursor(); + QTextCharFormat fmt = cur.charFormat(); + fmt.setBackground(Qt::transparent); + cur.setCharFormat(fmt); + + // Highlight matches. + QTextDocument *doc = ed->document(); + QRegExp re(pattern); + cur = doc->find(re); + + int a = cur.position(); + while ( !cur.isNull() ) { + if ( cur.hasSelection() ) { + fmt.setBackground(Qt::yellow); + cur.setCharFormat(fmt); + } else { + cur.movePosition(QTextCursor::NextCharacter); + } + cur = doc->find(re, cur); + int b = cur.position(); + if (a == b) { + cur.movePosition(QTextCursor::NextCharacter); cur = doc->find(re, cur); - int b = cur.position(); - if (a == b) { - cur.movePosition(QTextCursor::NextCharacter); - cur = doc->find(re, cur); - b = cur.position(); - if (a == b) break; - } - a = b; + b = cur.position(); + if (a == b) break; } + a = b; } +} - void changeStatusMessage(FakeVimHandler *, const QString &contents, int cursorPos) +class StatusData +{ +public: + void setStatusMessage(const QString &msg, int pos) { - m_statusMessage = cursorPos == -1 ? contents - : contents.left(cursorPos) + QChar(10073) + contents.mid(cursorPos); - updateStatusBar(); + m_statusMessage = pos == -1 ? msg : msg.left(pos) + QChar(10073) + msg.mid(pos); } - void changeExtraInformation(FakeVimHandler *handler, const QString &info) + void setStatusInfo(const QString &info) { - QMessageBox::information(handler->widget(), tr("Information"), info); + m_statusData = info; } - void updateStatusBar() + QString currentStatusLine() const { - int slack = 80 - m_statusMessage.size() - m_statusData.size(); - QString msg = m_statusMessage + QString(slack, ' ') + m_statusData; - m_mainWindow->statusBar()->showMessage(msg); - } - - void handleExCommand(FakeVimHandler *, bool *handled, const ExCommand &cmd) - { - if (cmd.matches("q", "quit") || cmd.matches("qa", "qall")) { - QApplication::quit(); - *handled = true; - } else { - *handled = false; - } + const int slack = 80 - m_statusMessage.size() - m_statusData.size(); + return m_statusMessage + QString(slack, ' ') + m_statusData; } private: - QMainWindow *m_mainWindow; QString m_statusMessage; QString m_statusData; }; -QWidget *createEditorWidget(bool usePlainTextEdit) +static QWidget *createEditorWidget(bool usePlainTextEdit) { QWidget *editor = 0; if (usePlainTextEdit) @@ -185,7 +149,7 @@ QWidget *createEditorWidget(bool usePlainTextEdit) return editor; } -void initHandler(FakeVimHandler &handler) +static void initHandler(FakeVimHandler &handler) { // Set some Vim options. handler.handleCommand("set expandtab"); @@ -200,7 +164,7 @@ void initHandler(FakeVimHandler &handler) handler.setupWidget(); } -void initMainWindow(QMainWindow &mainWindow, QWidget *centralWidget, const QString &title) +static void initMainWindow(QMainWindow &mainWindow, QWidget *centralWidget, const QString &title) { mainWindow.setWindowTitle(QString("FakeVim (%1)").arg(title)); mainWindow.setCentralWidget(centralWidget); @@ -220,22 +184,6 @@ void readFile(FakeVimHandler &handler, const QString &editFileName) handler.handleCommand("r " + editFileName); } -void connectSignals(FakeVimHandler &handler, Proxy &proxy) -{ - QObject::connect(&handler, &FakeVimHandler::commandBufferChanged, - &proxy, &Proxy::changeStatusMessage); - QObject::connect(&handler, &FakeVimHandler::selectionChanged, - &proxy, &Proxy::changeSelection); - QObject::connect(&handler, &FakeVimHandler::extraInformationChanged, - &proxy, &Proxy::changeExtraInformation); - QObject::connect(&handler, &FakeVimHandler::statusDataChanged, - &proxy, &Proxy::changeStatusData); - QObject::connect(&handler, &FakeVimHandler::highlightMatches, - &proxy, &Proxy::highlightMatches); - QObject::connect(&handler, &FakeVimHandler::handleExCommandRequested, - &proxy, &Proxy::handleExCommand); -} - int main(int argc, char *argv[]) { QApplication app(argc, argv); @@ -250,16 +198,51 @@ int main(int argc, char *argv[]) // Create editor widget. QWidget *editor = createEditorWidget(usePlainTextEdit); - // Create FakeVimHandler instance which will emulate Vim behavior in editor widget. - FakeVimHandler handler(editor, nullptr); - // Create main window. QMainWindow mainWindow; initMainWindow(mainWindow, editor, usePlainTextEdit ? "QPlainTextEdit" : "QTextEdit"); - // Connect slots to FakeVimHandler signals. - Proxy proxy(&mainWindow); - connectSignals(handler, proxy); + // Keep track of status line related data. + StatusData statusData; + + // Create FakeVimHandler instance which will emulate Vim behavior in editor widget. + FakeVimHandler handler(editor, nullptr); + + handler.commandBufferChanged.connect([&](const QString &msg, int cursorPos, int, int) { + statusData.setStatusMessage(msg, cursorPos); + mainWindow.statusBar()->showMessage(statusData.currentStatusLine()); + }); + + handler.selectionChanged.connect([&handler](const QList<QTextEdit::ExtraSelection> &s) { + QWidget *widget = handler.widget(); + if (auto ed = qobject_cast<QPlainTextEdit *>(widget)) + ed->setExtraSelections(s); + else if (auto ed = qobject_cast<QTextEdit *>(widget)) + ed->setExtraSelections(s); + }); + + handler.extraInformationChanged.connect([&](const QString &info) { + statusData.setStatusInfo(info); + mainWindow.statusBar()->showMessage(statusData.currentStatusLine()); + }); + + handler.statusDataChanged.connect([&](const QString &info) { + statusData.setStatusInfo(info); + mainWindow.statusBar()->showMessage(statusData.currentStatusLine()); + }); + + handler.highlightMatches.connect([&](const QString &needle) { + highlightMatches(handler.widget(), needle); + }); + + handler.handleExCommandRequested.connect([](bool *handled, const ExCommand &cmd) { + if (cmd.matches("q", "quit") || cmd.matches("qa", "qall")) { + QApplication::quit(); + *handled = true; + } else { + *handled = false; + } + }); // Initialize FakeVimHandler. initHandler(handler); @@ -269,5 +252,3 @@ int main(int argc, char *argv[]) return app.exec(); } - -#include "main.moc" |