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