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