texteditor.cpp
author insilmaril
Tue, 24 Oct 2006 15:36:38 +0000
changeset 394 67cfa6e6b863
parent 393 053b8645e3e9
child 395 7ced3733ba60
permissions -rw-r--r--
1.8.58 - More undoCommands, spanish translation of doc
     1 #include "texteditor.h"
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <typeinfo>
     6 
     7 #include "noteobj.h"
     8 #include "version.h"
     9 
    10 extern int statusbarTime;
    11 extern QSettings settings;
    12 
    13 extern QAction *actionViewToggleNoteEditor;
    14 
    15 extern QString iconPath;
    16 
    17 using namespace std;
    18 
    19 
    20 ///////////////////////////////////////////////////////////////////////
    21 ///////////////////////////////////////////////////////////////////////
    22 
    23 
    24 TextEditor::TextEditor()
    25 {
    26     printer = new QPrinter( QPrinter::HighResolution );
    27 	printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
    28 
    29     e = new QTextEdit( this);
    30     e->setFocus();
    31 	e->setTextFormat(Qt::RichText);		// default
    32 	e->setTabStopWidth (20);		// unit is pixel
    33 	e->setColor (Qt::black);
    34 	e->setAutoFillBackground (true);
    35 	connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
    36     setCentralWidget( e );
    37     statusBar()->message( tr("Ready","Statusbar message"), statusbarTime);
    38 	setCaption(__VYM " - " +tr ("Note Editor"));
    39 
    40 
    41 	connect(e, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
    42             this, SLOT(formatChanged(const QTextCharFormat &)));
    43 
    44 
    45 
    46 	// Toolbars
    47 	setupFileActions();
    48 	setupEditActions();
    49 	setupFormatActions();
    50 	setupSettingsActions();
    51 	
    52 	// Various states
    53 	setInactive();
    54 
    55 	// Load Settings
    56 	resize (settings.value ( "/noteeditor/geometry/size", QSize(450,600)).toSize());
    57 	move   (settings.value ( "/noteeditor/geometry/pos", QPoint (250,50)).toPoint());
    58 	
    59 	if (settings.value ( "/noteeditor/showWithMain",true).toBool())
    60 		setShowWithMain(true);
    61 	else	
    62 		setShowWithMain(false);
    63 
    64 	varFont.fromString( settings.value
    65 		("/noteeditor/fonts/varFont",
    66 		"Nimbus Sans l,14,-1,5,48,0,0,0,0,0").toString() 
    67 	);
    68 	fixedFont.fromString (settings.value(
    69 		"/noteeditor/fonts/fixedFont",
    70 		"Courier,14,-1,5,48,0,0,0,1,0").toString() 
    71 	);
    72 	QString s=settings.value ("/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("/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( "/noteeditor/geometry/size", size() );
    97 	settings.setValue( "/noteeditor/geometry/pos", pos() );
    98 	settings.setValue ("/noteeditor/state",saveState(0));
    99 	
   100 	settings.setValue( "/noteeditor/showWithMain",showWithMain());
   101 
   102 	QString s;
   103 	if (actionSettingsFonthintDefault->isOn() )
   104 		s="fixed";
   105 	else	
   106 		s="variable";
   107 	settings.setValue( "/noteeditor/fonts/fonthintDefault",s );
   108 	settings.setValue("/noteeditor/fonts/varFont", varFont.toString() );
   109 	settings.setValue("/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 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 		if (fn.isEmpty() )
   161 		{
   162 			filename="";
   163 			statusBar()->message( tr("No filename available for this note.","Statusbar message"), statusbarTime );
   164 		}	
   165 		else
   166 		{
   167 			filename=fn;
   168 			statusBar()->message( tr(QString( "Current filename is %1" ).arg( filename ),"Statusbar message"), statusbarTime );
   169 		}	
   170 }
   171 
   172 QString TextEditor::getFilename()
   173 {
   174 	return filename;
   175 }
   176 
   177 void TextEditor::setFilenameHint(const QString &fnh)
   178 {
   179 	filenameHint=fnh;
   180 }
   181 
   182 QString TextEditor::getFilenameHint()
   183 {
   184 	return filenameHint;
   185 }
   186 
   187 bool TextEditor::findText(const QString &t, const QTextDocument::FindFlags &flags)
   188 {
   189 	if (e->find (t,flags))
   190 		return true;
   191 	else	
   192 		return false;
   193 }
   194 
   195 void TextEditor::setupFileActions()
   196 {
   197     QToolBar *tb = addToolBar ( tr("Note Actions") );
   198 	tb->setObjectName ("noteEditorFileActions");
   199     QMenu *fileMenu = menuBar()->addMenu( tr( "&Note" ));
   200 
   201     QAction *a;
   202     a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this);
   203 	a->setStatusTip (tr( "Import" ) );
   204 	a->setShortcut( Qt::CTRL + Qt::Key_O );
   205     connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) );
   206 	tb->addAction (a);
   207 	fileMenu->addAction (a);
   208 	actionFileLoad=a;
   209 
   210     fileMenu->addSeparator();
   211     a = new QAction( QPixmap(iconPath+"filesave.png" ), tr( "&Export..." ),this);
   212 	a->setStatusTip (tr( "Export Note (HTML)" ) );
   213 	a->setShortcut( Qt::CTRL + Qt::Key_S );
   214     connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) );
   215 	tb->addAction (a);
   216 	fileMenu->addAction (a);
   217 	actionFileSave=a;
   218 	
   219     a = new QAction(  QPixmap(), tr( "Export &As... (HTML)" ), this);
   220 	a->setStatusTip (tr( "Export Note As (HTML) " ));
   221     connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) );
   222 	fileMenu->addAction (a);
   223 	actionFileSaveAs=a;
   224 
   225     a = new QAction(QPixmap(), tr( "Export &As...(ASCII)" ), this);
   226 	a->setStatusTip ( tr( "Export Note As (ASCII) " ) );
   227 	a->setShortcut(Qt::ALT + Qt::Key_X );
   228     connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) );
   229 	fileMenu->addAction (a);
   230 	actionFileSaveAs=a;
   231 
   232     fileMenu->addSeparator();
   233     a = new QAction( QPixmap(iconPath+"fileprint.png" ), tr( "&Print..." ),this);
   234 	a->setStatusTip (tr( "Print Note" ) );
   235 	a->setShortcut( Qt::CTRL + Qt::Key_P );
   236     connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) );
   237 	tb->addAction (a);
   238 	fileMenu->addAction (a);
   239 	actionFilePrint=a;
   240 }
   241 
   242 void TextEditor::setupEditActions()
   243 {
   244     QToolBar *tb = addToolBar ( tr( "Edit Actions" ));
   245 	tb->setObjectName ("noteEditorEditActions");
   246     QMenu *editMenu = menuBar()->addMenu ( tr( "&Edit" ));
   247 
   248     QAction *a;
   249     a = new QAction(QPixmap(iconPath+"undo.png"), tr( "&Undo" ), this );
   250 	a->setStatusTip ( tr( "Undo" ) );
   251 	a->setShortcut(Qt::CTRL + Qt::Key_Z );
   252     connect( a, SIGNAL( activated() ), e, SLOT( undo() ) );
   253 	editMenu->addAction (a);
   254 	tb->addAction (a);
   255 	actionEditUndo=a;
   256 	
   257     a = new QAction(QPixmap(iconPath+"redo.png" ), tr( "&Redo" ),this); 
   258 	a->setStatusTip ( tr( "Redo" ) );
   259 	a->setShortcut( Qt::CTRL + Qt::Key_Y );
   260     connect( a, SIGNAL( activated() ), e, SLOT( redo() ) );
   261 	editMenu->addAction (a);
   262 	tb->addAction (a);
   263 	actionEditRedo=a;
   264 
   265     editMenu->addSeparator();
   266     a = new QAction(QPixmap(), tr( "Select and copy &all" ),this); 
   267 	a->setStatusTip ( tr( "Select and copy all" ) );
   268 	a->setShortcut( Qt::CTRL + Qt::Key_A );
   269     connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) );
   270 	editMenu->addAction (a);
   271 
   272     editMenu->addSeparator();
   273     a = new QAction(QPixmap(iconPath+"editcopy.png" ), tr( "&Copy" ),this);
   274 	a->setStatusTip ( tr( "Copy" ) );
   275 	a->setShortcut( Qt::CTRL + Qt::Key_C );
   276     connect( a, SIGNAL( activated() ), e, SLOT( copy() ) );
   277 	editMenu->addAction (a);
   278 	tb->addAction (a);
   279 	actionEditCopy=a;
   280 	
   281     a = new QAction(QPixmap(iconPath+"editcut.png" ), tr( "Cu&t" ),this);
   282 	a->setStatusTip ( tr( "Cut" ) );
   283 	a->setShortcut( Qt::CTRL + Qt::Key_X );
   284     connect( a, SIGNAL( activated() ), e, SLOT( cut() ) );
   285 	editMenu->addAction (a);
   286 	tb->addAction (a);
   287 	actionEditCut=a;
   288 
   289     a = new QAction(QPixmap(iconPath+"editpaste.png" ), tr( "&Paste" ),this);
   290 	a->setStatusTip ( tr( "Paste" ) );
   291 	a->setShortcut( Qt::CTRL + Qt::Key_V );
   292     connect( a, SIGNAL( activated() ), e, SLOT( paste() ) );
   293 	editMenu->addAction (a);
   294 	tb->addAction (a);
   295 	actionEditPaste=a;
   296 	
   297     a = new QAction( QPixmap( iconPath+"edittrash.png"), tr( "&Delete All" ), this);
   298 	a->setStatusTip (tr( "Delete all" ) );
   299     connect( a, SIGNAL( activated() ), e, SLOT( clear() ) );
   300 	editMenu->addAction (a);
   301 	tb->addAction (a);
   302 	actionEditDeleteAll=a;
   303 
   304 }
   305 
   306 void TextEditor::setupFormatActions()
   307 {
   308     QToolBar *tb = addToolBar ( tr("Format Actions" ));
   309 	tb->setObjectName ("noteEditorFormatActions");
   310     QMenu *formatMenu = menuBar()->addMenu ( tr( "F&ormat" ));
   311 
   312     QAction *a;
   313 
   314     a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" );
   315 	a->setStatusTip (tr( "Toggle font hint for the whole text" ) );
   316 	a->setToggleAction (true);
   317 	a->setOn (settings.value("/noteeditor/fonts/useFixedByDefault",false).toBool() );
   318     connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
   319 	formatMenu->addAction (a);
   320 	tb->addAction (a);
   321 	actionFormatUseFixedFont=a;
   322 
   323     comboFont = new QComboBox;
   324 	tb->addWidget (comboFont);
   325     QFontDatabase fontDB;
   326     comboFont->insertStringList( fontDB.families() );
   327     connect( comboFont, SIGNAL( activated( const QString & ) ),
   328 	     this, SLOT( textFamily( const QString & ) ) );
   329     comboFont->addItem( QApplication::font().family() );
   330     comboSize = new QComboBox;
   331 	tb->addWidget (comboSize);
   332 	QList<int> sizes=fontDB.standardSizes();
   333 	QList<int>::iterator i = sizes.begin();
   334 	while (i != sizes.end()) 
   335 	{
   336 		++i; // increment i before using it
   337 		comboSize->insertItem ( QString::number(*i));
   338 	}	
   339     connect( comboSize, SIGNAL( activated( const QString & ) ),
   340 	     this, SLOT( textSize( const QString & ) ) );
   341     comboSize->addItem ( QString::number( QApplication::font().pointSize() ) );
   342 
   343     formatMenu->addSeparator();
   344 
   345     QPixmap pix( 16, 16 );
   346     pix.fill( e->color());
   347     a = new QAction( pix, tr( "&Color..." ), this);
   348 	formatMenu->addAction (a);
   349 	tb->addAction (a);
   350     connect( a, SIGNAL( activated() ), this, SLOT( textColor() ) );
   351     actionTextColor=a;
   352 
   353     a = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this);
   354 	a->setShortcut(Qt::CTRL + Qt::Key_B );
   355     connect( a, SIGNAL( activated() ), this, SLOT( textBold() ) );
   356 	tb->addAction (a);
   357 	formatMenu->addAction (a);
   358     a->setToggleAction( true );
   359     actionTextBold=a;
   360 	
   361     a = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ),  this);
   362 	a->setShortcut(Qt::CTRL + Qt::Key_I);
   363     connect( a, SIGNAL( activated() ), this, SLOT( textItalic() ) );
   364 	tb->addAction (a);
   365 	formatMenu->addAction (a);
   366     a->setToggleAction( true );
   367     actionTextItalic=a;
   368 	
   369     a = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this);
   370 	a->setShortcut(Qt::CTRL + Qt::Key_U );
   371     connect( a, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
   372 	tb->addAction (a);
   373 	formatMenu->addAction (a);
   374     a->setToggleAction( true );
   375     actionTextUnderline=a;
   376     formatMenu->addSeparator();
   377 
   378     QActionGroup *grp2 = new QActionGroup( this );
   379     grp2->setExclusive(true);
   380     a = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 );
   381 	a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B );
   382     a->setToggleAction( true );
   383 	tb->addAction (a);
   384 	formatMenu->addAction (a);
   385     connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
   386     actionAlignSubScript=a;
   387 
   388     a = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2  );
   389 	a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P );
   390     a->setToggleAction( true );
   391 	tb->addAction (a);
   392 	formatMenu->addAction (a);
   393     connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
   394     actionAlignSuperScript=a;
   395     QActionGroup *grp = new QActionGroup( this );
   396     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
   397 
   398     formatMenu->addSeparator();
   399 
   400     a = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp );
   401 	a->setShortcut( Qt::CTRL+Qt::Key_L );
   402     a->setToggleAction( true );
   403 	tb->addAction (a);
   404 	formatMenu->addAction (a);
   405     actionAlignLeft=a;
   406     a = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp);
   407     a->setShortcut(  Qt::CTRL + Qt::Key_E);
   408     a->setToggleAction( true );
   409 	tb->addAction (a);
   410 	formatMenu->addAction (a);
   411     actionAlignCenter=a;
   412     a = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp);
   413 	a->setShortcut(Qt::CTRL + Qt::Key_R );
   414     a->setToggleAction( true );
   415 	tb->addAction (a);
   416 	formatMenu->addAction (a);
   417     actionAlignRight=a;
   418     a = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp );
   419 	a->setShortcut(Qt::CTRL + Qt::Key_J );
   420     a->setToggleAction( true );
   421 	tb->addAction (a);
   422 	formatMenu->addAction (a);
   423     actionAlignJustify=a;
   424 }
   425 
   426 void TextEditor::setupSettingsActions()
   427 {
   428     QMenu *settingsMenu = menuBar()->addMenu ( tr( "&Settings" ));
   429 
   430     QAction *a;
   431     a = new QAction(tr( "Set &fixed font" ), this);
   432 	a->setStatusTip ( tr( "Set fixed font" ));
   433     connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
   434 	settingsMenu->addAction (a);
   435 	actionSettingsFixedFont=a;
   436 
   437     a = new QAction(tr( "Set &variable font" ), this);
   438 	a->setStatusTip ( tr( "Set variable font" ) );
   439     connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
   440 	settingsMenu->addAction (a);
   441 	actionSettingsVarFont=a;
   442 
   443     a = new QAction(tr( "&fixed font is default" ),  this);
   444 	a->setStatusTip (tr( "Used fixed font by default" ) );
   445 	a->setToggleAction (true);
   446 	// set state later in constructor...
   447 	settingsMenu->addAction (a);
   448 	actionSettingsFonthintDefault=a;
   449 }
   450 
   451 void TextEditor::textLoad()
   452 {
   453 	if (state!=inactiveEditor)
   454 	{
   455 		if (e->text().length()) 
   456 		{
   457 			QMessageBox mb( __VYM " - " +tr("Note Editor"),
   458 				"Loading will overwrite the existing note",
   459 				QMessageBox::Warning,
   460 				QMessageBox::Yes | QMessageBox::Default,
   461 				QMessageBox::Cancel,
   462 				0 );
   463 			mb.setButtonText( QMessageBox::Yes, "Load note" );
   464 			switch( mb.exec() ) {
   465 				case QMessageBox::Cancel:
   466 					return;
   467 					break;
   468 			}
   469 		} 
   470 		// Load note
   471 		QFileDialog *fd=new QFileDialog( this);
   472 		QStringList types;
   473 		types<< "VYM notes (*.html)" <<
   474 			"ASCII texts (*.txt)" <<
   475 			"All filed (*)";
   476 		fd->setFilters (types);
   477 		fd->setDirectory (QDir().current());
   478 		fd->show();
   479 		QString fn;
   480 		if ( fd->exec() == QDialog::Accepted )
   481 			fn = fd->selectedFile();
   482 
   483 		if ( !fn.isEmpty() )
   484 		{
   485 			QFile f( fn );
   486 			if ( !f.open( QIODevice::ReadOnly ) )
   487 			return;
   488 
   489 			QTextStream ts( &f );
   490 			setText( ts.read() );
   491 			editorChanged();
   492 		}
   493 	}
   494 }
   495 
   496 void TextEditor::closeEvent( QCloseEvent* ce )
   497 {
   498     ce->accept();	// TextEditor can be reopened with show()
   499     showwithmain=false;
   500 	emit (textEditorClosed() );
   501     return;
   502 }
   503 
   504 QString TextEditor::getText()
   505 {
   506 	if (e->toPlainText().isEmpty())
   507 		return "";
   508 	else	
   509 		return e->text();
   510 }
   511 
   512 void TextEditor::editorChanged()
   513 {
   514 	if (isEmpty())
   515 		state=emptyEditor;
   516 	else
   517 		state=filledEditor;
   518 
   519 		if (state==emptyEditor)
   520 			setState (emptyEditor);
   521 		else
   522 			setState (filledEditor);
   523 	// SLOT is LinkableMapObj, which will update systemFlag
   524 	emit (textHasChanged() );
   525 }
   526 
   527 
   528 void TextEditor::setText(QString t)
   529 {
   530 	e->setReadOnly(false);
   531 	e->setText(t);
   532 	enableActions();
   533 }
   534 
   535 void TextEditor::setInactive()
   536 {
   537 	state=inactiveEditor;
   538 	setText("");
   539 	setState (inactiveEditor);
   540 	e->setReadOnly (true);
   541 
   542 	disableActions();
   543 }
   544 
   545 void TextEditor::editCopyAll()
   546 {
   547 	e->selectAll();
   548 	e->copy();
   549 }
   550 
   551 void TextEditor::textSaveAs()
   552 {
   553     QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   554 					       this,"export note dialog",tr("Export Note to single file") );
   555 
   556     if ( !fn.isEmpty() ) 
   557 	{
   558 		QFile file (fn);
   559 		if (file.exists())
   560 		{
   561 			QMessageBox mb( __VYM,
   562 				tr("The file ") + fn + 
   563 				tr(" exists already. "
   564 				"Do you want to overwrite it?"),
   565 			QMessageBox::Warning,
   566 			QMessageBox::Yes | QMessageBox::Default,
   567 			QMessageBox::Cancel | QMessageBox::Escape,
   568 			Qt::NoButton );
   569 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   570 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   571 			switch( mb.exec() ) {
   572 				case QMessageBox::Yes:
   573 					// save 
   574 					filename = fn;
   575 					textSave();
   576 					return;
   577 				case QMessageBox::Cancel:
   578 					// do nothing
   579 					break;
   580 			}
   581 		} else
   582 		{
   583 			filename = fn;
   584 			textSave();
   585 			return;
   586 		}			
   587     }
   588 	statusBar()->message(tr( "Couldn't export note ") + fn, statusbarTime );
   589 }
   590 
   591 
   592 void TextEditor::textSave()
   593 {
   594     if ( filename.isEmpty() ) 
   595 	{
   596 		textSaveAs();
   597 		return;
   598     }
   599 
   600     QString text = e->text();
   601     QFile f( filename );
   602     if ( !f.open( QIODevice::WriteOnly ) ) 
   603 	{
   604 		statusBar()->message( QString("Could not write to %1").arg(filename),
   605 					  statusbarTime );
   606 		return;
   607     }
   608 
   609     QTextStream t( &f );
   610     t << text;
   611     f.close();
   612 
   613     e->setModified( FALSE );
   614 
   615     statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
   616 }
   617 
   618 void TextEditor::textExportAsASCII()
   619 {
   620 	QString text = NoteObj (e->text()).getNoteASCII();
   621 	QString fn,s;
   622 	if (!filenameHint.isEmpty())
   623 	{
   624 		if (!filenameHint.contains (".txt"))
   625 			s=filenameHint+".txt";
   626 		else	
   627 			s=filenameHint;
   628 	} else	
   629 		s=QString::null;
   630 	fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   631 	int ret=-1;
   632 
   633     if ( !fn.isEmpty() ) 
   634 	{
   635 		QFile file (fn);
   636 		if (file.exists())
   637 		{
   638 			QMessageBox mb( "VYM",
   639 				tr("The file ") + fn + 
   640 				tr(" exists already. "
   641 				"Do you want to overwrite it?"),
   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 Document"));
   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