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