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