1
/***************************************************************************
2
File : TextFormatButtons.cpp
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)
9
***************************************************************************/
11
/***************************************************************************
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. *
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. *
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 *
28
***************************************************************************/
30
#include "TextFormatButtons.h"
31
#include "SymbolDialog.h"
33
#include <QPushButton>
34
#include <QHBoxLayout>
37
TextFormatButtons::TextFormatButtons(QTextEdit * textEdit, QWidget * parent)
40
connectedTextEdit = textEdit;
42
QHBoxLayout * layout = new QHBoxLayout(this);
44
layout->setSpacing(0);
47
QFont font = this->font();
48
font.setPointSize(14);
50
buttonCurve = new QPushButton( QPixmap(":/lineSymbol.xpm"), QString());
51
buttonCurve->setMaximumWidth(40);
52
buttonCurve->setMinimumHeight(35);
53
buttonCurve->setFont(font);
54
layout->addWidget(buttonCurve);
56
buttonSubscript = new QPushButton(QPixmap(":/index.xpm"),QString());
57
buttonSubscript->setMaximumWidth(40);
58
buttonSubscript->setMinimumHeight(35);
59
buttonSubscript->setFont(font);
60
layout->addWidget(buttonSubscript);
62
buttonSuperscript = new QPushButton(QPixmap(":/exp.xpm"),QString());
63
buttonSuperscript->setMaximumWidth(40);
64
buttonSuperscript->setMinimumHeight(35);
65
buttonSuperscript->setFont(font);
66
layout->addWidget(buttonSuperscript);
68
buttonLowerGreek = new QPushButton(QString(QChar(0x3B1)));
69
buttonLowerGreek->setFont(font);
70
buttonLowerGreek->setMaximumWidth(40);
71
layout->addWidget(buttonLowerGreek);
73
buttonUpperGreek = new QPushButton(QString(QChar(0x393)));
74
buttonUpperGreek->setFont(font);
75
buttonUpperGreek->setMaximumWidth(40);
76
layout->addWidget(buttonUpperGreek);
78
buttonMathSymbols = new QPushButton(QString(QChar(0x222B)));
79
buttonMathSymbols->setFont(font);
80
buttonMathSymbols->setMaximumWidth(40);
81
layout->addWidget(buttonMathSymbols);
83
buttonArrowSymbols = new QPushButton(QString(QChar(0x2192)));
84
buttonArrowSymbols->setFont(font);
85
buttonArrowSymbols->setMaximumWidth(40);
86
layout->addWidget(buttonArrowSymbols);
90
font.setPointSize(14);
92
buttonBold = new QPushButton(tr("B","Button bold"));
93
buttonBold->setFont(font);
94
buttonBold->setMaximumWidth(40);
95
layout->addWidget(buttonBold);
99
font.setPointSize(14);
101
buttonItalics = new QPushButton(tr("It","Button italics"));
102
buttonItalics->setFont(font);
103
buttonItalics->setMaximumWidth(40);
104
layout->addWidget(buttonItalics);
107
font.setUnderline(true);
108
font.setPointSize(14);
110
buttonUnderline = new QPushButton(tr("U","Button underline"));
111
buttonUnderline->setFont(font);
112
buttonUnderline->setMaximumWidth(40);
113
layout->addWidget(buttonUnderline);
114
layout->addStretch();
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()) );
128
void TextFormatButtons::showLowerGreek()
130
SymbolDialog *greekLetters = new SymbolDialog(SymbolDialog::lowerGreek, this, Qt::Tool);
131
greekLetters->setAttribute(Qt::WA_DeleteOnClose);
132
QFont f = connectedTextEdit->font();
135
greekLetters->setFont(f);
136
connect(greekLetters, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
137
greekLetters->show();
138
greekLetters->setFocus();
141
void TextFormatButtons::showUpperGreek()
143
SymbolDialog *greekLetters = new SymbolDialog(SymbolDialog::upperGreek, this, Qt::Tool);
144
greekLetters->setAttribute(Qt::WA_DeleteOnClose);
145
QFont f = connectedTextEdit->font();
148
greekLetters->setFont(f);
149
connect(greekLetters, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
150
greekLetters->show();
151
greekLetters->setFocus();
154
void TextFormatButtons::showMathSymbols()
156
SymbolDialog *mathSymbols = new SymbolDialog(SymbolDialog::mathSymbols, this, Qt::Tool);
157
mathSymbols->setAttribute(Qt::WA_DeleteOnClose);
158
QFont f = connectedTextEdit->font();
161
mathSymbols->setFont(f);
162
connect(mathSymbols, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
164
mathSymbols->setFocus();
167
void TextFormatButtons::showArrowSymbols()
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();
175
arrowSymbols->setFont(f);
176
connect(arrowSymbols, SIGNAL(addLetter(const QString&)), this, SLOT(addSymbol(const QString&)));
177
arrowSymbols->show();
178
arrowSymbols->setFocus();
181
void TextFormatButtons::addSymbol(const QString & letter)
183
connectedTextEdit->textCursor().insertText(letter);
186
void TextFormatButtons::addCurve()
188
formatText("\\c{","}");
191
void TextFormatButtons::addUnderline()
193
formatText("<u>","</u>");
196
void TextFormatButtons::addItalics()
198
formatText("<i>","</i>");
201
void TextFormatButtons::addBold()
203
formatText("<b>","</b>");
206
void TextFormatButtons::addSubscript()
208
formatText("<sub>","</sub>");
211
void TextFormatButtons::addSuperscript()
213
formatText("<sup>","</sup>");
216
void TextFormatButtons::formatText(const QString & prefix, const QString & postfix)
218
QTextCursor cursor = connectedTextEdit->textCursor();
219
QString markedText = cursor.selectedText();
220
cursor.insertText(prefix+markedText+postfix);
221
if(markedText.isEmpty())
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);
231
// give focus back to text edit
232
connectedTextEdit->setFocus();
235
void TextFormatButtons::toggleCurveButton(bool enable)
237
buttonCurve->setVisible(enable);