TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kate_kdatatool.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2002 Joseph Wenninger <jowenn@jowenn.at> and Daniel Naber <daniel.naber@t-online.de>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License version 2 as published by the Free Software Foundation.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; see the file COPYING.LIB. If not, write to
  12. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  13. Boston, MA 02110-1301, USA.
  14. */
  15. //BEGIN includes
  16. #include "kate_kdatatool.h"
  17. #include "kate_kdatatool.moc"
  18. #include <kgenericfactory.h>
  19. #include <tdeaction.h>
  20. #include <tdetexteditor/view.h>
  21. #include <kdebug.h>
  22. #include <kdatatool.h>
  23. #include <tdetexteditor/document.h>
  24. #include <tdetexteditor/selectioninterface.h>
  25. #include <tdepopupmenu.h>
  26. #include <tdetexteditor/viewcursorinterface.h>
  27. #include <tdetexteditor/editinterface.h>
  28. #include <tdemessagebox.h>
  29. //END includes
  30. K_EXPORT_COMPONENT_FACTORY( tdetexteditor_kdatatool, KGenericFactory<KTextEditor::KDataToolPlugin>( "tdetexteditor_kdatatool" ) )
  31. namespace KTextEditor {
  32. KDataToolPlugin::KDataToolPlugin( TQObject *parent, const char* name, const TQStringList& )
  33. : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name )
  34. {
  35. }
  36. KDataToolPlugin::~KDataToolPlugin ()
  37. {
  38. }
  39. void KDataToolPlugin::addView(KTextEditor::View *view)
  40. {
  41. KDataToolPluginView *nview = new KDataToolPluginView (view);
  42. nview->setView (view);
  43. m_views.append (nview);
  44. }
  45. void KDataToolPlugin::removeView(KTextEditor::View *view)
  46. {
  47. for (uint z=0; z < m_views.count(); z++)
  48. {
  49. if (m_views.at(z)->parentClient() == view)
  50. {
  51. KDataToolPluginView *nview = m_views.at(z);
  52. m_views.remove (nview);
  53. delete nview;
  54. }
  55. }
  56. }
  57. KDataToolPluginView::KDataToolPluginView( KTextEditor::View *view )
  58. :m_menu(0),m_notAvailable(0)
  59. {
  60. view->insertChildClient (this);
  61. setInstance( KGenericFactory<KDataToolPlugin>::instance() );
  62. m_menu = new TDEActionMenu(i18n("Data Tools"), actionCollection(), "popup_dataTool");
  63. connect(m_menu->popupMenu(), TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(aboutToShow()));
  64. setXMLFile("tdetexteditor_kdatatoolui.rc");
  65. m_view = view;
  66. }
  67. KDataToolPluginView::~KDataToolPluginView()
  68. {
  69. m_view->removeChildClient (this);
  70. delete m_menu;
  71. }
  72. void KDataToolPluginView::aboutToShow()
  73. {
  74. kdDebug()<<"KTextEditor::KDataToolPluginView::aboutToShow"<<endl;
  75. TQString word;
  76. m_singleWord = false;
  77. m_wordUnderCursor = TQString::null;
  78. // unplug old actions, if any:
  79. TDEAction *ac;
  80. for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
  81. m_menu->remove(ac);
  82. }
  83. if (m_notAvailable) {
  84. m_menu->remove(m_notAvailable);
  85. delete m_notAvailable;
  86. m_notAvailable=0;
  87. }
  88. if ( selectionInterface(m_view->document())->hasSelection() )
  89. {
  90. word = selectionInterface(m_view->document())->selection();
  91. if ( word.find(' ') == -1 && word.find('\t') == -1 && word.find('\n') == -1 )
  92. m_singleWord = true;
  93. else
  94. m_singleWord = false;
  95. } else {
  96. // No selection -> use word under cursor
  97. KTextEditor::EditInterface *ei;
  98. KTextEditor::ViewCursorInterface *ci;
  99. KTextEditor::View *v = (KTextEditor::View*)m_view;
  100. ei = KTextEditor::editInterface(v->document());
  101. ci = KTextEditor::viewCursorInterface(v);
  102. uint line, col;
  103. ci->cursorPositionReal(&line, &col);
  104. TQString tmp_line = ei->textLine(line);
  105. m_wordUnderCursor = "";
  106. // find begin of word:
  107. m_singleWord_start = 0;
  108. for(int i = col; i >= 0; i--) {
  109. TQChar ch = tmp_line.at(i);
  110. if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
  111. {
  112. m_singleWord_start = i+1;
  113. break;
  114. }
  115. m_wordUnderCursor = ch + m_wordUnderCursor;
  116. }
  117. // find end of word:
  118. m_singleWord_end = tmp_line.length();
  119. for(uint i = col+1; i < tmp_line.length(); i++) {
  120. TQChar ch = tmp_line.at(i);
  121. if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
  122. {
  123. m_singleWord_end = i;
  124. break;
  125. }
  126. m_wordUnderCursor += ch;
  127. }
  128. if( ! m_wordUnderCursor.isEmpty() )
  129. {
  130. m_singleWord = true;
  131. m_singleWord_line = line;
  132. } else {
  133. m_notAvailable = new TDEAction(i18n("(not available)"), TQString::null, 0, this,
  134. TQT_SLOT(slotNotAvailable()), actionCollection(),"dt_n_av");
  135. m_menu->insert(m_notAvailable);
  136. return;
  137. }
  138. }
  139. TDEInstance *inst=instance();
  140. TQValueList<KDataToolInfo> tools;
  141. tools += KDataToolInfo::query( TQSTRING_OBJECT_NAME_STRING, "text/plain", inst );
  142. if( m_singleWord )
  143. tools += KDataToolInfo::query( TQSTRING_OBJECT_NAME_STRING, "application/x-singleword", inst );
  144. m_actionList = KDataToolAction::dataToolActionList( tools, this,
  145. TQT_SLOT( slotToolActivated( const KDataToolInfo &, const TQString & ) ) );
  146. for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
  147. m_menu->insert(ac);
  148. }
  149. if( m_actionList.isEmpty() ) {
  150. m_notAvailable = new TDEAction(i18n("(not available)"), TQString::null, 0, this,
  151. TQT_SLOT(slotNotAvailable()), actionCollection(),"dt_n_av");
  152. m_menu->insert(m_notAvailable);
  153. }
  154. }
  155. void KDataToolPluginView::slotNotAvailable()
  156. {
  157. KMessageBox::sorry(0, i18n("Data tools are only available when text is selected, "
  158. "or when the right mouse button is clicked over a word. If no data tools are offered "
  159. "even when text is selected, you need to install them. Some data tools are part "
  160. "of the KOffice package."));
  161. }
  162. void KDataToolPluginView::slotToolActivated( const KDataToolInfo &info, const TQString &command )
  163. {
  164. KDataTool* tool = info.createTool( );
  165. if ( !tool )
  166. {
  167. kdWarning() << "Could not create Tool !" << endl;
  168. return;
  169. }
  170. TQString text;
  171. if ( selectionInterface(m_view->document())->hasSelection() )
  172. text = selectionInterface(m_view->document())->selection();
  173. else
  174. text = m_wordUnderCursor;
  175. TQString mimetype = "text/plain";
  176. TQString datatype = TQSTRING_OBJECT_NAME_STRING;
  177. // If unsupported (and if we have a single word indeed), try application/x-singleword
  178. if ( !info.mimeTypes().contains( mimetype ) && m_singleWord )
  179. mimetype = "application/x-singleword";
  180. kdDebug() << "Running tool with datatype=" << datatype << " mimetype=" << mimetype << endl;
  181. TQString origText = text;
  182. if ( tool->run( command, &text, datatype, mimetype) )
  183. {
  184. kdDebug() << "Tool ran. Text is now " << text << endl;
  185. if ( origText != text )
  186. {
  187. uint line, col;
  188. viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
  189. if ( ! selectionInterface(m_view->document())->hasSelection() )
  190. {
  191. KTextEditor::SelectionInterface *si;
  192. si = KTextEditor::selectionInterface(m_view->document());
  193. si->setSelection(m_singleWord_line, m_singleWord_start, m_singleWord_line, m_singleWord_end);
  194. }
  195. // replace selection with 'text'
  196. selectionInterface(m_view->document())->removeSelectedText();
  197. viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
  198. editInterface(m_view->document())->insertText(line, col, text);
  199. // fixme: place cursor at the end:
  200. /* No idea yet (Joseph Wenninger)
  201. for ( uint i = 0; i < text.length(); i++ ) {
  202. viewCursorInterface(m_view)->cursorRight();
  203. } */
  204. }
  205. }
  206. delete tool;
  207. }
  208. }