00001
00021 #include "defaulteditor.h"
00022 #include "core.h"
00023
00024 #include <kgenericfactory.h>
00025 #include <kapplication.h>
00026 #include <kaction.h>
00027 #include <kiconloader.h>
00028 #include <kdebug.h>
00029
00030 #include <kaction.h>
00031 #include <kcolordialog.h>
00032 #include <kfiledialog.h>
00033 #include <kinstance.h>
00034 #include <klocale.h>
00035 #include <kstdaction.h>
00036 #include <kprinter.h>
00037 #include <kfinddialog.h>
00038 #include <kfind.h>
00039 #include <kreplacedialog.h>
00040 #include <kreplace.h>
00041
00042 #include <qtextedit.h>
00043 #include <qwidget.h>
00044
00045 typedef KGenericFactory<DefaultEditor> DefaultEditorFactory;
00046 K_EXPORT_COMPONENT_FACTORY( libkomposer_defaulteditor,
00047 DefaultEditorFactory( "komposer_defaulteditor" ) )
00048
00049 DefaultEditor::DefaultEditor( QObject *parent, const char *name, const QStringList &args )
00050 : Editor( parent, name, args ), m_textEdit( 0 )
00051 {
00052 setInstance( DefaultEditorFactory::instance() );
00053
00054 m_textEdit = new QTextEdit( 0 );
00055
00056 createActions( actionCollection() );
00057
00058 setXMLFile( "defaulteditorui.rc" );
00059 }
00060
00061 DefaultEditor::~DefaultEditor()
00062 {
00063 }
00064
00065
00066 QWidget*
00067 DefaultEditor::widget()
00068 {
00069 return m_textEdit;
00070 }
00071
00072 QString
00073 DefaultEditor::text() const
00074 {
00075 return m_textEdit->text();
00076 }
00077
00078 void
00079 DefaultEditor::setText( const QString &text )
00080 {
00081 m_textEdit->setText( text );
00082 }
00083
00084 void
00085 DefaultEditor::changeSignature( const QString &sig )
00086 {
00087 QString text = m_textEdit->text();
00088
00089 int sigStart = text.findRev( "-- " );
00090 QString sigText = QString( "-- \n%1" ).arg( sig );
00091
00092 text.replace( sigStart, text.length(), sigText );
00093 }
00094
00095 void
00096 DefaultEditor::createActions( KActionCollection *ac )
00097 {
00098
00099
00100
00101 (void) KStdAction::open( this, SLOT(open()), ac );
00102 (void) KStdAction::openRecent( this, SLOT(openURL(const KURL &)), ac );
00103 (void) KStdAction::save( this, SLOT(save()), ac );
00104 (void) KStdAction::saveAs( this, SLOT(saveAs()), ac );
00105
00106
00107
00108
00109 KAction *actionUndo = KStdAction::undo( m_textEdit, SLOT(undo()), ac );
00110 actionUndo->setEnabled( false );
00111 connect( m_textEdit, SIGNAL(undoAvailable(bool)),
00112 actionUndo, SLOT(setEnabled(bool)) );
00113
00114 KAction *actionRedo = KStdAction::redo( m_textEdit, SLOT(redo()), ac );
00115 actionRedo->setEnabled( false );
00116 connect( m_textEdit, SIGNAL(redoAvailable(bool)),
00117 actionRedo, SLOT(setEnabled(bool)) );
00118
00119 KAction *action_cut = KStdAction::cut( m_textEdit, SLOT(cut()), ac );
00120 action_cut->setEnabled( false );
00121 connect( m_textEdit, SIGNAL(copyAvailable(bool)),
00122 action_cut, SLOT(setEnabled(bool)) );
00123
00124 KAction *action_copy = KStdAction::copy( m_textEdit, SLOT(copy()), ac );
00125 action_copy->setEnabled( false );
00126 connect( m_textEdit, SIGNAL(copyAvailable(bool)),
00127 action_copy, SLOT(setEnabled(bool)) );
00128
00129 (void) KStdAction::print( this, SLOT(print()), ac );
00130
00131 (void) KStdAction::paste( m_textEdit, SLOT(paste()), ac );
00132 (void) new KAction( i18n( "C&lear" ), 0,
00133 m_textEdit, SLOT(removeSelectedText()),
00134 ac, "edit_clear" );
00135
00136 (void) KStdAction::selectAll( m_textEdit, SLOT(selectAll()), ac );
00137
00138
00139
00140
00141 (void) KStdAction::zoomIn( m_textEdit, SLOT(zoomIn()), ac );
00142 (void) KStdAction::zoomOut( m_textEdit, SLOT(zoomOut()), ac );
00143
00144
00145
00146
00147 m_actionBold = new KToggleAction( i18n("&Bold"), "text_bold", CTRL+Key_B,
00148 ac, "format_bold" );
00149 connect( m_actionBold, SIGNAL(toggled(bool)),
00150 m_textEdit, SLOT(setBold(bool)) );
00151
00152 m_actionItalic = new KToggleAction( i18n("&Italic"), "text_italic", CTRL+Key_I,
00153 ac, "format_italic" );
00154
00155 connect( m_actionItalic, SIGNAL(toggled(bool)),
00156 m_textEdit, SLOT(setItalic(bool) ));
00157
00158 m_actionUnderline = new KToggleAction( i18n("&Underline"), "text_under", CTRL+Key_U,
00159 ac, "format_underline" );
00160
00161 connect( m_actionUnderline, SIGNAL(toggled(bool)),
00162 m_textEdit, SLOT(setUnderline(bool)) );
00163
00164 (void) new KAction( i18n("Text &Color..."), "colorpicker", 0,
00165 this, SLOT(formatColor()),
00166 ac, "format_color" );
00167
00168
00169
00170
00171 m_actionFont = new KFontAction( i18n("&Font"), 0,
00172 ac, "format_font" );
00173 connect( m_actionFont, SIGNAL(activated(const QString &)),
00174 m_textEdit, SLOT(setFamily(const QString &)) );
00175
00176
00177 m_actionFontSize = new KFontSizeAction( i18n("Font &Size"), 0,
00178 ac, "format_font_size" );
00179 connect( m_actionFontSize, SIGNAL(fontSizeChanged(int)),
00180 m_textEdit, SLOT(setPointSize(int)) );
00181
00182
00183
00184
00185 m_actionAlignLeft = new KToggleAction( i18n("Align &Left"), "text_left", 0,
00186 ac, "format_align_left" );
00187 connect( m_actionAlignLeft, SIGNAL(toggled(bool)),
00188 this, SLOT(setAlignLeft(bool)) );
00189
00190 m_actionAlignCenter = new KToggleAction( i18n("Align &Center"), "text_center", 0,
00191 ac, "format_align_center" );
00192 connect( m_actionAlignCenter, SIGNAL(toggled(bool)),
00193 this, SLOT(setAlignCenter(bool)) );
00194
00195 m_actionAlignRight = new KToggleAction( i18n("Align &Right"), "text_right", 0,
00196 ac, "format_align_right" );
00197 connect( m_actionAlignRight, SIGNAL(toggled(bool)),
00198 this, SLOT(setAlignRight(bool)) );
00199
00200 m_actionAlignJustify = new KToggleAction( i18n("&Justify"), "text_block", 0,
00201 ac, "format_align_justify" );
00202 connect( m_actionAlignJustify, SIGNAL(toggled(bool)),
00203 this, SLOT(setAlignJustify(bool)) );
00204
00205 m_actionAlignLeft->setExclusiveGroup( "alignment" );
00206 m_actionAlignCenter->setExclusiveGroup( "alignment" );
00207 m_actionAlignRight->setExclusiveGroup( "alignment" );
00208 m_actionAlignJustify->setExclusiveGroup( "alignment" );
00209
00210
00211
00212
00213 (void) KStdAction::spelling( this, SLOT(checkSpelling()), ac );
00214
00215
00216
00217
00218 updateActions();
00219
00220 connect( m_textEdit, SIGNAL(currentFontChanged(const QFont &)),
00221 this, SLOT( updateFont() ) );
00222 connect( m_textEdit, SIGNAL(currentFontChanged(const QFont &)),
00223 this, SLOT(updateCharFmt()) );
00224 connect( m_textEdit, SIGNAL(cursorPositionChanged(int, int)),
00225 this, SLOT(updateAligment()) );
00226 }
00227
00228 void
00229 DefaultEditor::updateActions()
00230 {
00231 updateCharFmt();
00232 updateAligment();
00233 updateFont();
00234 }
00235
00236 void
00237 DefaultEditor::updateCharFmt()
00238 {
00239 m_actionBold->setChecked( m_textEdit->bold() );
00240 m_actionItalic->setChecked( m_textEdit->italic() );
00241 m_actionUnderline->setChecked( m_textEdit->underline() );
00242 }
00243
00244 void
00245 DefaultEditor::updateAligment()
00246 {
00247 int align = m_textEdit->alignment();
00248
00249 switch ( align ) {
00250 case AlignRight:
00251 m_actionAlignRight->setChecked( true );
00252 break;
00253 case AlignCenter:
00254 m_actionAlignCenter->setChecked( true );
00255 break;
00256 case AlignLeft:
00257 m_actionAlignLeft->setChecked( true );
00258 break;
00259 case AlignJustify:
00260 m_actionAlignJustify->setChecked( true );
00261 break;
00262 default:
00263 break;
00264 }
00265 }
00266
00267 void
00268 DefaultEditor::updateFont()
00269 {
00270 if ( m_textEdit->pointSize() > 0 )
00271 m_actionFontSize->setFontSize( m_textEdit->pointSize() );
00272 m_actionFont->setFont( m_textEdit->family() );
00273 }
00274
00275 void
00276 DefaultEditor::formatColor()
00277 {
00278 QColor col;
00279
00280 int s = KColorDialog::getColor( col, m_textEdit->color(), m_textEdit );
00281 if ( s != QDialog::Accepted )
00282 return;
00283
00284 m_textEdit->setColor( col );
00285 }
00286
00287 void
00288 DefaultEditor::setAlignLeft( bool yes )
00289 {
00290 if ( yes )
00291 m_textEdit->setAlignment( AlignLeft );
00292 }
00293
00294 void
00295 DefaultEditor::setAlignRight( bool yes )
00296 {
00297 if ( yes )
00298 m_textEdit->setAlignment( AlignRight );
00299 }
00300
00301 void
00302 DefaultEditor::setAlignCenter( bool yes )
00303 {
00304 if ( yes )
00305 m_textEdit->setAlignment( AlignCenter );
00306 }
00307
00308 void
00309 DefaultEditor::setAlignJustify( bool yes )
00310 {
00311 if ( yes )
00312 m_textEdit->setAlignment( AlignJustify );
00313 }
00314
00315
00316
00317
00318
00319 bool
00320 DefaultEditor::open()
00321 {
00322 KURL url = KFileDialog::getOpenURL();
00323 if ( url.isEmpty() )
00324 return false;
00325
00326
00327
00328 return true;
00329 }
00330
00331 bool
00332 DefaultEditor::saveAs()
00333 {
00334 KURL url = KFileDialog::getSaveURL();
00335 if ( url.isEmpty() )
00336 return false;
00337
00338
00339
00340 return true;
00341 }
00342
00343 void
00344 DefaultEditor::checkSpelling()
00345 {
00346 QString s;
00347 if ( m_textEdit->hasSelectedText() )
00348 s = m_textEdit->selectedText();
00349 else
00350 s = m_textEdit->text();
00351
00352
00353 }
00354
00355 bool
00356 DefaultEditor::print()
00357 {
00358 return true;
00359 }
00360
00361 #include "defaulteditor.moc"