texteditor.cpp
author insilmaril
Wed, 16 Jan 2008 15:45:18 +0000
changeset 653 8be6ac05960b
parent 645 2abfdb7e85f4
child 664 01c028006e6d
permissions -rw-r--r--
Fixed missing MapCenter
     1 #include "texteditor.h"
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <typeinfo>
     6 
     7 #include "noteobj.h"
     8 #include "settings.h"
     9 
    10 extern int statusbarTime;
    11 extern Settings settings;
    12 
    13 extern QAction *actionViewToggleNoteEditor;
    14 
    15 extern QString iconPath;
    16 extern QString vymName;
    17 
    18 using namespace std;
    19 
    20 
    21 ///////////////////////////////////////////////////////////////////////
    22 ///////////////////////////////////////////////////////////////////////
    23 
    24 
    25 TextEditor::TextEditor()
    26 {
    27     printer = new QPrinter( QPrinter::HighResolution );
    28 	printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
    29 
    30     e = new QTextEdit( this);
    31     e->setFocus();
    32 	e->setTextFormat(Qt::RichText);		// default
    33 	e->setTabStopWidth (20);		// unit is pixel
    34 	e->setColor (Qt::black);
    35 	e->setAutoFillBackground (true);
    36 	connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
    37     setCentralWidget( e );
    38     statusBar()->message( tr("Ready","Statusbar message"), statusbarTime);
    39 	setCaption(vymName +" - " +tr ("Note Editor","Window caption"));
    40 
    41 
    42 	connect(e, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
    43             this, SLOT(formatChanged(const QTextCharFormat &)));
    44 
    45 
    46 
    47 	// Toolbars
    48 	setupFileActions();
    49 	setupEditActions();
    50 	setupFormatActions();
    51 	setupSettingsActions();
    52 	
    53 	// Various states
    54 	blockChangedSignal=false;
    55 	setInactive();
    56 
    57 	// Load Settings
    58 	resize (settings.value ( "/satellite/noteeditor/geometry/size", QSize(450,600)).toSize());
    59 	move   (settings.value ( "/satellite/noteeditor/geometry/pos", QPoint (250,50)).toPoint());
    60 	
    61 	setShowWithMain (settings.value ( "/satellite/noteeditor/showWithMain",true).toBool());
    62 
    63 	varFont.fromString( settings.value
    64 		("/satellite/noteeditor/fonts/varFont",
    65 		"Nimbus Sans l,14,-1,5,48,0,0,0,0,0").toString() 
    66 	);
    67 	fixedFont.fromString (settings.value(
    68 		"/satellite/noteeditor/fonts/fixedFont",
    69 		"Courier,14,-1,5,48,0,0,0,1,0").toString() 
    70 	);
    71 	QString s=settings.value ("/satellite/noteeditor/fonts/fonthintDefault","variable").toString();
    72 	if (s == "fixed")
    73 	{	
    74 		actionSettingsFonthintDefault->setOn (true);
    75 		e->setCurrentFont (fixedFont);
    76 	} else	
    77 	{
    78 		actionSettingsFonthintDefault->setOn (false);
    79 		e->setCurrentFont (varFont);
    80 	}	
    81 	filenameHint="";
    82 
    83 	// Restore position of toolbars
    84 	restoreState (settings.value("/satellite/noteeditor/state",0).toByteArray());
    85 
    86 	// Save settings in vymrc
    87 	settings.setValue("/mainwindow/printerName",printer->printerName());
    88 }
    89 
    90 
    91 TextEditor::~TextEditor()
    92 {
    93     if (printer) delete printer;
    94 	// Save Settings
    95 	settings.setValue( "/satellite/noteeditor/geometry/size", size() );
    96 	settings.setValue( "/satellite/noteeditor/geometry/pos", pos() );
    97 	settings.setValue ("/satellite/noteeditor/state",saveState(0));
    98 	
    99 	settings.setValue( "/satellite/noteeditor/showWithMain",showwithmain);
   100 
   101 	QString s;
   102 	if (actionSettingsFonthintDefault->isOn() )
   103 		s="fixed";
   104 	else	
   105 		s="variable";
   106 	settings.setValue( "/satellite/noteeditor/fonts/fonthintDefault",s );
   107 	settings.setValue("/satellite/noteeditor/fonts/varFont", varFont.toString() );
   108 	settings.setValue("/satellite/noteeditor/fonts/fixedFont", fixedFont.toString() );
   109 
   110 
   111 }
   112 
   113 bool TextEditor::isEmpty()
   114 {
   115 	if (e->toPlainText().length()>0)
   116 		return false;
   117 	else
   118 		return true;
   119 }
   120 
   121 void TextEditor::setShowWithMain(bool v)
   122 {
   123 	showwithmain=v;
   124 }
   125 
   126 bool TextEditor::showWithMain()
   127 {
   128 	return showwithmain;
   129 }
   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","Menubar" ));
   200 
   201     QAction *a;
   202     a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this);
   203 	a->setStatusTip (tr( "Import","Status tip for Note menu" ) );
   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)","Status tip for Note menu" ) );
   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) ","Status tip for Note Menu"  ));
   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) ","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ));
   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","Status tip for note menu" ) );
   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","Status tip for note menu" ) );
   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( vymName + " - " +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 	hide();
   501 	emit (windowClosed() );
   502     return;
   503 }
   504 
   505 QString TextEditor::getText()
   506 {
   507 	if (e->toPlainText().isEmpty())
   508 		return "";
   509 	else	
   510 		return e->text();
   511 }
   512 
   513 void TextEditor::editorChanged()
   514 {
   515 	if (isEmpty())
   516 		state=emptyEditor;
   517 	else
   518 		state=filledEditor;
   519 
   520 		if (state==emptyEditor)
   521 			setState (emptyEditor);
   522 		else
   523 			setState (filledEditor);
   524 	// SLOT is LinkableMapObj, which will update systemFlag
   525 	if (!blockChangedSignal) emit (textHasChanged() );
   526 }
   527 
   528 
   529 void TextEditor::setText(QString t)
   530 {
   531 	blockChangedSignal=true;
   532 	e->setReadOnly(false);
   533 	e->setText(t);
   534 	enableActions();
   535 	blockChangedSignal=false;
   536 }
   537 
   538 void TextEditor::setInactive()
   539 {
   540 	state=inactiveEditor;
   541 	setText("");
   542 	setState (inactiveEditor);
   543 	e->setReadOnly (true);
   544 
   545 	disableActions();
   546 }
   547 
   548 void TextEditor::editCopyAll()
   549 {
   550 	e->selectAll();
   551 	e->copy();
   552 }
   553 
   554 void TextEditor::textSaveAs()
   555 {
   556     QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   557 					       this,"export note dialog",tr("Export Note to single file") );
   558 
   559     if ( !fn.isEmpty() ) 
   560 	{
   561 		QFile file (fn);
   562 		if (file.exists())
   563 		{
   564 			QMessageBox mb( vymName,
   565 				tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn),
   566 			QMessageBox::Warning,
   567 			QMessageBox::Yes | QMessageBox::Default,
   568 			QMessageBox::Cancel | QMessageBox::Escape,
   569 			Qt::NoButton );
   570 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   571 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   572 			switch( mb.exec() ) {
   573 				case QMessageBox::Yes:
   574 					// save 
   575 					filename = fn;
   576 					textSave();
   577 					return;
   578 				case QMessageBox::Cancel:
   579 					// do nothing
   580 					break;
   581 			}
   582 		} else
   583 		{
   584 			filename = fn;
   585 			textSave();
   586 			return;
   587 		}			
   588     }
   589 	statusBar()->message(tr( "Couldn't export note ","dialog 'save note as'") + fn, statusbarTime );
   590 }
   591 
   592 
   593 void TextEditor::textSave()
   594 {
   595     if ( filename.isEmpty() ) 
   596 	{
   597 		textSaveAs();
   598 		return;
   599     }
   600 
   601     QString text = e->text();
   602     QFile f( filename );
   603     if ( !f.open( QIODevice::WriteOnly ) ) 
   604 	{
   605 		statusBar()->message( QString("Could not write to %1").arg(filename),
   606 					  statusbarTime );
   607 		return;
   608     }
   609 
   610     QTextStream t( &f );
   611     t << text;
   612     f.close();
   613 
   614     e->setModified( FALSE );
   615 
   616     statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
   617 }
   618 
   619 void TextEditor::textExportAsASCII()
   620 {
   621 	QString text = NoteObj (e->text()).getNoteASCII();
   622 	QString fn,s;
   623 	if (!filenameHint.isEmpty())
   624 	{
   625 		if (!filenameHint.contains (".txt"))
   626 			s=filenameHint+".txt";
   627 		else	
   628 			s=filenameHint;
   629 	} else	
   630 		s=QString::null;
   631 	fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   632 	int ret=-1;
   633 
   634     if ( !fn.isEmpty() ) 
   635 	{
   636 		QFile file (fn);
   637 		if (file.exists())
   638 		{
   639 			QMessageBox mb( vymName,
   640 				tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn),
   641 			QMessageBox::Warning,
   642 			QMessageBox::Yes | QMessageBox::Default,
   643 			QMessageBox::Cancel | QMessageBox::Escape,
   644 			Qt::NoButton );
   645 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   646 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   647 			ret=mb.exec();
   648 		}	
   649 		if (ret==QMessageBox::Cancel)
   650 			return;
   651 			
   652 		// save 
   653 		if ( !file.open( QIODevice::WriteOnly ) ) 
   654 			statusBar()->message( QString("Could not write to %1").arg(filename),
   655 						  statusbarTime );
   656 		else
   657 		{
   658 			QTextStream t( &file );
   659 			t << text;
   660 			file.close();
   661 
   662 			statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
   663 		}
   664     }
   665 }
   666 
   667 
   668 void TextEditor::textPrint()
   669 {
   670 
   671     QTextDocument *document = e->document();
   672     QPrinter printer;
   673 
   674     QPrintDialog *dialog = new QPrintDialog(&printer, this);
   675     dialog->setWindowTitle(tr("Print Note"));
   676     if (dialog->exec() != QDialog::Accepted)
   677         return;
   678 
   679     document->print(&printer);
   680 }
   681 
   682 void TextEditor::textEditUndo()
   683 {
   684 }
   685 
   686 void TextEditor::toggleFonthint()
   687 {
   688 	setUpdatesEnabled (false);
   689 	e->selectAll ();
   690 	if (!actionFormatUseFixedFont->isOn() ) 
   691 		e->setCurrentFont (varFont);
   692 	else	
   693 		e->setCurrentFont (fixedFont);
   694 	e->selectAll ();
   695 	setUpdatesEnabled (true);
   696 	repaint();
   697 }
   698 
   699 void TextEditor::setFixedFont()
   700 {
   701 	bool ok;
   702 	QFont font =QFontDialog::getFont(
   703                     &ok, fixedFont, this );
   704     if ( ok ) 
   705         // font is set to the font the user selected
   706 		fixedFont=font;
   707 }
   708 
   709 void TextEditor::setVarFont()
   710 {
   711 	bool ok;
   712 	QFont font =QFontDialog::getFont(
   713                     &ok, varFont, this );
   714     if ( ok ) 
   715         // font is set to the font the user selected
   716 		varFont=font;
   717 }
   718 
   719 void TextEditor::textBold()
   720 {
   721     e->setBold( actionTextBold->isOn() );
   722 }
   723 
   724 void TextEditor::textUnderline()
   725 {
   726     e->setUnderline( actionTextUnderline->isOn() );
   727 }
   728 
   729 void TextEditor::textItalic()
   730 {
   731     e->setItalic( actionTextItalic->isOn() );
   732 }
   733 
   734 void TextEditor::textFamily( const QString &f )
   735 {
   736     e->setFamily( f );
   737 }
   738 
   739 void TextEditor::textSize( const QString &p )
   740 {
   741     e->setPointSize( p.toInt() );
   742 }
   743 
   744 
   745 void TextEditor::textColor()
   746 {
   747     QColor col = QColorDialog::getColor( e->color(), this );
   748     if ( !col.isValid() ) return;
   749     e->setColor( col );
   750     QPixmap pix( 16, 16 );
   751     pix.fill( Qt::black );
   752     actionTextColor->setIconSet( pix );
   753 }
   754 
   755 void TextEditor::textAlign( QAction *a )
   756 {
   757     if ( a == actionAlignLeft )
   758 		e->setAlignment( Qt::AlignLeft );
   759     else if ( a == actionAlignCenter )
   760 		e->setAlignment( Qt::AlignHCenter );
   761     else if ( a == actionAlignRight )
   762 		e->setAlignment( Qt::AlignRight );
   763     else if ( a == actionAlignJustify )
   764 		e->setAlignment( Qt::AlignJustify );
   765 }
   766 
   767 void TextEditor::textVAlign()
   768 {
   769 	QTextCharFormat format;
   770 
   771     if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
   772 		format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
   773     } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
   774 		format.setVerticalAlignment(QTextCharFormat::AlignSubScript);
   775     } else {
   776 		format.setVerticalAlignment(QTextCharFormat::AlignNormal);
   777     }
   778 	e->mergeCurrentCharFormat(format);
   779 }
   780 
   781 
   782 void TextEditor::fontChanged( const QFont &f )
   783 {
   784 	int i=comboFont->findText(f.family());
   785     if (i>=0) comboFont->setCurrentIndex (i);
   786 	i=comboSize->findText(QString::number(f.pointSize()));
   787 	if (i>=0) comboSize->setCurrentIndex(i);
   788     actionTextBold->setOn( f.bold() );
   789     actionTextItalic->setOn( f.italic() );
   790     actionTextUnderline->setOn( f.underline() );
   791 }
   792 
   793 void TextEditor::colorChanged( const QColor &c )
   794 {
   795     QPixmap pix( 16, 16 );
   796     pix.fill( c );
   797     actionTextColor->setIconSet( pix );
   798 }
   799 
   800 void TextEditor::formatChanged( const QTextCharFormat &f )
   801 {
   802 	fontChanged(f.font());
   803     colorChanged(f.foreground().color());
   804     alignmentChanged(e->alignment());
   805 	verticalAlignmentChanged (f.verticalAlignment());
   806 }
   807 
   808 void TextEditor::alignmentChanged( int a )
   809 {
   810     if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft ))
   811 		actionAlignLeft->setOn( true );
   812     else if ( ( a & Qt::AlignHCenter ) )
   813 		actionAlignCenter->setOn( true );
   814     else if ( ( a & Qt::AlignRight ) )
   815 		actionAlignRight->setOn( true );
   816     else if ( ( a & Qt::AlignJustify ) )
   817 		actionAlignJustify->setOn( true );
   818 }
   819 
   820 void TextEditor::verticalAlignmentChanged(QTextCharFormat::VerticalAlignment a)
   821 {
   822 	actionAlignSubScript->setOn (false);
   823 	actionAlignSuperScript->setOn (false);
   824 	switch (a)
   825 	{
   826 		case QTextCharFormat::AlignSuperScript: 
   827 			actionAlignSuperScript->setOn (true);
   828 			break;
   829 		case QTextCharFormat::AlignSubScript:
   830 			actionAlignSubScript->setOn (true);
   831 			break;
   832 		default: ;	
   833 	}
   834 }
   835 
   836 
   837 
   838 void TextEditor::enableActions()
   839 {
   840 	actionFileLoad->setEnabled(true);
   841 	actionFileSave->setEnabled(true);
   842 	actionFileSaveAs->setEnabled(true);
   843 	actionFilePrint->setEnabled(true);
   844 	actionEditUndo->setEnabled(true);
   845 	actionEditRedo->setEnabled(true);
   846 	actionEditCopy->setEnabled(true);
   847 	actionEditCut->setEnabled(true);
   848 	actionEditPaste->setEnabled(true);
   849 	actionEditDeleteAll->setEnabled(true);
   850 	actionFormatUseFixedFont->setEnabled(true);
   851 }
   852 
   853 void TextEditor::disableActions()
   854 {
   855 	actionFileLoad->setEnabled(false);
   856 	actionFileSave->setEnabled(false);
   857 	actionFileSaveAs->setEnabled(false);
   858 	actionFilePrint->setEnabled(false);
   859 	actionEditUndo->setEnabled(false);
   860 	actionEditRedo->setEnabled(false);
   861 	actionEditCopy->setEnabled(false);
   862 	actionEditCut->setEnabled(false);
   863 	actionEditPaste->setEnabled(false);
   864 	actionEditDeleteAll->setEnabled(false);
   865 	actionFormatUseFixedFont->setEnabled(false);
   866 }
   867 
   868 void TextEditor::setState (EditorState s)
   869 {
   870 	
   871 	QPalette p=palette();
   872 	QColor c;
   873 	switch (s)
   874 	{
   875 		case emptyEditor:    c=QColor (150,150,150); break;
   876 		case filledEditor:   c=QColor (255,255,255); break;
   877 		case inactiveEditor: c=QColor (0,0,0);
   878 	}
   879     p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
   880     p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
   881     e->setPalette(p);
   882 }
   883 
   884