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