texteditor.cpp
author insilmaril
Fri, 08 Jul 2005 07:24:43 +0000
changeset 123 09edde7ae30e
parent 106 4083860dd82e
child 124 e0f6a21cc6d6
permissions -rw-r--r--
changes for 1.6.9
     1 #include "texteditor.h"
     2 
     3 #include <qcanvas.h>
     4 #include <qstatusbar.h>
     5 #include <qmessagebox.h>
     6 #include <qaction.h>
     7 #include <qapplication.h>
     8 #include <qpainter.h>
     9 #include <qprinter.h>
    10 #include <qfile.h>
    11 #include <qfiledialog.h>
    12 #include <qtoolbar.h>
    13 #include <qpopupmenu.h>
    14 #include <qmenubar.h>
    15 #include <qtextedit.h>
    16 #include <qaccel.h>
    17 #include <qtextstream.h>
    18 #include <qpaintdevicemetrics.h>
    19 #include <qsettings.h>
    20 #include <qfontdialog.h>
    21 #include <qmessagebox.h>
    22 #include <qcolordialog.h>
    23 #include <qregexp.h>
    24 #include <qlineedit.h>
    25 #include <qsimplerichtext.h>
    26 
    27 #include <iostream>
    28 #include <cstdlib>
    29 #include <typeinfo>
    30 
    31 #include "icons/fileopen.xpm"
    32 #include "icons/filesave.xpm"
    33 #include "icons/fileprint.xpm"
    34 #include "icons/editundo.xpm"	
    35 #include "icons/editredo.xpm"	
    36 #include "icons/editcopy.xpm"
    37 #include "icons/editcut.xpm"
    38 #include "icons/editpaste.xpm"
    39 #include "icons/edittrash.xpm"
    40 #include "icons/formatfixedfont.xpm"
    41 #include "icons/formattextbold.xpm"
    42 #include "icons/formattextitalic.xpm"
    43 #include "icons/formattextunder.xpm"
    44 #include "icons/formattextleft.xpm"
    45 #include "icons/formattextcenter.xpm"
    46 #include "icons/formattextright.xpm"
    47 #include "icons/formattextjustify.xpm"
    48 
    49 extern QCanvas* actCanvas;
    50 extern int statusbarTime;
    51 extern QSettings settings;
    52 
    53 extern QAction *actionViewToggleNoteEditor;
    54 
    55 using namespace std;
    56 
    57 
    58 ///////////////////////////////////////////////////////////////////////
    59 ///////////////////////////////////////////////////////////////////////
    60 
    61 TextEditor::TextEditor()
    62 {
    63     printer = new QPrinter( QPrinter::HighResolution );
    64 	printer->setPrinterName (settings.readEntry("/vym/mainwindow/printerName",printer->printerName()));
    65 
    66 	// Editor
    67     e = new QTextEdit( this, "editor" );
    68     e->setFocus();
    69 	e->setTextFormat(RichText);		// default
    70 	e->setTabStopWidth (20);		// unit is pixel
    71 	e->setColor (black);
    72 	connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
    73     setCentralWidget( e );
    74     statusBar()->message( "Ready", statusbarTime);
    75 	setCaption("VYM - Note Editor");
    76 
    77 	// Toolbars
    78 	setupFileActions();
    79 	setupEditActions();
    80 	setupFormatActions();
    81 	setupSettingsActions();
    82 	
    83 	// Various states
    84 	emptyPaper   = QBrush(gray);
    85 	filledPaper  = QBrush(white);
    86 	inactivePaper= QBrush(black);
    87 	setInactive();
    88 
    89 	// Load Settings
    90 	resize (settings.readNumEntry( "/vym/noteeditor/geometry/width", 450),
    91 	        settings.readNumEntry( "/vym/noteeditor/geometry/height",600));
    92 	move   (settings.readNumEntry( "/vym/noteeditor/geometry/posX", 150),
    93 	        settings.readNumEntry( "/vym/noteeditor/geometry/posY",  50));
    94 	
    95 	if (settings.readEntry( "/vym/noteeditor/showWithMain","yes") =="yes")
    96 		setShowWithMain(true);
    97 	else	
    98 		setShowWithMain(false);
    99 
   100 	varFont.fromString( settings.readEntry 
   101 		("/vym/noteeditor/fonts/varFont",
   102 		"Nimbus Sans l,14,-1,5,48,0,0,0,0,0") 
   103 	);
   104 	fixedFont.fromString (settings.readEntry (
   105 		"/vym/noteeditor/fonts/fixedFont",
   106 		"Courier,14,-1,5,48,0,0,0,1,0") 
   107 	);
   108 	QString s=settings.readEntry ("/vym/noteeditor/fonts/fonthintDefault","variable");
   109 	if (s == "fixed")
   110 	{	
   111 		actionSettingsFonthintDefault->setOn (true);
   112 		e->setCurrentFont (fixedFont);
   113 	} else	
   114 	{
   115 		actionSettingsFonthintDefault->setOn (false);
   116 		e->setCurrentFont (varFont);
   117 	}	
   118 <<<<<<< texteditor.cpp
   119 	filenameHint="";
   120 
   121 	// Save settings in vymrc
   122 	settings.writeEntry("/vym/mainwindow/printerName",printer->printerName());
   123 =======
   124 	filenameHint="";
   125 >>>>>>> 1.4
   126 }
   127 
   128 
   129 TextEditor::~TextEditor()
   130 {
   131     if (printer) delete printer;
   132 	// Save Settings
   133 	settings.writeEntry( "/vym/noteeditor/geometry/width", width() );
   134 	settings.writeEntry( "/vym/noteeditor/geometry/height", height() );
   135 	settings.writeEntry( "/vym/noteeditor/geometry/posX", pos().x() );
   136 	settings.writeEntry( "/vym/noteeditor/geometry/posY", pos().y() );
   137 	
   138 	if (showWithMain())
   139 		settings.writeEntry( "/vym/noteeditor/showWithMain","yes");
   140 	else	
   141 		settings.writeEntry( "/vym/noteeditor/showWithMain","no");
   142 
   143 	QString s;
   144 	if (actionSettingsFonthintDefault->isOn() )
   145 		s="fixed";
   146 	else	
   147 		s="variable";
   148 	settings.writeEntry( "/vym/noteeditor/fonts/fonthintDefault",s );
   149 	settings.writeEntry ("/vym/noteeditor/fonts/varFont",
   150 		varFont.toString() );
   151 	settings.writeEntry ("/vym/noteeditor/fonts/fixedFont",
   152 		fixedFont.toString() );
   153 }
   154 
   155 bool TextEditor::isEmpty()
   156 {
   157 	if (e->text().length())
   158 		return false;
   159 	else
   160 		return true;
   161 }
   162 
   163 void TextEditor::setShowWithMain(bool v)
   164 {
   165 	showwithmain=v;
   166 }
   167 
   168 bool TextEditor::showWithMain()
   169 {
   170 	return showwithmain;
   171 }
   172 
   173 void TextEditor::setFontHint (const QString &fh)
   174 {
   175 	if (fh=="fixed")
   176 		actionFormatUseFixedFont->setOn (true);
   177 	else
   178 		actionFormatUseFixedFont->setOn (false);
   179 }
   180 
   181 
   182 QString TextEditor::getFontHint()
   183 {
   184 	if (actionFormatUseFixedFont->isOn())
   185 		return "fixed";
   186 	else	
   187 		return "var";
   188 }
   189 
   190 QString TextEditor::getFontHintDefault()
   191 {
   192 	if (actionSettingsFonthintDefault->isOn())
   193 		return "fixed";
   194 	else	
   195 		return "var";
   196 }
   197 
   198 void TextEditor::setFilename(const QString &fn)
   199 {
   200 	if (state==filledEditor)
   201 		if (fn.isEmpty() )
   202 		{
   203 			filename="";
   204 			statusBar()->message( "No filename available for this note.", statusbarTime );
   205 		}	
   206 		else
   207 		{
   208 			filename=fn;
   209 			statusBar()->message( QString( "Current filename is %1" ).arg( filename ), statusbarTime );
   210 		}	
   211 }
   212 
   213 QString TextEditor::getFilename()
   214 {
   215 	return filename;
   216 }
   217 
   218 void TextEditor::setFilenameHint(const QString &fnh)
   219 {
   220 	filenameHint=fnh;
   221 }
   222 
   223 QString TextEditor::getFilenameHint()
   224 {
   225 	return filenameHint;
   226 }
   227 
   228 bool TextEditor::findText(const QString &t, const bool &cs)
   229 {
   230 	bool wo=false;	// word matches
   231 	if (e->find (t, cs, wo, true, 0, 0 ))
   232 		return true;
   233 	else	
   234 		return false;
   235 }
   236 
   237 void TextEditor::setupFileActions()
   238 {
   239     QToolBar *tb = new QToolBar( this );
   240     tb->setLabel( "File Actions" );
   241     QPopupMenu *menu = new QPopupMenu( this );
   242     menuBar()->insertItem( tr( "&File" ), menu );
   243 
   244     QAction *a;
   245     a = new QAction( tr( "Import" ), QPixmap( fileopen_xpm), tr( "&Import..." ), CTRL + Key_O, this, "fileImport" );
   246     connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) );
   247 	a->setEnabled(false);
   248     a->addTo( tb );
   249     a->addTo( menu );
   250 	actionFileLoad=a;
   251 
   252     menu->insertSeparator();
   253     a = new QAction( tr( "Export Note (HTML)" ), QPixmap( filesave_xpm ), tr( "&Export..." ), CTRL + Key_S, this, "fileSave" );
   254     connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) );
   255     a->addTo( tb );
   256     a->addTo( menu );
   257 	actionFileSave=a;
   258 	
   259     a = new QAction( tr( "Export Note As (HTML) " ), QPixmap(), tr( "Export &As... (HTML)" ), 0, this, "exportHTML" );
   260     connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) );
   261     a->addTo( menu );
   262 	actionFileSaveAs=a;
   263 
   264     a = new QAction( tr( "Export Note As (ASCII) " ), QPixmap(), tr( "Export &As...(ASCII)" ), ALT + Key_X, this, "exportASCII" );
   265     connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) );
   266     a->addTo( menu );
   267 	actionFileSaveAs=a;
   268 
   269     menu->insertSeparator();
   270     a = new QAction( tr( "Print Note" ), QPixmap( fileprint_xpm ), tr( "&Print..." ), CTRL + Key_P, this, "filePrint" );
   271     connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) );
   272     a->addTo( tb );
   273     a->addTo( menu );
   274 	actionFilePrint=a;
   275 }
   276 
   277 void TextEditor::setupEditActions()
   278 {
   279     QToolBar *tb = new QToolBar( this );
   280     tb->setLabel( "Edit Actions" );
   281     QPopupMenu *menu = new QPopupMenu( this );
   282     menuBar()->insertItem( tr( "&Edit" ), menu );
   283 
   284     QAction *a;
   285     a = new QAction( tr( "Undo" ), QPixmap(editundo_xpm), tr( "&Undo" ), CTRL + Key_Z, this, "undoEvent" );
   286     connect( a, SIGNAL( activated() ), e, SLOT( undo() ) );
   287     a->addTo( menu );
   288     a->addTo( tb);
   289 	actionEditUndo=a;
   290 	
   291     a = new QAction( tr( "Redo" ), QPixmap( editredo_xpm ), tr( "&Redo" ), CTRL + Key_Y, this, "editRedo" ); 
   292     connect( a, SIGNAL( activated() ), e, SLOT( redo() ) );
   293     a->addTo( tb );
   294     a->addTo( menu );
   295 	actionEditRedo=a;
   296 
   297     menu->insertSeparator();
   298     a = new QAction( tr( "Select and copy all" ), QPixmap(), tr( "Select and copy &all" ), CTRL + Key_A, this, "editcopyall" ); 
   299     connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) );
   300     a->addTo( menu );
   301 
   302     menu->insertSeparator();
   303     a = new QAction( tr( "Copy" ), QPixmap( editcopy_xpm ), tr( "&Copy" ), CTRL + Key_C, this, "editCopy" );
   304     connect( a, SIGNAL( activated() ), e, SLOT( copy() ) );
   305     a->addTo( tb );
   306     a->addTo( menu );
   307 	actionEditCopy=a;
   308 	
   309     a = new QAction( tr( "Cut" ), QPixmap( editcut_xpm ), tr( "Cu&t" ), CTRL + Key_X, this, "editCut" );
   310     connect( a, SIGNAL( activated() ), e, SLOT( cut() ) );
   311     a->addTo( tb );
   312     a->addTo( menu );
   313 	actionEditCut=a;
   314 
   315     a = new QAction( tr( "Paste" ), QPixmap( editpaste_xpm ), tr( "&Paste" ), CTRL + Key_V, this, "editPaste" );
   316     connect( a, SIGNAL( activated() ), e, SLOT( paste() ) );
   317     a->addTo( tb );
   318     a->addTo( menu );
   319 	actionEditPaste=a;
   320 	
   321     a = new QAction( tr( "Delete all" ), QPixmap( edittrash_xpm ), tr( "&Delete All" ), 0, this, "editDeleteAll" );
   322     connect( a, SIGNAL( activated() ), e, SLOT( clear() ) );
   323     a->addTo( tb );
   324     a->addTo( menu );
   325 	actionEditDeleteAll=a;
   326 
   327 	a = new QAction( tr( "Convert paragraphs to linebreaks" ), QPixmap(), tr( "&Convert Paragraphs" ), ALT + Key_P, this, "editConvertPar" );
   328     connect( a, SIGNAL( activated() ), this, SLOT( textConvertPar() ) );
   329     a->addTo( menu );
   330 	actionEditConvertPar=a;
   331 
   332 	a = new QAction( tr( "Join all lines of a paragraph" ), QPixmap(), tr( "&Join lines" ), ALT + Key_J, this, "editJoinLines" );
   333     connect( a, SIGNAL( activated() ), this, SLOT( textJoinLines() ) );
   334     a->addTo( menu );
   335 	actionEditJoinLines=a;
   336 }
   337 
   338 void TextEditor::setupFormatActions()
   339 {
   340     QToolBar *tb = new QToolBar( this );
   341     tb->setLabel( "Format Actions" );
   342     QPopupMenu *menu = new QPopupMenu( this );
   343     menuBar()->insertItem( tr( "&Format" ), menu );
   344 
   345     QAction *a;
   346 
   347     a = new QAction( tr( "Toggle font hint for the whole text" ), QPixmap(formatfixedfont_xpm), tr( "&Font hint" ), ALT + Key_I, this, "fontHint" );
   348 	a->setToggleAction (true);
   349 	a->setOn (settings.readBoolEntry ("/vym/noteeditor/fonts/useFixedByDefault",false) );
   350     connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
   351     a->addTo( menu );
   352     a->addTo( tb );
   353 	actionFormatUseFixedFont=a;
   354 
   355 	menu->insertSeparator();
   356 
   357     comboFont = new QComboBox( true, tb );
   358     QFontDatabase db;
   359     comboFont->insertStringList( db.families() );
   360     connect( comboFont, SIGNAL( activated( const QString & ) ),
   361 	     this, SLOT( textFamily( const QString & ) ) );
   362     comboFont->lineEdit()->setText( QApplication::font().family() );
   363 
   364     comboSize = new QComboBox( true, tb );
   365     QValueList<int> sizes = db.standardSizes();
   366     QValueList<int>::Iterator it = sizes.begin();
   367     for ( ; it != sizes.end(); ++it )
   368 	comboSize->insertItem( QString::number( *it ) );
   369     connect( comboSize, SIGNAL( activated( const QString & ) ),
   370 	     this, SLOT( textSize( const QString & ) ) );
   371     comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
   372 
   373     menu->insertSeparator();
   374 
   375     QPixmap pix( 16, 16 );
   376     pix.fill( e->color());
   377     actionTextColor = new QAction( pix, tr( "&Color..." ), 0, this, "textColor" );
   378     connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
   379     actionTextColor->addTo( tb );
   380     actionTextColor->addTo( menu );
   381 
   382     actionTextBold = new QAction( QPixmap (formattextbold_xpm), tr( "&Bold" ), CTRL + Key_B, this, "textBold" );
   383     connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
   384     actionTextBold->addTo( tb );
   385     actionTextBold->addTo( menu );
   386     actionTextBold->setToggleAction( true );
   387     actionTextItalic = new QAction( QPixmap(formattextitalic_xpm ), tr( "&Italic" ), CTRL + Key_I, this, "textItalic" );
   388     connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
   389     actionTextItalic->addTo( tb );
   390     actionTextItalic->addTo( menu );
   391     actionTextItalic->setToggleAction( true );
   392     actionTextUnderline = new QAction( QPixmap (formattextunder_xpm ), tr( "&Underline" ), CTRL + Key_U, this, "textUnderline" );
   393     connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
   394     actionTextUnderline->addTo( tb );
   395     actionTextUnderline->addTo( menu );
   396     actionTextUnderline->setToggleAction( true );
   397     menu->insertSeparator();
   398 
   399     QActionGroup *grp = new QActionGroup( this );
   400     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
   401 
   402     actionAlignLeft = new QAction( QPixmap (formattextleft_xpm ), tr( "&Left" ), CTRL + Key_L, grp, "textLeft" );
   403     actionAlignLeft->setToggleAction( true );
   404     actionAlignCenter = new QAction( QPixmap (formattextcenter_xpm ), tr( "C&enter" ), CTRL + Key_E, grp, "textCenter" );
   405     actionAlignCenter->setToggleAction( true );
   406     actionAlignRight = new QAction( QPixmap (formattextright_xpm ), tr( "&Right" ), CTRL + Key_R, grp, "textRight" );
   407     actionAlignRight->setToggleAction( true );
   408     actionAlignJustify = new QAction( QPixmap ( formattextjustify_xpm ), tr( "&Justify" ), CTRL + Key_J, grp, "textjustify" );
   409     actionAlignJustify->setToggleAction( true );
   410 
   411     grp->addTo( tb );
   412     grp->addTo( menu );
   413 
   414     connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
   415 	     this, SLOT( fontChanged( const QFont & ) ) );
   416     connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
   417 	     this, SLOT( colorChanged( const QColor & ) ) );
   418     connect( e, SIGNAL( currentAlignmentChanged( int ) ),
   419 	     this, SLOT( alignmentChanged( int ) ) );
   420 
   421 }
   422 
   423 void TextEditor::setupSettingsActions()
   424 {
   425     QPopupMenu *menu = new QPopupMenu( this );
   426     menuBar()->insertItem( tr( "&Settings" ), menu );
   427 
   428     QAction *a;
   429     a = new QAction( tr( "Set fixed font" ), QPixmap(), tr( "Set &fixed font" ), 0, this, "setFixedFont" );
   430     connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
   431     a->addTo( menu );
   432 	actionSettingsFixedFont=a;
   433 
   434     a = new QAction( tr( "Set variable font" ), QPixmap(), tr( "Set &variable font" ), 0, this, "setvariableFont" );
   435     connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
   436     a->addTo( menu );
   437 	actionSettingsVarFont=a;
   438 
   439     a = new QAction( tr( "Used fixed font by default" ), QPixmap(), tr( "&fixed font is default" ), 0, this, "fonthintDefault" );
   440 	a->setToggleAction (true);
   441 	// set state later in constructor...
   442     a->addTo( menu );
   443 	actionSettingsFonthintDefault=a;
   444 }
   445 
   446 void TextEditor::textLoad()
   447 {
   448 	if (state!=inactiveEditor)
   449 	{
   450 		if (e->length()) 
   451 		{
   452 			QMessageBox mb( "VYM - Note Editor",
   453 				"Loading will overwrite the existing note",
   454 				QMessageBox::Warning,
   455 				QMessageBox::Yes | QMessageBox::Default,
   456 				QMessageBox::Cancel,
   457 				0 );
   458 			mb.setButtonText( QMessageBox::Yes, "Load note" );
   459 			switch( mb.exec() ) {
   460 				case QMessageBox::Cancel:
   461 					return;
   462 					break;
   463 			}
   464 		} 
   465 		// Load note
   466 		QFileDialog *fd=new QFileDialog( this);
   467 		fd->addFilter ("ASCII texts (*.txt)");
   468 		fd->addFilter ("VYM notes (*.html)");
   469 		fd->show();
   470 		QString fn;
   471 		if ( fd->exec() == QDialog::Accepted )
   472 			fn = fd->selectedFile();
   473 
   474 		if ( !fn.isEmpty() )
   475 		{
   476 			QFile f( fn );
   477 			if ( !f.open( IO_ReadOnly ) )
   478 			return;
   479 
   480 			QTextStream ts( &f );
   481 			setText( ts.read() );
   482 			editorChanged();
   483 		}
   484 	}
   485 }
   486 
   487 void TextEditor::closeEvent( QCloseEvent* ce )
   488 {
   489     if ( !e->isModified() ) 
   490 	{
   491 		ce->accept();	// TextEditor can be reopened with show()
   492 		actionViewToggleNoteEditor->setOn (false);
   493 		showwithmain=false;
   494 		return;
   495     }
   496 }
   497 
   498 QString TextEditor::getText()
   499 {
   500 	return e->text();
   501 }
   502 
   503 void TextEditor::editorChanged()
   504 {
   505 	// received, when QTextEdit::text() has changed
   506 	EditorState	oldstate=state;
   507 
   508 	if (isEmpty())
   509 		state=emptyEditor;
   510 	else
   511 		state=filledEditor;
   512 
   513 	if (state != oldstate)
   514 	{
   515 		if (state==emptyEditor)
   516 			e->setPaper (emptyPaper);
   517 		else
   518 			e->setPaper (filledPaper);
   519 	}
   520 	// SLOT is LinkableMapObj, which will update systemFlag
   521 	emit (textHasChanged() );
   522 }
   523 
   524 
   525 void TextEditor::setText(QString t)
   526 {
   527 	if ( !QStyleSheet::mightBeRichText( t ) )
   528 		t = QStyleSheet::convertFromPlainText( t, QStyleSheetItem::WhiteSpaceNormal );
   529 	e->setReadOnly(false);
   530 	e->setText(t);
   531 	editorChanged();	//not called automagically
   532 
   533 	enableActions();
   534 }
   535 
   536 void TextEditor::setInactive()
   537 {
   538 	setText("");
   539 	state=inactiveEditor;
   540 	e->setPaper (inactivePaper);
   541 	e->setReadOnly (true);
   542 
   543 	disableActions();
   544 }
   545 
   546 void TextEditor::editCopyAll()
   547 {
   548 	e->selectAll();
   549 	e->copy();
   550 }
   551 
   552 void TextEditor::textSaveAs()
   553 {
   554     QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   555 					       this,"export note dialog",tr("Export Note to single file") );
   556 
   557     if ( !fn.isEmpty() ) 
   558 	{
   559 		QFile file (fn);
   560 		if (file.exists())
   561 		{
   562 			QMessageBox mb( "VYM",
   563 				tr("The file ") + fn + 
   564 				tr(" exists already. "
   565 				"Do you want to overwrite it?"),
   566 			QMessageBox::Warning,
   567 			QMessageBox::Yes | QMessageBox::Default,
   568 			QMessageBox::Cancel | QMessageBox::Escape,
   569 			QMessageBox::NoButton );
   570 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   571 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   572 			switch( mb.exec() ) {
   573 				case QMessageBox::Yes:
   574 					// save 
   575 					filename = fn;
   576 					textSave();
   577 					return;
   578 				case QMessageBox::Cancel:
   579 					// do nothing
   580 					break;
   581 			}
   582 		} else
   583 		{
   584 			filename = fn;
   585 			textSave();
   586 			return;
   587 		}			
   588     }
   589 	statusBar()->message(tr( "Couldn't export note ") + fn, statusbarTime );
   590 }
   591 
   592 
   593 void TextEditor::textSave()
   594 {
   595     if ( filename.isEmpty() ) 
   596 	{
   597 		textSaveAs();
   598 		return;
   599     }
   600 
   601     QString text = e->text();
   602     QFile f( filename );
   603     if ( !f.open( IO_WriteOnly ) ) 
   604 	{
   605 		statusBar()->message( QString("Could not write to %1").arg(filename),
   606 					  statusbarTime );
   607 		return;
   608     }
   609 
   610     QTextStream t( &f );
   611     t << text;
   612     f.close();
   613 
   614     e->setModified( FALSE );
   615 
   616     statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
   617 }
   618 
   619 void TextEditor::textConvertPar()
   620 {
   621 	// In X11 a copy&paste generates paragraphs, 
   622 	// which is not always wanted
   623 	// This function replaces paragraphs by linebreaks.
   624 	int parFrom, parTo, indFrom, indTo;
   625 	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   626 	QString t;
   627 	if (parFrom>-1)
   628 		t=e->selectedText();
   629 	else
   630 		t=e->text();
   631 
   632 	QRegExp re("<p.*>");
   633 	re.setMinimal(true);
   634 	t.replace (re,"");
   635 	t.replace ("</p>","<br />");
   636 	if (parFrom>-1)
   637 	{
   638 		e->setCursorPosition (parFrom,indFrom);
   639 		e->cut();
   640 		// Tried to simply insert the changed text with
   641 		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   642 		// but then the html would be quoted. So I use the ugly
   643 		// way: insert a marker, replace it in whole text of QTextEdit
   644 		QString marker="R3PlAcEMeL4teR!";
   645 		e->insert (marker);
   646 		e->setText (e->text().replace(marker,t));
   647 	} else
   648 		e->setText(t);
   649 }
   650 
   651 void TextEditor::textJoinLines()
   652 {
   653 	int parFrom, parTo, indFrom, indTo;
   654 	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   655 	QString t;
   656 	if (parFrom>-1)
   657 		t=e->selectedText();
   658 	else
   659 		t=e->text();
   660 	// In addition to textConvertPar it is sometimes
   661 	// useful to join all lines of a paragraph
   662 	QRegExp re("</p>\n+<p>(?!</p>)");
   663 	re.setMinimal(true);
   664 	t.replace (re," ");
   665 
   666 	// Above we may have introduced new " " at beginning of a
   667 	// paragraph - remove it.
   668 	re.setPattern("<p> ");
   669 	t.replace (re,"<p>");
   670 	if (parFrom>-1)
   671 	{
   672 		e->setCursorPosition (parFrom,indFrom);
   673 		e->cut();
   674 		// Tried to simply insert the changed text with
   675 		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   676 		// but then the html would be quoted. So I use the ugly
   677 		// way: insert a marker, replace it in whole text of QTextEdit
   678 		QString marker="R3PlAcEMeL4teR!";
   679 		e->insert (marker);
   680 		e->setText (e->text().replace(marker,t));
   681 	} else
   682 		e->setText(t);
   683 }
   684 
   685 QString TextEditor::textConvertToASCII(const QString &t)
   686 {
   687 	QString r=t;
   688 
   689 	// convert all "<br*>" to "\n"
   690 	QRegExp re("<br.*>");
   691 	re.setMinimal(true);
   692 	r.replace (re,"\n");
   693 
   694 	// convert all "</p>" to "\n"
   695 	re.setPattern ("/p");
   696 	r.replace (re,"\n");
   697 	
   698 	// remove all remaining tags 
   699 	re.setPattern ("<.*>");
   700 	r.replace (re,"");
   701 
   702 	// convert "&", "<" and ">"
   703 	re.setPattern ("&gt;");
   704 	r.replace (re,">");
   705 	re.setPattern ("&lt;");
   706 	r.replace (re,"<");
   707 	re.setPattern ("&amp;");
   708 	r.replace (re,"&");
   709 	return r;
   710 }
   711 
   712 void TextEditor::textExportAsASCII()
   713 {
   714 	QString text = textConvertToASCII( e->text());
   715 	QString fn,s;
   716 	if (!filenameHint.isEmpty())
   717 	{
   718 		if (!filenameHint.contains (".txt"))
   719 			s=filenameHint+".txt";
   720 		else	
   721 			s=filenameHint;
   722 	} else	
   723 		s=QString::null;
   724 	fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   725 	int ret=-1;
   726 
   727     if ( !fn.isEmpty() ) 
   728 	{
   729 		QFile file (fn);
   730 		if (file.exists())
   731 		{
   732 			QMessageBox mb( "VYM",
   733 				tr("The file ") + fn + 
   734 				tr(" exists already. "
   735 				"Do you want to overwrite it?"),
   736 			QMessageBox::Warning,
   737 			QMessageBox::Yes | QMessageBox::Default,
   738 			QMessageBox::Cancel | QMessageBox::Escape,
   739 			QMessageBox::NoButton );
   740 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   741 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   742 			ret=mb.exec();
   743 		}	
   744 		if (ret==QMessageBox::Cancel)
   745 			return;
   746 			
   747 		// save 
   748 		if ( !file.open( IO_WriteOnly ) ) 
   749 			statusBar()->message( QString("Could not write to %1").arg(filename),
   750 						  statusbarTime );
   751 		else
   752 		{
   753 			QTextStream t( &file );
   754 			t << text;
   755 			file.close();
   756 
   757 			statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
   758 		}
   759     }
   760 }
   761 
   762 
   763 void TextEditor::textPrint()
   764 {
   765     printer->setFullPage(TRUE);
   766     if ( printer->setup( this ) ) 
   767 	{
   768 		QPainter p( printer );
   769 		// Check that there is a valid device to print to.
   770 		if ( !p.device() ) return;
   771 		QPaintDeviceMetrics metrics( p.device() );
   772 		int dpiy = metrics.logicalDpiY();
   773 		int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
   774 		QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
   775 		QFont font( e->currentFont() );
   776 		font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
   777 
   778 		QSimpleRichText richText( e->text(), font,
   779 					  e->context(),
   780 					  e->styleSheet(),
   781 					  e->mimeSourceFactory(),
   782 					  body.height() );
   783 		richText.setWidth( &p, body.width() );
   784 		QRect view( body );
   785 		int page = 1;
   786 		do 
   787 		{
   788 			richText.draw( &p, body.left(), body.top(), view, colorGroup() );
   789 			view.moveBy( 0, body.height() );
   790 			p.translate( 0 , -body.height() );
   791 			p.setFont( font );
   792 			p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
   793 				view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
   794 			if ( view.top()  >= richText.height() )
   795 			break;
   796 			printer->newPage();
   797 			page++;
   798 		} while (TRUE);
   799     }
   800 }
   801 
   802 void TextEditor::textEditUndo()
   803 {
   804 }
   805 
   806 void TextEditor::toggleFonthint()
   807 {
   808 	setUpdatesEnabled (false);
   809 	e->selectAll (true);
   810 	if (!actionFormatUseFixedFont->isOn() ) 
   811 		e->setCurrentFont (varFont);
   812 	else	
   813 		e->setCurrentFont (fixedFont);
   814 	e->selectAll (false);
   815 	setUpdatesEnabled (true);
   816 	repaint();
   817 }
   818 
   819 void TextEditor::setFixedFont()
   820 {
   821 	bool ok;
   822 	QFont font =QFontDialog::getFont(
   823                     &ok, fixedFont, this );
   824     if ( ok ) 
   825         // font is set to the font the user selected
   826 		fixedFont=font;
   827 }
   828 
   829 void TextEditor::setVarFont()
   830 {
   831 	bool ok;
   832 	QFont font =QFontDialog::getFont(
   833                     &ok, varFont, this );
   834     if ( ok ) 
   835         // font is set to the font the user selected
   836 		varFont=font;
   837 }
   838 
   839 void TextEditor::textBold()
   840 {
   841     e->setBold( actionTextBold->isOn() );
   842 }
   843 
   844 void TextEditor::textUnderline()
   845 {
   846     e->setUnderline( actionTextUnderline->isOn() );
   847 }
   848 
   849 void TextEditor::textItalic()
   850 {
   851     e->setItalic( actionTextItalic->isOn() );
   852 }
   853 
   854 void TextEditor::textFamily( const QString &f )
   855 {
   856     e->setFamily( f );
   857 }
   858 
   859 void TextEditor::textSize( const QString &p )
   860 {
   861     e->setPointSize( p.toInt() );
   862 }
   863 
   864 
   865 void TextEditor::textColor()
   866 {
   867     QColor col = QColorDialog::getColor( e->color(), this );
   868     if ( !col.isValid() )
   869 	return;
   870     e->setColor( col );
   871     QPixmap pix( 16, 16 );
   872     pix.fill( black );
   873     actionTextColor->setIconSet( pix );
   874 }
   875 
   876 void TextEditor::textAlign( QAction *a )
   877 {
   878     if ( a == actionAlignLeft )
   879 	e->setAlignment( AlignLeft );
   880     else if ( a == actionAlignCenter )
   881 	e->setAlignment( AlignHCenter );
   882     else if ( a == actionAlignRight )
   883 	e->setAlignment( AlignRight );
   884     else if ( a == actionAlignJustify )
   885 	e->setAlignment( AlignJustify );
   886 }
   887 
   888 void TextEditor::fontChanged( const QFont &f )
   889 {
   890     comboFont->lineEdit()->setText( f.family() );
   891     comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
   892     actionTextBold->setOn( f.bold() );
   893     actionTextItalic->setOn( f.italic() );
   894     actionTextUnderline->setOn( f.underline() );
   895 }
   896 
   897 void TextEditor::colorChanged( const QColor &c )
   898 {
   899     QPixmap pix( 16, 16 );
   900     pix.fill( c );
   901     actionTextColor->setIconSet( pix );
   902 }
   903 
   904 void TextEditor::alignmentChanged( int a )
   905 {
   906     if ( ( a == AlignAuto ) || ( a & AlignLeft ))
   907 	actionAlignLeft->setOn( true );
   908     else if ( ( a & AlignHCenter ) )
   909 	actionAlignCenter->setOn( true );
   910     else if ( ( a & AlignRight ) )
   911 	actionAlignRight->setOn( true );
   912     else if ( ( a & AlignJustify ) )
   913 	actionAlignJustify->setOn( true );
   914 }
   915 
   916 
   917 
   918 void TextEditor::enableActions()
   919 {
   920 	actionFileLoad->setEnabled(true);
   921 	actionFileSave->setEnabled(true);
   922 	actionFileSaveAs->setEnabled(true);
   923 	actionFilePrint->setEnabled(true);
   924 	actionEditUndo->setEnabled(true);
   925 	actionEditRedo->setEnabled(true);
   926 	actionEditCopy->setEnabled(true);
   927 	actionEditCut->setEnabled(true);
   928 	actionEditPaste->setEnabled(true);
   929 	actionEditDeleteAll->setEnabled(true);
   930 	actionEditConvertPar->setEnabled(true);
   931 	actionEditJoinLines->setEnabled(true);
   932 	actionFormatUseFixedFont->setEnabled(true);
   933 }
   934 
   935 void TextEditor::disableActions()
   936 {
   937 	actionFileLoad->setEnabled(false);
   938 	actionFileSave->setEnabled(false);
   939 	actionFileSaveAs->setEnabled(false);
   940 	actionFilePrint->setEnabled(false);
   941 	actionEditUndo->setEnabled(false);
   942 	actionEditRedo->setEnabled(false);
   943 	actionEditCopy->setEnabled(false);
   944 	actionEditCut->setEnabled(false);
   945 	actionEditPaste->setEnabled(false);
   946 	actionEditDeleteAll->setEnabled(false);
   947 	actionEditConvertPar->setEnabled(false);
   948 	actionEditJoinLines->setEnabled(false);
   949 	actionFormatUseFixedFont->setEnabled(false);
   950 }
   951 
   952