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