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