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