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