diff options
author | hjk <hjk121@nokiamail.com> | 2013-03-08 17:50:16 +0100 |
---|---|---|
committer | hjk <hjk121@nokiamail.com> | 2013-03-08 17:53:14 +0100 |
commit | d44bac6f430fc9d6d545d101e722b9dda443482d (patch) | |
tree | 5927271e8457af3b27f94c6532b3ed50bd45d253 /tests/manual/fakevim | |
parent | e1f08408203c447ed830d488eefc9ba9b21423eb (diff) |
FakeVim: Make plugin standalone if necessary
It's possible to use FakeVim as standalone plugin for a QTextEdit or
QPlainTextEdit widget, so there should be minimum dependencies on
Qt Creator code.
Change-Id: I415ed87f5e4d97ea78d9d25a8f0c82701ef1f70d
Reviewed-by: hjk <hjk121@nokiamail.com>
Diffstat (limited to 'tests/manual/fakevim')
-rw-r--r-- | tests/manual/fakevim/fakevim.pro | 14 | ||||
-rw-r--r-- | tests/manual/fakevim/main.cpp | 216 |
2 files changed, 178 insertions, 52 deletions
diff --git a/tests/manual/fakevim/fakevim.pro b/tests/manual/fakevim/fakevim.pro index 4cb3db33901..69eacc5042c 100644 --- a/tests/manual/fakevim/fakevim.pro +++ b/tests/manual/fakevim/fakevim.pro @@ -1,15 +1,19 @@ include(../../auto/qttest.pri) include($$IDE_SOURCE_TREE/src/libs/utils/utils.pri) +DEFINES += FAKEVIM_STANDALONE FAKEVIMDIR = $$IDE_SOURCE_TREE/src/plugins/fakevim -LIBSDIR = $$IDE_SOURCE_TREE/src/libs +UTILSDIR = $$IDE_SOURCE_TREE/src/libs/utils SOURCES += main.cpp \ $$FAKEVIMDIR/fakevimhandler.cpp \ - $$FAKEVIMDIR/fakevimactions.cpp + $$FAKEVIMDIR/fakevimactions.cpp \ + $$UTILSDIR/hostosinfo.cpp \ + $$UTILSDIR/qtcassert.cpp HEADERS += $$FAKEVIMDIR/fakevimhandler.h \ - $$FAKEVIMDIR/fakevimactions.h - -INCLUDEPATH += $$FAKEVIMDIR $$LIBSDIR + $$FAKEVIMDIR/fakevimactions.h \ + $$UTILSDIR/hostosinfo.h \ + $$UTILSDIR/qtcassert.h +INCLUDEPATH += $$FAKEVIMDIR $$UTILSDIR diff --git a/tests/manual/fakevim/main.cpp b/tests/manual/fakevim/main.cpp index 59ad34523b4..fd965122be1 100644 --- a/tests/manual/fakevim/main.cpp +++ b/tests/manual/fakevim/main.cpp @@ -29,17 +29,56 @@ #include "fakevimhandler.h" -#include <QDebug> - #include <QApplication> +#include <QFontMetrics> #include <QMainWindow> #include <QMessageBox> +#include <QPainter> #include <QPlainTextEdit> #include <QStatusBar> #include <QTextEdit> using namespace FakeVim::Internal; +typedef QLatin1String _; + +/** + * Simple editor widget. + * @tparam TextEdit QTextEdit or QPlainTextEdit as base class + */ +template <typename TextEdit> +class Editor : public TextEdit +{ +public: + Editor(QWidget *parent = 0) : TextEdit(parent) + { + TextEdit::setCursorWidth(0); + } + + void paintEvent(QPaintEvent *e) + { + TextEdit::paintEvent(e); + + // Draw text cursor. + QRect rect = TextEdit::cursorRect(); + if ( e->rect().contains(rect) ) { + QPainter painter(TextEdit::viewport()); + + if ( TextEdit::overwriteMode() ) { + QFontMetrics fm(TextEdit::font()); + rect.setWidth(fm.width(QLatin1Char('m'))); + painter.setPen(Qt::NoPen); + painter.setBrush(TextEdit::palette().color(QPalette::Base)); + painter.setCompositionMode(QPainter::CompositionMode_Difference); + } else { + rect.setWidth(TextEdit::cursorWidth()); + painter.setPen(TextEdit::palette().color(QPalette::Text)); + } + painter.drawRect(rect); + } + } +}; + class Proxy : public QObject { Q_OBJECT @@ -64,6 +103,44 @@ public slots: updateStatusBar(); } + void highlightMatches(const QString &pattern) + { + QTextEdit *ed = qobject_cast<QTextEdit *>(m_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); + b = cur.position(); + if (a == b) break; + } + a = b; + } + } + void changeStatusMessage(const QString &contents, int cursorPos) { m_statusMessage = cursorPos == -1 ? contents @@ -83,6 +160,18 @@ public slots: m_mainWindow->statusBar()->showMessage(msg); } + void handleExCommand(bool *handled, const ExCommand &cmd) + { + if (cmd.matches(_("q"), _("quit")) || cmd.matches(_("qa"), _("qall"))) { + QApplication::quit(); + } else { + *handled = false; + return; + } + + *handled = true; + } + private: QWidget *m_widget; QMainWindow *m_mainWindow; @@ -90,55 +179,63 @@ private: QString m_statusData; }; -int main(int argc, char *argv[]) +QWidget *createEditorWidget(bool usePlainTextEdit) { - QApplication app(argc, argv); - - QStringList args = app.arguments(); - (void) args.takeFirst(); - - QWidget *widget = 0; - QString title; - bool usePlainTextEdit = args.size() < 2; + QWidget *editor = 0; if (usePlainTextEdit) { - QPlainTextEdit *w = new QPlainTextEdit; + Editor<QPlainTextEdit> *w = new Editor<QPlainTextEdit>; w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - title = QLatin1String("PlainTextEdit"); - widget = w; + editor = w; } else { - QTextEdit *w = new QTextEdit; + Editor<QTextEdit> *w = new Editor<QTextEdit>; w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - title = QLatin1String("TextEdit"); - widget = w; + editor = w; } - widget->setObjectName(QLatin1String("Editor")); - //widget->resize(450, 350); - widget->setFocus(); + editor->setObjectName(_("Editor")); + editor->setFocus(); + + return editor; +} - QMainWindow mw; - Proxy proxy(widget, &mw); +void initHandler(FakeVimHandler &handler) +{ + // Set some Vim options. + handler.handleCommand(_("set expandtab")); + handler.handleCommand(_("set shiftwidth=8")); + handler.handleCommand(_("set tabstop=16")); + handler.handleCommand(_("set autoindent")); - FakeVimHandler handler(widget, 0); + // Try to source file "fakevimrc" from current directory. + handler.handleCommand(_("source fakevimrc")); + + handler.installEventFilter(); + handler.setupWidget(); +} - mw.setWindowTitle(QLatin1String("Fakevim (") + title + QLatin1Char(')')); - mw.setCentralWidget(widget); - mw.resize(600, 650); - mw.move(0, 0); - mw.show(); +void initMainWindow(QMainWindow &mainWindow, QWidget *centralWidget, const QString &title) +{ + mainWindow.setWindowTitle(QString(_("FakeVim (%1)")).arg(title)); + mainWindow.setCentralWidget(centralWidget); + mainWindow.resize(600, 650); + mainWindow.move(0, 0); + mainWindow.show(); - QFont font = widget->font(); - //: -misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-iso8859-1 - //font.setFamily("Misc"); - font.setFamily(QLatin1String("Monospace")); - //font.setStretch(QFont::SemiCondensed); + // Set monospace font for editor and status bar. + QFont font = QApplication::font(); + font.setFamily(_("Monospace")); + centralWidget->setFont(font); + mainWindow.statusBar()->setFont(font); +} - widget->setFont(font); - mw.statusBar()->setFont(font); +void readFile(FakeVimHandler &handler, const QString &editFileName) +{ + handler.handleCommand(QString(_("r %1")).arg(editFileName)); +} - QObject::connect(&handler, SIGNAL(commandBufferChanged(QString,int)), +void connectSignals(FakeVimHandler &handler, Proxy &proxy) +{ + QObject::connect(&handler, SIGNAL(commandBufferChanged(QString,int,int,int,QObject*)), &proxy, SLOT(changeStatusMessage(QString,int))); - //QObject::connect(&handler, SIGNAL(quitRequested(bool)), - // &app, SLOT(quit())); QObject::connect(&handler, SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)), &proxy, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>))); @@ -146,17 +243,42 @@ int main(int argc, char *argv[]) &proxy, SLOT(changeExtraInformation(QString))); QObject::connect(&handler, SIGNAL(statusDataChanged(QString)), &proxy, SLOT(changeStatusData(QString))); + QObject::connect(&handler, SIGNAL(highlightMatches(QString)), + &proxy, SLOT(highlightMatches(QString))); + QObject::connect(&handler, SIGNAL(handleExCommandRequested(bool*,ExCommand)), + &proxy, SLOT(handleExCommand(bool*,ExCommand))); +} - theFakeVimSetting(ConfigUseFakeVim)->setValue(true); - theFakeVimSetting(ConfigShiftWidth)->setValue(8); - theFakeVimSetting(ConfigTabStop)->setValue(8); - theFakeVimSetting(ConfigAutoIndent)->setValue(true); - theFakeVimSetting(ConfigIsKeyword)->setValue(QLatin1String("@,48-57,_,192-255,a-z,A-Z")); +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); - handler.installEventFilter(); - handler.setupWidget(); - if (args.size() >= 1) - handler.handleCommand(QLatin1String("r ") + args.at(0)); + QStringList args = app.arguments(); + + // If first argument is present use QPlainTextEdit instead on QTextEdit; + bool usePlainTextEdit = args.size() > 1; + // Second argument is path to file to edit. + const QString editFileName = args.value(2, QString(_("/usr/share/vim/vim73/tutor/tutor"))); + + // Create editor widget. + QWidget *editor = createEditorWidget(usePlainTextEdit); + + // Create FakeVimHandler instance which will emulate Vim behavior in editor widget. + FakeVimHandler handler(editor, 0); + + // Create main window. + QMainWindow mainWindow; + initMainWindow(mainWindow, editor, usePlainTextEdit ? _("QPlainTextEdit") : _("QTextEdit")); + + // Connect slots to FakeVimHandler signals. + Proxy proxy(editor, &mainWindow); + connectSignals(handler, proxy); + + // Initialize FakeVimHandler. + initHandler(handler); + + // Read file content to editor. + readFile(handler, editFileName); return app.exec(); } |