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