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