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