main.cpp 5.37 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Bill King's avatar
Bill King committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Bill King's avatar
Bill King committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Bill King's avatar
Bill King committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Bill King's avatar
Bill King committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Bill King's avatar
Bill King committed
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Bill King's avatar
Bill King committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

30
#include "fakevimhandler.h"
hjk's avatar
hjk committed
31

32
#include <QDebug>
hjk's avatar
hjk committed
33

34
35
36
37
38
39
#include <QApplication>
#include <QMainWindow>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QStatusBar>
#include <QTextEdit>
hjk's avatar
hjk committed
40
41
42

using namespace FakeVim::Internal;

43
44
45
46
47
class Proxy : public QObject
{
    Q_OBJECT

public:
48
49
    Proxy(QWidget *widget, QMainWindow *mw, QObject *parent = 0)
      : QObject(parent), m_widget(widget), m_mainWindow(mw)
50
    {}
51
52

public slots:
53
    void changeSelection(const QList<QTextEdit::ExtraSelection> &s)
54
    {
55
        if (QPlainTextEdit *ed = qobject_cast<QPlainTextEdit *>(m_widget))
56
            ed->setExtraSelections(s);
57
        else if (QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget))
58
59
            ed->setExtraSelections(s);
    }
60

61
62
63
64
65
66
    void changeStatusData(const QString &info)
    {
        m_statusData = info;
        updateStatusBar();
    }

hjk's avatar
hjk committed
67
    void changeStatusMessage(const QString &contents, int cursorPos)
68
    {
hjk's avatar
hjk committed
69
70
        m_statusMessage = cursorPos == -1 ? contents
            : contents.left(cursorPos) + QChar(10073) + contents.mid(cursorPos);
71
72
73
        updateStatusBar();
    }

74
    void changeExtraInformation(const QString &info)
75
    {
76
        QMessageBox::information(m_widget, tr("Information"), info);
77
    }
78

79
80
81
    void updateStatusBar()
    {
        int slack = 80 - m_statusMessage.size() - m_statusData.size();
82
        QString msg = m_statusMessage + QString(slack, QLatin1Char(' ')) + m_statusData;
83
84
        m_mainWindow->statusBar()->showMessage(msg);
    }
85
86
87

private:
    QWidget *m_widget;
88
89
90
    QMainWindow *m_mainWindow;
    QString m_statusMessage;
    QString m_statusData;
91
92
};

hjk's avatar
hjk committed
93
94
95
96
97
98
99
100
101
102
103
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QStringList args = app.arguments();
    (void) args.takeFirst();

    QWidget *widget = 0;
    QString title;
    bool usePlainTextEdit = args.size() < 2;
    if (usePlainTextEdit) {
104
105
        QPlainTextEdit *w = new QPlainTextEdit;
        w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
106
        title = QLatin1String("PlainTextEdit");
107
        widget = w;
hjk's avatar
hjk committed
108
    } else {
109
110
        QTextEdit *w = new QTextEdit;
        w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
111
        title = QLatin1String("TextEdit");
112
        widget = w;
hjk's avatar
hjk committed
113
    }
114
    widget->setObjectName(QLatin1String("Editor"));
115
    //widget->resize(450, 350);
hjk's avatar
hjk committed
116
117
    widget->setFocus();

118
119
    QMainWindow mw;
    Proxy proxy(widget, &mw);
120

121
    FakeVimHandler handler(widget, 0);
hjk's avatar
hjk committed
122

123
    mw.setWindowTitle(QLatin1String("Fakevim (") + title + QLatin1Char(')'));
hjk's avatar
hjk committed
124
    mw.setCentralWidget(widget);
125
    mw.resize(600, 650);
hjk's avatar
hjk committed
126
127
    mw.move(0, 0);
    mw.show();
128

129
130
131
    QFont font = widget->font();
    //: -misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-iso8859-1
    //font.setFamily("Misc");
132
    font.setFamily(QLatin1String("Monospace"));
133
134
135
    //font.setStretch(QFont::SemiCondensed);

    widget->setFont(font);
hjk's avatar
hjk committed
136
137
    mw.statusBar()->setFont(font);

hjk's avatar
hjk committed
138
139
    QObject::connect(&handler, SIGNAL(commandBufferChanged(QString,int)),
        &proxy, SLOT(changeStatusMessage(QString,int)));
140
141
    //QObject::connect(&handler, SIGNAL(quitRequested(bool)),
    //    &app, SLOT(quit()));
142
    QObject::connect(&handler,
143
144
        SIGNAL(selectionChanged(QList<QTextEdit::ExtraSelection>)),
        &proxy, SLOT(changeSelection(QList<QTextEdit::ExtraSelection>)));
145
    QObject::connect(&handler, SIGNAL(extraInformationChanged(QString)),
146
        &proxy, SLOT(changeExtraInformation(QString)));
147
148
    QObject::connect(&handler, SIGNAL(statusDataChanged(QString)),
        &proxy, SLOT(changeStatusData(QString)));
hjk's avatar
hjk committed
149

150
    theFakeVimSetting(ConfigUseFakeVim)->setValue(true);
151
152
    theFakeVimSetting(ConfigShiftWidth)->setValue(8);
    theFakeVimSetting(ConfigTabStop)->setValue(8);
153
    theFakeVimSetting(ConfigAutoIndent)->setValue(true);
154
    theFakeVimSetting(ConfigIsKeyword)->setValue(QLatin1String("@,48-57,_,192-255,a-z,A-Z"));
155

156
    handler.installEventFilter();
157
    handler.setupWidget();
hjk's avatar
hjk committed
158
    if (args.size() >= 1)
159
        handler.handleCommand(QLatin1String("r ") + args.at(0));
hjk's avatar
hjk committed
160
161
162

    return app.exec();
}
163
164

#include "main.moc"