~ubuntu-branches/debian/sid/kexi/sid

« back to all changes in this revision

Viewing changes to src/widget/kexieditor.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2017-06-24 20:10:10 UTC
  • Revision ID: package-import@ubuntu.com-20170624201010-5lrzd5r2vwthwifp
Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
 
3
   Copyright (C) 2004-2005 Jarosław Staniek <staniek@kde.org>
 
4
   Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
 
5
 
 
6
   This program is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2 of the License, or (at your option) any later version.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public License
 
17
   along with this program; see the file COPYING.  If not, write to
 
18
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301, USA.
 
20
*/
 
21
 
 
22
#include "kexieditor.h"
 
23
 
 
24
#include <KexiMainWindowIface.h>
 
25
#include <kexiutils/utils.h>
 
26
 
 
27
#include <QList>
 
28
#include <QMenu>
 
29
#include <QVBoxLayout>
 
30
 
 
31
//uncomment this to enable KTextEdit-based editor
 
32
//#define KTEXTEDIT_BASED_TEXT_EDITOR
 
33
 
 
34
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
35
# include <KTextEdit>
 
36
#else
 
37
# include <KTextEditor/Document>
 
38
# include <KTextEditor/Editor>
 
39
# include <KTextEditor/View>
 
40
# include <KTextEditor/ConfigInterface>
 
41
#endif
 
42
 
 
43
/** Used for the shared action framework to redirect shared actions like
 
44
copy and paste to the editor. */
 
45
class KexiEditorSharedActionConnector : public KexiSharedActionConnector
 
46
{
 
47
public:
 
48
    KexiEditorSharedActionConnector(KexiActionProxy* proxy, QObject* obj)
 
49
            : KexiSharedActionConnector(proxy, obj) {
 
50
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
51
        plugSharedAction("edit_cut", SLOT(cut()));
 
52
        plugSharedAction("edit_copy", SLOT(copy()));
 
53
        plugSharedAction("edit_paste", SLOT(paste()));
 
54
        plugSharedAction("edit_clear", SLOT(clear()));
 
55
        plugSharedAction("edit_undo", SLOT(undo()));
 
56
        plugSharedAction("edit_redo", SLOT(redo()));
 
57
        plugSharedAction("edit_select_all", SLOT(selectAll()));
 
58
#else
 
59
        QList<QByteArray> actions;
 
60
        actions << "edit_cut" << "edit_copy" << "edit_paste" << "edit_clear"
 
61
            << "edit_undo" << "edit_redo" << "edit_select_all";
 
62
//! @todo KEXI3 plugSharedActionsToExternalGUI(actions, dynamic_cast<KXMLGUIClient*>(obj));
 
63
#endif
 
64
    }
 
65
};
 
66
 
 
67
//! @internal
 
68
class KexiEditor::Private
 
69
{
 
70
public:
 
71
    Private() {}
 
72
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
73
    KTextEdit *view;
 
74
#else
 
75
    KTextEditor::Document *doc;
 
76
    KTextEditor::View *view;
 
77
#endif
 
78
};
 
79
 
 
80
KexiEditor::KexiEditor(QWidget *parent)
 
81
        : KexiView(parent)
 
82
        , d(new Private())
 
83
{
 
84
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
85
    d->view = new KTextEdit("", QString(), this);
 
86
    //adjust font
 
87
    connect(d->view, SIGNAL(textChanged()), this, SIGNAL(textChanged()));
 
88
    QFont f("Courier");
 
89
    f.setStyleStrategy(QFont::PreferAntialias);
 
90
    f.setPointSize(d->view->font().pointSize());
 
91
    d->view->setFont(f);
 
92
    d->view->setCheckSpellingEnabled(false);
 
93
#else
 
94
    QWidget *fr = new QWidget(this);
 
95
    QVBoxLayout *layout = new QVBoxLayout(fr);
 
96
    layout->setContentsMargins(0, 0, 0, 0);
 
97
 
 
98
    KTextEditor::Editor *editor = KTextEditor::Editor::instance();
 
99
    if (!editor)
 
100
        return;
 
101
//! @todo error handling!
 
102
 
 
103
    d->doc = editor->createDocument(fr);
 
104
    if (!d->doc)
 
105
        return;
 
106
    d->view = d->doc->createView(fr);
 
107
    // suppresing default saving mechanism of KTextEditor
 
108
    d->view->action("file_save")->setEnabled(false);
 
109
    // set word wrap by default
 
110
    KTextEditor::ConfigInterface *configIface
 
111
            = qobject_cast<KTextEditor::ConfigInterface*>(d->view);
 
112
    configIface->setConfigValue("dynamic-word-wrap", true);
 
113
 
 
114
//! @todo KEXI3 Q3PopupMenu *pop = qobject_cast<Q3PopupMenu*>( mainWin->factory()->container("edit", mainWin) );
 
115
//! @todo KEXI3 d->view->setContextMenu(pop);
 
116
    d->view->setContextMenu(d->view->defaultContextMenu());
 
117
 
 
118
    connect(d->doc, SIGNAL(textChanged(KTextEditor::Document*)),
 
119
            this, SLOT(slotTextChanged(KTextEditor::Document*)));
 
120
#endif
 
121
    KexiEditorSharedActionConnector c(this, d->view);
 
122
    d->view->installEventFilter(this);
 
123
 
 
124
    layout->addWidget(d->view);
 
125
    setViewWidget(fr, false/*!focus*/);
 
126
    setFocusProxy(d->view);
 
127
}
 
128
 
 
129
KexiEditor::~KexiEditor()
 
130
{
 
131
    delete d;
 
132
}
 
133
 
 
134
void KexiEditor::updateActions(bool activated)
 
135
{
 
136
    KexiView::updateActions(activated);
 
137
}
 
138
 
 
139
bool KexiEditor::isAdvancedEditor()
 
140
{
 
141
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
142
    return false;
 
143
#else
 
144
    return true;
 
145
#endif
 
146
}
 
147
 
 
148
QString KexiEditor::text()
 
149
{
 
150
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
151
    return d->view->text();
 
152
#else
 
153
    if (!d->doc)
 
154
        return QString();
 
155
    return d->doc->text();
 
156
#endif
 
157
}
 
158
 
 
159
void KexiEditor::setText(const QString &text)
 
160
{
 
161
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
162
    const bool was_dirty = m_parentView ? m_parentView->idDirty() : idDirty();
 
163
    d->view->setText(text);
 
164
    setDirty(was_dirty);
 
165
#else
 
166
    if (!d->doc)
 
167
        return;
 
168
    const bool was_dirty = isDirty();
 
169
    d->doc->setText(text);
 
170
    setDirty(was_dirty);
 
171
#endif
 
172
}
 
173
 
 
174
void KexiEditor::setHighlightMode(const QString& highlightmodename)
 
175
{
 
176
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
177
#else
 
178
    if (!d->doc)
 
179
        return;
 
180
    QString n = highlightmodename;
 
181
    if (n == "javascript" || n == "qtscript")
 
182
        n = "JavaScript";
 
183
    else if (n.size() > 0)
 
184
        n = n[0].toLower() + n.mid(1);
 
185
    if (!d->doc->setMode(n))
 
186
        d->doc->setMode(QString()); // don't highlight
 
187
    if (!d->doc->setHighlightingMode(n))
 
188
        d->doc->setHighlightingMode(QString()); //hl->setHlMode(0); // 0=None, don't highlight anything.
 
189
 
 
190
    //! @todo Code from 3540345ea16477d05e84, these signals no longer exist
 
191
    // QMetaObject::invokeMethod(d->view, "modeChanged", Q_ARG(KTextEditor::Document*, d->doc));
 
192
    // QMetaObject::invokeMethod(d->view, "highlightingModeChanged", Q_ARG(KTextEditor::Document*, d->doc));
 
193
#endif
 
194
}
 
195
 
 
196
void KexiEditor::slotConfigureEditor()
 
197
{
 
198
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
199
//! @todo show configuration...
 
200
#else
 
201
    if (!d->doc)
 
202
        return;
 
203
    KTextEditor::Editor *editor = KTextEditor::Editor::instance();
 
204
    if (!editor)
 
205
        return;
 
206
    editor->configDialog(this);
 
207
//! @todo use d->doc->editor()->writeConfig() or KTextEditor::ConfigInterface to save changes
 
208
#endif
 
209
}
 
210
 
 
211
void KexiEditor::jump(int character)
 
212
{
 
213
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
214
    const int numRows = d->view->paragraphs();
 
215
    int row = 0, col = 0;
 
216
    for (int ch = 0; row < numRows; row++) {
 
217
        const int rowLen = d->view->paragraphLength(row) + 1;
 
218
        if ((ch + rowLen) > character) {
 
219
            col = character - ch;
 
220
            break;
 
221
        }
 
222
        ch += rowLen;
 
223
    }
 
224
    d->view->setCursorPosition(row, col);
 
225
#else
 
226
    if (!d->doc)
 
227
        return;
 
228
    const int numRows = d->doc->lines();
 
229
    int row = 0, col = 0;
 
230
    for (int ch = 0; row < numRows; row++) {
 
231
        const int rowLen = d->doc->lineLength(row) + 1;
 
232
        if ((ch + rowLen) > character) {
 
233
            col = character - ch;
 
234
            break;
 
235
        }
 
236
        ch += rowLen;
 
237
    }
 
238
    d->view->setCursorPosition(KTextEditor::Cursor(row, col));
 
239
#endif
 
240
}
 
241
 
 
242
void KexiEditor::setCursorPosition(int line, int col)
 
243
{
 
244
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
245
    d->view->setCursorPosition(line, col);
 
246
#else
 
247
    d->view->setCursorPosition(KTextEditor::Cursor(line, col));
 
248
#endif
 
249
}
 
250
 
 
251
void KexiEditor::clearUndoRedo()
 
252
{
 
253
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
254
    //! @todo how to remove undo/redo from a KTextEdit?
 
255
#else
 
256
//! @todo KEXI3 KexiEditor::clearUndoRedo()
 
257
#endif
 
258
}
 
259
 
 
260
void KexiEditor::slotTextChanged(KTextEditor::Document *)
 
261
{
 
262
    emit textChanged();
 
263
}
 
264
 
 
265
QMenu* KexiEditor::defaultContextMenu()
 
266
{
 
267
#ifdef KTEXTEDIT_BASED_TEXT_EDITOR
 
268
    return d->view->createStandardContextMenu();
 
269
#else
 
270
    QMenu* menu = d->view->defaultContextMenu();
 
271
    menu->addSeparator();
 
272
    menu->addAction(d->view->action("edit_find"));
 
273
    menu->addAction(d->view->action("edit_find_next"));
 
274
    menu->addAction(d->view->action("edit_find_prev"));
 
275
    menu->addAction(d->view->action("edit_replace"));
 
276
    menu->addAction(d->view->action("go_goto_line"));
 
277
    return menu;
 
278
#endif
 
279
}
 
280