defaulteditor.cpp
00001 00021 #include "defaulteditor.h" 00022 #include "core.h" 00023 00024 #include <kgenericfactory.h> 00025 #include <tdeapplication.h> 00026 #include <tdeaction.h> 00027 #include <kiconloader.h> 00028 #include <kdebug.h> 00029 00030 #include <tdeaction.h> 00031 #include <kcolordialog.h> 00032 #include <tdefiledialog.h> 00033 #include <kinstance.h> 00034 #include <tdelocale.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 <tqtextedit.h> 00043 #include <tqwidget.h> 00044 00045 typedef KGenericFactory<DefaultEditor> DefaultEditorFactory; 00046 K_EXPORT_COMPONENT_FACTORY( libkomposer_defaulteditor, 00047 DefaultEditorFactory( "komposer_defaulteditor" ) ) 00048 00049 DefaultEditor::DefaultEditor( TQObject *parent, const char *name, const TQStringList &args ) 00050 : Editor( parent, name, args ), m_textEdit( 0 ) 00051 { 00052 setInstance( DefaultEditorFactory::instance() ); 00053 00054 m_textEdit = new TQTextEdit( 0 ); 00055 00056 createActions( actionCollection() ); 00057 00058 setXMLFile( "defaulteditorui.rc" ); 00059 } 00060 00061 DefaultEditor::~DefaultEditor() 00062 { 00063 } 00064 00065 00066 TQWidget* 00067 DefaultEditor::widget() 00068 { 00069 return m_textEdit; 00070 } 00071 00072 TQString 00073 DefaultEditor::text() const 00074 { 00075 return m_textEdit->text(); 00076 } 00077 00078 void 00079 DefaultEditor::setText( const TQString &text ) 00080 { 00081 m_textEdit->setText( text ); 00082 } 00083 00084 void 00085 DefaultEditor::changeSignature( const TQString &sig ) 00086 { 00087 TQString text = m_textEdit->text(); 00088 00089 int sigStart = text.findRev( "-- " ); 00090 TQString sigText = TQString( "-- \n%1" ).arg( sig ); 00091 00092 text.replace( sigStart, text.length(), sigText ); 00093 } 00094 00095 void 00096 DefaultEditor::createActions( TDEActionCollection *ac ) 00097 { 00098 // 00099 // File Actions 00100 // 00101 (void) KStdAction::open( this, TQT_SLOT(open()), ac ); 00102 (void) KStdAction::openRecent( this, TQT_SLOT(openURL(const KURL &)), ac ); 00103 (void) KStdAction::save( this, TQT_SLOT(save()), ac ); 00104 (void) KStdAction::saveAs( this, TQT_SLOT(saveAs()), ac ); 00105 00106 // 00107 // Edit Actions 00108 // 00109 TDEAction *actionUndo = KStdAction::undo( m_textEdit, TQT_SLOT(undo()), ac ); 00110 actionUndo->setEnabled( false ); 00111 connect( m_textEdit, TQT_SIGNAL(undoAvailable(bool)), 00112 actionUndo, TQT_SLOT(setEnabled(bool)) ); 00113 00114 TDEAction *actionRedo = KStdAction::redo( m_textEdit, TQT_SLOT(redo()), ac ); 00115 actionRedo->setEnabled( false ); 00116 connect( m_textEdit, TQT_SIGNAL(redoAvailable(bool)), 00117 actionRedo, TQT_SLOT(setEnabled(bool)) ); 00118 00119 TDEAction *action_cut = KStdAction::cut( m_textEdit, TQT_SLOT(cut()), ac ); 00120 action_cut->setEnabled( false ); 00121 connect( m_textEdit, TQT_SIGNAL(copyAvailable(bool)), 00122 action_cut, TQT_SLOT(setEnabled(bool)) ); 00123 00124 TDEAction *action_copy = KStdAction::copy( m_textEdit, TQT_SLOT(copy()), ac ); 00125 action_copy->setEnabled( false ); 00126 connect( m_textEdit, TQT_SIGNAL(copyAvailable(bool)), 00127 action_copy, TQT_SLOT(setEnabled(bool)) ); 00128 00129 (void) KStdAction::print( this, TQT_SLOT(print()), ac ); 00130 00131 (void) KStdAction::paste( m_textEdit, TQT_SLOT(paste()), ac ); 00132 (void) new TDEAction( i18n( "C&lear" ), 0, 00133 m_textEdit, TQT_SLOT(removeSelectedText()), 00134 ac, "edit_clear" ); 00135 00136 (void) KStdAction::selectAll( m_textEdit, TQT_SLOT(selectAll()), ac ); 00137 00138 // 00139 // View Actions 00140 // 00141 (void) KStdAction::zoomIn( m_textEdit, TQT_SLOT(zoomIn()), ac ); 00142 (void) KStdAction::zoomOut( m_textEdit, TQT_SLOT(zoomOut()), ac ); 00143 00144 // 00145 // Character Formatting 00146 // 00147 m_actionBold = new TDEToggleAction( i18n("&Bold"), "format-text-bold", CTRL+Key_B, 00148 ac, "format_bold" ); 00149 connect( m_actionBold, TQT_SIGNAL(toggled(bool)), 00150 m_textEdit, TQT_SLOT(setBold(bool)) ); 00151 00152 m_actionItalic = new TDEToggleAction( i18n("&Italic"), "format-text-italic", CTRL+Key_I, 00153 ac, "format_italic" ); 00154 00155 connect( m_actionItalic, TQT_SIGNAL(toggled(bool)), 00156 m_textEdit, TQT_SLOT(setItalic(bool) )); 00157 00158 m_actionUnderline = new TDEToggleAction( i18n("&Underline"), "format-text-underline", CTRL+Key_U, 00159 ac, "format_underline" ); 00160 00161 connect( m_actionUnderline, TQT_SIGNAL(toggled(bool)), 00162 m_textEdit, TQT_SLOT(setUnderline(bool)) ); 00163 00164 (void) new TDEAction( i18n("Text &Color..."), "colorpicker", 0, 00165 this, TQT_SLOT(formatColor()), 00166 ac, "format_color" ); 00167 00168 // 00169 // Font 00170 // 00171 m_actionFont = new TDEFontAction( i18n("&Font"), 0, 00172 ac, "format_font" ); 00173 connect( m_actionFont, TQT_SIGNAL(activated(const TQString &)), 00174 m_textEdit, TQT_SLOT(setFamily(const TQString &)) ); 00175 00176 00177 m_actionFontSize = new TDEFontSizeAction( i18n("Font &Size"), 0, 00178 ac, "format_font_size" ); 00179 connect( m_actionFontSize, TQT_SIGNAL(fontSizeChanged(int)), 00180 m_textEdit, TQT_SLOT(setPointSize(int)) ); 00181 00182 // 00183 // Alignment 00184 // 00185 m_actionAlignLeft = new TDEToggleAction( i18n("Align &Left"), "format-text-direction-ltr", 0, 00186 ac, "format_align_left" ); 00187 connect( m_actionAlignLeft, TQT_SIGNAL(toggled(bool)), 00188 this, TQT_SLOT(setAlignLeft(bool)) ); 00189 00190 m_actionAlignCenter = new TDEToggleAction( i18n("Align &Center"), "text_center", 0, 00191 ac, "format_align_center" ); 00192 connect( m_actionAlignCenter, TQT_SIGNAL(toggled(bool)), 00193 this, TQT_SLOT(setAlignCenter(bool)) ); 00194 00195 m_actionAlignRight = new TDEToggleAction( i18n("Align &Right"), "format-text-direction-rtl", 0, 00196 ac, "format_align_right" ); 00197 connect( m_actionAlignRight, TQT_SIGNAL(toggled(bool)), 00198 this, TQT_SLOT(setAlignRight(bool)) ); 00199 00200 m_actionAlignJustify = new TDEToggleAction( i18n("&Justify"), "text_block", 0, 00201 ac, "format_align_justify" ); 00202 connect( m_actionAlignJustify, TQT_SIGNAL(toggled(bool)), 00203 this, TQT_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 // Tools 00212 // 00213 (void) KStdAction::spelling( this, TQT_SLOT(checkSpelling()), ac ); 00214 00215 // 00216 // Setup enable/disable 00217 // 00218 updateActions(); 00219 00220 connect( m_textEdit, TQT_SIGNAL(currentFontChanged(const TQFont &)), 00221 this, TQT_SLOT( updateFont() ) ); 00222 connect( m_textEdit, TQT_SIGNAL(currentFontChanged(const TQFont &)), 00223 this, TQT_SLOT(updateCharFmt()) ); 00224 connect( m_textEdit, TQT_SIGNAL(cursorPositionChanged(int, int)), 00225 this, TQT_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 TQColor col; 00279 00280 int s = KColorDialog::getColor( col, m_textEdit->color(), m_textEdit ); 00281 if ( s != TQDialog::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 // Content Actions 00317 // 00318 00319 bool 00320 DefaultEditor::open() 00321 { 00322 KURL url = KFileDialog::getOpenURL(); 00323 if ( url.isEmpty() ) 00324 return false; 00325 00326 //fixme 00327 //return openURL( url ); 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 //FIXME 00339 //return KParts::ReadWritePart::saveAs( url ); 00340 return true; 00341 } 00342 00343 void 00344 DefaultEditor::checkSpelling() 00345 { 00346 TQString s; 00347 if ( m_textEdit->hasSelectedText() ) 00348 s = m_textEdit->selectedText(); 00349 else 00350 s = m_textEdit->text(); 00351 00352 //KSpell::modalCheck( s ); 00353 } 00354 00355 bool 00356 DefaultEditor::print() 00357 { 00358 return true; 00359 } 00360 00361 #include "defaulteditor.moc"