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