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