| # kate Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# kate_kdatatool.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 2002 Joseph Wenninger <jowenn@jowenn.at> and Daniel Naber <daniel.naber@t-online.de>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019 //BEGIN includes
    00020 #include "kate_kdatatool.h"
    00021 #include "kate_kdatatool.moc"
    00022 #include <kgenericfactory.h>
    00023 #include <kaction.h>
    00024 #include <ktexteditor/view.h>
    00025 #include <kdebug.h>
    00026 #include <kdatatool.h>
    00027 #include <ktexteditor/document.h>
    00028 #include <ktexteditor/selectioninterface.h>
    00029 #include <kpopupmenu.h>
    00030 #include <ktexteditor/viewcursorinterface.h>
    00031 #include <ktexteditor/editinterface.h>
    00032 #include <kmessagebox.h>
    00033 //END includes
    00034
    00035
    00036 K_EXPORT_COMPONENT_FACTORY( ktexteditor_kdatatool, KGenericFactory<KTextEditor::KDataToolPlugin>( "ktexteditor_kdatatool" ) )
    00037
    00038 namespace KTextEditor {
    00039
    00040 KDataToolPlugin::KDataToolPlugin( QObject *parent, const char* name, const QStringList& )
    00041     : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name )
    00042 {
    00043 }
    00044
    00045
    00046 KDataToolPlugin::~KDataToolPlugin ()
    00047 {
    00048 }
    00049
    00050 void KDataToolPlugin::addView(KTextEditor::View *view)
    00051 {
    00052     KDataToolPluginView *nview = new KDataToolPluginView (view);
    00053     nview->setView (view);
    00054     m_views.append (nview);
    00055 }
    00056
    00057 void KDataToolPlugin::removeView(KTextEditor::View *view)
    00058 {
    00059     for (uint z=0; z < m_views.count(); z++)
    00060         {
    00061         if (m_views.at(z)->parentClient() == view)
    00062         {
    00063             KDataToolPluginView *nview = m_views.at(z);
    00064             m_views.remove (nview);
    00065             delete nview;
    00066         }
    00067     }
    00068 }
    00069
    00070
    00071 KDataToolPluginView::KDataToolPluginView( KTextEditor::View *view )
    00072     :m_menu(0),m_notAvailable(0)
    00073 {
    00074
    00075     view->insertChildClient (this);
    00076     setInstance( KGenericFactory<KDataToolPlugin>::instance() );
    00077
    00078     m_menu = new KActionMenu(i18n("Data Tools"), actionCollection(), "popup_dataTool");
    00079     connect(m_menu->popupMenu(), SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
    00080     setXMLFile("ktexteditor_kdatatoolui.rc");
    00081
    00082     m_view = view;
    00083 }
    00084
    00085 KDataToolPluginView::~KDataToolPluginView()
    00086 {
    00087         m_view->removeChildClient (this);
    00088     delete m_menu;
    00089 }
    00090
    00091 void KDataToolPluginView::aboutToShow()
    00092 {
    00093     kdDebug()<<"KTextEditor::KDataToolPluginView::aboutToShow"<<endl;
    00094     QString word;
    00095     m_singleWord = false;
    00096     m_wordUnderCursor = QString::null;
    00097
    00098     // unplug old actions, if any:
    00099     KAction *ac;
    00100     for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
    00101         m_menu->remove(ac);
    00102     }
    00103     if (m_notAvailable) {
    00104         m_menu->remove(m_notAvailable);
    00105         delete m_notAvailable;
    00106         m_notAvailable=0;
    00107     }
    00108     if ( selectionInterface(m_view->document())->hasSelection() )
    00109     {
    00110         word = selectionInterface(m_view->document())->selection();
    00111         if ( word.find(' ') == -1 && word.find('\t') == -1 && word.find('\n') == -1 )
    00112             m_singleWord = true;
    00113         else
    00114             m_singleWord = false;
    00115     } else {
    00116         // No selection -> use word under cursor
    00117         KTextEditor::EditInterface *ei;
    00118         KTextEditor::ViewCursorInterface *ci;
    00119         KTextEditor::View *v = (KTextEditor::View*)m_view;
    00120         ei = KTextEditor::editInterface(v->document());
    00121         ci = KTextEditor::viewCursorInterface(v);
    00122         uint line, col;
    00123         ci->cursorPositionReal(&line, &col);
    00124         QString tmp_line = ei->textLine(line);
    00125         m_wordUnderCursor = "";
    00126         // find begin of word:
    00127         m_singleWord_start = 0;
    00128         for(int i = col; i >= 0; i--) {
    00129             QChar ch = tmp_line.at(i);
    00130             if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
    00131             {
    00132                 m_singleWord_start = i+1;
    00133                 break;
    00134             }
    00135             m_wordUnderCursor = ch + m_wordUnderCursor;
    00136         }
    00137         // find end of word:
    00138         m_singleWord_end = tmp_line.length();
    00139         for(uint i = col+1; i < tmp_line.length(); i++) {
    00140             QChar ch = tmp_line.at(i);
    00141             if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
    00142             {
    00143                 m_singleWord_end = i;
    00144                 break;
    00145             }
    00146             m_wordUnderCursor += ch;
    00147         }
    00148         if( ! m_wordUnderCursor.isEmpty() )
    00149         {
    00150             m_singleWord = true;
    00151             m_singleWord_line = line;
    00152         } else {
    00153             m_notAvailable = new KAction(i18n("(not available)"), QString::null, 0, this,
    00154                     SLOT(slotNotAvailable()), actionCollection(),"dt_n_av");
    00155             m_menu->insert(m_notAvailable);
    00156             return;
    00157         }
    00158     }
    00159
    00160     KInstance *inst=instance();
    00161
    00162     QValueList<KDataToolInfo> tools;
    00163     tools += KDataToolInfo::query( "QString", "text/plain", inst );
    00164     if( m_singleWord )
    00165         tools += KDataToolInfo::query( "QString", "application/x-singleword", inst );
    00166
    00167     m_actionList = KDataToolAction::dataToolActionList( tools, this,
    00168         SLOT( slotToolActivated( const KDataToolInfo &, const QString & ) ) );
    00169
    00170     for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
    00171         m_menu->insert(ac);
    00172     }
    00173
    00174     if( m_actionList.isEmpty() ) {
    00175         m_notAvailable = new KAction(i18n("(not available)"), QString::null, 0, this,
    00176             SLOT(slotNotAvailable()), actionCollection(),"dt_n_av");
    00177         m_menu->insert(m_notAvailable);
    00178     }
    00179 }
    00180
    00181 void KDataToolPluginView::slotNotAvailable()
    00182 {
    00183     KMessageBox::sorry(0, i18n("Data tools are only available when text is selected, "
    00184         "or when the right mouse button is clicked over a word. If no data tools are offered "
    00185         "even when text is selected, you need to install them. Some data tools are part "
    00186         "of the KOffice package."));
    00187 }
    00188
    00189 void KDataToolPluginView::slotToolActivated( const KDataToolInfo &info, const QString &command )
    00190 {
    00191
    00192     KDataTool* tool = info.createTool( );
    00193     if ( !tool )
    00194     {
    00195         kdWarning() << "Could not create Tool !" << endl;
    00196         return;
    00197     }
    00198
    00199     QString text;
    00200     if ( selectionInterface(m_view->document())->hasSelection() )
    00201         text = selectionInterface(m_view->document())->selection();
    00202     else
    00203         text = m_wordUnderCursor;
    00204
    00205     QString mimetype = "text/plain";
    00206     QString datatype = "QString";
    00207
    00208     // If unsupported (and if we have a single word indeed), try application/x-singleword
    00209     if ( !info.mimeTypes().contains( mimetype ) && m_singleWord )
    00210         mimetype = "application/x-singleword";
    00211
    00212     kdDebug() << "Running tool with datatype=" << datatype << " mimetype=" << mimetype << endl;
    00213
    00214     QString origText = text;
    00215
    00216     if ( tool->run( command, &text, datatype, mimetype) )
    00217     {
    00218         kdDebug() << "Tool ran. Text is now " << text << endl;
    00219         if ( origText != text )
    00220         {
    00221             uint line, col;
    00222             viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
    00223             if ( ! selectionInterface(m_view->document())->hasSelection() )
    00224             {
    00225                 KTextEditor::SelectionInterface *si;
    00226                 si = KTextEditor::selectionInterface(m_view->document());
    00227                 si->setSelection(m_singleWord_line, m_singleWord_start, m_singleWord_line, m_singleWord_end);
    00228             }
    00229
    00230             // replace selection with 'text'
    00231             selectionInterface(m_view->document())->removeSelectedText();
    00232             viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
    00233             editInterface(m_view->document())->insertText(line, col, text);
    00234              // fixme: place cursor at the end:
    00235              /* No idea yet (Joseph Wenninger)
    00236              for ( uint i = 0; i < text.length(); i++ ) {
    00237                 viewCursorInterface(m_view)->cursorRight();
    00238              } */
    00239         }
    00240     }
    00241
    00242     delete tool;
    00243 }
    00244
    00245
    00246 }

This file is part of the documentation for kate Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:36:29 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003