File indexing completed on 2024-05-12 16:39:51
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "richtextdialog.h" 0021 0022 #include <KexiIcon.h> 0023 0024 #include <QAction> 0025 #include <QVBoxLayout> 0026 #include <QDialogButtonBox> 0027 #include <QPushButton> 0028 0029 #include <KToolBar> 0030 #include <KFontRequester> 0031 #include <KColorCombo> 0032 #include <KTextEdit> 0033 #include <KLocalizedString> 0034 0035 using namespace KFormDesigner; 0036 0037 ////////////////////////////////////////////////////////////////////////////////// 0038 //////////////// A simple dialog to edit rich text //////////////////////////// 0039 ////////////////////////////////////////////////////////////////////////////////// 0040 0041 class Q_DECL_HIDDEN RichTextDialog::Private 0042 { 0043 public: 0044 Private(); 0045 ~Private(); 0046 0047 QAction *fontComboAction, *colorComboAction, *boldTextAction, 0048 *italicTextAction, *underlineTextAction, 0049 *subscriptTextAction, *superscriptTextAction, 0050 *alignLeftAction, *alignRightAction, *alignCenterAction, *alignJustifyAction; 0051 QActionGroup* alignActionGroup; 0052 KToolBar *toolbar; 0053 KTextEdit *edit; 0054 KFontRequester *fontCombo; 0055 KColorCombo *colorCombo; 0056 }; 0057 0058 RichTextDialog::Private::Private() 0059 { 0060 0061 } 0062 0063 RichTextDialog::Private::~Private() 0064 { 0065 0066 } 0067 0068 RichTextDialog::RichTextDialog(QWidget *parent, const QString &text) 0069 : QDialog(parent), d(new Private()) 0070 { 0071 setObjectName("richtext_dialog"); 0072 setModal(true); 0073 setWindowTitle(xi18nc("@title:window", "Edit Rich Text")); 0074 0075 QVBoxLayout *mainLayout = new QVBoxLayout; 0076 setLayout(mainLayout); 0077 0078 d->toolbar = new KToolBar(this); 0079 mainLayout->addWidget(d->toolbar); 0080 0081 d->fontCombo = new KFontRequester(d->toolbar); 0082 d->fontComboAction = d->toolbar->addWidget(d->fontCombo); 0083 connect(d->fontCombo, SIGNAL(fontSelected(QFont)), 0084 this, SLOT(changeFont(QFont))); 0085 0086 d->toolbar->addSeparator(); 0087 0088 d->colorCombo = new KColorCombo(d->toolbar); 0089 d->colorComboAction = d->toolbar->addWidget(d->colorCombo); 0090 connect(d->colorCombo, SIGNAL(activated(QColor)), this, SLOT(changeColor(QColor))); 0091 0092 d->boldTextAction = d->toolbar->addAction(koIcon("format-text-bold"), xi18n("Bold")); 0093 d->boldTextAction->setCheckable(true); 0094 d->italicTextAction = d->toolbar->addAction(koIcon("format-text-italic"), xi18n("Italic")); 0095 d->italicTextAction->setCheckable(true); 0096 d->underlineTextAction = d->toolbar->addAction(koIcon("format-text-underline"), xi18n("Underline")); 0097 d->underlineTextAction->setCheckable(true); 0098 d->toolbar->addSeparator(); 0099 0100 d->superscriptTextAction = d->toolbar->addAction(koIcon("format-text-superscript"), xi18n("Superscript")); 0101 d->superscriptTextAction->setCheckable(true); 0102 d->subscriptTextAction = d->toolbar->addAction(koIcon("format-text-subscript"), xi18n("Subscript")); 0103 d->subscriptTextAction->setCheckable(true); 0104 d->toolbar->addSeparator(); 0105 0106 d->alignActionGroup = new QActionGroup(this); 0107 d->alignLeftAction = d->toolbar->addAction(koIcon("format-justify-left"), xi18n("Left Align")); 0108 d->alignLeftAction->setCheckable(true); 0109 d->alignActionGroup->addAction(d->alignLeftAction); 0110 d->alignCenterAction = d->toolbar->addAction(koIcon("format-justify-center"), xi18n("Centered")); 0111 d->alignCenterAction->setCheckable(true); 0112 d->alignActionGroup->addAction(d->alignCenterAction); 0113 d->alignRightAction = d->toolbar->addAction(koIcon("format-justify-right"), xi18n("Right Align")); 0114 d->alignRightAction->setCheckable(true); 0115 d->alignActionGroup->addAction(d->alignRightAction); 0116 d->alignJustifyAction = d->toolbar->addAction(koIcon("format-justify-fill"), xi18n("Justified")); 0117 d->alignJustifyAction->setCheckable(true); 0118 d->alignActionGroup->addAction(d->alignJustifyAction); 0119 0120 connect(d->toolbar, SIGNAL(actionTriggered(QAction*)), 0121 this, SLOT(slotActionTriggered(QAction*))); 0122 0123 d->edit = new KTextEdit(text); 0124 d->edit->setAcceptRichText(true); 0125 mainLayout->addWidget(d->edit); 0126 0127 connect(d->edit, SIGNAL(currentCharFormatChanged(QTextCharFormat)), 0128 this, SLOT(slotCurrentCharFormatChanged(QTextCharFormat))); 0129 0130 d->edit->moveCursor(QTextCursor::End); 0131 slotCurrentCharFormatChanged(d->edit->currentCharFormat()); 0132 d->edit->setFocus(); 0133 0134 // buttons 0135 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); 0136 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); 0137 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0138 okButton->setDefault(true); 0139 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0140 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); 0141 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); 0142 mainLayout->addWidget(buttonBox); 0143 } 0144 0145 RichTextDialog::~RichTextDialog() 0146 { 0147 delete d; 0148 } 0149 0150 QString 0151 RichTextDialog::text() const 0152 { 0153 return d->edit->toHtml(); 0154 } 0155 0156 void 0157 RichTextDialog::changeFont(const QFont &font) 0158 { 0159 d->edit->setCurrentFont(font); 0160 } 0161 0162 void 0163 RichTextDialog::changeColor(const QColor &color) 0164 { 0165 d->edit->setTextColor(color); 0166 } 0167 0168 void 0169 RichTextDialog::slotActionTriggered(QAction* action) 0170 { 0171 const bool isChecked = action->isChecked(); 0172 0173 if (action == d->boldTextAction) 0174 d->edit->setFontWeight(isChecked ? QFont::Bold : QFont::Normal); 0175 else if (action == d->italicTextAction) 0176 d->edit->setFontItalic(isChecked); 0177 else if (action == d->underlineTextAction) 0178 d->edit->setFontUnderline(isChecked); 0179 else if (action == d->superscriptTextAction) { 0180 if (isChecked && d->subscriptTextAction->isChecked()) { 0181 d->subscriptTextAction->setChecked(false); 0182 } 0183 QTextCharFormat currentCharFormat = d->edit->currentCharFormat(); 0184 currentCharFormat.setVerticalAlignment( 0185 isChecked ? QTextCharFormat::AlignSuperScript : QTextCharFormat::AlignNormal); 0186 d->edit->setCurrentCharFormat(currentCharFormat); 0187 } else if (action == d->subscriptTextAction) { 0188 if (isChecked && d->superscriptTextAction->isChecked()) { 0189 d->superscriptTextAction->setChecked(false); 0190 } 0191 QTextCharFormat currentCharFormat = d->edit->currentCharFormat(); 0192 currentCharFormat.setVerticalAlignment( 0193 isChecked ? QTextCharFormat::AlignSubScript : QTextCharFormat::AlignNormal); 0194 d->edit->setCurrentCharFormat(currentCharFormat); 0195 } else if (action == d->alignLeftAction) { 0196 if (isChecked) 0197 d->edit->setAlignment(Qt::AlignLeft); 0198 } else if (action == d->alignCenterAction) { 0199 if (isChecked) 0200 d->edit->setAlignment(Qt::AlignCenter); 0201 } else if (action == d->alignRightAction) { 0202 if (isChecked) 0203 d->edit->setAlignment(Qt::AlignRight); 0204 } else if (action == d->alignJustifyAction) { 0205 if (isChecked) 0206 d->edit->setAlignment(Qt::AlignJustify); 0207 } 0208 } 0209 0210 void 0211 RichTextDialog::slotCurrentCharFormatChanged(const QTextCharFormat& f) 0212 { 0213 d->superscriptTextAction->setChecked(f.verticalAlignment() == QTextCharFormat::AlignSuperScript); 0214 d->subscriptTextAction->setChecked(f.verticalAlignment() == QTextCharFormat::AlignSubScript); 0215 0216 switch (d->edit->alignment()) { 0217 case Qt::AlignLeft: 0218 d->alignLeftAction->setChecked(true); 0219 break; 0220 case Qt::AlignCenter: 0221 d->alignCenterAction->setChecked(true); 0222 break; 0223 case Qt::AlignRight: 0224 d->alignRightAction->setChecked(true); 0225 break; 0226 case Qt::AlignJustify: 0227 d->alignJustifyAction->setChecked(true); 0228 break; 0229 } 0230 0231 //! @todo add more formatting options (buttons) 0232 } 0233