• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

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

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.1
This website is maintained by Timothy Pearson.