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