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 }
00132 
00133 void KexiEditor::updateActions(bool activated)
00134 {
00135     KexiViewBase::updateActions(activated);
00136 }
00137 
00138 bool KexiEditor::isAdvancedEditor()
00139 {
00140 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00141     return false;
00142 #else
00143     return true;
00144 #endif
00145 }
00146 
00147 QString KexiEditor::text()
00148 {
00149 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00150     return d->view->text();
00151 #else
00152     if (!d->doc)
00153         return QString::null;
00154     KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
00155     return eIface->text();
00156 #endif
00157 }
00158 
00159 void KexiEditor::setText(const QString &text)
00160 {
00161 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00162     const bool was_dirty = m_parentView ? m_parentView->dirty() : dirty();
00163     d->view->setText(text);
00164     setDirty(was_dirty);
00165 #else
00166     if (!d->doc)
00167         return;
00168     const bool was_dirty = dirty();
00169     KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
00170     eIface->setText(text);
00171     KTextEditor::UndoInterface *undoIface = KTextEditor::undoInterface(d->doc);
00172     undoIface->clearUndo();
00173     undoIface->clearRedo();
00174     setDirty(was_dirty);
00175 #endif
00176 }
00177 
00178 void KexiEditor::setHighlightMode(const QString& highlightmodename)
00179 {
00180 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00181 #else
00182     KTextEditor::HighlightingInterface *hl = KTextEditor::highlightingInterface( d->doc );
00183     for(uint i = 0; i < hl->hlModeCount(); i++) {
00184             //kdDebug() << "hlmode("<<i<<"): " << hl->hlModeName(i) << endl;
00185             if (hl->hlModeName(i).contains(highlightmodename, false))  {
00186                 hl->setHlMode(i);
00187                 return;
00188             }
00189     }
00190     hl->setHlMode(0); // 0=None, don't highlight anything.
00191 #endif
00192 }
00193 
00194 void KexiEditor::slotConfigureEditor()
00195 {
00196 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00197     //TODO show errormessage?
00198 #else
00199     KTextEditor::ConfigInterface *config = KTextEditor::configInterface( d->doc );
00200     if (config)
00201         config->configDialog();
00202 #endif
00203 }
00204 
00205 void KexiEditor::jump(int character)
00206 {
00207 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00208     const int numRows = d->view->paragraphs();
00209     int row = 0, col = 0;
00210     for (int ch = 0; row < numRows; row++) {
00211         const int rowLen = d->view->paragraphLength(row)+1;
00212         if ((ch + rowLen) > character) {
00213             col = character-ch;
00214             break;
00215         }
00216         ch += rowLen;
00217     }
00218     d->view->setCursorPosition(row, col);
00219 #else
00220     if (!d->doc)
00221         return;
00222     KTextEditor::EditInterface *ei = KTextEditor::editInterface(d->doc);
00223     const int numRows = ei->numLines();
00224     int row = 0, col = 0;
00225     for (int ch = 0; row < numRows; row++) {
00226         const int rowLen = ei->lineLength(row)+1;
00227         if ((ch + rowLen) > character) {
00228             col = character-ch;
00229             break;
00230         }
00231         ch += rowLen;
00232     }
00233     KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface(d->view);
00234     ci->setCursorPositionReal(row, col);
00235 #endif
00236 }
00237 
00238 void KexiEditor::setCursorPosition(int line, int col)
00239 {
00240 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00241     d->view->setCursorPosition(line, col);
00242 #else
00243     KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface( d->view );
00244     ci->setCursorPosition(line, col);
00245 #endif
00246 }
00247 
00248 void KexiEditor::clearUndoRedo()
00249 {
00250 #ifdef KTEXTEDIT_BASED_SQL_EDITOR
00251     //TODO how to remove undo/redo from a KTextEdit?
00252 #else
00253     KTextEditor::UndoInterface* u = KTextEditor::undoInterface( d->doc );
00254     u->clearUndo();
00255     u->clearRedo();
00256 #endif
00257 }
00258 
00259 #include "kexieditor.moc"
00260 
KDE Home | KDE Accessibility Home | Description of Access Keys