kexi

kexieditor.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
3 Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
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 <keximainwindow.h>
25
26#include <tqlayout.h>
27#include <tqframe.h>
28#include <tdelocale.h>
29#include <kdebug.h>
30
31//uncomment this to enable KTextEdit-based editor
32//#define KTEXTEDIT_BASED_SQL_EDITOR
33
34#ifdef KTEXTEDIT_BASED_SQL_EDITOR
35# include <ktextedit.h>
36#else
37# include <tdetexteditor/document.h>
38# include <tdetexteditor/view.h>
39# include <tdetexteditor/editorchooser.h>
40# include <tdetexteditor/editinterface.h>
41# include <tdetexteditor/viewcursorinterface.h>
42# include <tdetexteditor/popupmenuinterface.h>
43# include <tdetexteditor/undointerface.h>
44# include <tdetexteditor/configinterface.h>
45# include <tdetexteditor/highlightinginterface.h>
46#endif
47
50class KexiEditorSharedActionConnector : public KexiSharedActionConnector
51{
52public:
53 KexiEditorSharedActionConnector( KexiActionProxy* proxy, TQObject* obj )
54 : KexiSharedActionConnector( proxy, obj )
55 {
56#ifdef KTEXTEDIT_BASED_SQL_EDITOR
57 plugSharedAction("edit_cut", TQ_SLOT(cut()));
58 plugSharedAction("edit_copy", TQ_SLOT(copy()));
59 plugSharedAction("edit_paste", TQ_SLOT(paste()));
60 plugSharedAction("edit_clear", TQ_SLOT(clear()));
61 plugSharedAction("edit_undo", TQ_SLOT(undo()));
62 plugSharedAction("edit_redo", TQ_SLOT(redo()));
63 plugSharedAction("edit_select_all", TQ_SLOT(selectAll()));
64#else
65 TQValueList<TQCString> actions;
66 actions << "edit_cut" << "edit_copy" << "edit_paste" << "edit_clear"
67 << "edit_undo" << "edit_redo" << "edit_select_all";
68 plugSharedActionsToExternalGUI(actions, dynamic_cast<KXMLGUIClient*>(obj));
69#endif
70 }
71};
72
74class KexiEditorPrivate {
75 public:
76#ifdef KTEXTEDIT_BASED_SQL_EDITOR
77 KTextEdit *view;
78#else
79 KTextEditor::Document *doc;
80 KTextEditor::View *view;
81#endif
82};
83
84KexiEditor::KexiEditor(KexiMainWindow *mainWin, TQWidget *parent, const char *name)
85 : KexiViewBase(mainWin, parent, name)
86 , d(new KexiEditorPrivate())
87{
88 TQVBoxLayout *layout = new TQVBoxLayout(this);
89#ifdef KTEXTEDIT_BASED_SQL_EDITOR
90 d->view = new KTextEdit( "", TQString(), this, "kexi_editor" );
91 //adjust font
92 connect(d->view, TQ_SIGNAL(textChanged()), this, TQ_SIGNAL(textChanged()));
93 TQFont f("Courier");
94 f.setStyleStrategy(TQFont::PreferAntialias);
95 f.setPointSize(d->view->font().pointSize());
96 d->view->setFont( f );
97 d->view->setCheckSpellingEnabled(false);
98#else
99 TQFrame *fr = new TQFrame(this);
100 fr->setFrameStyle(TQFrame::Sunken|TQFrame::WinPanel);
101 layout->addWidget(fr);
102 layout = new TQVBoxLayout(fr);
103 layout->setMargin( 2 );
104
105 d->doc = KTextEditor::EditorChooser::createDocument(fr);
106 if (!d->doc)
107 return;
108 d->view = d->doc->createView(fr, 0L);
109
110 KTextEditor::PopupMenuInterface *popupInt = dynamic_cast<KTextEditor::PopupMenuInterface*>( d->view );
111 if(popupInt) {
112 TQPopupMenu *pop = (TQPopupMenu*) mainWin->factory()->container("edit", mainWin);
113 if(pop) {
114 //plugSharedAction("edit_undo", pop);
115 popupInt->installPopup(pop);
116 }
117 }
118
119 connect(d->doc, TQ_SIGNAL(textChanged()), this, TQ_SIGNAL(textChanged()));
120#endif
121 KexiEditorSharedActionConnector c(this, d->view);
122 d->view->installEventFilter(this);
123
124 layout->addWidget(d->view);
125 setViewWidget(d->view, true/*focus*/);
126 d->view->show();
127}
128
130{
131 delete d;
132}
133
134void KexiEditor::updateActions(bool activated)
135{
137}
138
140{
141#ifdef KTEXTEDIT_BASED_SQL_EDITOR
142 return false;
143#else
144 return true;
145#endif
146}
147
149{
150#ifdef KTEXTEDIT_BASED_SQL_EDITOR
151 return d->view->text();
152#else
153 if (!d->doc)
154 return TQString();
155 KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
156 return eIface->text();
157#endif
158}
159
160void KexiEditor::setText(const TQString &text)
161{
162#ifdef KTEXTEDIT_BASED_SQL_EDITOR
163 const bool was_dirty = m_parentView ? m_parentView->dirty() : dirty();
164 d->view->setText(text);
165 setDirty(was_dirty);
166#else
167 if (!d->doc)
168 return;
169 const bool was_dirty = dirty();
170 KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
171 eIface->setText(text);
172 KTextEditor::UndoInterface *undoIface = KTextEditor::undoInterface(d->doc);
173 undoIface->clearUndo();
174 undoIface->clearRedo();
175 setDirty(was_dirty);
176#endif
177}
178
179void KexiEditor::setHighlightMode(const TQString& highlightmodename)
180{
181#ifdef KTEXTEDIT_BASED_SQL_EDITOR
182#else
183 KTextEditor::HighlightingInterface *hl = KTextEditor::highlightingInterface( d->doc );
184 for(uint i = 0; i < hl->hlModeCount(); i++) {
185 //kdDebug() << "hlmode("<<i<<"): " << hl->hlModeName(i) << endl;
186 if (hl->hlModeName(i).contains(highlightmodename, false)) {
187 hl->setHlMode(i);
188 return;
189 }
190 }
191 hl->setHlMode(0); // 0=None, don't highlight anything.
192#endif
193}
194
196{
197#ifdef KTEXTEDIT_BASED_SQL_EDITOR
198 //TODO show errormessage?
199#else
200 KTextEditor::ConfigInterface *config = KTextEditor::configInterface( d->doc );
201 if (config)
202 config->configDialog();
203#endif
204}
205
206void KexiEditor::jump(int character)
207{
208#ifdef KTEXTEDIT_BASED_SQL_EDITOR
209 const int numRows = d->view->paragraphs();
210 int row = 0, col = 0;
211 for (int ch = 0; row < numRows; row++) {
212 const int rowLen = d->view->paragraphLength(row)+1;
213 if ((ch + rowLen) > character) {
214 col = character-ch;
215 break;
216 }
217 ch += rowLen;
218 }
219 d->view->setCursorPosition(row, col);
220#else
221 if (!d->doc)
222 return;
223 KTextEditor::EditInterface *ei = KTextEditor::editInterface(d->doc);
224 const int numRows = ei->numLines();
225 int row = 0, col = 0;
226 for (int ch = 0; row < numRows; row++) {
227 const int rowLen = ei->lineLength(row)+1;
228 if ((ch + rowLen) > character) {
229 col = character-ch;
230 break;
231 }
232 ch += rowLen;
233 }
234 KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface(d->view);
235 ci->setCursorPositionReal(row, col);
236#endif
237}
238
239void KexiEditor::setCursorPosition(int line, int col)
240{
241#ifdef KTEXTEDIT_BASED_SQL_EDITOR
242 d->view->setCursorPosition(line, col);
243#else
244 KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface( d->view );
245 ci->setCursorPosition(line, col);
246#endif
247}
248
250{
251#ifdef KTEXTEDIT_BASED_SQL_EDITOR
252 //TODO how to remove undo/redo from a KTextEdit?
253#else
254 KTextEditor::UndoInterface* u = KTextEditor::undoInterface( d->doc );
255 u->clearUndo();
256 u->clearRedo();
257#endif
258}
259
260#include "kexieditor.moc"
261
void textChanged()
virtual ~KexiEditor()
Destructor.
void setText(const TQString &text)
virtual void updateActions(bool activated)
static bool isAdvancedEditor()
KexiEditor(KexiMainWindow *mainWin, TQWidget *parent, const char *name=0)
Constructor.
void clearUndoRedo()
Clear all remembered undo/redo-actions.
void setCursorPosition(int line, int col)
Set the cursor position to line and col .
void setHighlightMode(const TQString &highlightmodename)
Set the highlight-mode to highlightmodename .
void slotConfigureEditor()
void jump(int character)
Find row and column for this character and jump to the position.
TQString text()
Kexi's main window interface This interface is implemented by KexiMainWindowImpl class.
Abstract helper class used to connect shared actions from outside of shared-action-aware object.
virtual bool dirty() const
KexiMainWindow * mainWin() const
virtual void updateActions(bool activated)