20 #include "kate_kdatatool.h"
21 #include "kate_kdatatool.moc"
22 #include <kgenericfactory.h>
23 #include <tdeaction.h>
24 #include <tdetexteditor/view.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>
38 namespace KTextEditor {
40 KDataToolPlugin::KDataToolPlugin( TQObject *parent,
const char* name,
const TQStringList& )
41 : KTextEditor::Plugin ( (KTextEditor::Document*) parent,
name )
46 KDataToolPlugin::~KDataToolPlugin ()
50 void KDataToolPlugin::addView(KTextEditor::View *view)
52 KDataToolPluginView *nview =
new KDataToolPluginView (view);
53 nview->setView (view);
54 m_views.append (nview);
57 void KDataToolPlugin::removeView(KTextEditor::View *view)
59 for (uint z=0; z < m_views.count(); z++)
61 if (m_views.at(z)->parentClient() == view)
63 KDataToolPluginView *nview = m_views.at(z);
64 m_views.remove (nview);
71 KDataToolPluginView::KDataToolPluginView( KTextEditor::View *view )
72 :m_menu(0),m_notAvailable(0)
75 view->insertChildClient (
this);
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");
85 KDataToolPluginView::~KDataToolPluginView()
87 m_view->removeChildClient (
this);
91 void KDataToolPluginView::aboutToShow()
93 kdDebug()<<
"KTextEditor::KDataToolPluginView::aboutToShow"<<
endl;
96 m_wordUnderCursor = TQString::null;
100 for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
103 if (m_notAvailable) {
104 m_menu->remove(m_notAvailable);
105 delete m_notAvailable;
108 if ( selectionInterface(m_view->document())->hasSelection() )
110 word = selectionInterface(m_view->document())->selection();
111 if ( word.find(
' ') == -1 && word.find(
'\t') == -1 && word.find(
'\n') == -1 )
114 m_singleWord =
false;
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);
123 ci->cursorPositionReal(&line, &col);
124 TQString tmp_line = ei->textLine(line);
125 m_wordUnderCursor =
"";
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 ==
'\'') )
132 m_singleWord_start = i+1;
135 m_wordUnderCursor = ch + m_wordUnderCursor;
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 ==
'\'') )
143 m_singleWord_end = i;
146 m_wordUnderCursor += ch;
148 if( ! m_wordUnderCursor.isEmpty() )
151 m_singleWord_line = line;
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);
162 TQValueList<KDataToolInfo> tools;
163 tools += KDataToolInfo::query(
"TQString",
"text/plain", inst );
165 tools += KDataToolInfo::query(
"TQString",
"application/x-singleword", inst );
167 m_actionList = KDataToolAction::dataToolActionList( tools,
this,
168 TQ_SLOT( slotToolActivated(
const KDataToolInfo &,
const TQString & ) ) );
170 for ( ac = m_actionList.first(); ac; ac = m_actionList.next() ) {
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);
181 void KDataToolPluginView::slotNotAvailable()
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."));
189 void KDataToolPluginView::slotToolActivated(
const KDataToolInfo &info,
const TQString &command )
192 KDataTool* tool = info.createTool( );
200 if ( selectionInterface(m_view->document())->hasSelection() )
201 text = selectionInterface(m_view->document())->selection();
203 text = m_wordUnderCursor;
205 TQString mimetype =
"text/plain";
206 TQString datatype =
"TQString";
209 if ( !info.mimeTypes().contains( mimetype ) && m_singleWord )
210 mimetype =
"application/x-singleword";
212 kdDebug() <<
"Running tool with datatype=" << datatype <<
" mimetype=" << mimetype <<
endl;
214 TQString origText = text;
216 if ( tool->run( command, &text, datatype, mimetype) )
218 kdDebug() <<
"Tool ran. Text is now " << text <<
endl;
219 if ( origText != text )
222 viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
223 if ( ! selectionInterface(m_view->document())->hasSelection() )
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);
231 selectionInterface(m_view->document())->removeSelectedText();
232 viewCursorInterface(m_view)->cursorPositionReal(&line, &col);
233 editInterface(m_view->document())->insertText(line, col, text);
static void sorry(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)
TQString name(StdAccel id)