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