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