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