kexi

kexieditor.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
00003    Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
00004    Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kexieditor.h"
00023 
00024 #include <keximainwindow.h>
00025 
00026 #include <qlayout.h>
00027 #include <qframe.h>
00028 #include <klocale.h>
00029 #include <kdebug.h>
00030 
00031 //uncomment this to enable KTextEdit-based editor
00032 //#define KTEXTEDIT_BASED_SQL_EDITOR
00033 
00034 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00035 # include <ktextedit.h>
00036 #else
00037 # include <ktexteditor/document.h>
00038 # include <ktexteditor/view.h>
00039 # include <ktexteditor/editorchooser.h>
00040 # include <ktexteditor/editinterface.h>
00041 # include <ktexteditor/viewcursorinterface.h>
00042 # include <ktexteditor/popupmenuinterface.h>
00043 # include <ktexteditor/undointerface.h>
00044 # include <ktexteditor/configinterface.h>
00045 # include <ktexteditor/highlightinginterface.h>
00046 #endif
00047 
00050 class KexiEditorSharedActionConnector : public KexiSharedActionConnector
00051 {
00052 public:
00053     KexiEditorSharedActionConnector( KexiActionProxy* proxy, QObject* obj )
00054         : KexiSharedActionConnector( proxy, obj )
00055     {
00056 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00057         plugSharedAction("edit_cut", SLOT(cut()));
00058         plugSharedAction("edit_copy", SLOT(copy()));
00059         plugSharedAction("edit_paste", SLOT(paste()));
00060         plugSharedAction("edit_clear", SLOT(clear()));
00061         plugSharedAction("edit_undo", SLOT(undo()));
00062         plugSharedAction("edit_redo", SLOT(redo()));
00063                 plugSharedAction("edit_select_all", SLOT(selectAll()));
00064 #else
00065         QValueList<QCString> actions;
00066         actions << "edit_cut" << "edit_copy" << "edit_paste" << "edit_clear"
00067             << "edit_undo" << "edit_redo" << "edit_select_all";
00068         plugSharedActionsToExternalGUI(actions, dynamic_cast<KXMLGUIClient*>(obj));
00069 #endif
00070     }
00071 };
00072 
00074 class KexiEditorPrivate {
00075     public:
00076 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00077         KTextEdit *view;
00078 #else
00079         KTextEditor::Document *doc;
00080         KTextEditor::View *view;
00081 #endif
00082 };
00083 
00084 KexiEditor::KexiEditor(KexiMainWindow *mainWin, QWidget *parent, const char *name)
00085     : KexiViewBase(mainWin, parent, name)
00086     , d(new KexiEditorPrivate())
00087 {
00088     QVBoxLayout *layout = new QVBoxLayout(this);
00089 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00090     d->view = new KTextEdit( "", QString::null, this, "kexi_editor" );
00091     //adjust font
00092     connect(d->view, SIGNAL(textChanged()), this, SIGNAL(textChanged()));
00093     QFont f("Courier");
00094     f.setStyleStrategy(QFont::PreferAntialias);
00095     f.setPointSize(d->view->font().pointSize());
00096     d->view->setFont( f );
00097     d->view->setCheckSpellingEnabled(false);
00098 #else
00099     QFrame *fr = new QFrame(this);
00100     fr->setFrameStyle(QFrame::Sunken|QFrame::WinPanel);
00101     layout->addWidget(fr);
00102     layout = new QVBoxLayout(fr);
00103     layout->setMargin( 2 );
00104 
00105     d->doc =  KTextEditor::EditorChooser::createDocument(fr);
00106     if (!d->doc)
00107         return;
00108     d->view = d->doc->createView(fr, 0L);
00109 
00110     KTextEditor::PopupMenuInterface *popupInt = dynamic_cast<KTextEditor::PopupMenuInterface*>( d->view );
00111     if(popupInt) {
00112             QPopupMenu *pop = (QPopupMenu*) mainWin->factory()->container("edit", mainWin);
00113             if(pop) {
00114                  //plugSharedAction("edit_undo", pop);
00115                  popupInt->installPopup(pop);
00116             }
00117     }
00118 
00119     connect(d->doc, SIGNAL(textChanged()), this, SIGNAL(textChanged()));
00120 #endif
00121     KexiEditorSharedActionConnector c(this, d->view);
00122     d->view->installEventFilter(this);
00123 
00124     layout->addWidget(d->view);
00125     setViewWidget(d->view, true/*focus*/);
00126     d->view->show();
00127 }
00128 
00129 KexiEditor::~KexiEditor()
00130 {
00131     delete d;
00132 }
00133 
00134 void KexiEditor::updateActions(bool activated)
00135 {
00136     KexiViewBase::updateActions(activated);
00137 }
00138 
00139 bool KexiEditor::isAdvancedEditor()
00140 {
00141 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00142     return false;
00143 #else
00144     return true;
00145 #endif
00146 }
00147 
00148 QString KexiEditor::text()
00149 {
00150 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00151     return d->view->text();
00152 #else
00153     if (!d->doc)
00154         return QString::null;
00155     KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
00156     return eIface->text();
00157 #endif
00158 }
00159 
00160 void KexiEditor::setText(const QString &text)
00161 {
00162 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00163     const bool was_dirty = m_parentView ? m_parentView->dirty() : dirty();
00164     d->view->setText(text);
00165     setDirty(was_dirty);
00166 #else
00167     if (!d->doc)
00168         return;
00169     const bool was_dirty = dirty();
00170     KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
00171     eIface->setText(text);
00172     KTextEditor::UndoInterface *undoIface = KTextEditor::undoInterface(d->doc);
00173     undoIface->clearUndo();
00174     undoIface->clearRedo();
00175     setDirty(was_dirty);
00176 #endif
00177 }
00178 
00179 void KexiEditor::setHighlightMode(const QString& highlightmodename)
00180 {
00181 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00182 #else
00183     KTextEditor::HighlightingInterface *hl = KTextEditor::highlightingInterface( d->doc );
00184     for(uint i = 0; i < hl->hlModeCount(); i++) {
00185             //kdDebug() << "hlmode("<<i<<"): " << hl->hlModeName(i) << endl;
00186             if (hl->hlModeName(i).contains(highlightmodename, false))  {
00187                 hl->setHlMode(i);
00188                 return;
00189             }
00190     }
00191     hl->setHlMode(0); // 0=None, don't highlight anything.
00192 #endif
00193 }
00194 
00195 void KexiEditor::slotConfigureEditor()
00196 {
00197 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00198     //TODO show errormessage?
00199 #else
00200     KTextEditor::ConfigInterface *config = KTextEditor::configInterface( d->doc );
00201     if (config)
00202         config->configDialog();
00203 #endif
00204 }
00205 
00206 void KexiEditor::jump(int character)
00207 {
00208 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00209     const int numRows = d->view->paragraphs();
00210     int row = 0, col = 0;
00211     for (int ch = 0; row < numRows; row++) {
00212         const int rowLen = d->view->paragraphLength(row)+1;
00213         if ((ch + rowLen) > character) {
00214             col = character-ch;
00215             break;
00216         }
00217         ch += rowLen;
00218     }
00219     d->view->setCursorPosition(row, col);
00220 #else
00221     if (!d->doc)
00222         return;
00223     KTextEditor::EditInterface *ei = KTextEditor::editInterface(d->doc);
00224     const int numRows = ei->numLines();
00225     int row = 0, col = 0;
00226     for (int ch = 0; row < numRows; row++) {
00227         const int rowLen = ei->lineLength(row)+1;
00228         if ((ch + rowLen) > character) {
00229             col = character-ch;
00230             break;
00231         }
00232         ch += rowLen;
00233     }
00234     KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface(d->view);
00235     ci->setCursorPositionReal(row, col);
00236 #endif
00237 }
00238 
00239 void KexiEditor::setCursorPosition(int line, int col)
00240 {
00241 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00242     d->view->setCursorPosition(line, col);
00243 #else
00244     KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface( d->view );
00245     ci->setCursorPosition(line, col);
00246 #endif
00247 }
00248 
00249 void KexiEditor::clearUndoRedo()
00250 {
00251 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00252     //TODO how to remove undo/redo from a KTextEdit?
00253 #else
00254     KTextEditor::UndoInterface* u = KTextEditor::undoInterface( d->doc );
00255     u->clearUndo();
00256     u->clearRedo();
00257 #endif
00258 }
00259 
00260 #include "kexieditor.moc"
00261 
KDE Home | KDE Accessibility Home | Description of Access Keys