1
/***************************************************************************
2
* Copyright (C) 2002 by Bernd Gehrmann *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
***************************************************************************/
12
#include "filterpart.h"
17
#include <kparts/part.h>
18
#include <ktexteditor/editinterface.h>
19
#include <ktexteditor/selectioninterface.h>
20
#include <ktexteditor/viewcursorinterface.h>
23
#include "kdevpartcontroller.h"
24
#include "kdevplugininfo.h"
25
#include "shellfilterdlg.h"
26
#include "shellinsertdlg.h"
27
#include "kdevfilterIface.h"
29
static const KDevPluginInfo data("kdevfilter");
30
K_EXPORT_COMPONENT_FACTORY( libkdevfilter, FilterFactory( data ) )
32
FilterPart::FilterPart(QObject *parent, const char *name, const QStringList &)
33
: KDevPlugin(&data, parent, name ? name : "FilterPart")
35
setInstance(FilterFactory::instance());
36
setXMLFile("kdevfilter.rc");
40
action = new KAction( i18n("Execute Command..."), 0,
41
this, SLOT(slotShellInsert()),
42
actionCollection(), "tools_insertshell" );
43
action->setToolTip(i18n("Execute shell command"));
44
action->setWhatsThis(i18n("<b>Execute shell command</b><p>Executes a shell command and outputs its result into the current document."));
46
action = new KAction( i18n("Filter Selection Through Command..."), 0,
47
this, SLOT(slotShellFilter()),
48
actionCollection(), "tools_filtershell" );
49
action->setToolTip(i18n("Filter selection through a shell command"));
50
action->setWhatsThis(i18n("<b>Filter selection through shell command</b><p>Filters selection through a shell command and outputs its result into the current document."));
55
new KDevFilterIface( this );
56
// (void) dcopClient();
60
FilterPart::~FilterPart()
62
delete m_insertDialog;
63
delete m_filterDialog;
67
void FilterPart::slotShellInsert()
69
/// @todo Disable menu item if no active part
71
KParts::ReadWritePart *part
72
= dynamic_cast<KParts::ReadWritePart*>(partController()->activePart());
73
QWidget *view = partController()->activeWidget();
75
kdDebug(9029) << "no rw part" << endl;
79
KTextEditor::EditInterface *editiface
80
= dynamic_cast<KTextEditor::EditInterface*>(part);
82
kdDebug(9029) << "no edit" << endl;
86
KTextEditor::ViewCursorInterface *cursoriface
87
= dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
89
kdDebug(9029) << "no viewcursor" << endl;
95
m_insertDialog = new ShellInsertDialog();
96
m_insertDialog->setCaption(i18n("Execute Command"));
98
if (m_insertDialog->exec()) {
100
cursoriface->cursorPositionReal(&line, &col);
101
editiface->insertText(line, col, m_insertDialog->text());
106
void FilterPart::slotShellFilter()
108
/// @todo Disable menu item if no active part
110
KParts::ReadWritePart *part
111
= dynamic_cast<KParts::ReadWritePart*>(partController()->activePart());
112
QWidget *view = partController()->activeWidget();
113
if (!part || !view) {
114
kdDebug(9029) << "no rw part" << endl;
118
KTextEditor::EditInterface *editiface
119
= dynamic_cast<KTextEditor::EditInterface*>(part);
121
kdDebug(9029) << "no edit" << endl;
125
KTextEditor::ViewCursorInterface *cursoriface
126
= dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
128
kdDebug(9029) << "no viewcursor" << endl;
132
KTextEditor::SelectionInterface *selectioniface
133
= dynamic_cast<KTextEditor::SelectionInterface*>(part);
134
if (!selectioniface) {
135
kdDebug(9029) << "no selection" << endl;
141
m_filterDialog = new ShellFilterDialog();
142
m_filterDialog->setCaption(i18n("Filter Selection Through Command"));
145
kdDebug(9029) << "Old text: " << selectioniface->selection()<< endl;
147
m_filterDialog->setText(selectioniface->selection());
149
if (m_filterDialog->exec()) {
151
// OUCH: KTextEditor doesn't allow to find out
152
// where the selection is
153
selectioniface->removeSelectedText();
154
cursoriface->cursorPositionReal(&line, &col);
155
kdDebug(9029) << "New text: " << m_filterDialog->text() << endl;
156
editiface->insertText(line, col, m_filterDialog->text());
160
#include "filterpart.moc"