~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/filter/filterpart.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2002 by Bernd Gehrmann                                  *
 
3
 *   bernd@kdevelop.org                                                    *
 
4
 *                                                                         *
 
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.                                   *
 
9
 *                                                                         *
 
10
 ***************************************************************************/
 
11
 
 
12
#include "filterpart.h"
 
13
 
 
14
#include <kaction.h>
 
15
#include <kdebug.h>
 
16
#include <klocale.h>
 
17
#include <kparts/part.h>
 
18
#include <ktexteditor/editinterface.h>
 
19
#include <ktexteditor/selectioninterface.h>
 
20
#include <ktexteditor/viewcursorinterface.h>
 
21
 
 
22
#include "kdevcore.h"
 
23
#include "kdevpartcontroller.h"
 
24
#include "kdevplugininfo.h"
 
25
#include "shellfilterdlg.h"
 
26
#include "shellinsertdlg.h"
 
27
#include "kdevfilterIface.h"
 
28
 
 
29
static const KDevPluginInfo data("kdevfilter");
 
30
K_EXPORT_COMPONENT_FACTORY( libkdevfilter, FilterFactory( data ) )
 
31
 
 
32
FilterPart::FilterPart(QObject *parent, const char *name, const QStringList &)
 
33
    : KDevPlugin(&data, parent, name ? name : "FilterPart")
 
34
{
 
35
    setInstance(FilterFactory::instance());
 
36
    setXMLFile("kdevfilter.rc");
 
37
 
 
38
    KAction *action;
 
39
 
 
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."));
 
45
 
 
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."));
 
51
 
 
52
    m_insertDialog = 0;
 
53
    m_filterDialog = 0;
 
54
 
 
55
    new KDevFilterIface( this );
 
56
    // (void) dcopClient();
 
57
}
 
58
 
 
59
 
 
60
FilterPart::~FilterPart()
 
61
{
 
62
    delete m_insertDialog;
 
63
    delete m_filterDialog;
 
64
}
 
65
 
 
66
 
 
67
void FilterPart::slotShellInsert()
 
68
{
 
69
    /// @todo Disable menu item if no active part
 
70
 
 
71
    KParts::ReadWritePart *part
 
72
        = dynamic_cast<KParts::ReadWritePart*>(partController()->activePart());
 
73
    QWidget *view = partController()->activeWidget();
 
74
    if (!part || !view) {
 
75
        kdDebug(9029) << "no rw part" << endl;
 
76
        return;
 
77
    }
 
78
 
 
79
    KTextEditor::EditInterface *editiface
 
80
        = dynamic_cast<KTextEditor::EditInterface*>(part);
 
81
    if (!editiface) {
 
82
        kdDebug(9029) << "no edit" << endl;
 
83
        return;
 
84
    }
 
85
 
 
86
    KTextEditor::ViewCursorInterface *cursoriface
 
87
        = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
 
88
    if (!cursoriface) {
 
89
        kdDebug(9029) << "no viewcursor" << endl;
 
90
        return;
 
91
    }
 
92
 
 
93
    if (!m_insertDialog)
 
94
    {
 
95
        m_insertDialog = new ShellInsertDialog();
 
96
        m_insertDialog->setCaption(i18n("Execute Command"));
 
97
    }
 
98
    if (m_insertDialog->exec()) {
 
99
        uint line, col;
 
100
        cursoriface->cursorPositionReal(&line, &col);
 
101
        editiface->insertText(line, col, m_insertDialog->text());
 
102
    }
 
103
}
 
104
 
 
105
 
 
106
void FilterPart::slotShellFilter()
 
107
{
 
108
    /// @todo Disable menu item if no active part
 
109
 
 
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;
 
115
        return;
 
116
    }
 
117
 
 
118
    KTextEditor::EditInterface *editiface
 
119
        = dynamic_cast<KTextEditor::EditInterface*>(part);
 
120
    if (!editiface) {
 
121
        kdDebug(9029) << "no edit" << endl;
 
122
        return;
 
123
    }
 
124
 
 
125
    KTextEditor::ViewCursorInterface *cursoriface
 
126
        = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
 
127
    if (!cursoriface) {
 
128
        kdDebug(9029) << "no viewcursor" << endl;
 
129
        return;
 
130
    }
 
131
 
 
132
    KTextEditor::SelectionInterface *selectioniface
 
133
        = dynamic_cast<KTextEditor::SelectionInterface*>(part);
 
134
    if (!selectioniface) {
 
135
        kdDebug(9029) << "no selection" << endl;
 
136
        return;
 
137
    }
 
138
 
 
139
    if (!m_filterDialog)
 
140
    {
 
141
        m_filterDialog = new ShellFilterDialog();
 
142
        m_filterDialog->setCaption(i18n("Filter Selection Through Command"));
 
143
    }
 
144
 
 
145
    kdDebug(9029) << "Old text: " << selectioniface->selection()<< endl;
 
146
 
 
147
    m_filterDialog->setText(selectioniface->selection());
 
148
 
 
149
    if (m_filterDialog->exec()) {
 
150
        uint line, col;
 
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());
 
157
    }
 
158
}
 
159
 
 
160
#include "filterpart.moc"