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