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