File: replace.cpp

package info (click to toggle)
sim 0.9.5~svn20080806-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 18,108 kB
  • ctags: 11,570
  • sloc: cpp: 119,605; sh: 9,986; ansic: 3,312; perl: 2,752; lex: 1,533; makefile: 839; xml: 206; python: 56
file content (145 lines) | stat: -rw-r--r-- 4,356 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/***************************************************************************
                          replace.cpp  -  description
                             -------------------
    begin                : Sun Mar 17 2002
    copyright            : (C) 2002 by Vladimir Shutoff
    email                : vovan@shutoff.ru
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.h"
#include "replacecfg.h"
#include "textshow.h"
#include "html.h"
#include "misc.h"

#include <qapplication.h>

using namespace std;
using namespace SIM;

Plugin *createReplacePlugin(unsigned base, bool, Buffer *cfg)
{
    Plugin *plugin = new ReplacePlugin(base, cfg);
    return plugin;
}

static PluginInfo info =
    {
        I18N_NOOP("Replace text"),
        I18N_NOOP("Plugin provides text replacing in message edit window"),
        VERSION,
        createReplacePlugin,
        PLUGIN_DEFAULT
    };

EXPORT_PROC PluginInfo* GetPluginInfo()
{
    return &info;
}

static DataDef replaceData[] =
    {
        { "Keys", DATA_ULONG, 1, 0 },
        { "Key", DATA_UTFLIST, 1, 0 },
        { "Value", DATA_UTFLIST, 1, 0 },
        { NULL, DATA_UNKNOWN, 0, 0 }
    };

ReplacePlugin::ReplacePlugin(unsigned base, Buffer *cfg)
        : Plugin(base)
{
    load_data(replaceData, &data, cfg);
    qApp->installEventFilter(this);
}

ReplacePlugin::~ReplacePlugin()
{
    free_data(replaceData, &data);
}

QCString ReplacePlugin::getConfig()
{
    return save_data(replaceData, &data);
}

QWidget *ReplacePlugin::createConfigWindow(QWidget *parent)
{
    return new ReplaceCfg(parent, this);
}

class _UnquoteParser : public HTMLParser
{
public:
    _UnquoteParser(const QString &text);
    QString m_text;
protected:
    virtual void text(const QString &text);
    virtual void tag_start(const QString &tag, const list<QString> &options);
    virtual void tag_end(const QString &tag);
};

_UnquoteParser::_UnquoteParser(const QString &text)
{
    parse(text);
}

void _UnquoteParser::text(const QString &text)
{
    m_text += text;
}

void _UnquoteParser::tag_start(const QString &tag, const list<QString>&)
{
    if (tag == "img")
        m_text += ' ';
    if (tag == "br")
        m_text += '\n';
}

void _UnquoteParser::tag_end(const QString&)
{
}

bool ReplacePlugin::eventFilter(QObject *o, QEvent *e)
{
    if ((e->type() == QEvent::KeyPress) && o->inherits("MsgTextEdit")){
        QKeyEvent *ke = (QKeyEvent*)e;
        if ((ke->key() == Key_Enter) || (ke->key() == Key_Return) || (ke->key() == Key_Space)){
            TextEdit *edit = (TextEdit*)o;
            int paraFrom, paraTo, indexFrom, indexTo;
            edit->getSelection(&paraFrom, &indexFrom, &paraTo, &indexTo);
            if ((paraFrom == paraTo) && (indexFrom == indexTo)){
                int parag, index;
                edit->getCursorPosition(&parag, &index);
                _UnquoteParser p(edit->text(parag));
                QString text = p.m_text.left(index);
                for (unsigned i = 1; i <= getKeys(); i++){
                    QString key = getKey(i);
                    if (key.length() > text.length())
                        continue;
                    if (key != text.mid(text.length() - key.length()))
                        continue;
                    if ((key.length() < text.length()) && !text[(int)(text.length() - key.length() - 1)].isSpace())
                        continue;
                    edit->setSelection(parag, index - key.length(), parag, index, 0);
                    edit->insert(getValue(i), false, false);
                    break;
                }
            }
        }
    }
    return QObject::eventFilter(o, e);
}

#ifndef NO_MOC_INCLUDES
#include "replace.moc"
#endif