~ubuntu-branches/ubuntu/lucid/konversation/lucid-updates

« back to all changes in this revision

Viewing changes to src/viewer/multilineedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2009-05-19 09:44:08 UTC
  • mfrom: (1.16.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: james.westby@ubuntu.com-20090519094408-yi3yv1v4zzbsu2nx
Tags: 1.1.75+svn969816-1
* New upstream development snapshot:
  - Last Changed Author: tjmchenry
  - Last Changed Rev: 969816
  - Last Changed Date: 2009-05-19 04:45:54 +0300
  - Translations as of Tue, 19 May 2009 09:21:13 are included.
* Update download URL in debian/copyright.
* Update Debian menu icons.
* Update konversation.install: install translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  This program is free software; you can redistribute it and/or modify
3
 
  it under the terms of the GNU General Public License as published by
4
 
  the Free Software Foundation; either version 2 of the License, or
5
 
  (at your option) any later version.
6
 
*/
7
 
 
8
 
/*
9
 
  Copyright (C) 2006 Dario Abatianni <eisfuchs@tigress.com>
10
 
*/
11
 
 
12
 
#include "multilineedit.h"
13
 
#include "application.h"
14
 
 
15
 
#include <KTextEdit>
16
 
 
17
 
#include <QVBoxLayout>
18
 
 
19
 
 
20
 
QString MultilineEdit::returnText;                // static
21
 
 
22
 
MultilineEdit::MultilineEdit(QWidget* parent, const QString& text) :
23
 
KDialog(parent )
24
 
{
25
 
    setButtons( KDialog::User1 | KDialog::User2 | KDialog::Ok | KDialog::Cancel );
26
 
    setButtonText( KDialog::User1, i18n("Add &Quotation Indicators") );
27
 
    setButtonText( KDialog::User2, i18n("Remove Newlines") );
28
 
    setDefaultButton( KDialog::Ok );
29
 
    setCaption( i18n("Edit Multiline Paste") );
30
 
    setModal( true );
31
 
 
32
 
    QVBoxLayout* dialogLayout=new QVBoxLayout(mainWidget());
33
 
    dialogLayout->setSpacing(spacingHint());
34
 
    // add the text editor
35
 
    textEditor=new KTextEdit(mainWidget());
36
 
    connect(textEditor, SIGNAL(textChanged()), this, SLOT(dislayNonprintingChars()));
37
 
    textEditor->setPlainText(text);
38
 
 
39
 
    returnText=text;
40
 
 
41
 
    dialogLayout->addWidget(textEditor);
42
 
 
43
 
    setInitialSize(Preferences::self()->multilineEditSize());
44
 
    show();
45
 
    connect( this, SIGNAL( okClicked() ), this, SLOT( slotOk() ) );
46
 
    connect( this, SIGNAL( cancelClicked() ), this, SLOT( slotCancel() ) );
47
 
    connect( this, SIGNAL( user1Clicked() ), this, SLOT( slotUser1() ) );
48
 
    connect( this, SIGNAL( user2Clicked() ), this, SLOT( slotUser2() ) );   
49
 
}
50
 
 
51
 
MultilineEdit::~MultilineEdit()
52
 
{
53
 
    Preferences::self()->setMultilineEditSize(size());
54
 
}
55
 
 
56
 
void MultilineEdit::slotCancel()
57
 
{
58
 
    returnText.clear();
59
 
    reject();
60
 
}
61
 
 
62
 
void MultilineEdit::slotOk()
63
 
{
64
 
    removeNonprintingChars();
65
 
    returnText=textEditor->toPlainText();
66
 
    accept();
67
 
}
68
 
 
69
 
void MultilineEdit::slotUser1()
70
 
{
71
 
    QStringList lines=textEditor->toPlainText().split('\n', QString::KeepEmptyParts);
72
 
    for( QStringList::iterator it=lines.begin() ; it!=lines.end() ; ++it )
73
 
        (*it) = "> " + (*it);
74
 
    textEditor->setText(lines.join("\n"));
75
 
}
76
 
 
77
 
void MultilineEdit::slotUser2()
78
 
{
79
 
    QString pattern("\n");
80
 
    QRegExp searchFor(pattern);
81
 
    searchFor.setPatternSyntax(QRegExp::FixedString);
82
 
    removeNonprintingChars();
83
 
    QString line=textEditor->toPlainText();
84
 
    int index=line.indexOf(searchFor);
85
 
    int patLen = pattern.length();
86
 
    while (index>=0)
87
 
    {
88
 
        QChar before,after;
89
 
        int length,nextLength;
90
 
        length=index;
91
 
        length+=patLen;
92
 
        if (index!=0) before = line.at(index-1);
93
 
        if (line.length() > length) after = line.at(length);
94
 
    
95
 
        if (before.isSpace() || after.isSpace())
96
 
        {
97
 
            line.replace(index, patLen, QString());
98
 
            nextLength = index;
99
 
        }
100
 
        else
101
 
        {
102
 
            line.replace(index, patLen, " ");
103
 
            nextLength = index+1;
104
 
        }
105
 
        index=line.indexOf(searchFor,nextLength);
106
 
    }
107
 
    textEditor->setText(line);
108
 
    dislayNonprintingChars();
109
 
}
110
 
 
111
 
QString MultilineEdit::edit(QWidget* parent, const QString& text)
112
 
{
113
 
    MultilineEdit dlg(parent,text);
114
 
    return dlg.exec() ? returnText : QString();
115
 
}
116
 
 
117
 
void MultilineEdit::dislayNonprintingChars()
118
 
{
119
 
    textEditor->blockSignals(true);
120
 
 
121
 
    const int position = textEditor->textCursor().position();
122
 
 
123
 
    removeNonprintingChars();
124
 
 
125
 
    const QString html = QString("<html>" + Qt::escape(textEditor->toPlainText()) + "</html>")
126
 
        .replace(' ', QString::fromUtf8("<span style=\"color:blue\">·</span>"))
127
 
        .replace('\n', QString::fromUtf8("<span style=\"color:blue\">¶</span><br/>"))
128
 
        .replace('\t', QString::fromUtf8("<span style=\"color:blue\">»</span>"))
129
 
        ;
130
 
    textEditor->setHtml(html);
131
 
 
132
 
    QTextCursor cursor = textEditor->textCursor();
133
 
    cursor.setPosition(position);
134
 
    textEditor->setTextCursor(cursor);
135
 
 
136
 
    textEditor->blockSignals(false);
137
 
}
138
 
 
139
 
void MultilineEdit::removeNonprintingChars()
140
 
{
141
 
    const bool blockSignals = textEditor->signalsBlocked();
142
 
    textEditor->blockSignals(true);
143
 
    QString text = textEditor->toPlainText();
144
 
    text
145
 
        .remove(QString::fromUtf8("¶"))
146
 
        .replace(QString::fromUtf8("»"), "\t")
147
 
        .replace(QString::fromUtf8("·"), " ")
148
 
        ;
149
 
    textEditor->setPlainText(text);
150
 
    textEditor->blockSignals(blockSignals);
151
 
}
152
 
 
153
 
#include "multilineedit.moc"