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