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