00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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
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
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
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
00231 selectionInterface(m_view->document())->removeSelectedText();
00232 viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
00233 editInterface(m_view->document())->insertText(line, col, text);
00234
00235
00236
00237
00238
00239 }
00240 }
00241
00242 delete tool;
00243 }
00244
00245
00246 }