00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "editorhelper.h"
00018 #include "global.h"
00019 #include "readwriteformbase.h"
00020 #include <qfontdatabase.h>
00021 #include <qcolordialog.h>
00022 #include <qaction.h>
00023 #include <qcolor.h>
00024 #include <qtoolbar.h>
00025 #include <qcombobox.h>
00026 #include <qapplication.h>
00027 #include <qlineedit.h>
00028 #include <qstylesheet.h>
00029 #include <qregexp.h>
00030 #include <qcolor.h>
00031
00032 EditorHelper::EditorHelper(ReadWriteFormBase *parent, QTextEdit *editor):
00033 QObject(parent), parent(parent), e(editor) {
00034 setupToolBar();
00035 setupEditorConnections();
00036 };
00037
00038 EditorHelper::~EditorHelper() {
00039 delete toolbar;
00040 };
00041
00042
00043
00044
00045
00046 void EditorHelper::setupEditorConnections() {
00047 connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
00048 this, SLOT( fontChanged( const QFont & ) ) );
00049 connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
00050 this, SLOT( colorChanged( const QColor & ) ) );
00051 connect( e, SIGNAL( currentAlignmentChanged( int ) ),
00052 this, SLOT( alignmentChanged( int ) ) );
00053 e->setTextFormat(Qt::RichText);
00054 e->styleSheet()->item("p")->setMargin(QStyleSheetItem::MarginVertical, 0);
00055
00056
00057 }
00058
00059 void EditorHelper::setupToolBar() {
00060 Q_ASSERT(parent);
00061 toolbar=new QToolBar(parent->mainWindow());
00062 toolbar->setLabel( "Edit Actions" );
00063
00064 QAction *a = new QAction("Undo", QIconSet(loadPixmap("editundo.png" )), "&Undo", CTRL + Key_Z, this, "editUndo" );
00065 connect( a, SIGNAL( activated() ), this, SLOT( editUndo() ) );
00066 a->addTo(toolbar );
00067
00068 a = new QAction("Redo", QIconSet( loadPixmap("editredo.png" )), "&Redo", CTRL + Key_Y, this, "editRedo" );
00069 connect( a, SIGNAL( activated() ), this, SLOT( editRedo() ) );
00070 a->addTo( toolbar );
00071
00072 comboStyle = new QComboBox( FALSE, toolbar );
00073 comboStyle->insertItem( "Standard" );
00074 comboStyle->insertItem( "Bullet List (Disc)" );
00075 comboStyle->insertItem( "Bullet List (Circle)" );
00076 comboStyle->insertItem( "Bullet List (Square)" );
00077 comboStyle->insertItem( "Ordered List (Decimal)" );
00078 comboStyle->insertItem( "Ordered List (Alpha lower)" );
00079 comboStyle->insertItem( "Ordered List (Alpha upper)" );
00080 connect( comboStyle, SIGNAL( activated( int ) ),
00081 this, SLOT( textStyle( int ) ) );
00082
00083 comboFont = new QComboBox( TRUE, toolbar );
00084 QFontDatabase db;
00085 comboFont->insertStringList( db.families() );
00086 connect( comboFont, SIGNAL( activated( const QString & ) ),
00087 this, SLOT( textFamily( const QString & ) ) );
00088 comboFont->lineEdit()->setText( QApplication::font().family() );
00089
00090 comboSize = new QComboBox( TRUE, toolbar );
00091 QValueList<int> sizes = db.standardSizes();
00092 QValueList<int>::Iterator it = sizes.begin();
00093 for ( ; it != sizes.end(); ++it )
00094 comboSize->insertItem( QString::number( *it ) );
00095
00096 connect( comboSize, SIGNAL( activated( const QString & ) ),
00097 this, SLOT( textSize( const QString & ) ) );
00098 comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
00099
00100 actionTextBold = new QAction( "Bold", QIconSet(loadPixmap("textbold.png" )), "&Bold", CTRL + Key_B, this, "textBold" );
00101 connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
00102 actionTextBold->addTo( toolbar );
00103 actionTextBold->setToggleAction( TRUE );
00104
00105 actionTextItalic = new QAction( "Italic", QIconSet(loadPixmap( "textitalic.png" )), "&Italic", CTRL + Key_I, this, "textItalic" );
00106 connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
00107 actionTextItalic->addTo( toolbar );
00108 actionTextItalic->setToggleAction( TRUE );
00109
00110 actionTextUnderline = new QAction( "Underline", QIconSet(loadPixmap("textunder.png" )), "&Underline", CTRL + Key_U, this, "textUnderline" );
00111 connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
00112 actionTextUnderline->addTo( toolbar );
00113 actionTextUnderline->setToggleAction( TRUE );
00114
00115 QActionGroup *grp = new QActionGroup( this );
00116 grp->setExclusive( TRUE );
00117 connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
00118
00119 actionAlignLeft = new QAction( "Left", QIconSet(loadPixmap("textleft.png")), "&Left", CTRL + Key_L, grp, "textLeft" );
00120 actionAlignLeft->addTo( toolbar );
00121 actionAlignLeft->setToggleAction( TRUE );
00122
00123 actionAlignCenter = new QAction("Center", QIconSet(loadPixmap("textcenter.png")), "C&enter", CTRL + Key_E, grp, "textCenter" );
00124 actionAlignCenter->addTo( toolbar );
00125 actionAlignCenter->setToggleAction( TRUE );
00126
00127 actionAlignRight = new QAction( "Right", QIconSet(loadPixmap("textright.png")), "&Right", CTRL + Key_R, grp, "textRight" );
00128 actionAlignRight->addTo( toolbar );
00129 actionAlignRight->setToggleAction( TRUE );
00130
00131 actionAlignJustify = new QAction( "Justify", QIconSet(loadPixmap("textjustify.png")), "&Justify", CTRL + Key_J, grp, "textjustify" );
00132 actionAlignJustify->addTo( toolbar );
00133 actionAlignJustify->setToggleAction( TRUE );
00134
00135 QPixmap pix( 16, 16 );
00136 pix.fill( black );
00137 actionTextColor = new QAction( "Color", pix, "&Color...", 0, this, "textColor" );
00138 connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
00139 actionTextColor->addTo( toolbar );
00140 }
00141
00142
00143 void EditorHelper::editUndo()
00144 {
00145 if ( !e )
00146 return;
00147 e->undo();
00148 }
00149
00150 void EditorHelper::editRedo()
00151 {
00152 if ( !e )
00153 return;
00154 e->redo();
00155 }
00156
00157 void EditorHelper::editCut()
00158 {
00159 if ( !e )
00160 return;
00161 e->cut();
00162 }
00163
00164 void EditorHelper::editCopy()
00165 {
00166 if ( !e )
00167 return;
00168 e->copy();
00169 }
00170
00171 void EditorHelper::editPaste()
00172 {
00173 if ( !e )
00174 return;
00175 e->paste();
00176 }
00177
00178 void EditorHelper::textBold()
00179 {
00180 if ( !e )
00181 return;
00182 e->setBold( actionTextBold->isOn() );
00183 }
00184
00185 void EditorHelper::textUnderline()
00186 {
00187 if ( !e )
00188 return;
00189 e->setUnderline( actionTextUnderline->isOn() );
00190 }
00191
00192 void EditorHelper::textItalic()
00193 {
00194 if ( !e )
00195 return;
00196 e->setItalic( actionTextItalic->isOn() );
00197 }
00198
00199 void EditorHelper::textFamily( const QString &f )
00200 {
00201 if ( !e )
00202 return;
00203 e->setFamily( f );
00204 e->viewport()->setFocus();
00205 }
00206
00207 void EditorHelper::textSize( const QString &p )
00208 {
00209 if ( !e )
00210 return;
00211 e->setPointSize( p.toInt() );
00212 e->viewport()->setFocus();
00213 }
00214
00215 void EditorHelper::textStyle( int i )
00216 {
00217 if ( !e )
00218 return;
00219 if ( i == 0 )
00220 e->setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc );
00221 else if ( i == 1 )
00222 e->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc );
00223 else if ( i == 2 )
00224 e->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListCircle );
00225 else if ( i == 3 )
00226 e->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListSquare );
00227 else if ( i == 4 )
00228 e->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDecimal );
00229 else if ( i == 5 )
00230 e->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListLowerAlpha );
00231 else if ( i == 6 )
00232 e->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListUpperAlpha );
00233 e->viewport()->setFocus();
00234 }
00235
00236 void EditorHelper::textColor()
00237 {
00238 if ( !e )
00239 return;
00240 QColor col = QColorDialog::getColor( e->color(), parent );
00241 if ( !col.isValid() )
00242 return;
00243 e->setColor( col );
00244 QPixmap pix( 16, 16 );
00245 pix.fill( black );
00246 actionTextColor->setIconSet( pix );
00247 }
00248
00249 void EditorHelper::textAlign( QAction *a )
00250 {
00251 if ( !e )
00252 return;
00253 if ( a == actionAlignLeft )
00254 e->setAlignment( AlignLeft );
00255 else if ( a == actionAlignCenter )
00256 e->setAlignment( AlignHCenter );
00257 else if ( a == actionAlignRight )
00258 e->setAlignment( AlignRight );
00259 else if ( a == actionAlignJustify )
00260 e->setAlignment( AlignJustify );
00261 }
00262
00263 void EditorHelper::fontChanged( const QFont &f )
00264 {
00265 comboFont->lineEdit()->setText( f.family() );
00266 comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
00267 actionTextBold->setOn( f.bold() );
00268 actionTextItalic->setOn( f.italic() );
00269 actionTextUnderline->setOn( f.underline() );
00270 }
00271
00272 void EditorHelper::colorChanged( const QColor &c )
00273 {
00274 QPixmap pix( 16, 16 );
00275 pix.fill( c );
00276 actionTextColor->setIconSet( pix );
00277 }
00278
00279 void EditorHelper::alignmentChanged( int a )
00280 {
00281 if ( ( a == AlignAuto ) || ( a & AlignLeft ))
00282 actionAlignLeft->setOn( TRUE );
00283 else if ( ( a & AlignHCenter ) )
00284 actionAlignCenter->setOn( TRUE );
00285 else if ( ( a & AlignRight ) )
00286 actionAlignRight->setOn( TRUE );
00287 else if ( ( a & AlignJustify ) )
00288 actionAlignJustify->setOn( TRUE );
00289 }