texteditor.cpp
author insilmaril
Thu, 21 Jan 2010 11:56:57 +0000
changeset 821 4a84d7e444d8
parent 807 f9f7922989d8
child 823 0bba81dde1bc
permissions -rw-r--r--
Various fixes, including combined progress bar for loading maps
     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<< "VYM notes (*.html)" <<
   525 			"ASCII texts (*.txt)" <<
   526 			"All filed (*)";
   527 		fd->setFilters (types);
   528 		fd->setDirectory (QDir().current());
   529 		fd->show();
   530 		QString fn;
   531 		if ( fd->exec() == QDialog::Accepted )
   532 			fn = fd->selectedFile();
   533 
   534 		if ( !fn.isEmpty() )
   535 		{
   536 			QFile f( fn );
   537 			if ( !f.open( QIODevice::ReadOnly ) )
   538 			return;
   539 
   540 			QTextStream ts( &f );
   541 			setText( ts.read() );
   542 			editorChanged();
   543 		}
   544 	}
   545 }
   546 
   547 void TextEditor::closeEvent( QCloseEvent* ce )
   548 {
   549     ce->accept();	// TextEditor can be reopened with show()
   550 	showwithmain=false;
   551 	hide();
   552 	emit (windowClosed() );
   553     return;
   554 }
   555 
   556 void TextEditor::editorChanged()
   557 {
   558 	if (isEmpty())
   559 		state=emptyEditor;
   560 	else
   561 		state=filledEditor;
   562 
   563 		if (state==emptyEditor)
   564 			setState (emptyEditor);
   565 		else
   566 			setState (filledEditor);
   567 	// SLOT is LinkableMapObj, which will update systemFlag
   568 	if (!blockChangedSignal) emit (textHasChanged() );
   569 }
   570 
   571 
   572 void TextEditor::setText(const QString &t)
   573 {
   574 	blockChangedSignal=true;
   575 	e->setReadOnly(false);
   576 	reset();
   577 	if (Qt::mightBeRichText (t))
   578 		e->setHtml(t);
   579 	else
   580 	{
   581 		actionFormatUseFixedFont->setChecked (true);
   582 		e->setPlainText(t);
   583 	}	
   584 	enableActions();
   585 	blockChangedSignal=false;
   586 }
   587 
   588 void TextEditor::setInactive()
   589 {
   590 	state=inactiveEditor;
   591 	setText("");
   592 	setState (inactiveEditor);
   593 	e->setReadOnly (true);
   594 
   595 	disableActions();
   596 }
   597 
   598 void TextEditor::editCopyAll()
   599 {
   600 	e->selectAll();
   601 	e->copy();
   602 }
   603 
   604 void TextEditor::textSaveAs()
   605 {
   606     QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   607 					       this,"export note dialog",tr("Export Note to single file") );
   608 
   609     if ( !fn.isEmpty() ) 
   610 	{
   611 		QFile file (fn);
   612 		if (file.exists())
   613 		{
   614 			QMessageBox mb( vymName,
   615 				tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn),
   616 			QMessageBox::Warning,
   617 			QMessageBox::Yes | QMessageBox::Default,
   618 			QMessageBox::Cancel | QMessageBox::Escape,
   619 			Qt::NoButton );
   620 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   621 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   622 			switch( mb.exec() ) {
   623 				case QMessageBox::Yes:
   624 					// save 
   625 					filename = fn;
   626 					textSave();
   627 					return;
   628 				case QMessageBox::Cancel:
   629 					// do nothing
   630 					break;
   631 			}
   632 		} else
   633 		{
   634 			filename = fn;
   635 			textSave();
   636 			return;
   637 		}			
   638     }
   639 	statusBar()->message(tr( "Couldn't export note ","dialog 'save note as'") + fn, statusbarTime );
   640 }
   641 
   642 
   643 void TextEditor::textSave()
   644 {
   645     if ( filename.isEmpty() ) 
   646 	{
   647 		textSaveAs();
   648 		return;
   649     }
   650 
   651     QString text = e->text();
   652     QFile f( filename );
   653     if ( !f.open( QIODevice::WriteOnly ) ) 
   654 	{
   655 		statusBar()->message( QString("Could not write to %1").arg(filename),
   656 					  statusbarTime );
   657 		return;
   658     }
   659 
   660     QTextStream t( &f );
   661     t << text;
   662     f.close();
   663 
   664     e->setModified( FALSE );
   665 
   666     statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
   667 }
   668 
   669 void TextEditor::textExportAsASCII()
   670 {
   671 	QString text = NoteObj (e->text()).getNoteASCII();
   672 	QString fn,s;
   673 	if (!filenameHint.isEmpty())
   674 	{
   675 		if (!filenameHint.contains (".txt"))
   676 			s=filenameHint+".txt";
   677 		else	
   678 			s=filenameHint;
   679 	} else	
   680 		s=QString::null;
   681 	fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   682 	int ret=-1;
   683 
   684     if ( !fn.isEmpty() ) 
   685 	{
   686 		QFile file (fn);
   687 		if (file.exists())
   688 		{
   689 			QMessageBox mb( vymName,
   690 				tr("The file %1\nexists already.\nDo you want to overwrite it?","dialog 'save note as'").arg(fn),
   691 			QMessageBox::Warning,
   692 			QMessageBox::Yes | QMessageBox::Default,
   693 			QMessageBox::Cancel | QMessageBox::Escape,
   694 			Qt::NoButton );
   695 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   696 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   697 			ret=mb.exec();
   698 		}	
   699 		if (ret==QMessageBox::Cancel)
   700 			return;
   701 			
   702 		// save 
   703 		if ( !file.open( QIODevice::WriteOnly ) ) 
   704 			statusBar()->message( QString("Could not write to %1").arg(filename),
   705 						  statusbarTime );
   706 		else
   707 		{
   708 			QTextStream t( &file );
   709 			t << text;
   710 			file.close();
   711 
   712 			statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
   713 		}
   714     }
   715 }
   716 
   717 
   718 void TextEditor::textPrint()
   719 {
   720 
   721     QTextDocument *document = e->document();
   722     QPrinter printer;
   723 
   724     QPrintDialog *dialog = new QPrintDialog(&printer, this);
   725     dialog->setWindowTitle(tr("Print Note"));
   726     if (dialog->exec() != QDialog::Accepted)
   727         return;
   728 
   729     document->print(&printer);
   730 }
   731 
   732 void TextEditor::textEditUndo()
   733 {
   734 }
   735 
   736 void TextEditor::toggleFonthint()
   737 {
   738 	setUpdatesEnabled (false);
   739 	e->selectAll ();
   740 	if (!actionFormatUseFixedFont->isOn() ) 
   741 		e->setCurrentFont (varFont);
   742 	else	
   743 		e->setCurrentFont (fixedFont);
   744 	e->selectAll ();
   745 	setUpdatesEnabled (true);
   746 	repaint();
   747 }
   748 
   749 void TextEditor::setFixedFont()
   750 {
   751 	bool ok;
   752 	QFont font =QFontDialog::getFont(
   753                     &ok, fixedFont, this );
   754     if ( ok ) 
   755         // font is set to the font the user selected
   756 		fixedFont=font;
   757 }
   758 
   759 void TextEditor::setVarFont()
   760 {
   761 	bool ok;
   762 	QFont font =QFontDialog::getFont(
   763                     &ok, varFont, this );
   764     if ( ok ) 
   765         // font is set to the font the user selected
   766 		varFont=font;
   767 }
   768 
   769 void TextEditor::textBold()
   770 {
   771     e->setBold( actionTextBold->isOn() );
   772 }
   773 
   774 void TextEditor::textUnderline()
   775 {
   776     e->setUnderline( actionTextUnderline->isOn() );
   777 }
   778 
   779 void TextEditor::textItalic()
   780 {
   781     e->setItalic( actionTextItalic->isOn() );
   782 }
   783 
   784 void TextEditor::textFamily( const QString &f )
   785 {
   786     e->setFamily( f );
   787 }
   788 
   789 void TextEditor::textSize( const QString &p )
   790 {
   791     e->setPointSize( p.toInt() );
   792 }
   793 
   794 
   795 void TextEditor::textColor()
   796 {
   797     QColor col = QColorDialog::getColor( e->color(), this );
   798     if ( !col.isValid() ) return;
   799     e->setColor( col );
   800     QPixmap pix( 16, 16 );
   801     pix.fill( Qt::black );
   802     actionTextColor->setIconSet( pix );
   803 }
   804 
   805 void TextEditor::textAlign( QAction *a )
   806 {
   807     if ( a == actionAlignLeft )
   808 		e->setAlignment( Qt::AlignLeft );
   809     else if ( a == actionAlignCenter )
   810 		e->setAlignment( Qt::AlignHCenter );
   811     else if ( a == actionAlignRight )
   812 		e->setAlignment( Qt::AlignRight );
   813     else if ( a == actionAlignJustify )
   814 		e->setAlignment( Qt::AlignJustify );
   815 }
   816 
   817 void TextEditor::textVAlign()
   818 {
   819 	QTextCharFormat format;
   820 
   821     if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
   822 		format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
   823     } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
   824 		format.setVerticalAlignment(QTextCharFormat::AlignSubScript);
   825     } else {
   826 		format.setVerticalAlignment(QTextCharFormat::AlignNormal);
   827     }
   828 	e->mergeCurrentCharFormat(format);
   829 }
   830 
   831 
   832 void TextEditor::fontChanged( const QFont &f )
   833 {
   834 	int i=comboFont->findText(f.family());
   835     if (i>=0) comboFont->setCurrentIndex (i);
   836 	i=comboSize->findText(QString::number(f.pointSize()));
   837 	if (i>=0) comboSize->setCurrentIndex(i);
   838     actionTextBold->setOn( f.bold() );
   839     actionTextItalic->setOn( f.italic() );
   840     actionTextUnderline->setOn( f.underline() );
   841 }
   842 
   843 void TextEditor::colorChanged( const QColor &c )
   844 {
   845     QPixmap pix( 16, 16 );
   846     pix.fill( c );
   847     actionTextColor->setIconSet( pix );
   848 }
   849 
   850 void TextEditor::formatChanged( const QTextCharFormat &f )
   851 {
   852 	fontChanged(f.font());
   853     colorChanged(f.foreground().color());
   854     alignmentChanged(e->alignment());
   855 	verticalAlignmentChanged (f.verticalAlignment());
   856 }
   857 
   858 void TextEditor::alignmentChanged( int a )
   859 {
   860     if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft ))
   861 		actionAlignLeft->setOn( true );
   862     else if ( ( a & Qt::AlignHCenter ) )
   863 		actionAlignCenter->setOn( true );
   864     else if ( ( a & Qt::AlignRight ) )
   865 		actionAlignRight->setOn( true );
   866     else if ( ( a & Qt::AlignJustify ) )
   867 		actionAlignJustify->setOn( true );
   868 }
   869 
   870 void TextEditor::verticalAlignmentChanged(QTextCharFormat::VerticalAlignment a)
   871 {
   872 	actionAlignSubScript->setOn (false);
   873 	actionAlignSuperScript->setOn (false);
   874 	switch (a)
   875 	{
   876 		case QTextCharFormat::AlignSuperScript: 
   877 			actionAlignSuperScript->setOn (true);
   878 			break;
   879 		case QTextCharFormat::AlignSubScript:
   880 			actionAlignSubScript->setOn (true);
   881 			break;
   882 		default: ;	
   883 	}
   884 }
   885 
   886 
   887 
   888 void TextEditor::enableActions()
   889 {
   890 	actionFileLoad->setEnabled(true);
   891 	actionFileSave->setEnabled(true);
   892 	actionFileSaveAs->setEnabled(true);
   893 	actionFilePrint->setEnabled(true);
   894 	actionEditUndo->setEnabled(true);
   895 	actionEditRedo->setEnabled(true);
   896 	actionEditCopy->setEnabled(true);
   897 	actionEditCut->setEnabled(true);
   898 	actionEditPaste->setEnabled(true);
   899 	actionEditDeleteAll->setEnabled(true);
   900 	actionFormatUseFixedFont->setEnabled(true);
   901 }
   902 
   903 void TextEditor::disableActions()
   904 {
   905 	actionFileLoad->setEnabled(false);
   906 	actionFileSave->setEnabled(false);
   907 	actionFileSaveAs->setEnabled(false);
   908 	actionFilePrint->setEnabled(false);
   909 	actionEditUndo->setEnabled(false);
   910 	actionEditRedo->setEnabled(false);
   911 	actionEditCopy->setEnabled(false);
   912 	actionEditCut->setEnabled(false);
   913 	actionEditPaste->setEnabled(false);
   914 	actionEditDeleteAll->setEnabled(false);
   915 	actionFormatUseFixedFont->setEnabled(false);
   916 }
   917 
   918 void TextEditor::setState (EditorState s)
   919 {
   920 	
   921 	QPalette p=palette();
   922 	QColor c;
   923 	switch (s)
   924 	{
   925 		case emptyEditor:    c=QColor (150,150,150); break;
   926 		case filledEditor:   c=QColor (255,255,255); break;
   927 		case inactiveEditor: c=QColor (0,0,0);
   928 	}
   929     p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
   930     p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
   931     e->setPalette(p);
   932 }
   933 
   934