richtextdialog.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19#include <tqlayout.h>
20
21#include <tdetoolbar.h>
22#include <tdefontcombo.h>
23#include <kcolorcombo.h>
24#include <tdetoolbarradiogroup.h>
25#include <kdebug.h>
26#include <tdelocale.h>
27
28#include "richtextdialog.h"
29
30namespace KFormDesigner {
31
35
36RichTextDialog::RichTextDialog(TQWidget *parent, const TQString &text)
37: KDialogBase(parent, "richtext_dialog", true, i18n("Edit Rich Text"), Ok|Cancel, Ok, false)
38{
39 TQFrame *frame = makeMainWidget();
40 TQVBoxLayout *l = new TQVBoxLayout(frame);
41 l->setAutoAdd(true);
42
43 m_toolbar = new TDEToolBar(frame);
44 m_toolbar->setFlat(true);
45 m_toolbar->show();
46
47 m_fcombo = new TDEFontCombo(m_toolbar);
48 m_toolbar->insertWidget(TBFont, 40, m_fcombo);
49 connect(m_fcombo, TQ_SIGNAL(textChanged(const TQString&)), this, TQ_SLOT(changeFont(const TQString &)));
50
51 m_toolbar->insertSeparator();
52
53 m_colCombo = new KColorCombo(m_toolbar);
54 m_toolbar->insertWidget(TBColor, 30, m_colCombo);
55 connect(m_colCombo, TQ_SIGNAL(activated(const TQColor&)), this, TQ_SLOT(changeColor(const TQColor&)));
56
57 m_toolbar->insertButton("format-text-bold", TBBold, true, i18n("Bold"));
58 m_toolbar->insertButton("format-text-italic", TBItalic, true, i18n("Italic"));
59 m_toolbar->insertButton("format-text-underline", TBUnder, true, i18n("Underline"));
60 m_toolbar->setToggle(TBBold, true);
61 m_toolbar->setToggle(TBItalic, true);
62 m_toolbar->setToggle(TBUnder, true);
63 m_toolbar->insertSeparator();
64
65 m_toolbar->insertButton("text_super", TBSuper, true, i18n("Superscript"));
66 m_toolbar->insertButton("text_sub", TBSub, true, i18n("Subscript"));
67 m_toolbar->setToggle(TBSuper, true);
68 m_toolbar->setToggle(TBSub, true);
69 m_toolbar->insertSeparator();
70
71 TDEToolBarRadioGroup *group = new TDEToolBarRadioGroup(m_toolbar);
72 m_toolbar->insertButton("format-text-direction-ltr", TBLeft, true, i18n("Left Align"));
73 m_toolbar->setToggle(TBLeft, true);
74 group->addButton(TBLeft);
75 m_toolbar->insertButton("text_center", TBCenter, true, i18n("Centered"));
76 m_toolbar->setToggle(TBCenter, true);
77 group->addButton(TBCenter);
78 m_toolbar->insertButton("format-text-direction-rtl", TBRight, true, i18n("Right Align"));
79 m_toolbar->setToggle(TBRight, true);
80 group->addButton(TBRight);
81 m_toolbar->insertButton("text_block", TBJustify, true, i18n("Justified"));
82 m_toolbar->setToggle(TBJustify, true);
83 group->addButton(TBJustify);
84
85 connect(m_toolbar, TQ_SIGNAL(toggled(int)), this, TQ_SLOT(buttonToggled(int)));
86
87 m_edit = new KTextEdit(text, TQString(), frame, "richtext_edit");
88 m_edit->setTextFormat(RichText);
89 m_edit->setFocus();
90
91 connect(m_edit, TQ_SIGNAL(cursorPositionChanged(int, int)), this, TQ_SLOT(cursorPositionChanged(int, int)));
92 connect(m_edit, TQ_SIGNAL(clicked(int, int)), this, TQ_SLOT(cursorPositionChanged(int, int)));
93 connect(m_edit, TQ_SIGNAL(currentVerticalAlignmentChanged(VerticalAlignment)), this, TQ_SLOT(slotVerticalAlignmentChanged(VerticalAlignment)));
94
95 m_edit->moveCursor(TQTextEdit::MoveEnd, false);
96 cursorPositionChanged(0, 0);
97 m_edit->show();
98 frame->show();
99}
100
101TQString
102RichTextDialog::text()
103{
104 return m_edit->text();
105}
106
107void
108RichTextDialog::changeFont(const TQString &font)
109{
110 m_edit->setFamily(font);
111}
112
113void
114RichTextDialog::changeColor(const TQColor &color)
115{
116 m_edit->setColor(color);
117}
118
119void
120RichTextDialog::buttonToggled(int id)
121{
122 bool isOn = m_toolbar->isButtonOn(id);
123
124 switch(id)
125 {
126 case TBBold: m_edit->setBold(isOn); break;
127 case TBItalic: m_edit->setItalic(isOn); break;
128 case TBUnder: m_edit->setUnderline(isOn); break;
129 case TBSuper:
130 {
131 if(isOn && m_toolbar->isButtonOn(TBSub))
132 m_toolbar->setButton(TBSub, false);
133 m_edit->setVerticalAlignment(isOn ? TQTextEdit::AlignSuperScript : TQTextEdit::AlignNormal);
134 break;
135 }
136 case TBSub:
137 {
138 if(isOn && m_toolbar->isButtonOn(TBSuper))
139 m_toolbar->setButton(TBSuper, false);
140 m_edit->setVerticalAlignment(isOn ? TQTextEdit::AlignSubScript : TQTextEdit::AlignNormal);
141 break;
142 }
143 case TBLeft: case TBCenter:
144 case TBRight: case TBJustify:
145 {
146 if(!isOn) break;
147 switch(id)
148 {
149 case TBLeft: m_edit->setAlignment(TQt::AlignLeft); break;
150 case TBCenter: m_edit->setAlignment(TQt::AlignCenter); break;
151 case TBRight: m_edit->setAlignment(TQt::AlignRight); break;
152 case TBJustify: m_edit->setAlignment(TQt::AlignJustify); break;
153 default: break;
154 }
155 }
156 default: break;
157 }
158
159}
160
161void
162RichTextDialog::cursorPositionChanged(int, int)
163{
164 m_fcombo->setCurrentFont(m_edit->currentFont().family());
165 m_colCombo->setColor(m_edit->color());
166 m_toolbar->setButton(TBBold, m_edit->bold());
167 m_toolbar->setButton(TBItalic, m_edit->italic());
168 m_toolbar->setButton(TBUnder, m_edit->underline());
169
170 int id = 0;
171 switch(m_edit->alignment())
172 {
173 case TQt::AlignLeft: id = TBLeft; break;
174 case TQt::AlignCenter: id = TBCenter; break;
175 case TQt::AlignRight: id = TBRight; break;
176 case TQt::AlignJustify: id = TBJustify; break;
177 default: id = TBLeft; break;
178 }
179 m_toolbar->setButton(id, true);
180}
181
182void
183RichTextDialog::slotVerticalAlignmentChanged(VerticalAlignment align)
184{
185 switch(align)
186 {
187 case TQTextEdit::AlignSuperScript:
188 {
189 m_toolbar->setButton(TBSuper, true);
190 m_toolbar->setButton(TBSub, false);
191 break;
192 }
193 case TQTextEdit::AlignSubScript:
194 {
195 m_toolbar->setButton(TBSub, true);
196 m_toolbar->setButton(TBSuper, false);
197 break;
198 }
199 default:
200 {
201 m_toolbar->setButton(TBSuper, false);
202 m_toolbar->setButton(TBSub, false);
203 }
204 }
205}
206
207
208}
209
210#include "richtextdialog.moc"
type * findParent(TQObject *o, const char *className, TQObject *&prevPrev)