defaulteditor.cpp
1 
21 #include "defaulteditor.h"
22 #include "core.h"
23 
24 #include <kgenericfactory.h>
25 #include <tdeapplication.h>
26 #include <tdeaction.h>
27 #include <kiconloader.h>
28 #include <kdebug.h>
29 
30 #include <tdeaction.h>
31 #include <kcolordialog.h>
32 #include <tdefiledialog.h>
33 #include <kinstance.h>
34 #include <tdelocale.h>
35 #include <kstdaction.h>
36 #include <kprinter.h>
37 #include <kfinddialog.h>
38 #include <kfind.h>
39 #include <kreplacedialog.h>
40 #include <kreplace.h>
41 
42 #include <tqtextedit.h>
43 #include <tqwidget.h>
44 
45 typedef KGenericFactory<DefaultEditor> DefaultEditorFactory;
46 K_EXPORT_COMPONENT_FACTORY( libkomposer_defaulteditor,
47  DefaultEditorFactory( "komposer_defaulteditor" ) )
48 
49 DefaultEditor::DefaultEditor( TQObject *parent, const char *name, const TQStringList &args )
50  : Editor( parent, name, args ), m_textEdit( 0 )
51 {
52  setInstance( DefaultEditorFactory::instance() );
53 
54  m_textEdit = new TQTextEdit( 0 );
55 
56  createActions( actionCollection() );
57 
58  setXMLFile( "defaulteditorui.rc" );
59 }
60 
61 DefaultEditor::~DefaultEditor()
62 {
63 }
64 
65 
66 TQWidget*
67 DefaultEditor::widget()
68 {
69  return m_textEdit;
70 }
71 
72 TQString
73 DefaultEditor::text() const
74 {
75  return m_textEdit->text();
76 }
77 
78 void
79 DefaultEditor::setText( const TQString &text )
80 {
81  m_textEdit->setText( text );
82 }
83 
84 void
85 DefaultEditor::changeSignature( const TQString &sig )
86 {
87  TQString text = m_textEdit->text();
88 
89  int sigStart = text.findRev( "-- " );
90  TQString sigText = TQString( "-- \n%1" ).arg( sig );
91 
92  text.replace( sigStart, text.length(), sigText );
93 }
94 
95 void
96 DefaultEditor::createActions( TDEActionCollection *ac )
97 {
98  //
99  // File Actions
100  //
101  (void) KStdAction::open( this, TQ_SLOT(open()), ac );
102  (void) KStdAction::openRecent( this, TQ_SLOT(openURL(const KURL &)), ac );
103  (void) KStdAction::save( this, TQ_SLOT(save()), ac );
104  (void) KStdAction::saveAs( this, TQ_SLOT(saveAs()), ac );
105 
106  //
107  // Edit Actions
108  //
109  TDEAction *actionUndo = KStdAction::undo( m_textEdit, TQ_SLOT(undo()), ac );
110  actionUndo->setEnabled( false );
111  connect( m_textEdit, TQ_SIGNAL(undoAvailable(bool)),
112  actionUndo, TQ_SLOT(setEnabled(bool)) );
113 
114  TDEAction *actionRedo = KStdAction::redo( m_textEdit, TQ_SLOT(redo()), ac );
115  actionRedo->setEnabled( false );
116  connect( m_textEdit, TQ_SIGNAL(redoAvailable(bool)),
117  actionRedo, TQ_SLOT(setEnabled(bool)) );
118 
119  TDEAction *action_cut = KStdAction::cut( m_textEdit, TQ_SLOT(cut()), ac );
120  action_cut->setEnabled( false );
121  connect( m_textEdit, TQ_SIGNAL(copyAvailable(bool)),
122  action_cut, TQ_SLOT(setEnabled(bool)) );
123 
124  TDEAction *action_copy = KStdAction::copy( m_textEdit, TQ_SLOT(copy()), ac );
125  action_copy->setEnabled( false );
126  connect( m_textEdit, TQ_SIGNAL(copyAvailable(bool)),
127  action_copy, TQ_SLOT(setEnabled(bool)) );
128 
129  (void) KStdAction::print( this, TQ_SLOT(print()), ac );
130 
131  (void) KStdAction::paste( m_textEdit, TQ_SLOT(paste()), ac );
132  (void) new TDEAction( i18n( "C&lear" ), 0,
133  m_textEdit, TQ_SLOT(removeSelectedText()),
134  ac, "edit_clear" );
135 
136  (void) KStdAction::selectAll( m_textEdit, TQ_SLOT(selectAll()), ac );
137 
138  //
139  // View Actions
140  //
141  (void) KStdAction::zoomIn( m_textEdit, TQ_SLOT(zoomIn()), ac );
142  (void) KStdAction::zoomOut( m_textEdit, TQ_SLOT(zoomOut()), ac );
143 
144  //
145  // Character Formatting
146  //
147  m_actionBold = new TDEToggleAction( i18n("&Bold"), "format-text-bold", CTRL+Key_B,
148  ac, "format_bold" );
149  connect( m_actionBold, TQ_SIGNAL(toggled(bool)),
150  m_textEdit, TQ_SLOT(setBold(bool)) );
151 
152  m_actionItalic = new TDEToggleAction( i18n("&Italic"), "format-text-italic", CTRL+Key_I,
153  ac, "format_italic" );
154 
155  connect( m_actionItalic, TQ_SIGNAL(toggled(bool)),
156  m_textEdit, TQ_SLOT(setItalic(bool) ));
157 
158  m_actionUnderline = new TDEToggleAction( i18n("&Underline"), "format-text-underline", CTRL+Key_U,
159  ac, "format_underline" );
160 
161  connect( m_actionUnderline, TQ_SIGNAL(toggled(bool)),
162  m_textEdit, TQ_SLOT(setUnderline(bool)) );
163 
164  (void) new TDEAction( i18n("Text &Color..."), "colorpicker", 0,
165  this, TQ_SLOT(formatColor()),
166  ac, "format_color" );
167 
168  //
169  // Font
170  //
171  m_actionFont = new TDEFontAction( i18n("&Font"), 0,
172  ac, "format_font" );
173  connect( m_actionFont, TQ_SIGNAL(activated(const TQString &)),
174  m_textEdit, TQ_SLOT(setFamily(const TQString &)) );
175 
176 
177  m_actionFontSize = new TDEFontSizeAction( i18n("Font &Size"), 0,
178  ac, "format_font_size" );
179  connect( m_actionFontSize, TQ_SIGNAL(fontSizeChanged(int)),
180  m_textEdit, TQ_SLOT(setPointSize(int)) );
181 
182  //
183  // Alignment
184  //
185  m_actionAlignLeft = new TDEToggleAction( i18n("Align &Left"), "format-text-direction-ltr", 0,
186  ac, "format_align_left" );
187  connect( m_actionAlignLeft, TQ_SIGNAL(toggled(bool)),
188  this, TQ_SLOT(setAlignLeft(bool)) );
189 
190  m_actionAlignCenter = new TDEToggleAction( i18n("Align &Center"), "text_center", 0,
191  ac, "format_align_center" );
192  connect( m_actionAlignCenter, TQ_SIGNAL(toggled(bool)),
193  this, TQ_SLOT(setAlignCenter(bool)) );
194 
195  m_actionAlignRight = new TDEToggleAction( i18n("Align &Right"), "format-text-direction-rtl", 0,
196  ac, "format_align_right" );
197  connect( m_actionAlignRight, TQ_SIGNAL(toggled(bool)),
198  this, TQ_SLOT(setAlignRight(bool)) );
199 
200  m_actionAlignJustify = new TDEToggleAction( i18n("&Justify"), "text_block", 0,
201  ac, "format_align_justify" );
202  connect( m_actionAlignJustify, TQ_SIGNAL(toggled(bool)),
203  this, TQ_SLOT(setAlignJustify(bool)) );
204 
205  m_actionAlignLeft->setExclusiveGroup( "alignment" );
206  m_actionAlignCenter->setExclusiveGroup( "alignment" );
207  m_actionAlignRight->setExclusiveGroup( "alignment" );
208  m_actionAlignJustify->setExclusiveGroup( "alignment" );
209 
210  //
211  // Tools
212  //
213  (void) KStdAction::spelling( this, TQ_SLOT(checkSpelling()), ac );
214 
215  //
216  // Setup enable/disable
217  //
218  updateActions();
219 
220  connect( m_textEdit, TQ_SIGNAL(currentFontChanged(const TQFont &)),
221  this, TQ_SLOT( updateFont() ) );
222  connect( m_textEdit, TQ_SIGNAL(currentFontChanged(const TQFont &)),
223  this, TQ_SLOT(updateCharFmt()) );
224  connect( m_textEdit, TQ_SIGNAL(cursorPositionChanged(int, int)),
225  this, TQ_SLOT(updateAligment()) );
226 }
227 
228 void
229 DefaultEditor::updateActions()
230 {
231  updateCharFmt();
232  updateAligment();
233  updateFont();
234 }
235 
236 void
237 DefaultEditor::updateCharFmt()
238 {
239  m_actionBold->setChecked( m_textEdit->bold() );
240  m_actionItalic->setChecked( m_textEdit->italic() );
241  m_actionUnderline->setChecked( m_textEdit->underline() );
242 }
243 
244 void
245 DefaultEditor::updateAligment()
246 {
247  int align = m_textEdit->alignment();
248 
249  switch ( align ) {
250  case AlignRight:
251  m_actionAlignRight->setChecked( true );
252  break;
253  case AlignCenter:
254  m_actionAlignCenter->setChecked( true );
255  break;
256  case AlignLeft:
257  m_actionAlignLeft->setChecked( true );
258  break;
259  case AlignJustify:
260  m_actionAlignJustify->setChecked( true );
261  break;
262  default:
263  break;
264  }
265 }
266 
267 void
268 DefaultEditor::updateFont()
269 {
270  if ( m_textEdit->pointSize() > 0 )
271  m_actionFontSize->setFontSize( m_textEdit->pointSize() );
272  m_actionFont->setFont( m_textEdit->family() );
273 }
274 
275 void
276 DefaultEditor::formatColor()
277 {
278  TQColor col;
279 
280  int s = KColorDialog::getColor( col, m_textEdit->color(), m_textEdit );
281  if ( s != TQDialog::Accepted )
282  return;
283 
284  m_textEdit->setColor( col );
285 }
286 
287 void
288 DefaultEditor::setAlignLeft( bool yes )
289 {
290  if ( yes )
291  m_textEdit->setAlignment( AlignLeft );
292 }
293 
294 void
295 DefaultEditor::setAlignRight( bool yes )
296 {
297  if ( yes )
298  m_textEdit->setAlignment( AlignRight );
299 }
300 
301 void
302 DefaultEditor::setAlignCenter( bool yes )
303 {
304  if ( yes )
305  m_textEdit->setAlignment( AlignCenter );
306 }
307 
308 void
309 DefaultEditor::setAlignJustify( bool yes )
310 {
311  if ( yes )
312  m_textEdit->setAlignment( AlignJustify );
313 }
314 
315 //
316 // Content Actions
317 //
318 
319 bool
320 DefaultEditor::open()
321 {
322  KURL url = KFileDialog::getOpenURL();
323  if ( url.isEmpty() )
324  return false;
325 
326  //fixme
327  //return openURL( url );
328  return true;
329 }
330 
331 bool
332 DefaultEditor::saveAs()
333 {
334  KURL url = KFileDialog::getSaveURL();
335  if ( url.isEmpty() )
336  return false;
337 
338  //FIXME
339  //return KParts::ReadWritePart::saveAs( url );
340  return true;
341 }
342 
343 void
344 DefaultEditor::checkSpelling()
345 {
346  TQString s;
347  if ( m_textEdit->hasSelectedText() )
348  s = m_textEdit->selectedText();
349  else
350  s = m_textEdit->text();
351 
352  //KSpell::modalCheck( s );
353 }
354 
355 bool
356 DefaultEditor::print()
357 {
358  return true;
359 }
360 
361 #include "defaulteditor.moc"