texteditor.cpp
author insilmaril
Tue, 06 Jun 2006 14:58:11 +0000
branchqt4-port
changeset 2 608f976aa7bb
parent 0 7a96bd401351
child 3 6a0342b3c519
permissions -rw-r--r--
First compilation on QT4
     1 #include "texteditor.h"
     2 
     3 #include <q3canvas.h>
     4 #include <qstatusbar.h>
     5 #include <qmessagebox.h>
     6 #include <qaction.h>
     7 #include <qapplication.h>
     8 #include <qpainter.h>
     9 #include <qprinter.h>
    10 #include <qfile.h>
    11 #include <q3filedialog.h>
    12 #include <q3toolbar.h>
    13 #include <q3popupmenu.h>
    14 #include <qmenubar.h>
    15 #include <q3textedit.h>
    16 #include <q3accel.h>
    17 #include <qtextstream.h>
    18 #include <q3paintdevicemetrics.h>
    19 #include <qsettings.h>
    20 #include <qfontdialog.h>
    21 #include <qmessagebox.h>
    22 #include <qcolordialog.h>
    23 #include <qregexp.h>
    24 #include <qlineedit.h>
    25 #include <q3simplerichtext.h>
    26 //Added by qt3to4:
    27 #include <Q3ValueList>
    28 #include <QPixmap>
    29 #include <Q3ActionGroup>
    30 #include <QCloseEvent>
    31 
    32 #include <iostream>
    33 #include <cstdlib>
    34 #include <typeinfo>
    35 
    36 #include "noteobj.h"
    37 #include "version.h"
    38 
    39 extern Q3Canvas* actCanvas;
    40 extern int statusbarTime;
    41 extern QSettings settings;
    42 
    43 extern QAction *actionViewToggleNoteEditor;
    44 
    45 extern QString iconPath;
    46 
    47 using namespace std;
    48 
    49 
    50 ///////////////////////////////////////////////////////////////////////
    51 ///////////////////////////////////////////////////////////////////////
    52 
    53 
    54 TextEditor::TextEditor()
    55 {
    56     printer = new QPrinter( QPrinter::HighResolution );
    57 	printer->setPrinterName (settings.readEntry("/vym/mainwindow/printerName",printer->printerName()));
    58 
    59 	// Editor (use MyTextEdit to wrap verticalAlignmentChanged to 
    60 	//         int argument. see header file)
    61     e = new MyTextEdit( this, "editor" );
    62     e->setFocus();
    63 	e->setTextFormat(Qt::RichText);		// default
    64 	e->setTabStopWidth (20);		// unit is pixel
    65 	e->setColor (Qt::black);
    66 	connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
    67     setCentralWidget( e );
    68     statusBar()->message( "Ready", statusbarTime);
    69 	setCaption(__VYM " - " +tr ("Note Editor"));
    70 
    71 	// Toolbars
    72 	setupFileActions();
    73 	setupEditActions();
    74 	setupFormatActions();
    75 	setupSettingsActions();
    76 	
    77 	// Various states
    78 	emptyPaper   = QBrush(Qt::gray);
    79 	filledPaper  = QBrush(Qt::white);
    80 	inactivePaper= QBrush(Qt::black);
    81 	setInactive();
    82 
    83 	// Load Settings
    84 	resize (settings.readNumEntry( "/vym/noteeditor/geometry/width", 450),
    85 	        settings.readNumEntry( "/vym/noteeditor/geometry/height",600));
    86 	move   (settings.readNumEntry( "/vym/noteeditor/geometry/posX", 150),
    87 	        settings.readNumEntry( "/vym/noteeditor/geometry/posY",  50));
    88 	
    89 	if (settings.readEntry( "/vym/noteeditor/showWithMain","yes") =="yes")
    90 		setShowWithMain(true);
    91 	else	
    92 		setShowWithMain(false);
    93 
    94 	varFont.fromString( settings.readEntry 
    95 		("/vym/noteeditor/fonts/varFont",
    96 		"Nimbus Sans l,14,-1,5,48,0,0,0,0,0") 
    97 	);
    98 	fixedFont.fromString (settings.readEntry (
    99 		"/vym/noteeditor/fonts/fixedFont",
   100 		"Courier,14,-1,5,48,0,0,0,1,0") 
   101 	);
   102 	QString s=settings.readEntry ("/vym/noteeditor/fonts/fonthintDefault","variable");
   103 	if (s == "fixed")
   104 	{	
   105 		actionSettingsFonthintDefault->setOn (true);
   106 		e->setCurrentFont (fixedFont);
   107 	} else	
   108 	{
   109 		actionSettingsFonthintDefault->setOn (false);
   110 		e->setCurrentFont (varFont);
   111 	}	
   112 	filenameHint="";
   113 
   114 	// Save settings in vymrc
   115 	settings.writeEntry("/vym/mainwindow/printerName",printer->printerName());
   116 }
   117 
   118 
   119 TextEditor::~TextEditor()
   120 {
   121     if (printer) delete printer;
   122 	// Save Settings
   123 	settings.writeEntry( "/vym/noteeditor/geometry/width", width() );
   124 	settings.writeEntry( "/vym/noteeditor/geometry/height", height() );
   125 	settings.writeEntry( "/vym/noteeditor/geometry/posX", pos().x() );
   126 	settings.writeEntry( "/vym/noteeditor/geometry/posY", pos().y() );
   127 	
   128 	if (showWithMain())
   129 		settings.writeEntry( "/vym/noteeditor/showWithMain","yes");
   130 	else	
   131 		settings.writeEntry( "/vym/noteeditor/showWithMain","no");
   132 
   133 	QString s;
   134 	if (actionSettingsFonthintDefault->isOn() )
   135 		s="fixed";
   136 	else	
   137 		s="variable";
   138 	settings.writeEntry( "/vym/noteeditor/fonts/fonthintDefault",s );
   139 	settings.writeEntry ("/vym/noteeditor/fonts/varFont",
   140 		varFont.toString() );
   141 	settings.writeEntry ("/vym/noteeditor/fonts/fixedFont",
   142 		fixedFont.toString() );
   143 }
   144 
   145 bool TextEditor::isEmpty()
   146 {
   147 	if (e->text().length())
   148 		return false;
   149 	else
   150 		return true;
   151 }
   152 
   153 void TextEditor::setShowWithMain(bool v)
   154 {
   155 	showwithmain=v;
   156 }
   157 
   158 bool TextEditor::showWithMain()
   159 {
   160 	return showwithmain;
   161 }
   162 
   163 void TextEditor::setFontHint (const QString &fh)
   164 {
   165 	if (fh=="fixed")
   166 		actionFormatUseFixedFont->setOn (true);
   167 	else
   168 		actionFormatUseFixedFont->setOn (false);
   169 }
   170 
   171 
   172 QString TextEditor::getFontHint()
   173 {
   174 	if (actionFormatUseFixedFont->isOn())
   175 		return "fixed";
   176 	else	
   177 		return "var";
   178 }
   179 
   180 QString TextEditor::getFontHintDefault()
   181 {
   182 	if (actionSettingsFonthintDefault->isOn())
   183 		return "fixed";
   184 	else	
   185 		return "var";
   186 }
   187 
   188 void TextEditor::setFilename(const QString &fn)
   189 {
   190 	if (state==filledEditor)
   191 		if (fn.isEmpty() )
   192 		{
   193 			filename="";
   194 			statusBar()->message( "No filename available for this note.", statusbarTime );
   195 		}	
   196 		else
   197 		{
   198 			filename=fn;
   199 			statusBar()->message( QString( "Current filename is %1" ).arg( filename ), statusbarTime );
   200 		}	
   201 }
   202 
   203 QString TextEditor::getFilename()
   204 {
   205 	return filename;
   206 }
   207 
   208 void TextEditor::setFilenameHint(const QString &fnh)
   209 {
   210 	filenameHint=fnh;
   211 }
   212 
   213 QString TextEditor::getFilenameHint()
   214 {
   215 	return filenameHint;
   216 }
   217 
   218 bool TextEditor::findText(const QString &t, const bool &cs)
   219 {
   220 	bool wo=false;	// word matches
   221 	if (e->find (t, cs, wo, true, 0, 0 ))
   222 		return true;
   223 	else	
   224 		return false;
   225 }
   226 
   227 void TextEditor::setupFileActions()
   228 {
   229     Q3ToolBar *tb = new Q3ToolBar( this );
   230     tb->setLabel( "File Actions" );
   231     Q3PopupMenu *menu = new Q3PopupMenu( this );
   232     menuBar()->insertItem( tr( "&File" ), menu );
   233 
   234     QAction *a;
   235     a = new QAction( QPixmap( iconPath+"fileopen.png"), tr( "&Import..." ),this);
   236 	a->setStatusTip (tr( "Import" ) );
   237 	a->setShortcut( Qt::CTRL + Qt::Key_O );
   238     connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) );
   239 	a->setEnabled(false);
   240     a->addTo( tb );
   241     a->addTo( menu );
   242 	actionFileLoad=a;
   243 
   244     menu->insertSeparator();
   245     a = new QAction( QPixmap(iconPath+"filesave.png" ), tr( "&Export..." ),this);
   246 	a->setStatusTip (tr( "Export Note (HTML)" ) );
   247 	a->setShortcut( Qt::CTRL + Qt::Key_S );
   248     connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) );
   249     a->addTo( tb );
   250     a->addTo( menu );
   251 	actionFileSave=a;
   252 	
   253     a = new QAction(  QPixmap(), tr( "Export &As... (HTML)" ), this);
   254 	a->setStatusTip (tr( "Export Note As (HTML) " ));
   255     connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) );
   256     a->addTo( menu );
   257 	actionFileSaveAs=a;
   258 
   259     a = new QAction(QPixmap(), tr( "Export &As...(ASCII)" ), this);
   260 	a->setStatusTip ( tr( "Export Note As (ASCII) " ) );
   261 	a->setShortcut(Qt::ALT + Qt::Key_X );
   262     connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) );
   263     a->addTo( menu );
   264 	actionFileSaveAs=a;
   265 
   266     menu->insertSeparator();
   267     a = new QAction( QPixmap(iconPath+"fileprint.png" ), tr( "&Print..." ),this);
   268 	a->setStatusTip (tr( "Print Note" ) );
   269 	a->setShortcut( Qt::CTRL + Qt::Key_P );
   270     connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) );
   271     a->addTo( tb );
   272     a->addTo( menu );
   273 	actionFilePrint=a;
   274 }
   275 
   276 void TextEditor::setupEditActions()
   277 {
   278     Q3ToolBar *tb = new Q3ToolBar( this );
   279     tb->setLabel( "Edit Actions" );
   280     Q3PopupMenu *menu = new Q3PopupMenu( this );
   281     menuBar()->insertItem( tr( "&Edit" ), menu );
   282 
   283     QAction *a;
   284     a = new QAction(QPixmap(iconPath+"undo.png"), tr( "&Undo" ), this );
   285 	a->setStatusTip ( tr( "Undo" ) );
   286 	a->setShortcut(Qt::CTRL + Qt::Key_Z );
   287     connect( a, SIGNAL( activated() ), e, SLOT( undo() ) );
   288     a->addTo( menu );
   289     a->addTo( tb);
   290 	actionEditUndo=a;
   291 	
   292     a = new QAction(QPixmap(iconPath+"redo.png" ), tr( "&Redo" ),this); 
   293 	a->setStatusTip ( tr( "Redo" ) );
   294 	a->setShortcut( Qt::CTRL + Qt::Key_Y );
   295     connect( a, SIGNAL( activated() ), e, SLOT( redo() ) );
   296     a->addTo( tb );
   297     a->addTo( menu );
   298 	actionEditRedo=a;
   299 
   300     menu->insertSeparator();
   301     a = new QAction(QPixmap(), tr( "Select and copy &all" ),this); 
   302 	a->setStatusTip ( tr( "Select and copy all" ) );
   303 	a->setShortcut( Qt::CTRL + Qt::Key_A );
   304     connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) );
   305     a->addTo( menu );
   306 
   307     menu->insertSeparator();
   308     a = new QAction(QPixmap(iconPath+"editcopy.png" ), tr( "&Copy" ),this);
   309 	a->setStatusTip ( tr( "Copy" ) );
   310 	a->setShortcut( Qt::CTRL + Qt::Key_C );
   311     connect( a, SIGNAL( activated() ), e, SLOT( copy() ) );
   312     a->addTo( tb );
   313     a->addTo( menu );
   314 	actionEditCopy=a;
   315 	
   316     a = new QAction(QPixmap(iconPath+"editcut.png" ), tr( "Cu&t" ),this);
   317 	a->setStatusTip ( tr( "Cut" ) );
   318 	a->setShortcut( Qt::CTRL + Qt::Key_X );
   319     connect( a, SIGNAL( activated() ), e, SLOT( cut() ) );
   320     a->addTo( tb );
   321     a->addTo( menu );
   322 	actionEditCut=a;
   323 
   324     a = new QAction(QPixmap(iconPath+"editpaste.png" ), tr( "&Paste" ),this);
   325 	a->setStatusTip ( tr( "Paste" ) );
   326 	a->setShortcut( Qt::CTRL + Qt::Key_V );
   327     connect( a, SIGNAL( activated() ), e, SLOT( paste() ) );
   328     a->addTo( tb );
   329     a->addTo( menu );
   330 	actionEditPaste=a;
   331 	
   332     a = new QAction( QPixmap( iconPath+"edittrash.png"), tr( "&Delete All" ), this);
   333 	a->setStatusTip (tr( "Delete all" ) );
   334     connect( a, SIGNAL( activated() ), e, SLOT( clear() ) );
   335     a->addTo( tb );
   336     a->addTo( menu );
   337 	actionEditDeleteAll=a;
   338 
   339 	a = new QAction(QPixmap(), tr( "&Convert Paragraphs" ),this);
   340 	a->setStatusTip(tr( "Convert paragraphs to linebreaks" )); 
   341 	a->setShortcut( Qt::ALT + Qt::Key_P );
   342     connect( a, SIGNAL( activated() ), this, SLOT( textConvertPar() ) );
   343     a->addTo( menu );
   344 	actionEditConvertPar=a;
   345 
   346 	a = new QAction( QPixmap(), tr( "&Join lines" ), this);
   347 	a->setStatusTip(tr( "Join all lines of a paragraph" ) ); 
   348 	a->setShortcut(Qt::ALT + Qt::Key_J );
   349     connect( a, SIGNAL( activated() ), this, SLOT( textJoinLines() ) );
   350     a->addTo( menu );
   351 	actionEditJoinLines=a;
   352 }
   353 
   354 void TextEditor::setupFormatActions()
   355 {
   356     Q3ToolBar *tb = new Q3ToolBar( this );
   357     tb->setLabel( "Format Actions" );
   358     Q3PopupMenu *menu = new Q3PopupMenu( this );
   359     menuBar()->insertItem( tr( "F&ormat" ), menu );
   360 
   361     QAction *a;
   362 
   363     a = new QAction( QPixmap(iconPath+"formatfixedfont.png"), tr( "&Font hint" ), Qt::ALT + Qt::Key_I, this, "fontHint" );
   364 	a->setStatusTip (tr( "Toggle font hint for the whole text" ) );
   365 	a->setToggleAction (true);
   366 	a->setOn (settings.readBoolEntry ("/vym/noteeditor/fonts/useFixedByDefault",false) );
   367     connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
   368     a->addTo( menu );
   369     a->addTo( tb );
   370 	actionFormatUseFixedFont=a;
   371 
   372 	menu->insertSeparator();
   373 
   374     comboFont = new QComboBox( true, tb );
   375     QFontDatabase db;
   376     comboFont->insertStringList( db.families() );
   377     connect( comboFont, SIGNAL( activated( const QString & ) ),
   378 	     this, SLOT( textFamily( const QString & ) ) );
   379     comboFont->lineEdit()->setText( QApplication::font().family() );
   380 
   381     comboSize = new QComboBox( true, tb );
   382     Q3ValueList<int> sizes = db.standardSizes();
   383     Q3ValueList<int>::Iterator it = sizes.begin();
   384     for ( ; it != sizes.end(); ++it )
   385 	comboSize->insertItem( QString::number( *it ) );
   386     connect( comboSize, SIGNAL( activated( const QString & ) ),
   387 	     this, SLOT( textSize( const QString & ) ) );
   388     comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
   389 
   390     menu->insertSeparator();
   391 
   392     QPixmap pix( 16, 16 );
   393     pix.fill( e->color());
   394     actionTextColor = new QAction( pix, tr( "&Color..." ), this);
   395     actionTextColor->addTo( tb );
   396     actionTextColor->addTo( menu );
   397     connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
   398 
   399     actionTextBold = new QAction( QPixmap (iconPath+"text_bold.png"), tr( "&Bold" ), this);
   400 	actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B );
   401     connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
   402     actionTextBold->addTo( tb );
   403     actionTextBold->addTo( menu );
   404     actionTextBold->setToggleAction( true );
   405     actionTextItalic = new QAction( QPixmap(iconPath+"text_italic.png"), tr( "&Italic" ),  this);
   406 	actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
   407     connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
   408     actionTextItalic->addTo( tb );
   409     actionTextItalic->addTo( menu );
   410     actionTextItalic->setToggleAction( true );
   411     actionTextUnderline = new QAction( QPixmap (iconPath+"text_under.png"), tr( "&Underline" ), this);
   412 	actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U );
   413     connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
   414     actionTextUnderline->addTo( tb );
   415     actionTextUnderline->addTo( menu );
   416     actionTextUnderline->setToggleAction( true );
   417     menu->insertSeparator();
   418 
   419     Q3ActionGroup *grp = new Q3ActionGroup( this );
   420     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
   421 
   422     actionAlignLeft = new QAction( QPixmap (iconPath+"text_left.png"), tr( "&Left" ),grp );
   423 	actionAlignLeft->setShortcut( Qt::CTRL+Qt::Key_L );
   424     actionAlignLeft->setToggleAction( true );
   425     actionAlignCenter = new QAction( QPixmap (iconPath+"text_center.png"), tr( "C&enter" ),grp);
   426     actionAlignCenter->setShortcut(  Qt::CTRL + Qt::Key_E);
   427     actionAlignCenter->setToggleAction( true );
   428     actionAlignRight = new QAction( QPixmap (iconPath+"text_right.png" ), tr( "&Right" ), grp);
   429 	actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R );
   430     actionAlignRight->setToggleAction( true );
   431     actionAlignJustify = new QAction( QPixmap ( iconPath+"text_block.png"), tr( "&Justify" ), grp );
   432 	actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J );
   433     actionAlignJustify->setToggleAction( true );
   434 
   435     grp->addTo( tb );
   436     grp->addTo( menu );
   437 
   438     Q3ActionGroup *grp2 = new Q3ActionGroup( this );
   439     grp2->setExclusive(false);
   440     actionAlignSubScript = new QAction( QPixmap (iconPath+"text_sub.png"), tr( "Subs&cript" ),grp2 );
   441 	actionAlignSubScript->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_B );
   442     actionAlignSubScript->setToggleAction( true );
   443     connect(actionAlignSubScript, SIGNAL(activated()), this, SLOT(textVAlign()));
   444 
   445     actionAlignSuperScript = new QAction( QPixmap (iconPath+"text_super.png"), tr( "Su&perscript" ),grp2  );
   446 	actionAlignSuperScript->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_P );
   447     actionAlignSuperScript->setToggleAction( true );
   448     connect(actionAlignSuperScript, SIGNAL(activated()), this, SLOT(textVAlign()));
   449 
   450     menu->insertSeparator();
   451     
   452     grp2->addTo(tb);
   453     grp2->addTo(menu);
   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     connect( e, SIGNAL( currentAlignmentChanged( int ) ),
   460 	     this, SLOT( alignmentChanged( int ) ) );
   461     connect( e, SIGNAL( currentVerticalAlignmentChanged(int)),
   462 	     this, SLOT( verticalAlignmentChanged(int)));
   463 
   464 }
   465 
   466 void TextEditor::setupSettingsActions()
   467 {
   468     Q3PopupMenu *menu = new Q3PopupMenu( this );
   469     menuBar()->insertItem( tr( "&Settings" ), menu );
   470 
   471     QAction *a;
   472     a = new QAction(tr( "Set &fixed font" ), this);
   473 	a->setStatusTip ( tr( "Set fixed font" ));
   474     connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
   475     a->addTo( menu );
   476 	actionSettingsFixedFont=a;
   477 
   478     a = new QAction(tr( "Set &variable font" ), this);
   479 	a->setStatusTip ( tr( "Set variable font" ) );
   480     connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
   481     a->addTo( menu );
   482 	actionSettingsVarFont=a;
   483 
   484     a = new QAction(tr( "&fixed font is default" ),  this);
   485 	a->setStatusTip (tr( "Used fixed font by default" ) );
   486 	a->setToggleAction (true);
   487 	// set state later in constructor...
   488     a->addTo( menu );
   489 	actionSettingsFonthintDefault=a;
   490 }
   491 
   492 void TextEditor::textLoad()
   493 {
   494 	if (state!=inactiveEditor)
   495 	{
   496 		if (e->length()) 
   497 		{
   498 			QMessageBox mb( __VYM " - " +tr("Note Editor"),
   499 				"Loading will overwrite the existing note",
   500 				QMessageBox::Warning,
   501 				QMessageBox::Yes | QMessageBox::Default,
   502 				QMessageBox::Cancel,
   503 				0 );
   504 			mb.setButtonText( QMessageBox::Yes, "Load note" );
   505 			switch( mb.exec() ) {
   506 				case QMessageBox::Cancel:
   507 					return;
   508 					break;
   509 			}
   510 		} 
   511 		// Load note
   512 		Q3FileDialog *fd=new Q3FileDialog( this);
   513 		fd->addFilter ("ASCII texts (*.txt)");
   514 		fd->addFilter ("VYM notes (*.html)");
   515 		fd->show();
   516 		QString fn;
   517 		if ( fd->exec() == QDialog::Accepted )
   518 			fn = fd->selectedFile();
   519 
   520 		if ( !fn.isEmpty() )
   521 		{
   522 			QFile f( fn );
   523 			if ( !f.open( QIODevice::ReadOnly ) )
   524 			return;
   525 
   526 			QTextStream ts( &f );
   527 			setText( ts.read() );
   528 			editorChanged();
   529 		}
   530 	}
   531 }
   532 
   533 void TextEditor::closeEvent( QCloseEvent* ce )
   534 {
   535     ce->accept();	// TextEditor can be reopened with show()
   536     actionViewToggleNoteEditor->setOn (false);
   537     showwithmain=false;
   538     return;
   539 }
   540 
   541 QString TextEditor::getText()
   542 {
   543 	return e->text();
   544 }
   545 
   546 void TextEditor::editorChanged()
   547 {
   548 	if (isEmpty())
   549 		state=emptyEditor;
   550 	else
   551 		state=filledEditor;
   552 
   553 		if (state==emptyEditor)
   554 			e->setPaper (emptyPaper);
   555 		else
   556 			e->setPaper (filledPaper);
   557 	// SLOT is LinkableMapObj, which will update systemFlag
   558 	emit (textHasChanged() );
   559 }
   560 
   561 
   562 void TextEditor::setText(QString t)
   563 {
   564 	if ( !Q3StyleSheet::mightBeRichText( t ) )
   565 		t = Q3StyleSheet::convertFromPlainText( t, Q3StyleSheetItem::WhiteSpaceNormal );
   566 	e->setReadOnly(false);
   567 	e->setText(t);
   568 //	editorChanged();	//not called automagically  FIXME or is it?
   569 
   570 	enableActions();
   571 }
   572 
   573 void TextEditor::setInactive()
   574 {
   575 	state=inactiveEditor;
   576 	setText("");
   577 	e->setPaper (inactivePaper);
   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 = Q3FileDialog::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 	int parFrom, parTo, indFrom, indTo;
   662 	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   663 	QString t;
   664 	if (parFrom>-1)
   665 		t=e->selectedText();
   666 	else
   667 		t=e->text();
   668 
   669 	QRegExp re("<p.*>");
   670 	re.setMinimal(true);
   671 	t.replace (re,"");
   672 	t.replace ("</p>","<br />");
   673 	if (parFrom>-1)
   674 	{
   675 		e->setCursorPosition (parFrom,indFrom);
   676 		e->cut();
   677 		// Tried to simply insert the changed text with
   678 		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   679 		// but then the html would be quoted. So I use the ugly
   680 		// way: insert a marker, replace it in whole text of QTextEdit
   681 		QString marker="R3PlAcEMeL4teR!";
   682 		e->insert (marker);
   683 		e->setText (e->text().replace(marker,t));
   684 	} else
   685 		e->setText(t);
   686 }
   687 
   688 void TextEditor::textJoinLines()
   689 {
   690 	int parFrom, parTo, indFrom, indTo;
   691 	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   692 	QString t;
   693 	if (parFrom>-1)
   694 		t=e->selectedText();
   695 	else
   696 		t=e->text();
   697 	// In addition to textConvertPar it is sometimes
   698 	// useful to join all lines of a paragraph
   699 	QRegExp re("</p>\n+<p>(?!</p>)");
   700 	re.setMinimal(true);
   701 	t.replace (re," ");
   702 
   703 	// Above we may have introduced new " " at beginning of a
   704 	// paragraph - remove it.
   705 	re.setPattern("<p> ");
   706 	t.replace (re,"<p>");
   707 	if (parFrom>-1)
   708 	{
   709 		e->setCursorPosition (parFrom,indFrom);
   710 		e->cut();
   711 		// Tried to simply insert the changed text with
   712 		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   713 		// but then the html would be quoted. So I use the ugly
   714 		// way: insert a marker, replace it in whole text of QTextEdit
   715 		QString marker="R3PlAcEMeL4teR!";
   716 		e->insert (marker);
   717 		e->setText (e->text().replace(marker,t));
   718 	} else
   719 		e->setText(t);
   720 }
   721 
   722 void TextEditor::textExportAsASCII()
   723 {
   724 	QString text = NoteObj (e->text()).getNoteASCII();
   725 	QString fn,s;
   726 	if (!filenameHint.isEmpty())
   727 	{
   728 		if (!filenameHint.contains (".txt"))
   729 			s=filenameHint+".txt";
   730 		else	
   731 			s=filenameHint;
   732 	} else	
   733 		s=QString::null;
   734 	fn = Q3FileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   735 	int ret=-1;
   736 
   737     if ( !fn.isEmpty() ) 
   738 	{
   739 		QFile file (fn);
   740 		if (file.exists())
   741 		{
   742 			QMessageBox mb( "VYM",
   743 				tr("The file ") + fn + 
   744 				tr(" exists already. "
   745 				"Do you want to overwrite it?"),
   746 			QMessageBox::Warning,
   747 			QMessageBox::Yes | QMessageBox::Default,
   748 			QMessageBox::Cancel | QMessageBox::Escape,
   749 			Qt::NoButton );
   750 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   751 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   752 			ret=mb.exec();
   753 		}	
   754 		if (ret==QMessageBox::Cancel)
   755 			return;
   756 			
   757 		// save 
   758 		if ( !file.open( QIODevice::WriteOnly ) ) 
   759 			statusBar()->message( QString("Could not write to %1").arg(filename),
   760 						  statusbarTime );
   761 		else
   762 		{
   763 			QTextStream t( &file );
   764 			t << text;
   765 			file.close();
   766 
   767 			statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
   768 		}
   769     }
   770 }
   771 
   772 
   773 void TextEditor::textPrint()
   774 {
   775     printer->setFullPage(TRUE);
   776     if ( printer->setup( this ) ) 
   777 	{
   778 		QPainter p( printer );
   779 		// Check that there is a valid device to print to.
   780 		if ( !p.device() ) return;
   781 		Q3PaintDeviceMetrics metrics( p.device() );
   782 		int dpiy = metrics.logicalDpiY();
   783 		int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
   784 		QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
   785 		QFont font( e->currentFont() );
   786 		font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
   787 
   788 		Q3SimpleRichText richText( e->text(), font,
   789 					  e->context(),
   790 					  e->styleSheet(),
   791 					  e->mimeSourceFactory(),
   792 					  body.height() );
   793 		richText.setWidth( &p, body.width() );
   794 		QRect view( body );
   795 		int page = 1;
   796 		do 
   797 		{
   798 			richText.draw( &p, body.left(), body.top(), view, colorGroup() );
   799 			view.moveBy( 0, body.height() );
   800 			p.translate( 0 , -body.height() );
   801 			p.setFont( font );
   802 			p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
   803 				view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
   804 			if ( view.top()  >= richText.height() )
   805 			break;
   806 			printer->newPage();
   807 			page++;
   808 		} while (TRUE);
   809     }
   810 }
   811 
   812 void TextEditor::textEditUndo()
   813 {
   814 }
   815 
   816 void TextEditor::toggleFonthint()
   817 {
   818 	setUpdatesEnabled (false);
   819 	e->selectAll (true);
   820 	if (!actionFormatUseFixedFont->isOn() ) 
   821 		e->setCurrentFont (varFont);
   822 	else	
   823 		e->setCurrentFont (fixedFont);
   824 	e->selectAll (false);
   825 	setUpdatesEnabled (true);
   826 	repaint();
   827 }
   828 
   829 void TextEditor::setFixedFont()
   830 {
   831 	bool ok;
   832 	QFont font =QFontDialog::getFont(
   833                     &ok, fixedFont, this );
   834     if ( ok ) 
   835         // font is set to the font the user selected
   836 		fixedFont=font;
   837 }
   838 
   839 void TextEditor::setVarFont()
   840 {
   841 	bool ok;
   842 	QFont font =QFontDialog::getFont(
   843                     &ok, varFont, this );
   844     if ( ok ) 
   845         // font is set to the font the user selected
   846 		varFont=font;
   847 }
   848 
   849 void TextEditor::textBold()
   850 {
   851     e->setBold( actionTextBold->isOn() );
   852 }
   853 
   854 void TextEditor::textUnderline()
   855 {
   856     e->setUnderline( actionTextUnderline->isOn() );
   857 }
   858 
   859 void TextEditor::textItalic()
   860 {
   861     e->setItalic( actionTextItalic->isOn() );
   862 }
   863 
   864 void TextEditor::textFamily( const QString &f )
   865 {
   866     e->setFamily( f );
   867 }
   868 
   869 void TextEditor::textSize( const QString &p )
   870 {
   871     e->setPointSize( p.toInt() );
   872 }
   873 
   874 
   875 void TextEditor::textColor()
   876 {
   877     QColor col = QColorDialog::getColor( e->color(), this );
   878     if ( !col.isValid() )
   879 	return;
   880     e->setColor( col );
   881     QPixmap pix( 16, 16 );
   882     pix.fill( Qt::black );
   883     actionTextColor->setIconSet( pix );
   884 }
   885 
   886 void TextEditor::textAlign( QAction *a )
   887 {
   888     if ( a == actionAlignLeft )
   889 	e->setAlignment( Qt::AlignLeft );
   890     else if ( a == actionAlignCenter )
   891 	e->setAlignment( Qt::AlignHCenter );
   892     else if ( a == actionAlignRight )
   893 	e->setAlignment( Qt::AlignRight );
   894     else if ( a == actionAlignJustify )
   895 	e->setAlignment( Qt::AlignJustify );
   896 }
   897 
   898 void TextEditor::textVAlign()
   899 {
   900     if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
   901 	e->setVerticalAlignment( Q3TextEdit::AlignSuperScript);
   902     } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
   903 	e->setVerticalAlignment( Q3TextEdit::AlignSubScript);
   904     } else {
   905 	e->setVerticalAlignment( Q3TextEdit::AlignNormal);
   906     }
   907 }
   908 
   909 
   910 void TextEditor::fontChanged( const QFont &f )
   911 {
   912     comboFont->lineEdit()->setText( f.family() );
   913     comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
   914     actionTextBold->setOn( f.bold() );
   915     actionTextItalic->setOn( f.italic() );
   916     actionTextUnderline->setOn( f.underline() );
   917 }
   918 
   919 void TextEditor::colorChanged( const QColor &c )
   920 {
   921     QPixmap pix( 16, 16 );
   922     pix.fill( c );
   923     actionTextColor->setIconSet( pix );
   924 }
   925 
   926 void TextEditor::alignmentChanged( int a )
   927 {
   928     if ( ( a == Qt::AlignLeft ) || ( a & Qt::AlignLeft ))
   929 	actionAlignLeft->setOn( true );
   930     else if ( ( a & Qt::AlignHCenter ) )
   931 	actionAlignCenter->setOn( true );
   932     else if ( ( a & Qt::AlignRight ) )
   933 	actionAlignRight->setOn( true );
   934     else if ( ( a & Qt::AlignJustify ) )
   935 	actionAlignJustify->setOn( true );
   936 }
   937 
   938 void TextEditor::verticalAlignmentChanged(int a) 
   939 {
   940     if (a == Q3TextEdit::AlignSuperScript ) {
   941 	actionAlignSuperScript->setOn(true);
   942 	actionAlignSubScript->setOn(false);
   943     } else if (a == Q3TextEdit::AlignSubScript ) {
   944 	actionAlignSuperScript->setOn(false);
   945 	actionAlignSubScript->setOn(true);
   946     } else {
   947 	actionAlignSuperScript->setOn(false);
   948 	actionAlignSubScript->setOn(false);
   949     }
   950 }
   951 
   952 
   953 
   954 void TextEditor::enableActions()
   955 {
   956 	actionFileLoad->setEnabled(true);
   957 	actionFileSave->setEnabled(true);
   958 	actionFileSaveAs->setEnabled(true);
   959 	actionFilePrint->setEnabled(true);
   960 	actionEditUndo->setEnabled(true);
   961 	actionEditRedo->setEnabled(true);
   962 	actionEditCopy->setEnabled(true);
   963 	actionEditCut->setEnabled(true);
   964 	actionEditPaste->setEnabled(true);
   965 	actionEditDeleteAll->setEnabled(true);
   966 	actionEditConvertPar->setEnabled(true);
   967 	actionEditJoinLines->setEnabled(true);
   968 	actionFormatUseFixedFont->setEnabled(true);
   969 }
   970 
   971 void TextEditor::disableActions()
   972 {
   973 	actionFileLoad->setEnabled(false);
   974 	actionFileSave->setEnabled(false);
   975 	actionFileSaveAs->setEnabled(false);
   976 	actionFilePrint->setEnabled(false);
   977 	actionEditUndo->setEnabled(false);
   978 	actionEditRedo->setEnabled(false);
   979 	actionEditCopy->setEnabled(false);
   980 	actionEditCut->setEnabled(false);
   981 	actionEditPaste->setEnabled(false);
   982 	actionEditDeleteAll->setEnabled(false);
   983 	actionEditConvertPar->setEnabled(false);
   984 	actionEditJoinLines->setEnabled(false);
   985 	actionFormatUseFixedFont->setEnabled(false);
   986 }
   987 
   988