kexi

richtextdialog.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 #include <qlayout.h>
00020 
00021 #include <ktoolbar.h>
00022 #include <kfontcombo.h>
00023 #include <kcolorcombo.h>
00024 #include <ktoolbarradiogroup.h>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 
00028 #include "richtextdialog.h"
00029 
00030 namespace KFormDesigner {
00031 
00035 
00036 RichTextDialog::RichTextDialog(QWidget *parent, const QString &text)
00037 : KDialogBase(parent, "richtext_dialog", true, i18n("Edit Rich Text"), Ok|Cancel, Ok, false)
00038 {
00039     QFrame *frame = makeMainWidget();
00040     QVBoxLayout *l = new QVBoxLayout(frame);
00041     l->setAutoAdd(true);
00042 
00043     m_toolbar = new KToolBar(frame);
00044     m_toolbar->setFlat(true);
00045     m_toolbar->show();
00046 
00047     m_fcombo = new KFontCombo(m_toolbar);
00048     m_toolbar->insertWidget(TBFont, 40, m_fcombo);
00049     connect(m_fcombo, SIGNAL(textChanged(const QString&)), this, SLOT(changeFont(const QString &)));
00050 
00051     m_toolbar->insertSeparator();
00052 
00053     m_colCombo = new KColorCombo(m_toolbar);
00054     m_toolbar->insertWidget(TBColor, 30, m_colCombo);
00055     connect(m_colCombo, SIGNAL(activated(const QColor&)), this, SLOT(changeColor(const QColor&)));
00056 
00057     m_toolbar->insertButton("text_bold", TBBold, true, i18n("Bold"));
00058     m_toolbar->insertButton("text_italic", TBItalic, true, i18n("Italic"));
00059     m_toolbar->insertButton("text_under", TBUnder, true, i18n("Underline"));
00060     m_toolbar->setToggle(TBBold, true);
00061     m_toolbar->setToggle(TBItalic, true);
00062     m_toolbar->setToggle(TBUnder, true);
00063     m_toolbar->insertSeparator();
00064 
00065     m_toolbar->insertButton("text_super", TBSuper, true, i18n("Superscript"));
00066     m_toolbar->insertButton("text_sub", TBSub, true, i18n("Subscript"));
00067     m_toolbar->setToggle(TBSuper, true);
00068     m_toolbar->setToggle(TBSub, true);
00069     m_toolbar->insertSeparator();
00070 
00071     KToolBarRadioGroup *group = new KToolBarRadioGroup(m_toolbar);
00072     m_toolbar->insertButton("text_left", TBLeft, true, i18n("Left Align"));
00073     m_toolbar->setToggle(TBLeft, true);
00074     group->addButton(TBLeft);
00075     m_toolbar->insertButton("text_center", TBCenter, true, i18n("Centered"));
00076     m_toolbar->setToggle(TBCenter, true);
00077     group->addButton(TBCenter);
00078     m_toolbar->insertButton("text_right", TBRight, true, i18n("Right Align"));
00079     m_toolbar->setToggle(TBRight, true);
00080     group->addButton(TBRight);
00081     m_toolbar->insertButton("text_block", TBJustify, true, i18n("Justified"));
00082     m_toolbar->setToggle(TBJustify, true);
00083     group->addButton(TBJustify);
00084 
00085     connect(m_toolbar, SIGNAL(toggled(int)), this, SLOT(buttonToggled(int)));
00086 
00087     m_edit = new KTextEdit(text, QString::null, frame, "richtext_edit");
00088     m_edit->setTextFormat(RichText);
00089     m_edit->setFocus();
00090 
00091     connect(m_edit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(cursorPositionChanged(int, int)));
00092     connect(m_edit, SIGNAL(clicked(int, int)), this, SLOT(cursorPositionChanged(int, int)));
00093     connect(m_edit, SIGNAL(currentVerticalAlignmentChanged(VerticalAlignment)), this, SLOT(slotVerticalAlignmentChanged(VerticalAlignment)));
00094 
00095     m_edit->moveCursor(QTextEdit::MoveEnd, false);
00096     cursorPositionChanged(0, 0);
00097     m_edit->show();
00098     frame->show();
00099 }
00100 
00101 QString
00102 RichTextDialog::text()
00103 {
00104     return m_edit->text();
00105 }
00106 
00107 void
00108 RichTextDialog::changeFont(const QString &font)
00109 {
00110     m_edit->setFamily(font);
00111 }
00112 
00113 void
00114 RichTextDialog::changeColor(const QColor &color)
00115 {
00116     m_edit->setColor(color);
00117 }
00118 
00119 void
00120 RichTextDialog::buttonToggled(int id)
00121 {
00122     bool isOn = m_toolbar->isButtonOn(id);
00123 
00124     switch(id)
00125     {
00126         case TBBold: m_edit->setBold(isOn); break;
00127         case TBItalic: m_edit->setItalic(isOn); break;
00128         case TBUnder: m_edit->setUnderline(isOn); break;
00129         case TBSuper:
00130         {
00131             if(isOn && m_toolbar->isButtonOn(TBSub))
00132                 m_toolbar->setButton(TBSub, false);
00133             m_edit->setVerticalAlignment(isOn ? QTextEdit::AlignSuperScript : QTextEdit::AlignNormal);
00134             break;
00135         }
00136         case TBSub:
00137         {
00138             if(isOn && m_toolbar->isButtonOn(TBSuper))
00139                 m_toolbar->setButton(TBSuper, false);
00140             m_edit->setVerticalAlignment(isOn ? QTextEdit::AlignSubScript : QTextEdit::AlignNormal);
00141             break;
00142         }
00143         case TBLeft: case TBCenter:
00144         case TBRight: case TBJustify:
00145         {
00146             if(!isOn)  break;
00147             switch(id)
00148             {
00149                 case TBLeft:  m_edit->setAlignment(Qt::AlignLeft); break;
00150                 case TBCenter:  m_edit->setAlignment(Qt::AlignCenter); break;
00151                 case TBRight:  m_edit->setAlignment(Qt::AlignRight); break;
00152                 case TBJustify:  m_edit->setAlignment(Qt::AlignJustify); break;
00153                 default: break;
00154             }
00155         }
00156         default: break;
00157     }
00158 
00159 }
00160 
00161 void
00162 RichTextDialog::cursorPositionChanged(int, int)
00163 {
00164     m_fcombo->setCurrentFont(m_edit->currentFont().family());
00165     m_colCombo->setColor(m_edit->color());
00166     m_toolbar->setButton(TBBold, m_edit->bold());
00167     m_toolbar->setButton(TBItalic, m_edit->italic());
00168     m_toolbar->setButton(TBUnder, m_edit->underline());
00169 
00170     int id = 0;
00171     switch(m_edit->alignment())
00172     {
00173         case Qt::AlignLeft:    id = TBLeft; break;
00174         case Qt::AlignCenter:  id = TBCenter; break;
00175         case Qt::AlignRight:   id = TBRight; break;
00176         case Qt::AlignJustify: id = TBJustify; break;
00177         default:  id = TBLeft; break;
00178     }
00179     m_toolbar->setButton(id, true);
00180 }
00181 
00182 void
00183 RichTextDialog::slotVerticalAlignmentChanged(VerticalAlignment align)
00184 {
00185     switch(align)
00186     {
00187         case QTextEdit::AlignSuperScript:
00188         {
00189             m_toolbar->setButton(TBSuper, true);
00190             m_toolbar->setButton(TBSub, false);
00191             break;
00192         }
00193         case QTextEdit::AlignSubScript:
00194         {
00195             m_toolbar->setButton(TBSub, true);
00196             m_toolbar->setButton(TBSuper, false);
00197             break;
00198         }
00199         default:
00200         {
00201             m_toolbar->setButton(TBSuper, false);
00202             m_toolbar->setButton(TBSub, false);
00203         }
00204     }
00205 }
00206 
00207 
00208 }
00209 
00210 #include "richtextdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys