texteditor.cpp
author insilmaril
Thu, 06 Jul 2006 14:47:01 +0000
branchqt4-port
changeset 9 f94317a94db1
parent 5 5cfbba1dc2f8
child 10 e2a80d9b5858
permissions -rw-r--r--
Text Editor now shows states again
     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 	a->setStatusTip(tr( "Convert paragraphs to linebreaks" )); 
   318 	a->setShortcut( Qt::ALT + Qt::Key_P );
   319     connect( a, SIGNAL( activated() ), this, SLOT( textConvertPar() ) );
   320 	editMenu->addAction (a);
   321 	actionEditConvertPar=a;
   322 
   323 	a = new QAction( QPixmap(), tr( "&Join lines" ), this);
   324 	a->setStatusTip(tr( "Join all lines of a paragraph" ) ); 
   325 	a->setShortcut(Qt::ALT + Qt::Key_J );
   326     connect( a, SIGNAL( activated() ), this, SLOT( textJoinLines() ) );
   327 	editMenu->addAction (a);
   328 	actionEditJoinLines=a;
   329 }
   330 
   331 void TextEditor::setupFormatActions()
   332 {
   333     QToolBar *tb = addToolBar ( tr("Format Actions" ));
   334     QMenu *formatMenu = menuBar()->addMenu ( tr( "F&ormat" ));
   335 
   336     QAction *a;
   337 
   338     a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" );
   339 	a->setStatusTip (tr( "Toggle font hint for the whole text" ) );
   340 	a->setToggleAction (true);
   341 	a->setOn (settings.readBoolEntry ("/vym/noteeditor/fonts/useFixedByDefault",false) );
   342     connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
   343 	formatMenu->addAction (a);
   344 	tb->addAction (a);
   345 	actionFormatUseFixedFont=a;
   346 
   347 //    comboFont = new QComboBox( true, tb );
   348     comboFont = new QComboBox;
   349 	tb->addWidget (comboFont);
   350     QFontDatabase fontDB;
   351     comboFont->insertStringList( fontDB.families() );
   352     connect( comboFont, SIGNAL( activated( const QString & ) ),
   353 	     this, SLOT( textFamily( const QString & ) ) );
   354     comboFont->addItem( QApplication::font().family() );
   355     comboSize = new QComboBox;
   356 	tb->addWidget (comboSize);
   357 	QList<int> sizes=fontDB.standardSizes();
   358 	QList<int>::iterator i = sizes.begin();
   359 	while (i != sizes.end()) 
   360 	{
   361 		++i; // increment i before using it
   362 		comboSize->insertItem ( QString::number(*i));
   363 	}	
   364     connect( comboSize, SIGNAL( activated( const QString & ) ),
   365 	     this, SLOT( textSize( const QString & ) ) );
   366     comboSize->addItem ( QString::number( QApplication::font().pointSize() ) );
   367 
   368     formatMenu->addSeparator();
   369 
   370     QPixmap pix( 16, 16 );
   371     pix.fill( e->color());
   372     a = new QAction( pix, tr( "&Color..." ), this);
   373 	formatMenu->addAction (a);
   374 	tb->addAction (a);
   375     connect( a, SIGNAL( activated() ), this, SLOT( textColor() ) );
   376     actionTextColor=a;
   377 
   378     a = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this);
   379 	a->setShortcut(Qt::CTRL + Qt::Key_B );
   380     connect( a, SIGNAL( activated() ), this, SLOT( textBold() ) );
   381 	tb->addAction (a);
   382 	formatMenu->addAction (a);
   383     a->setToggleAction( true );
   384     actionTextBold=a;
   385 	
   386     a = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ),  this);
   387 	a->setShortcut(Qt::CTRL + Qt::Key_I);
   388     connect( a, SIGNAL( activated() ), this, SLOT( textItalic() ) );
   389 	tb->addAction (a);
   390 	formatMenu->addAction (a);
   391     a->setToggleAction( true );
   392     actionTextItalic=a;
   393 	
   394     a = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this);
   395 	a->setShortcut(Qt::CTRL + Qt::Key_U );
   396     connect( a, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
   397 	tb->addAction (a);
   398 	formatMenu->addAction (a);
   399     a->setToggleAction( true );
   400     actionTextUnderline=a;
   401     formatMenu->addSeparator();
   402 
   403     QActionGroup *grp = new QActionGroup( this );
   404     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
   405 
   406     a = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp );
   407 	a->setShortcut( Qt::CTRL+Qt::Key_L );
   408     a->setToggleAction( true );
   409 	tb->addAction (a);
   410 	formatMenu->addAction (a);
   411     actionAlignLeft=a;
   412     a = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp);
   413     a->setShortcut(  Qt::CTRL + Qt::Key_E);
   414     a->setToggleAction( true );
   415 	tb->addAction (a);
   416 	formatMenu->addAction (a);
   417     actionAlignCenter=a;
   418     a = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp);
   419 	a->setShortcut(Qt::CTRL + Qt::Key_R );
   420     a->setToggleAction( true );
   421 	tb->addAction (a);
   422 	formatMenu->addAction (a);
   423     actionAlignRight=a;
   424     a = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp );
   425 	a->setShortcut(Qt::CTRL + Qt::Key_J );
   426     a->setToggleAction( true );
   427 	tb->addAction (a);
   428 	formatMenu->addAction (a);
   429     actionAlignJustify=a;
   430 
   431     QActionGroup *grp2 = new QActionGroup( this );
   432     grp2->setExclusive(true);
   433     a = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 );
   434 	a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B );
   435     a->setToggleAction( true );
   436 	tb->addAction (a);
   437 	formatMenu->addAction (a);
   438     connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
   439     actionAlignSubScript=a;
   440 
   441     a = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2  );
   442 	a->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P );
   443     a->setToggleAction( true );
   444 	tb->addAction (a);
   445 	formatMenu->addAction (a);
   446     connect(a, SIGNAL(activated()), this, SLOT(textVAlign()));
   447     actionAlignSuperScript=a;
   448 
   449     formatMenu->addSeparator();
   450     
   451     connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
   452 	     this, SLOT( fontChanged( const QFont & ) ) );
   453     connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
   454 	     this, SLOT( colorChanged( const QColor & ) ) );
   455 		 /*
   456     connect( e, SIGNAL( currentAlignmentChanged( int ) ),
   457 	     this, SLOT( alignmentChanged( int ) ) );
   458     connect( e, SIGNAL( currentVerticalAlignmentChanged(int)),
   459 	     this, SLOT( verticalAlignmentChanged(int)));
   460 		 */
   461 
   462 }
   463 
   464 void TextEditor::setupSettingsActions()
   465 {
   466     QMenu *settingsMenu = menuBar()->addMenu ( tr( "&Settings" ));
   467 
   468     QAction *a;
   469     a = new QAction(tr( "Set &fixed font" ), this);
   470 	a->setStatusTip ( tr( "Set fixed font" ));
   471     connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
   472 	settingsMenu->addAction (a);
   473 	actionSettingsFixedFont=a;
   474 
   475     a = new QAction(tr( "Set &variable font" ), this);
   476 	a->setStatusTip ( tr( "Set variable font" ) );
   477     connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
   478 	settingsMenu->addAction (a);
   479 	actionSettingsVarFont=a;
   480 
   481     a = new QAction(tr( "&fixed font is default" ),  this);
   482 	a->setStatusTip (tr( "Used fixed font by default" ) );
   483 	a->setToggleAction (true);
   484 	// set state later in constructor...
   485 	settingsMenu->addAction (a);
   486 	actionSettingsFonthintDefault=a;
   487 }
   488 
   489 void TextEditor::textLoad()
   490 {
   491 	if (state!=inactiveEditor)
   492 	{
   493 		if (e->text().length()) 
   494 		{
   495 			QMessageBox mb( __VYM " - " +tr("Note Editor"),
   496 				"Loading will overwrite the existing note",
   497 				QMessageBox::Warning,
   498 				QMessageBox::Yes | QMessageBox::Default,
   499 				QMessageBox::Cancel,
   500 				0 );
   501 			mb.setButtonText( QMessageBox::Yes, "Load note" );
   502 			switch( mb.exec() ) {
   503 				case QMessageBox::Cancel:
   504 					return;
   505 					break;
   506 			}
   507 		} 
   508 		// Load note
   509 		QFileDialog *fd=new QFileDialog( this);
   510 		QStringList types;
   511 		types<< "VYM notes (*.html)" <<
   512 			"ASCII texts (*.txt)" <<
   513 			"All filed (*)";
   514 		fd->setFilters (types);
   515 		fd->setDirectory (QDir().current());
   516 		fd->show();
   517 		QString fn;
   518 		if ( fd->exec() == QDialog::Accepted )
   519 			fn = fd->selectedFile();
   520 
   521 		if ( !fn.isEmpty() )
   522 		{
   523 			QFile f( fn );
   524 			if ( !f.open( QIODevice::ReadOnly ) )
   525 			return;
   526 
   527 			QTextStream ts( &f );
   528 			setText( ts.read() );
   529 			editorChanged();
   530 		}
   531 	}
   532 }
   533 
   534 void TextEditor::closeEvent( QCloseEvent* ce )
   535 {
   536     ce->accept();	// TextEditor can be reopened with show()
   537     actionViewToggleNoteEditor->setOn (false);
   538     showwithmain=false;
   539     return;
   540 }
   541 
   542 QString TextEditor::getText()
   543 {
   544 	if (e->toPlainText().isEmpty())
   545 		return "";
   546 	else	
   547 		return e->text();
   548 }
   549 
   550 void TextEditor::editorChanged()
   551 {
   552 	if (isEmpty())
   553 		state=emptyEditor;
   554 	else
   555 		state=filledEditor;
   556 
   557 		if (state==emptyEditor)
   558 			setState (emptyEditor);
   559 		else
   560 			setState (filledEditor);
   561 	// SLOT is LinkableMapObj, which will update systemFlag
   562 	emit (textHasChanged() );
   563 }
   564 
   565 
   566 void TextEditor::setText(QString t)
   567 {
   568 	e->setReadOnly(false);
   569 	e->setText(t);
   570 	enableActions();
   571 }
   572 
   573 void TextEditor::setInactive()
   574 {
   575 	state=inactiveEditor;
   576 	setText("");
   577 	setState (inactiveEditor);
   578 	e->setReadOnly (true);
   579 
   580 	disableActions();
   581 }
   582 
   583 void TextEditor::editCopyAll()
   584 {
   585 	e->selectAll();
   586 	e->copy();
   587 }
   588 
   589 void TextEditor::textSaveAs()
   590 {
   591     QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   592 					       this,"export note dialog",tr("Export Note to single file") );
   593 
   594     if ( !fn.isEmpty() ) 
   595 	{
   596 		QFile file (fn);
   597 		if (file.exists())
   598 		{
   599 			QMessageBox mb( __VYM,
   600 				tr("The file ") + fn + 
   601 				tr(" exists already. "
   602 				"Do you want to overwrite it?"),
   603 			QMessageBox::Warning,
   604 			QMessageBox::Yes | QMessageBox::Default,
   605 			QMessageBox::Cancel | QMessageBox::Escape,
   606 			Qt::NoButton );
   607 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   608 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   609 			switch( mb.exec() ) {
   610 				case QMessageBox::Yes:
   611 					// save 
   612 					filename = fn;
   613 					textSave();
   614 					return;
   615 				case QMessageBox::Cancel:
   616 					// do nothing
   617 					break;
   618 			}
   619 		} else
   620 		{
   621 			filename = fn;
   622 			textSave();
   623 			return;
   624 		}			
   625     }
   626 	statusBar()->message(tr( "Couldn't export note ") + fn, statusbarTime );
   627 }
   628 
   629 
   630 void TextEditor::textSave()
   631 {
   632     if ( filename.isEmpty() ) 
   633 	{
   634 		textSaveAs();
   635 		return;
   636     }
   637 
   638     QString text = e->text();
   639     QFile f( filename );
   640     if ( !f.open( QIODevice::WriteOnly ) ) 
   641 	{
   642 		statusBar()->message( QString("Could not write to %1").arg(filename),
   643 					  statusbarTime );
   644 		return;
   645     }
   646 
   647     QTextStream t( &f );
   648     t << text;
   649     f.close();
   650 
   651     e->setModified( FALSE );
   652 
   653     statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
   654 }
   655 
   656 void TextEditor::textConvertPar()
   657 {
   658 	// In X11 a copy&paste generates paragraphs, 
   659 	// which is not always wanted
   660 	// This function replaces paragraphs by linebreaks.
   661 
   662 	e->setBackgroundRole (QPalette::Text);
   663 	cout << "dark\n";
   664 
   665 	QString t;
   666 	t=e->text();
   667 	QRegExp re("<p.*>");
   668 	re.setMinimal(true);
   669 	t.replace (re,"");
   670 	t.replace ("</p>","<br />");
   671 	e->setText(t);
   672 
   673 	/* FIXME QT3 use seletion ()
   674 	int parFrom, parTo, indFrom, indTo;
   675 	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   676 	if (parFrom>-1)
   677 		t=e->selectedText();
   678 	else
   679 		t=e->text();
   680 
   681 	QRegExp re("<p.*>");
   682 	re.setMinimal(true);
   683 	t.replace (re,"");
   684 	t.replace ("</p>","<br />");
   685 	if (parFrom>-1)
   686 	{
   687 		e->setCursorPosition (parFrom,indFrom);
   688 		e->cut();
   689 		// Tried to simply insert the changed text with
   690 		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   691 		// but then the html would be quoted. So I use the ugly
   692 		// way: insert a marker, replace it in whole text of QTextEdit
   693 		QString marker="R3PlAcEMeL4teR!";
   694 		e->insert (marker);
   695 		e->setText (e->text().replace(marker,t));
   696 	} else
   697 		e->setText(t);
   698 	*/	
   699 }
   700 
   701 void TextEditor::textJoinLines()
   702 {
   703 	e->setBackgroundRole (QPalette::Light);
   704 	cout << "light\n";
   705 
   706 /* FIXME QT3
   707 	int parFrom, parTo, indFrom, indTo;
   708 	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   709 	QString t;
   710 	if (parFrom>-1)
   711 		t=e->selectedText();
   712 	else
   713 		t=e->text();
   714 	// In addition to textConvertPar it is sometimes
   715 	// useful to join all lines of a paragraph
   716 	QRegExp re("</p>\n+<p>(?!</p>)");
   717 	re.setMinimal(true);
   718 	t.replace (re," ");
   719 
   720 	// Above we may have introduced new " " at beginning of a
   721 	// paragraph - remove it.
   722 	re.setPattern("<p> ");
   723 	t.replace (re,"<p>");
   724 	if (parFrom>-1)
   725 	{
   726 		e->setCursorPosition (parFrom,indFrom);
   727 		e->cut();
   728 		// Tried to simply insert the changed text with
   729 		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   730 		// but then the html would be quoted. So I use the ugly
   731 		// way: insert a marker, replace it in whole text of QTextEdit
   732 		QString marker="R3PlAcEMeL4teR!";
   733 		e->insert (marker);
   734 		e->setText (e->text().replace(marker,t));
   735 	} else
   736 		e->setText(t);
   737 */		
   738 }
   739 
   740 void TextEditor::textExportAsASCII()
   741 {
   742 	QString text = NoteObj (e->text()).getNoteASCII();
   743 	QString fn,s;
   744 	if (!filenameHint.isEmpty())
   745 	{
   746 		if (!filenameHint.contains (".txt"))
   747 			s=filenameHint+".txt";
   748 		else	
   749 			s=filenameHint;
   750 	} else	
   751 		s=QString::null;
   752 	fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   753 	int ret=-1;
   754 
   755     if ( !fn.isEmpty() ) 
   756 	{
   757 		QFile file (fn);
   758 		if (file.exists())
   759 		{
   760 			QMessageBox mb( "VYM",
   761 				tr("The file ") + fn + 
   762 				tr(" exists already. "
   763 				"Do you want to overwrite it?"),
   764 			QMessageBox::Warning,
   765 			QMessageBox::Yes | QMessageBox::Default,
   766 			QMessageBox::Cancel | QMessageBox::Escape,
   767 			Qt::NoButton );
   768 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   769 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   770 			ret=mb.exec();
   771 		}	
   772 		if (ret==QMessageBox::Cancel)
   773 			return;
   774 			
   775 		// save 
   776 		if ( !file.open( QIODevice::WriteOnly ) ) 
   777 			statusBar()->message( QString("Could not write to %1").arg(filename),
   778 						  statusbarTime );
   779 		else
   780 		{
   781 			QTextStream t( &file );
   782 			t << text;
   783 			file.close();
   784 
   785 			statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
   786 		}
   787     }
   788 }
   789 
   790 
   791 void TextEditor::textPrint()
   792 {
   793 
   794     QTextDocument *document = e->document();
   795     QPrinter printer;
   796 
   797     QPrintDialog *dialog = new QPrintDialog(&printer, this);
   798     dialog->setWindowTitle(tr("Print Document"));
   799     if (dialog->exec() != QDialog::Accepted)
   800         return;
   801 
   802     document->print(&printer);
   803 }
   804 
   805 void TextEditor::textEditUndo()
   806 {
   807 }
   808 
   809 void TextEditor::toggleFonthint()
   810 {
   811 	setUpdatesEnabled (false);
   812 	e->selectAll ();
   813 	if (!actionFormatUseFixedFont->isOn() ) 
   814 		e->setCurrentFont (varFont);
   815 	else	
   816 		e->setCurrentFont (fixedFont);
   817 	e->selectAll ();
   818 	setUpdatesEnabled (true);
   819 	repaint();
   820 }
   821 
   822 void TextEditor::setFixedFont()
   823 {
   824 	bool ok;
   825 	QFont font =QFontDialog::getFont(
   826                     &ok, fixedFont, this );
   827     if ( ok ) 
   828         // font is set to the font the user selected
   829 		fixedFont=font;
   830 }
   831 
   832 void TextEditor::setVarFont()
   833 {
   834 	bool ok;
   835 	QFont font =QFontDialog::getFont(
   836                     &ok, varFont, this );
   837     if ( ok ) 
   838         // font is set to the font the user selected
   839 		varFont=font;
   840 }
   841 
   842 void TextEditor::textBold()
   843 {
   844     e->setBold( actionTextBold->isOn() );
   845 }
   846 
   847 void TextEditor::textUnderline()
   848 {
   849     e->setUnderline( actionTextUnderline->isOn() );
   850 }
   851 
   852 void TextEditor::textItalic()
   853 {
   854     e->setItalic( actionTextItalic->isOn() );
   855 }
   856 
   857 void TextEditor::textFamily( const QString &f )
   858 {
   859     e->setFamily( f );
   860 }
   861 
   862 void TextEditor::textSize( const QString &p )
   863 {
   864     e->setPointSize( p.toInt() );
   865 }
   866 
   867 
   868 void TextEditor::textColor()
   869 {
   870     QColor col = QColorDialog::getColor( e->color(), this );
   871     if ( !col.isValid() )
   872 	return;
   873     e->setColor( col );
   874     QPixmap pix( 16, 16 );
   875     pix.fill( Qt::black );
   876     actionTextColor->setIconSet( pix );
   877 }
   878 
   879 void TextEditor::textAlign( QAction *a )
   880 {
   881     if ( a == actionAlignLeft )
   882 	e->setAlignment( Qt::AlignLeft );
   883     else if ( a == actionAlignCenter )
   884 	e->setAlignment( Qt::AlignHCenter );
   885     else if ( a == actionAlignRight )
   886 	e->setAlignment( Qt::AlignRight );
   887     else if ( a == actionAlignJustify )
   888 	e->setAlignment( Qt::AlignJustify );
   889 }
   890 
   891 void TextEditor::textVAlign()
   892 {
   893 /* FIXME QT3
   894     if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
   895 	e->setVerticalAlignment( QTextEdit::AlignSuperScript);
   896     } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
   897 	e->setVerticalAlignment( QTextEdit::AlignSubScript);
   898     } else {
   899 	e->setVerticalAlignment( QTextEdit::AlignNormal);
   900     }
   901 */	
   902 }
   903 
   904 
   905 void TextEditor::fontChanged( const QFont &f )
   906 {
   907 /*
   908     comboFont->lineEdit()->setText( f.family() );
   909     comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
   910 */	
   911     actionTextBold->setOn( f.bold() );
   912     actionTextItalic->setOn( f.italic() );
   913     actionTextUnderline->setOn( f.underline() );
   914 }
   915 
   916 void TextEditor::colorChanged( const QColor &c )
   917 {
   918     QPixmap pix( 16, 16 );
   919     pix.fill( c );
   920     actionTextColor->setIconSet( pix );
   921 }
   922 
   923 void TextEditor::alignmentChanged( int a )
   924 {
   925     if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft ))
   926 	actionAlignLeft->setOn( true );
   927     else if ( ( a & Qt::AlignHCenter ) )
   928 	actionAlignCenter->setOn( true );
   929     else if ( ( a & Qt::AlignRight ) )
   930 	actionAlignRight->setOn( true );
   931     else if ( ( a & Qt::AlignJustify ) )
   932 	actionAlignJustify->setOn( true );
   933 }
   934 
   935 void TextEditor::verticalAlignmentChanged(int a) 
   936 {
   937 	/* FIXME QT3
   938     if (a == QTextEdit::AlignSuperScript ) {
   939 	actionAlignSuperScript->setOn(true);
   940 	actionAlignSubScript->setOn(false);
   941     } else if (a == QTextEdit::AlignSubScript ) {
   942 	actionAlignSuperScript->setOn(false);
   943 	actionAlignSubScript->setOn(true);
   944     } else {
   945 	actionAlignSuperScript->setOn(false);
   946 	actionAlignSubScript->setOn(false);
   947     }
   948 	*/
   949 }
   950 
   951 
   952 
   953 void TextEditor::enableActions()
   954 {
   955 	actionFileLoad->setEnabled(true);
   956 	actionFileSave->setEnabled(true);
   957 	actionFileSaveAs->setEnabled(true);
   958 	actionFilePrint->setEnabled(true);
   959 	actionEditUndo->setEnabled(true);
   960 	actionEditRedo->setEnabled(true);
   961 	actionEditCopy->setEnabled(true);
   962 	actionEditCut->setEnabled(true);
   963 	actionEditPaste->setEnabled(true);
   964 	actionEditDeleteAll->setEnabled(true);
   965 	actionEditConvertPar->setEnabled(true);
   966 	actionEditJoinLines->setEnabled(true);
   967 	actionFormatUseFixedFont->setEnabled(true);
   968 }
   969 
   970 void TextEditor::disableActions()
   971 {
   972 	actionFileLoad->setEnabled(false);
   973 	actionFileSave->setEnabled(false);
   974 	actionFileSaveAs->setEnabled(false);
   975 	actionFilePrint->setEnabled(false);
   976 	actionEditUndo->setEnabled(false);
   977 	actionEditRedo->setEnabled(false);
   978 	actionEditCopy->setEnabled(false);
   979 	actionEditCut->setEnabled(false);
   980 	actionEditPaste->setEnabled(false);
   981 	actionEditDeleteAll->setEnabled(false);
   982 	actionEditConvertPar->setEnabled(false);
   983 	actionEditJoinLines->setEnabled(false);
   984 	actionFormatUseFixedFont->setEnabled(false);
   985 }
   986 
   987 void TextEditor::setState (EditorState s)
   988 {
   989 	
   990 	QPalette p=palette();
   991 	QColor c;
   992 	switch (s)
   993 	{
   994 		case emptyEditor:    c=QColor (150,150,150); break;
   995 		case filledEditor:   c=QColor (255,255,255); break;
   996 		case inactiveEditor: c=QColor (0,0,0);
   997 	}
   998     p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
   999     p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
  1000     e->setPalette(p);
  1001 }
  1002 
  1003