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 <kpluginfactory.h>
00023 #include <kpluginloader.h>
00024 #include <kaction.h>
00025 #include <kactioncollection.h>
00026 #include <ktexteditor/view.h>
00027 #include <kdebug.h>
00028 #include <kdatatool.h>
00029 #include <ktexteditor/document.h>
00030 #include <kmenu.h>
00031 #include <kmessagebox.h>
00032 #include <kaboutdata.h>
00033 #include <kactionmenu.h>
00034 #include <klocale.h>
00035
00036
00037 K_PLUGIN_FACTORY( KDataToolPluginFactory, registerPlugin<KTextEditor::KDataToolPlugin>(); )
00038 K_EXPORT_PLUGIN( KDataToolPluginFactory( KAboutData("ktexteditor_kdatatool", "ktexteditor_plugins", ki18n("DataTool"), "0.1", ki18n("Data tool"), KAboutData::License_LGPL_V2) ) )
00039
00040 namespace KTextEditor {
00041
00042 KDataToolPlugin::KDataToolPlugin( QObject *parent, const QVariantList& )
00043 : KTextEditor::Plugin ( parent )
00044 {
00045 }
00046
00047
00048 KDataToolPlugin::~KDataToolPlugin ()
00049 {
00050 }
00051
00052 void KDataToolPlugin::addView(KTextEditor::View *view)
00053 {
00054 KDataToolPluginView *nview = new KDataToolPluginView (view);
00055 nview->setView (view);
00056 m_views.append (nview);
00057 }
00058
00059 void KDataToolPlugin::removeView(KTextEditor::View *view)
00060 {
00061 for (int z=0; z < m_views.count(); z++)
00062 {
00063 if (m_views.at(z)->parentClient() == view)
00064 {
00065 KDataToolPluginView *nview = m_views.at(z);
00066 m_views.removeAll (nview);
00067 delete nview;
00068 }
00069 }
00070 }
00071
00072
00073 KDataToolPluginView::KDataToolPluginView( KTextEditor::View *view )
00074 :m_menu(0),m_notAvailable(0)
00075 {
00076 setComponentData( KDataToolPluginFactory::componentData() );
00077
00078 m_menu = new KActionMenu(i18n("Data Tools"), this);
00079 actionCollection()->addAction("popup_dataTool", m_menu);
00080 connect(m_menu->menu(), SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
00081 setXMLFile("ktexteditor_kdatatoolui.rc");
00082
00083 m_view = view;
00084 }
00085
00086 KDataToolPluginView::~KDataToolPluginView()
00087 {
00088 m_view->removeChildClient (this);
00089 delete m_menu;
00090 }
00091
00092 void KDataToolPluginView::aboutToShow()
00093 {
00094 kDebug( 13040 )<<"KTextEditor::KDataToolPluginView::aboutToShow";
00095 QString word;
00096 m_singleWord = false;
00097 m_wordUnderCursor.clear();
00098
00099
00100 foreach (QAction *ac, m_actionList) {
00101 m_menu->removeAction(ac);
00102 }
00103 if (m_notAvailable) {
00104 m_menu->removeAction(m_notAvailable);
00105 delete m_notAvailable;
00106 m_notAvailable=0;
00107 }
00108 if ( m_view->selection() )
00109 {
00110 word = m_view->selectionText();
00111 if ( word.indexOf(' ') == -1 && word.indexOf('\t') == -1 && word.indexOf('\n') == -1 )
00112 m_singleWord = true;
00113 else
00114 m_singleWord = false;
00115 } else {
00116
00117 KTextEditor::View *v = (KTextEditor::View*)m_view;
00118 int line, col;
00119 line = v->cursorPosition().line();
00120 col = v->cursorPosition().column();
00121 QString tmp_line = v->document()->line(line);
00122 m_wordUnderCursor = "";
00123
00124 m_singleWord_start = 0;
00125 for(int i = col; i >= 0; i--) {
00126 QChar ch = tmp_line.at(i);
00127 if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
00128 {
00129 m_singleWord_start = i+1;
00130 break;
00131 }
00132 m_wordUnderCursor = ch + m_wordUnderCursor;
00133 }
00134
00135 m_singleWord_end = tmp_line.length();
00136 for(int i = col+1; i < tmp_line.length(); i++) {
00137 QChar ch = tmp_line.at(i);
00138 if( ! (ch.isLetter() || ch == '-' || ch == '\'') )
00139 {
00140 m_singleWord_end = i;
00141 break;
00142 }
00143 m_wordUnderCursor += ch;
00144 }
00145 if( ! m_wordUnderCursor.isEmpty() )
00146 {
00147 m_singleWord = true;
00148 m_singleWord_line = line;
00149 } else {
00150 m_notAvailable = new KAction(i18n("(not available)"), this );
00151 actionCollection()->addAction("dt_n_av", m_notAvailable);
00152 connect( m_notAvailable, SIGNAL( triggered( bool ) ), this, SLOT(slotNotAvailable()) );
00153 m_menu->addAction(m_notAvailable);
00154 return;
00155 }
00156 }
00157
00158 KComponentData inst=componentData();
00159
00160 QList<KDataToolInfo> tools;
00161 tools += KDataToolInfo::query( "QString", "text/plain", inst );
00162 if( m_singleWord )
00163 tools += KDataToolInfo::query( "QString", "application/x-singleword", inst );
00164
00165 m_actionList = KDataToolAction::dataToolActionList( tools, this,
00166 SLOT( slotToolActivated( const KDataToolInfo &, const QString & ) ),
00167 actionCollection());
00168
00169 foreach (QAction* ac, m_actionList)
00170 m_menu->addAction(ac);
00171
00172 if( m_actionList.isEmpty() ) {
00173 m_notAvailable = new KAction(i18n("(not available)"), this);
00174 actionCollection()->addAction("dt_n_av", m_notAvailable);
00175 connect( m_notAvailable, SIGNAL( triggered( bool ) ), this, SLOT(slotNotAvailable()) );
00176 m_menu->addAction(m_notAvailable);
00177 }
00178 }
00179
00180 void KDataToolPluginView::slotNotAvailable()
00181 {
00182 KMessageBox::sorry(0, i18n("Data tools are only available when text is selected, "
00183 "or when the right mouse button is clicked over a word. If no data tools are offered "
00184 "even when text is selected, you need to install them. Some data tools are part "
00185 "of the KOffice package."));
00186 }
00187
00188 void KDataToolPluginView::slotToolActivated( const KDataToolInfo &info, const QString &command )
00189 {
00190
00191 KDataTool* tool = info.createTool( );
00192 if ( !tool )
00193 {
00194 kWarning() << "Could not create Tool !";
00195 return;
00196 }
00197
00198 QString text;
00199 if ( m_view->selection() )
00200 text = m_view->selectionText();
00201 else
00202 text = m_wordUnderCursor;
00203
00204 QString mimetype = "text/plain";
00205 QString datatype = "QString";
00206
00207
00208 if ( !info.mimeTypes().contains( mimetype ) && m_singleWord )
00209 mimetype = "application/x-singleword";
00210
00211 kDebug( 13040 ) << "Running tool with datatype=" << datatype << " mimetype=" << mimetype;
00212
00213 QString origText = text;
00214
00215 if ( tool->run( command, &text, datatype, mimetype) )
00216 {
00217 kDebug( 13040 ) << "Tool ran. Text is now " << text;
00218 if ( origText != text )
00219 {
00220 int line, col;
00221 line = m_view->cursorPosition().line();
00222 col = m_view->cursorPosition().column();
00223 if ( !m_view->selection() )
00224 {
00225 m_view->setSelection(KTextEditor::Range(m_singleWord_line, m_singleWord_start, m_singleWord_line, m_singleWord_end));
00226 }
00227
00228
00229 m_view->removeSelectionText();
00230 m_view->document()->insertText(m_view->cursorPosition(), text);
00231
00232
00233
00234
00235
00236 }
00237 }
00238
00239 delete tool;
00240 }
00241
00242
00243 }