~ubuntu-branches/ubuntu/maverick/scidavis/maverick

« back to all changes in this revision

Viewing changes to scidavis/src/TextFormatButtons.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : TextFormatButtons.cpp
 
3
    Project              : SciDAVis
 
4
    --------------------------------------------------------------------
 
5
    Copyright            : (C) 2006 by Ion Vasilief, Tilman Benkert
 
6
    Email (use @ for *)  : ion_vasilief*yahoo.fr, thzs*gmx.net
 
7
    Description          : Widget with text format buttons (connected to a QTextEdit)
 
8
 
 
9
 ***************************************************************************/
 
10
 
 
11
/***************************************************************************
 
12
 *                                                                         *
 
13
 *  This program is free software; you can redistribute it and/or modify   *
 
14
 *  it under the terms of the GNU General Public License as published by   *
 
15
 *  the Free Software Foundation; either version 2 of the License, or      *
 
16
 *  (at your option) any later version.                                    *
 
17
 *                                                                         *
 
18
 *  This program is distributed in the hope that it will be useful,        *
 
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
21
 *  GNU General Public License for more details.                           *
 
22
 *                                                                         *
 
23
 *   You should have received a copy of the GNU General Public License     *
 
24
 *   along with this program; if not, write to the Free Software           *
 
25
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
26
 *   Boston, MA  02110-1301  USA                                           *
 
27
 *                                                                         *
 
28
 ***************************************************************************/
 
29
 
 
30
#include "TextFormatButtons.h"
 
31
#include "SymbolDialog.h"
 
32
#include <QTextEdit>
 
33
#include <QPushButton>
 
34
#include <QHBoxLayout>
 
35
#include <QString>
 
36
 
 
37
TextFormatButtons::TextFormatButtons(QTextEdit * textEdit, QWidget * parent)
 
38
: QWidget(parent)
 
39
{
 
40
        connectedTextEdit = textEdit;
 
41
 
 
42
        QHBoxLayout * layout = new QHBoxLayout(this);
 
43
        layout->setMargin(0);
 
44
        layout->setSpacing(0);
 
45
    layout->addStretch();
 
46
 
 
47
        QFont font = this->font();
 
48
        font.setPointSize(14);
 
49
 
 
50
        buttonCurve = new QPushButton( QPixmap(":/lineSymbol.xpm"), QString());
 
51
        buttonCurve->setMaximumWidth(40);
 
52
        buttonCurve->setMinimumHeight(35);
 
53
        buttonCurve->setFont(font);
 
54
        layout->addWidget(buttonCurve);
 
55
 
 
56
        buttonSubscript = new QPushButton(QPixmap(":/index.xpm"),QString());
 
57
        buttonSubscript->setMaximumWidth(40);
 
58
        buttonSubscript->setMinimumHeight(35);
 
59
        buttonSubscript->setFont(font);
 
60
        layout->addWidget(buttonSubscript);
 
61
 
 
62
        buttonSuperscript = new QPushButton(QPixmap(":/exp.xpm"),QString());
 
63
        buttonSuperscript->setMaximumWidth(40);
 
64
        buttonSuperscript->setMinimumHeight(35);
 
65
        buttonSuperscript->setFont(font);
 
66
        layout->addWidget(buttonSuperscript);
 
67
 
 
68
        buttonLowerGreek = new QPushButton(QString(QChar(0x3B1)));
 
69
        buttonLowerGreek->setFont(font);
 
70
        buttonLowerGreek->setMaximumWidth(40);
 
71
        layout->addWidget(buttonLowerGreek);
 
72
 
 
73
        buttonUpperGreek = new QPushButton(QString(QChar(0x393)));
 
74
        buttonUpperGreek->setFont(font);
 
75
        buttonUpperGreek->setMaximumWidth(40);
 
76
        layout->addWidget(buttonUpperGreek);
 
77
 
 
78
        buttonMathSymbols = new QPushButton(QString(QChar(0x222B)));
 
79
        buttonMathSymbols->setFont(font);
 
80
        buttonMathSymbols->setMaximumWidth(40);
 
81
        layout->addWidget(buttonMathSymbols);
 
82
 
 
83
        buttonArrowSymbols = new QPushButton(QString(QChar(0x2192)));
 
84
        buttonArrowSymbols->setFont(font);
 
85
        buttonArrowSymbols->setMaximumWidth(40);
 
86
        layout->addWidget(buttonArrowSymbols);
 
87
 
 
88
        font = this->font();
 
89
        font.setBold(true);
 
90
        font.setPointSize(14);
 
91
 
 
92
        buttonBold = new QPushButton(tr("B","Button bold"));
 
93
        buttonBold->setFont(font);
 
94
        buttonBold->setMaximumWidth(40);
 
95
        layout->addWidget(buttonBold);
 
96
 
 
97
        font = this->font();
 
98
        font.setItalic(true);
 
99
        font.setPointSize(14);
 
100
 
 
101
        buttonItalics = new QPushButton(tr("It","Button italics"));
 
102
        buttonItalics->setFont(font);
 
103
        buttonItalics->setMaximumWidth(40);
 
104
        layout->addWidget(buttonItalics);
 
105
 
 
106
        font = this->font();
 
107
        font.setUnderline(true);
 
108
        font.setPointSize(14);
 
109
 
 
110
        buttonUnderline = new QPushButton(tr("U","Button underline"));
 
111
        buttonUnderline->setFont(font);
 
112
        buttonUnderline->setMaximumWidth(40);
 
113
        layout->addWidget(buttonUnderline);
 
114
    layout->addStretch();
 
115
 
 
116
        connect( buttonCurve, SIGNAL(clicked()), this, SLOT(addCurve()) );
 
117
        connect( buttonSuperscript, SIGNAL(clicked()), this, SLOT(addSuperscript()) );
 
118
        connect( buttonSubscript, SIGNAL(clicked()), this, SLOT(addSubscript()) );
 
119
        connect( buttonUnderline, SIGNAL(clicked()), this, SLOT(addUnderline()) );
 
120
        connect( buttonItalics, SIGNAL(clicked()), this, SLOT(addItalics()) );
 
121
        connect( buttonBold, SIGNAL(clicked()), this, SLOT(addBold()) );
 
122
        connect( buttonLowerGreek, SIGNAL(clicked()), this, SLOT(showLowerGreek()) );
 
123
        connect( buttonUpperGreek, SIGNAL(clicked()), this, SLOT(showUpperGreek()) );
 
124
        connect( buttonMathSymbols, SIGNAL(clicked()), this, SLOT(showMathSymbols()) );
 
125
        connect( buttonArrowSymbols, SIGNAL(clicked()), this, SLOT(showArrowSymbols()) );
 
126
}
 
127
 
 
128
void TextFormatButtons::showLowerGreek()
 
129
{
 
130
        SymbolDialog *greekLetters = new SymbolDialog(SymbolDialog::lowerGreek, this, Qt::Tool);
 
131
        greekLetters->setAttribute(Qt::WA_DeleteOnClose);
 
132
        QFont f = connectedTextEdit->font();
 
133
        if(f.pointSize()<14)
 
134
                f.setPointSize(14);
 
135
        greekLetters->setFont(f);
 
136
        connect(greekLetters, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
 
137
        greekLetters->show();
 
138
        greekLetters->setFocus();
 
139
}
 
140
 
 
141
void TextFormatButtons::showUpperGreek()
 
142
{
 
143
        SymbolDialog *greekLetters = new SymbolDialog(SymbolDialog::upperGreek, this, Qt::Tool);
 
144
        greekLetters->setAttribute(Qt::WA_DeleteOnClose);
 
145
        QFont f = connectedTextEdit->font();
 
146
        if(f.pointSize()<14)
 
147
                f.setPointSize(14);
 
148
        greekLetters->setFont(f);
 
149
        connect(greekLetters, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
 
150
        greekLetters->show();
 
151
        greekLetters->setFocus();
 
152
}
 
153
 
 
154
void TextFormatButtons::showMathSymbols()
 
155
{
 
156
        SymbolDialog *mathSymbols = new SymbolDialog(SymbolDialog::mathSymbols, this, Qt::Tool);
 
157
        mathSymbols->setAttribute(Qt::WA_DeleteOnClose);
 
158
        QFont f = connectedTextEdit->font();
 
159
        if(f.pointSize()<14)
 
160
                f.setPointSize(14);
 
161
        mathSymbols->setFont(f);
 
162
        connect(mathSymbols, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
 
163
        mathSymbols->show();
 
164
        mathSymbols->setFocus();
 
165
}
 
166
 
 
167
void TextFormatButtons::showArrowSymbols()
 
168
{
 
169
        SymbolDialog *arrowSymbols = new SymbolDialog(SymbolDialog::arrowSymbols, this, Qt::Tool);
 
170
        arrowSymbols->setAttribute(Qt::WA_DeleteOnClose);
 
171
        arrowSymbols->setFont(connectedTextEdit->font());
 
172
        QFont f = connectedTextEdit->font();
 
173
        if(f.pointSize()<14)
 
174
                f.setPointSize(14);
 
175
        arrowSymbols->setFont(f);
 
176
        connect(arrowSymbols, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
 
177
        arrowSymbols->show();
 
178
        arrowSymbols->setFocus();
 
179
}
 
180
 
 
181
void TextFormatButtons::addSymbol(const QString & letter)
 
182
{
 
183
        connectedTextEdit->textCursor().insertText(letter);
 
184
}
 
185
 
 
186
void TextFormatButtons::addCurve()
 
187
{
 
188
        formatText("\\c{","}");
 
189
}
 
190
 
 
191
void TextFormatButtons::addUnderline()
 
192
{
 
193
        formatText("<u>","</u>");
 
194
}
 
195
 
 
196
void TextFormatButtons::addItalics()
 
197
{
 
198
        formatText("<i>","</i>");
 
199
}
 
200
 
 
201
void TextFormatButtons::addBold()
 
202
{
 
203
        formatText("<b>","</b>");
 
204
}
 
205
 
 
206
void TextFormatButtons::addSubscript()
 
207
{
 
208
        formatText("<sub>","</sub>");
 
209
}
 
210
 
 
211
void TextFormatButtons::addSuperscript()
 
212
{
 
213
        formatText("<sup>","</sup>");
 
214
}
 
215
 
 
216
void TextFormatButtons::formatText(const QString & prefix, const QString & postfix)
 
217
{
 
218
        QTextCursor cursor = connectedTextEdit->textCursor();
 
219
        QString markedText = cursor.selectedText();
 
220
        cursor.insertText(prefix+markedText+postfix);
 
221
        if(markedText.isEmpty())
 
222
        {
 
223
                // if no text is marked, place cursor inside the <..></..> statement
 
224
                // instead of after it
 
225
                cursor.movePosition(QTextCursor::PreviousCharacter,QTextCursor::MoveAnchor,postfix.size());
 
226
                // the next line makes the selection visible to the user
 
227
                // (the line above only changes the selection in the
 
228
                // underlying QTextDocument)
 
229
                connectedTextEdit->setTextCursor(cursor);
 
230
        }
 
231
        // give focus back to text edit
 
232
        connectedTextEdit->setFocus();
 
233
}
 
234
 
 
235
void TextFormatButtons::toggleCurveButton(bool enable)
 
236
{
 
237
        buttonCurve->setVisible(enable);
 
238
}