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