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

« back to all changes in this revision

Viewing changes to parts/texttools/texttoolswidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

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 "texttoolswidget.h"
13
 
 
14
 
#include <qheader.h>
15
 
#include <qregexp.h>
16
 
#include <qtimer.h>
17
 
#include <kdebug.h>
18
 
#include <klocale.h>
19
 
#include <kparts/part.h>
20
 
#include <kpopupmenu.h>
21
 
#include <ktexteditor/viewcursorinterface.h>
22
 
#include <ktexteditor/selectioninterface.h>
23
 
#include <ktexteditor/editinterface.h>
24
 
 
25
 
#include "kdevmainwindow.h"
26
 
#include "kdevpartcontroller.h"
27
 
#include "texttoolspart.h"
28
 
 
29
 
 
30
 
class TextStructItem : public QListViewItem
31
 
{
32
 
public:
33
 
    TextStructItem(QListView *parent)
34
 
        : QListViewItem(parent)
35
 
    {}
36
 
    TextStructItem(QListViewItem *parent)
37
 
        : QListViewItem(parent)
38
 
    {
39
 
        QListViewItem *item = this;
40
 
        while (item->nextSibling())
41
 
            item = item->nextSibling();
42
 
        if (item != this)
43
 
            moveItem(item);
44
 
    }
45
 
    
46
 
    QString text(int) const
47
 
    {
48
 
        return extra.isNull()? tag : QString("%1: %2").arg(tag).arg(extra);
49
 
    }
50
 
    TextStructItem *parentStructItem()
51
 
    { return static_cast<TextStructItem*>(parent()); }
52
 
    
53
 
    QString tag;
54
 
    QString extra;
55
 
    int pos;
56
 
    int endpos;
57
 
};
58
 
 
59
 
 
60
 
TextToolsWidget::TextToolsWidget(TextToolsPart *part, QWidget *parent, const char *name)
61
 
    : KListView(parent, name)
62
 
{
63
 
    setResizeMode(QListView::LastColumn);
64
 
    setSorting(-1);
65
 
    header()->hide();
66
 
    addColumn(QString::null);
67
 
 
68
 
    m_part = part;
69
 
 
70
 
    m_timer = new QTimer(this);
71
 
    connect( this, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)),
72
 
             this, SLOT(slotItemPressed(int,QListViewItem*)) );
73
 
    //    connect( this, SIGNAL(doubleClicked(QListViewItem*)),
74
 
    //             this, SLOT(slotItemPressed(int,QListViewItem*)) );
75
 
    connect( this, SIGNAL(returnPressed(QListViewItem*)),
76
 
             this, SLOT(slotReturnPressed(QListViewItem*)) );
77
 
    connect( this, SIGNAL(contextMenu(KListView*, QListViewItem*, const QPoint&)),
78
 
             this, SLOT(slotContextMenu(KListView*, QListViewItem*, const QPoint&)) );
79
 
}
80
 
 
81
 
 
82
 
TextToolsWidget::~TextToolsWidget()
83
 
{}
84
 
 
85
 
 
86
 
void TextToolsWidget::slotItemPressed(int button, QListViewItem *item)
87
 
{
88
 
    if (!item)
89
 
        return;
90
 
 
91
 
    TextStructItem *tsitem = static_cast<TextStructItem*>(item);
92
 
    int searchedPos = tsitem->pos;
93
 
    int searchedEndpos = tsitem->endpos;
94
 
    kdDebug(9030) << "Searched pos " << searchedPos << ", " << searchedEndpos << endl;
95
 
    
96
 
    int endline = 0;
97
 
    int endcol = 0;
98
 
    int line = 0;
99
 
    int col = 0;
100
 
 
101
 
    int len = m_cachedText.length();
102
 
    int pos = 0;
103
 
    while (pos < len) {
104
 
        if (pos == searchedPos) {
105
 
            line = endline;
106
 
            col = endcol;
107
 
        }
108
 
        if (pos == searchedEndpos)
109
 
            break;
110
 
        QChar ch = m_cachedText[pos];
111
 
        if (ch == '\n') {
112
 
            ++endline;
113
 
            endcol = 0;
114
 
        } else {
115
 
            ++endcol;
116
 
        }
117
 
        ++pos;
118
 
    }
119
 
 
120
 
    KParts::Part *rwpart
121
 
        = dynamic_cast<KParts::Part*>(m_part->partController()->activePart());
122
 
    QWidget *view = m_part->partController()->activeWidget();
123
 
 
124
 
    KTextEditor::ViewCursorInterface *cursorIface
125
 
        = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
126
 
    if (cursorIface) {
127
 
        kdDebug(9030) << "set cursor " << line << ", " << col << endl;
128
 
        cursorIface->setCursorPosition(line, col);
129
 
    }
130
 
    
131
 
    if (button == MidButton) {
132
 
        KTextEditor::SelectionInterface *selectionIface
133
 
            = dynamic_cast<KTextEditor::SelectionInterface*>(rwpart);
134
 
        if (selectionIface) {
135
 
            kdDebug(9030) << "set selection " << line << ", " << col
136
 
                      << ", " << endline << ", " << endcol << endl;
137
 
            selectionIface->setSelection((int)line, (int)col, (int)endline, (int)endcol+1);
138
 
        }
139
 
    }
140
 
 
141
 
    m_part->mainWindow()->lowerView(this);
142
 
}
143
 
 
144
 
 
145
 
void TextToolsWidget::slotReturnPressed(QListViewItem *item)
146
 
{
147
 
    slotItemPressed(LeftButton, item);
148
 
}
149
 
 
150
 
 
151
 
void TextToolsWidget::slotContextMenu(KListView *, QListViewItem *item, const QPoint &)
152
 
{
153
 
    if (!item)
154
 
        return;
155
 
 
156
 
#if 0
157
 
    KPopupMenu popup(i18n("Text Structure"), this);
158
 
    popup.exec(p);
159
 
#endif
160
 
}
161
 
 
162
 
 
163
 
void TextToolsWidget::stop()
164
 
{
165
 
    disconnect( m_timer );
166
 
    m_relevantTags.clear();
167
 
    m_emptyTags.clear();
168
 
    m_cachedText = QString::null;
169
 
}
170
 
 
171
 
 
172
 
void TextToolsWidget::setMode(Mode mode, KParts::Part *part)
173
 
{
174
 
    connect( part, SIGNAL(textChanged()),
175
 
             this, SLOT(startTimer()) );
176
 
    m_editIface = dynamic_cast<KTextEditor::EditInterface*>(part);
177
 
    
178
 
    switch (mode) {
179
 
    case HTML:
180
 
        m_relevantTags << "h1" << "h2" << "h3" << "h4"
181
 
                       << "table" << "tr";
182
 
        m_emptyTags << "br" << "hr" << "img" << "input" << "p" << "meta";
183
 
        
184
 
        connect( m_timer, SIGNAL(timeout()),
185
 
                 this, SLOT(parseXML()) );
186
 
        break;
187
 
    case Docbook:
188
 
        m_relevantTags << "chapter" << "sect1" << "sect2"
189
 
                       << "para" << "formalpara";
190
 
        connect( m_timer, SIGNAL(timeout()),
191
 
                 this, SLOT(parseXML()) );
192
 
        break;
193
 
    case LaTeX:
194
 
        connect( m_timer, SIGNAL(timeout()),
195
 
                 this, SLOT(parseLaTeX()) );
196
 
        break;
197
 
    default: ;
198
 
    }
199
 
 
200
 
    m_timer->start(0, true);
201
 
}
202
 
 
203
 
 
204
 
void TextToolsWidget::startTimer()
205
 
{
206
 
    kdDebug(9030) << "Starting parse timer" << endl;
207
 
    m_timer->start(1000, true);
208
 
}
209
 
 
210
 
 
211
 
void TextToolsWidget::parseXML()
212
 
{
213
 
    kdDebug(9030) << "Starting to parse XML" << endl;
214
 
    clear();
215
 
    QString text = m_editIface->text();
216
 
    m_cachedText = text;
217
 
 
218
 
    TextStructItem *currentItem = new TextStructItem(this);
219
 
    currentItem->tag = "Root";
220
 
    currentItem->pos = -1;
221
 
    currentItem->endpos = -1;
222
 
    
223
 
    int len = text.length();
224
 
    for (int pos=0; pos+1 < len; ++pos) {
225
 
        QChar ch1 = text[pos];
226
 
        QChar ch2 = text[pos+1];
227
 
 
228
 
        if (ch1 == '<' && ch2 == '?') {
229
 
            
230
 
            // PHP and other similar stuff
231
 
            QString tag;
232
 
            int endpos = pos+2;
233
 
            bool foundspace = false;
234
 
            while (endpos+1 < len) {
235
 
                QChar ch3 = text[endpos];
236
 
                QChar ch4 = text[endpos+1];
237
 
                if ((ch3 == ' ' || ch3 == '\t' || ch3 == '\n') && !foundspace) {
238
 
                    tag = text.mid(pos+2, endpos-pos-2).lower();
239
 
                    foundspace = true;
240
 
                } else if (ch3 == '?' && ch4 == '>') {
241
 
                    if (!foundspace)
242
 
                        tag = text.mid(pos+2, endpos-pos-2).lower();
243
 
                    break;
244
 
                }
245
 
                ++endpos;
246
 
            }
247
 
 
248
 
            TextStructItem *item = new TextStructItem(currentItem);
249
 
            item->tag = "<?" + tag + "?>";
250
 
            item->pos = pos;
251
 
            item->endpos = endpos+1;
252
 
 
253
 
            pos = endpos+1;
254
 
            
255
 
        } else  if (ch1 == '<' && ch2 == '!') {
256
 
            
257
 
            // Processing instructions like !DOCTYPE
258
 
            QString tag;
259
 
            int endpos = pos+2;
260
 
            bool foundspace = false;
261
 
            while (endpos+1 < len) {
262
 
                QChar ch3 = text[endpos];
263
 
                if ((ch3 == ' ' || ch3 == '\t' || ch3 == '\n') && !foundspace) {
264
 
                    tag = text.mid(pos+2, endpos-pos-2).lower();
265
 
                    foundspace = true;
266
 
                } else if (ch3 == '>') {
267
 
                    if (!foundspace)
268
 
                        tag = text.mid(pos+2, endpos-pos-2).lower();
269
 
                    break;
270
 
                }
271
 
                ++endpos;
272
 
            }
273
 
 
274
 
            TextStructItem *item = new TextStructItem(currentItem);
275
 
            item->tag = "<!" + tag + ">";
276
 
            item->pos = pos;
277
 
            item->endpos = endpos+1;
278
 
 
279
 
            pos = endpos+1;
280
 
            
281
 
        } else if (ch1 == '<' && ch2 == '/') {
282
 
 
283
 
            QString tag;
284
 
            int endpos = pos+2;
285
 
            while (endpos < len) {
286
 
                QChar ch3 = text[endpos];
287
 
                if (ch3 == '>') {
288
 
                    tag = text.mid(pos+2, endpos-pos-2).lower();
289
 
                    break;
290
 
                }
291
 
                ++endpos;
292
 
            }
293
 
 
294
 
            if (!m_relevantTags.contains(tag)) {
295
 
                pos = endpos;
296
 
                continue;
297
 
            }
298
 
            
299
 
            TextStructItem *closingItem = currentItem;
300
 
            while (closingItem->parent() && closingItem->tag != tag)
301
 
                closingItem = closingItem->parentStructItem();
302
 
            if (closingItem->parent()) {
303
 
                closingItem->endpos = endpos;
304
 
                currentItem = closingItem->parentStructItem();
305
 
            } else {
306
 
                kdDebug(9030) << "found no opening tag " << tag << "." << endl;
307
 
            }
308
 
            
309
 
            pos = endpos;
310
 
            
311
 
        } else if (ch1 == '<') {
312
 
 
313
 
            QString tag;
314
 
            int endpos = pos+1;
315
 
            bool foundspace = false;
316
 
            while (endpos < len) {
317
 
                QChar ch3 = text[endpos];
318
 
                if ((ch3 == ' ' || ch3 == '\t' || ch3 == '\n') && !foundspace) {
319
 
                    tag = text.mid(pos+1, endpos-pos-1).lower();
320
 
                    foundspace = true;
321
 
                } else if (ch3 == '>') {
322
 
                    if (!foundspace) {
323
 
                        tag = text.mid(pos+1, endpos-pos-1).lower();
324
 
                    }
325
 
                    break;
326
 
                }
327
 
                ++endpos;
328
 
            }
329
 
 
330
 
            if (!m_relevantTags.contains(tag)) {
331
 
                pos = endpos;
332
 
                continue;
333
 
            }
334
 
 
335
 
            TextStructItem *item = new TextStructItem(currentItem);
336
 
            item->tag = tag;
337
 
            item->pos = pos;
338
 
            item->endpos = -1;
339
 
 
340
 
            if (m_emptyTags.contains(tag))
341
 
                item->endpos = endpos;
342
 
            else
343
 
                currentItem = item;
344
 
            pos = endpos;
345
 
 
346
 
        }
347
 
    }
348
 
 
349
 
    //    firstChild()->setOpen(true);
350
 
    QListViewItemIterator it(this);
351
 
    for (; it.current(); ++it)
352
 
        it.current()->setOpen(true);
353
 
}
354
 
 
355
 
 
356
 
void TextToolsWidget::parseLaTeX()
357
 
{
358
 
    kdDebug(9030) << "Starting to parse LaTeX" << endl;
359
 
    clear();
360
 
    QString text = m_editIface->text();
361
 
    m_cachedText = text;
362
 
 
363
 
    TextStructItem *currentItem = new TextStructItem(this);
364
 
    currentItem->tag = "Root";
365
 
    currentItem->pos = -1;
366
 
    currentItem->endpos = -1;
367
 
 
368
 
    QString hierarchyLevels = "Root,chapter,section,subsection,subsubsection";
369
 
    QRegExp re("\n[ \t]*s*\\\\(chapter|section|subsection|subsubsection)\\{([^}]*)\\}");
370
 
    
371
 
    int pos=0;
372
 
    for (;;) {
373
 
        pos = re.search(text, pos);
374
 
        if (pos == -1)
375
 
            break;
376
 
        QString tag = re.cap(1);
377
 
        QString title = re.cap(2);
378
 
        kdDebug(9030) << "Match with " << tag << " and title " << title << endl;
379
 
        int level = hierarchyLevels.find(tag);
380
 
        while (currentItem->parent() && level <= hierarchyLevels.find(currentItem->tag))
381
 
            currentItem = currentItem->parentStructItem();
382
 
        
383
 
        TextStructItem *item = new TextStructItem(currentItem);
384
 
        item->tag = tag;
385
 
        item->extra = title;
386
 
        item->pos = pos+1;
387
 
        item->endpos = pos+re.matchedLength()-1; // lie
388
 
 
389
 
        if (level > hierarchyLevels.find(currentItem->tag))
390
 
            currentItem = item;
391
 
 
392
 
        pos = pos+re.matchedLength();
393
 
    }
394
 
 
395
 
    QListViewItemIterator it(this);
396
 
    for (; it.current(); ++it)
397
 
        it.current()->setOpen(true);
398
 
}
399
 
 
400
 
#include "texttoolswidget.moc"