summaryrefslogtreecommitdiffstats
path: root/parts/replace/replace_part.cpp
blob: 779ba401238e537fb595e12d9db8ef593c986cf5 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003 by Jens Dagerbo                                    *
 *   jens.dagerbo@swipnet.se                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "replace_part.h"

#include <tqwhatsthis.h>

#include <kaction.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdevgenericfactory.h>
#include <kdevplugininfo.h>
#include <kdebug.h>
#include <kstringhandler.h>
#include <kdevcore.h>
#include <kdevmainwindow.h>

#include "replace_widget.h"

static const KDevPluginInfo data("kdevreplace");

typedef KDevGenericFactory<ReplacePart> ReplaceFactory;
K_EXPORT_COMPONENT_FACTORY(libkdevreplace, ReplaceFactory(data))

ReplacePart::ReplacePart(TQObject *parent, const char *name, const TQStringList& )
        : KDevPlugin( &data, parent, name ? name : "ReplacePart" )
{
    setInstance(ReplaceFactory::instance());
    setXMLFile("kdevpart_replace.rc");

    m_widget = new ReplaceWidget(this);
    m_widget->setIcon( SmallIcon("filefind") );
    m_widget->setCaption(i18n("Replace"));

    TQWhatsThis::add
        (m_widget, i18n("<b>Replace</b><p>"
                        "This window shows a preview of a string replace "
                        "operation. Uncheck a line to exclude that replacement. "
                        "Uncheck a file to exclude the whole file from the "
                        "operation. "
                        "Clicking on a line in the list will automatically "
                        "open the corresponding source file and set the "
                        "cursor to the line with the match." ));

    mainWindow()->embedOutputView( m_widget, i18n("Replace"), i18n("Project wide string replacement") );
    mainWindow()->setViewAvailable( m_widget, false );

    action = new KAction(i18n("Find-Select-Replace..."), 0,
                                   CTRL+SHIFT+Key_R, this, TQT_SLOT(slotReplace()), actionCollection(), "edit_replace_across");
    action->setToolTip( i18n("Project wide string replacement") );
    action->setWhatsThis( i18n("<b>Find-Select-Replace</b><p>"
                               "Opens the project wide string replacement dialog. There you "
                               "can enter a string or a regular expression which is then "
                               "searched for within all files in the locations "
                               "you specify. Matches will be displayed in the <b>Replace</b> window, you "
                               "can replace them with the specified string, exclude them from replace operation or cancel the whole replace.") );

	connect( core(), TQT_SIGNAL(contextMenu(TQPopupMenu *, const Context *)), this, TQT_SLOT(contextMenu(TQPopupMenu *, const Context *)) );
    connect( core(), TQT_SIGNAL(projectOpened()), this, TQT_SLOT(enableAction()));
    connect( core(), TQT_SIGNAL(projectClosed()), this, TQT_SLOT(disableAction()));
}

void ReplacePart::enableAction()
{
    action->setEnabled(true);
}

void ReplacePart::disableAction()
{
    action->setEnabled(false);
}

ReplacePart::~ReplacePart()
{
    if ( m_widget )
        mainWindow()->removeView( m_widget );
    delete m_widget;
}

void ReplacePart::slotReplace()
{
    m_widget->showDialog();
}

void ReplacePart::contextMenu(TQPopupMenu *popup, const Context *context)
{
    if (!context->hasType( Context::EditorContext ))
        return;

    const EditorContext *econtext = static_cast<const EditorContext*>(context);
    TQString ident = econtext->currentWord();
    if (!ident.isEmpty()) {
        m_popupstr = ident;
        TQString squeezed = KStringHandler::csqueeze(ident, 30);
        int id = popup->insertItem( i18n("Replace Project Wide: %1").tqarg(squeezed),
                           this, TQT_SLOT(slotReplace()) );
        popup->setWhatsThis(id, i18n("<b>Replace Project Wide</b><p>Opens the find in files dialog "
                               "and sets the pattern to the text under the cursor."));
        popup->insertSeparator();
    }
}

#include "replace_part.moc"