texteditor.cpp
branchvendor
changeset 36 67e7883ab157
child 2 608f976aa7bb
child 53 1532402be6c2
child 94 6783e13bb05d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/texteditor.cpp	Sun Jan 30 12:59:07 2005 +0000
     1.3 @@ -0,0 +1,921 @@
     1.4 +#include "texteditor.h"
     1.5 +
     1.6 +#include <qcanvas.h>
     1.7 +#include <qstatusbar.h>
     1.8 +#include <qmessagebox.h>
     1.9 +#include <qaction.h>
    1.10 +#include <qapplication.h>
    1.11 +#include <qpainter.h>
    1.12 +#include <qprinter.h>
    1.13 +#include <qfile.h>
    1.14 +#include <qfiledialog.h>
    1.15 +#include <qtoolbar.h>
    1.16 +#include <qpopupmenu.h>
    1.17 +#include <qmenubar.h>
    1.18 +#include <qtextedit.h>
    1.19 +#include <qaccel.h>
    1.20 +#include <qtextstream.h>
    1.21 +#include <qpaintdevicemetrics.h>
    1.22 +#include <qsettings.h>
    1.23 +#include <qfontdialog.h>
    1.24 +#include <qmessagebox.h>
    1.25 +#include <qcolordialog.h>
    1.26 +#include <qregexp.h>
    1.27 +#include <qlineedit.h>
    1.28 +#include <qsimplerichtext.h>
    1.29 +
    1.30 +#include <iostream>
    1.31 +#include <stdlib.h>
    1.32 +#include <typeinfo>
    1.33 +
    1.34 +#include "icons/fileopen.xpm"
    1.35 +#include "icons/filesave.xpm"
    1.36 +#include "icons/fileprint.xpm"
    1.37 +#include "icons/editundo.xpm"	
    1.38 +#include "icons/editredo.xpm"	
    1.39 +#include "icons/editcopy.xpm"
    1.40 +#include "icons/editcut.xpm"
    1.41 +#include "icons/editpaste.xpm"
    1.42 +#include "icons/edittrash.xpm"
    1.43 +#include "icons/formatfixedfont.xpm"
    1.44 +#include "icons/formattextbold.xpm"
    1.45 +#include "icons/formattextitalic.xpm"
    1.46 +#include "icons/formattextunder.xpm"
    1.47 +#include "icons/formattextleft.xpm"
    1.48 +#include "icons/formattextcenter.xpm"
    1.49 +#include "icons/formattextright.xpm"
    1.50 +#include "icons/formattextjustify.xpm"
    1.51 +
    1.52 +extern QCanvas* actCanvas;
    1.53 +extern int statusbarTime;
    1.54 +extern QSettings settings;
    1.55 +
    1.56 +using namespace std;
    1.57 +
    1.58 +
    1.59 +///////////////////////////////////////////////////////////////////////
    1.60 +///////////////////////////////////////////////////////////////////////
    1.61 +
    1.62 +TextEditor::TextEditor()
    1.63 +{
    1.64 +    printer = new QPrinter( QPrinter::HighResolution );
    1.65 +
    1.66 +	// Editor
    1.67 +    e = new QTextEdit( this, "editor" );
    1.68 +    e->setFocus();
    1.69 +	e->setTextFormat(RichText);		// default
    1.70 +	e->setTabStopWidth (20);		// unit is pixel
    1.71 +	e->setColor (black);
    1.72 +	connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
    1.73 +    setCentralWidget( e );
    1.74 +    statusBar()->message( "Ready", statusbarTime);
    1.75 +	setCaption("VYM - Note Editor");
    1.76 +
    1.77 +	// Toolbars
    1.78 +	setupFileActions();
    1.79 +	setupEditActions();
    1.80 +	setupFormatActions();
    1.81 +	setupSettingsActions();
    1.82 +	
    1.83 +	// Various states
    1.84 +	emptyPaper   = QBrush(gray);
    1.85 +	filledPaper  = QBrush(white);
    1.86 +	inactivePaper= QBrush(black);
    1.87 +	setInactive();
    1.88 +
    1.89 +	// Load Settings
    1.90 +	resize (settings.readNumEntry( "/vym/noteeditor/geometry/width", 450),
    1.91 +	        settings.readNumEntry( "/vym/noteeditor/geometry/height",600));
    1.92 +	move   (settings.readNumEntry( "/vym/noteeditor/geometry/posX", 150),
    1.93 +	        settings.readNumEntry( "/vym/noteeditor/geometry/posY",  50));
    1.94 +	
    1.95 +	if (settings.readEntry( "/vym/noteeditor/showWithMain","yes") =="yes")
    1.96 +		setShowWithMain(true);
    1.97 +	else	
    1.98 +		setShowWithMain(false);
    1.99 +
   1.100 +	varFont.fromString( settings.readEntry 
   1.101 +		("/vym/noteeditor/fonts/varFont",
   1.102 +		"Nimbus Sans l,14,-1,5,48,0,0,0,0,0") 
   1.103 +	);
   1.104 +	fixedFont.fromString (settings.readEntry (
   1.105 +		"/vym/noteeditor/fonts/fixedFont",
   1.106 +		"Courier,14,-1,5,48,0,0,0,1,0") 
   1.107 +	);
   1.108 +	QString s=settings.readEntry ("/vym/noteeditor/fonts/fonthintDefault","variable");
   1.109 +	if (s == "fixed")
   1.110 +	{	
   1.111 +		actionSettingsFonthintDefault->setOn (true);
   1.112 +		e->setCurrentFont (fixedFont);
   1.113 +	} else	
   1.114 +	{
   1.115 +		actionSettingsFonthintDefault->setOn (false);
   1.116 +		e->setCurrentFont (varFont);
   1.117 +	}	
   1.118 +}
   1.119 +
   1.120 +
   1.121 +TextEditor::~TextEditor()
   1.122 +{
   1.123 +    if (printer) delete printer;
   1.124 +	// Save Settings
   1.125 +	settings.writeEntry( "/vym/noteeditor/geometry/width", width() );
   1.126 +	settings.writeEntry( "/vym/noteeditor/geometry/height", height() );
   1.127 +	settings.writeEntry( "/vym/noteeditor/geometry/posX", pos().x() );
   1.128 +	settings.writeEntry( "/vym/noteeditor/geometry/posY", pos().y() );
   1.129 +	
   1.130 +	if (showWithMain())
   1.131 +		settings.writeEntry( "/vym/noteeditor/showWithMain","yes");
   1.132 +	else	
   1.133 +		settings.writeEntry( "/vym/noteeditor/showWithMain","no");
   1.134 +
   1.135 +	QString s;
   1.136 +	if (actionSettingsFonthintDefault->isOn() )
   1.137 +		s="fixed";
   1.138 +	else	
   1.139 +		s="variable";
   1.140 +	settings.writeEntry( "/vym/noteeditor/fonts/fonthintDefault",s );
   1.141 +	settings.writeEntry ("/vym/noteeditor/fonts/varFont",
   1.142 +		varFont.toString() );
   1.143 +	settings.writeEntry ("/vym/noteeditor/fonts/fixedFont",
   1.144 +		fixedFont.toString() );
   1.145 +}
   1.146 +
   1.147 +bool TextEditor::isEmpty()
   1.148 +{
   1.149 +	if (e->text().length())
   1.150 +		return false;
   1.151 +	else
   1.152 +		return true;
   1.153 +}
   1.154 +
   1.155 +void TextEditor::setShowWithMain(bool v)
   1.156 +{
   1.157 +	showwithmain=v;
   1.158 +}
   1.159 +
   1.160 +bool TextEditor::showWithMain()
   1.161 +{
   1.162 +	return showwithmain;
   1.163 +}
   1.164 +
   1.165 +void TextEditor::setFontHint (const QString &fh)
   1.166 +{
   1.167 +	if (fh=="fixed")
   1.168 +		actionFormatUseFixedFont->setOn (true);
   1.169 +	else
   1.170 +		actionFormatUseFixedFont->setOn (false);
   1.171 +}
   1.172 +
   1.173 +
   1.174 +QString TextEditor::getFontHint()
   1.175 +{
   1.176 +	if (actionFormatUseFixedFont->isOn())
   1.177 +		return "fixed";
   1.178 +	else	
   1.179 +		return "var";
   1.180 +}
   1.181 +
   1.182 +QString TextEditor::getFontHintDefault()
   1.183 +{
   1.184 +	if (actionSettingsFonthintDefault->isOn())
   1.185 +		return "fixed";
   1.186 +	else	
   1.187 +		return "var";
   1.188 +}
   1.189 +
   1.190 +void TextEditor::setFilename(const QString &fn)
   1.191 +{
   1.192 +	if (state==filledEditor)
   1.193 +		if (fn.isEmpty() )
   1.194 +		{
   1.195 +			filename="";
   1.196 +			statusBar()->message( "No filename available for this note.", statusbarTime );
   1.197 +		}	
   1.198 +		else
   1.199 +		{
   1.200 +			filename=fn;
   1.201 +			statusBar()->message( QString( "Current filename is %1" ).arg( filename ), statusbarTime );
   1.202 +		}	
   1.203 +}
   1.204 +
   1.205 +QString TextEditor::getFilename()
   1.206 +{
   1.207 +	return filename;
   1.208 +}
   1.209 +
   1.210 +bool TextEditor::findText(const QString &t, const bool &cs)
   1.211 +{
   1.212 +	bool wo=false;	// word matches
   1.213 +	if (e->find (t, cs, wo, true, 0, 0 ))
   1.214 +		return true;
   1.215 +	else	
   1.216 +		return false;
   1.217 +}
   1.218 +
   1.219 +void TextEditor::setupFileActions()
   1.220 +{
   1.221 +    QToolBar *tb = new QToolBar( this );
   1.222 +    tb->setLabel( "File Actions" );
   1.223 +    QPopupMenu *menu = new QPopupMenu( this );
   1.224 +    menuBar()->insertItem( tr( "&File" ), menu );
   1.225 +
   1.226 +    QAction *a;
   1.227 +    a = new QAction( tr( "Import" ), QPixmap( fileopen_xpm), tr( "&Import..." ), CTRL + Key_O, this, "fileImport" );
   1.228 +    connect( a, SIGNAL( activated() ), this, SLOT( textLoad() ) );
   1.229 +	a->setEnabled(false);
   1.230 +    a->addTo( tb );
   1.231 +    a->addTo( menu );
   1.232 +	actionFileLoad=a;
   1.233 +
   1.234 +    menu->insertSeparator();
   1.235 +    a = new QAction( tr( "Export Note (HTML)" ), QPixmap( filesave_xpm ), tr( "&Export..." ), CTRL + Key_S, this, "fileSave" );
   1.236 +    connect( a, SIGNAL( activated() ), this, SLOT( textSave() ) );
   1.237 +    a->addTo( tb );
   1.238 +    a->addTo( menu );
   1.239 +	actionFileSave=a;
   1.240 +	
   1.241 +    a = new QAction( tr( "Export Note As (HTML) " ), QPixmap(), tr( "Export &As... (HTML)" ), 0, this, "exportHTML" );
   1.242 +    connect( a, SIGNAL( activated() ), this, SLOT( textSaveAs() ) );
   1.243 +    a->addTo( menu );
   1.244 +	actionFileSaveAs=a;
   1.245 +
   1.246 +    a = new QAction( tr( "Export Note As (ASCII) " ), QPixmap(), tr( "Export &As...(ASCII)" ), ALT + Key_X, this, "exportASCII" );
   1.247 +    connect( a, SIGNAL( activated() ), this, SLOT( textExportAsASCII() ) );
   1.248 +    a->addTo( menu );
   1.249 +	actionFileSaveAs=a;
   1.250 +
   1.251 +    menu->insertSeparator();
   1.252 +    a = new QAction( tr( "Print Note" ), QPixmap( fileprint_xpm ), tr( "&Print..." ), CTRL + Key_P, this, "filePrint" );
   1.253 +    connect( a, SIGNAL( activated() ), this, SLOT( textPrint() ) );
   1.254 +    a->addTo( tb );
   1.255 +    a->addTo( menu );
   1.256 +	actionFilePrint=a;
   1.257 +}
   1.258 +
   1.259 +void TextEditor::setupEditActions()
   1.260 +{
   1.261 +    QToolBar *tb = new QToolBar( this );
   1.262 +    tb->setLabel( "Edit Actions" );
   1.263 +    QPopupMenu *menu = new QPopupMenu( this );
   1.264 +    menuBar()->insertItem( tr( "&Edit" ), menu );
   1.265 +
   1.266 +    QAction *a;
   1.267 +    a = new QAction( tr( "Undo" ), QPixmap(editundo_xpm), tr( "&Undo" ), CTRL + Key_Z, this, "undoEvent" );
   1.268 +    connect( a, SIGNAL( activated() ), e, SLOT( undo() ) );
   1.269 +    a->addTo( menu );
   1.270 +    a->addTo( tb);
   1.271 +	actionEditUndo=a;
   1.272 +	
   1.273 +    a = new QAction( tr( "Redo" ), QPixmap( editredo_xpm ), tr( "&Redo" ), CTRL + Key_Y, this, "editRedo" ); 
   1.274 +    connect( a, SIGNAL( activated() ), e, SLOT( redo() ) );
   1.275 +    a->addTo( tb );
   1.276 +    a->addTo( menu );
   1.277 +	actionEditRedo=a;
   1.278 +
   1.279 +    menu->insertSeparator();
   1.280 +    a = new QAction( tr( "Select and copy all" ), QPixmap(), tr( "Select and copy &all" ), CTRL + Key_A, this, "editcopyall" ); 
   1.281 +    connect( a, SIGNAL( activated() ), this, SLOT( editCopyAll() ) );
   1.282 +    a->addTo( menu );
   1.283 +
   1.284 +    menu->insertSeparator();
   1.285 +    a = new QAction( tr( "Copy" ), QPixmap( editcopy_xpm ), tr( "&Copy" ), CTRL + Key_C, this, "editCopy" );
   1.286 +    connect( a, SIGNAL( activated() ), e, SLOT( copy() ) );
   1.287 +    a->addTo( tb );
   1.288 +    a->addTo( menu );
   1.289 +	actionEditCopy=a;
   1.290 +	
   1.291 +    a = new QAction( tr( "Cut" ), QPixmap( editcut_xpm ), tr( "Cu&t" ), CTRL + Key_X, this, "editCut" );
   1.292 +    connect( a, SIGNAL( activated() ), e, SLOT( cut() ) );
   1.293 +    a->addTo( tb );
   1.294 +    a->addTo( menu );
   1.295 +	actionEditCut=a;
   1.296 +
   1.297 +    a = new QAction( tr( "Paste" ), QPixmap( editpaste_xpm ), tr( "&Paste" ), CTRL + Key_V, this, "editPaste" );
   1.298 +    connect( a, SIGNAL( activated() ), e, SLOT( paste() ) );
   1.299 +    a->addTo( tb );
   1.300 +    a->addTo( menu );
   1.301 +	actionEditPaste=a;
   1.302 +	
   1.303 +    a = new QAction( tr( "Delete all" ), QPixmap( edittrash_xpm ), tr( "&Delete All" ), 0, this, "editDeleteAll" );
   1.304 +    connect( a, SIGNAL( activated() ), e, SLOT( clear() ) );
   1.305 +    a->addTo( tb );
   1.306 +    a->addTo( menu );
   1.307 +	actionEditDeleteAll=a;
   1.308 +
   1.309 +	a = new QAction( tr( "Convert paragraphs to linebreaks" ), QPixmap(), tr( "&Convert Paragraphs" ), ALT + Key_P, this, "editConvertPar" );
   1.310 +    connect( a, SIGNAL( activated() ), this, SLOT( textConvertPar() ) );
   1.311 +    a->addTo( menu );
   1.312 +	actionEditConvertPar=a;
   1.313 +
   1.314 +	a = new QAction( tr( "Join all lines of a paragraph" ), QPixmap(), tr( "&Join lines" ), ALT + Key_J, this, "editJoinLines" );
   1.315 +    connect( a, SIGNAL( activated() ), this, SLOT( textJoinLines() ) );
   1.316 +    a->addTo( menu );
   1.317 +	actionEditJoinLines=a;
   1.318 +}
   1.319 +
   1.320 +void TextEditor::setupFormatActions()
   1.321 +{
   1.322 +    QToolBar *tb = new QToolBar( this );
   1.323 +    tb->setLabel( "Format Actions" );
   1.324 +    QPopupMenu *menu = new QPopupMenu( this );
   1.325 +    menuBar()->insertItem( tr( "&Format" ), menu );
   1.326 +
   1.327 +    QAction *a;
   1.328 +
   1.329 +    a = new QAction( tr( "Toggle font hint for the whole text" ), QPixmap(formatfixedfont_xpm), tr( "&Font hint" ), ALT + Key_I, this, "fontHint" );
   1.330 +	a->setToggleAction (true);
   1.331 +	a->setOn (settings.readBoolEntry ("/vym/noteeditor/fonts/useFixedByDefault",false) );
   1.332 +    connect( a, SIGNAL( activated() ), this, SLOT( toggleFonthint() ) );
   1.333 +    a->addTo( menu );
   1.334 +    a->addTo( tb );
   1.335 +	actionFormatUseFixedFont=a;
   1.336 +
   1.337 +	menu->insertSeparator();
   1.338 +
   1.339 +    comboFont = new QComboBox( true, tb );
   1.340 +    QFontDatabase db;
   1.341 +    comboFont->insertStringList( db.families() );
   1.342 +    connect( comboFont, SIGNAL( activated( const QString & ) ),
   1.343 +	     this, SLOT( textFamily( const QString & ) ) );
   1.344 +    comboFont->lineEdit()->setText( QApplication::font().family() );
   1.345 +
   1.346 +    comboSize = new QComboBox( true, tb );
   1.347 +    QValueList<int> sizes = db.standardSizes();
   1.348 +    QValueList<int>::Iterator it = sizes.begin();
   1.349 +    for ( ; it != sizes.end(); ++it )
   1.350 +	comboSize->insertItem( QString::number( *it ) );
   1.351 +    connect( comboSize, SIGNAL( activated( const QString & ) ),
   1.352 +	     this, SLOT( textSize( const QString & ) ) );
   1.353 +    comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
   1.354 +
   1.355 +    menu->insertSeparator();
   1.356 +
   1.357 +    QPixmap pix( 16, 16 );
   1.358 +    pix.fill( e->color());
   1.359 +    actionTextColor = new QAction( pix, tr( "&Color..." ), 0, this, "textColor" );
   1.360 +    connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
   1.361 +    actionTextColor->addTo( tb );
   1.362 +    actionTextColor->addTo( menu );
   1.363 +
   1.364 +    actionTextBold = new QAction( QPixmap (formattextbold_xpm), tr( "&Bold" ), CTRL + Key_B, this, "textBold" );
   1.365 +    connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
   1.366 +    actionTextBold->addTo( tb );
   1.367 +    actionTextBold->addTo( menu );
   1.368 +    actionTextBold->setToggleAction( true );
   1.369 +    actionTextItalic = new QAction( QPixmap(formattextitalic_xpm ), tr( "&Italic" ), CTRL + Key_I, this, "textItalic" );
   1.370 +    connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
   1.371 +    actionTextItalic->addTo( tb );
   1.372 +    actionTextItalic->addTo( menu );
   1.373 +    actionTextItalic->setToggleAction( true );
   1.374 +    actionTextUnderline = new QAction( QPixmap (formattextunder_xpm ), tr( "&Underline" ), CTRL + Key_U, this, "textUnderline" );
   1.375 +    connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
   1.376 +    actionTextUnderline->addTo( tb );
   1.377 +    actionTextUnderline->addTo( menu );
   1.378 +    actionTextUnderline->setToggleAction( true );
   1.379 +    menu->insertSeparator();
   1.380 +
   1.381 +    QActionGroup *grp = new QActionGroup( this );
   1.382 +    connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
   1.383 +
   1.384 +    actionAlignLeft = new QAction( QPixmap (formattextleft_xpm ), tr( "&Left" ), CTRL + Key_L, grp, "textLeft" );
   1.385 +    actionAlignLeft->setToggleAction( true );
   1.386 +    actionAlignCenter = new QAction( QPixmap (formattextcenter_xpm ), tr( "C&enter" ), CTRL + Key_E, grp, "textCenter" );
   1.387 +    actionAlignCenter->setToggleAction( true );
   1.388 +    actionAlignRight = new QAction( QPixmap (formattextright_xpm ), tr( "&Right" ), CTRL + Key_R, grp, "textRight" );
   1.389 +    actionAlignRight->setToggleAction( true );
   1.390 +    actionAlignJustify = new QAction( QPixmap ( formattextjustify_xpm ), tr( "&Justify" ), CTRL + Key_J, grp, "textjustify" );
   1.391 +    actionAlignJustify->setToggleAction( true );
   1.392 +
   1.393 +    grp->addTo( tb );
   1.394 +    grp->addTo( menu );
   1.395 +
   1.396 +    connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
   1.397 +	     this, SLOT( fontChanged( const QFont & ) ) );
   1.398 +    connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
   1.399 +	     this, SLOT( colorChanged( const QColor & ) ) );
   1.400 +    connect( e, SIGNAL( currentAlignmentChanged( int ) ),
   1.401 +	     this, SLOT( alignmentChanged( int ) ) );
   1.402 +
   1.403 +}
   1.404 +
   1.405 +void TextEditor::setupSettingsActions()
   1.406 +{
   1.407 +    QPopupMenu *menu = new QPopupMenu( this );
   1.408 +    menuBar()->insertItem( tr( "&Settings" ), menu );
   1.409 +
   1.410 +    QAction *a;
   1.411 +    a = new QAction( tr( "Set fixed font" ), QPixmap(), tr( "Set &fixed font" ), 0, this, "setFixedFont" );
   1.412 +    connect( a, SIGNAL( activated() ), this, SLOT( setFixedFont() ) );
   1.413 +    a->addTo( menu );
   1.414 +	actionSettingsFixedFont=a;
   1.415 +
   1.416 +    a = new QAction( tr( "Set variable font" ), QPixmap(), tr( "Set &variable font" ), 0, this, "setvariableFont" );
   1.417 +    connect( a, SIGNAL( activated() ), this, SLOT( setVarFont() ) );
   1.418 +    a->addTo( menu );
   1.419 +	actionSettingsVarFont=a;
   1.420 +
   1.421 +    a = new QAction( tr( "Used fixed font by default" ), QPixmap(), tr( "&fixed font is default" ), 0, this, "fonthintDefault" );
   1.422 +	a->setToggleAction (true);
   1.423 +	// set state later in constructor...
   1.424 +    a->addTo( menu );
   1.425 +	actionSettingsFonthintDefault=a;
   1.426 +}
   1.427 +
   1.428 +void TextEditor::textLoad()
   1.429 +{
   1.430 +	if (state!=inactiveEditor)
   1.431 +	{
   1.432 +		if (e->length()) 
   1.433 +		{
   1.434 +			QMessageBox mb( "VYM - Note Editor",
   1.435 +				"Loading will overwrite the existing note",
   1.436 +				QMessageBox::Warning,
   1.437 +				QMessageBox::Yes | QMessageBox::Default,
   1.438 +				QMessageBox::Cancel,
   1.439 +				0 );
   1.440 +			mb.setButtonText( QMessageBox::Yes, "Load note" );
   1.441 +			switch( mb.exec() ) {
   1.442 +				case QMessageBox::Cancel:
   1.443 +					return;
   1.444 +					break;
   1.445 +			}
   1.446 +		} 
   1.447 +		// Load note
   1.448 +		QFileDialog *fd=new QFileDialog( this);
   1.449 +		fd->addFilter ("ASCII texts (*.txt)");
   1.450 +		fd->addFilter ("VYM notes (*.html)");
   1.451 +		fd->show();
   1.452 +		QString fn;
   1.453 +		if ( fd->exec() == QDialog::Accepted )
   1.454 +			fn = fd->selectedFile();
   1.455 +
   1.456 +		if ( !fn.isEmpty() )
   1.457 +		{
   1.458 +			QFile f( fn );
   1.459 +			if ( !f.open( IO_ReadOnly ) )
   1.460 +			return;
   1.461 +
   1.462 +			QTextStream ts( &f );
   1.463 +			setText( ts.read() );
   1.464 +			editorChanged();
   1.465 +		}
   1.466 +	}
   1.467 +}
   1.468 +
   1.469 +void TextEditor::closeEvent( QCloseEvent* ce )
   1.470 +{
   1.471 +    if ( !e->isModified() ) 
   1.472 +	{
   1.473 +		ce->accept();	// TextEditor can be reopened with show()
   1.474 +		return;
   1.475 +    }
   1.476 +}
   1.477 +
   1.478 +QString TextEditor::getText()
   1.479 +{
   1.480 +	return e->text();
   1.481 +}
   1.482 +
   1.483 +void TextEditor::editorChanged()
   1.484 +{
   1.485 +	// received, when QTextEdit::text() has changed
   1.486 +	EditorState	oldstate=state;
   1.487 +
   1.488 +	if (isEmpty())
   1.489 +		state=emptyEditor;
   1.490 +	else
   1.491 +		state=filledEditor;
   1.492 +
   1.493 +	if (state != oldstate)
   1.494 +	{
   1.495 +		if (state==emptyEditor)
   1.496 +			e->setPaper (emptyPaper);
   1.497 +		else
   1.498 +			e->setPaper (filledPaper);
   1.499 +	}
   1.500 +	// SLOT is LinkableMapObj, which will update systemFlag
   1.501 +	emit (textHasChanged() );
   1.502 +}
   1.503 +
   1.504 +
   1.505 +void TextEditor::setText(QString t)
   1.506 +{
   1.507 +	if ( !QStyleSheet::mightBeRichText( t ) )
   1.508 +		t = QStyleSheet::convertFromPlainText( t, QStyleSheetItem::WhiteSpaceNormal );
   1.509 +	e->setReadOnly(false);
   1.510 +	e->setText(t);
   1.511 +	editorChanged();	//not called automagically
   1.512 +
   1.513 +	enableActions();
   1.514 +}
   1.515 +
   1.516 +void TextEditor::setInactive()
   1.517 +{
   1.518 +	setText("");
   1.519 +	state=inactiveEditor;
   1.520 +	e->setPaper (inactivePaper);
   1.521 +	e->setReadOnly (true);
   1.522 +
   1.523 +	disableActions();
   1.524 +}
   1.525 +
   1.526 +void TextEditor::editCopyAll()
   1.527 +{
   1.528 +	e->selectAll();
   1.529 +	e->copy();
   1.530 +}
   1.531 +
   1.532 +void TextEditor::textSaveAs()
   1.533 +{
   1.534 +    QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   1.535 +					       this,"export note dialog",tr("Export Note to single file") );
   1.536 +
   1.537 +    if ( !fn.isEmpty() ) 
   1.538 +	{
   1.539 +		QFile file (fn);
   1.540 +		if (file.exists())
   1.541 +		{
   1.542 +			QMessageBox mb( "VYM",
   1.543 +				tr("The file ") + fn + 
   1.544 +				tr(" exists already. "
   1.545 +				"Do you want to overwrite it?"),
   1.546 +			QMessageBox::Warning,
   1.547 +			QMessageBox::Yes | QMessageBox::Default,
   1.548 +			QMessageBox::Cancel | QMessageBox::Escape,
   1.549 +			QMessageBox::NoButton );
   1.550 +			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   1.551 +			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   1.552 +			switch( mb.exec() ) {
   1.553 +				case QMessageBox::Yes:
   1.554 +					// save 
   1.555 +					filename = fn;
   1.556 +					textSave();
   1.557 +					return;
   1.558 +				case QMessageBox::Cancel:
   1.559 +					// do nothing
   1.560 +					break;
   1.561 +			}
   1.562 +		} else
   1.563 +		{
   1.564 +			filename = fn;
   1.565 +			textSave();
   1.566 +			return;
   1.567 +		}			
   1.568 +    }
   1.569 +	statusBar()->message(tr( "Couldn't export note ") + fn, statusbarTime );
   1.570 +}
   1.571 +
   1.572 +
   1.573 +void TextEditor::textSave()
   1.574 +{
   1.575 +    if ( filename.isEmpty() ) 
   1.576 +	{
   1.577 +		textSaveAs();
   1.578 +		return;
   1.579 +    }
   1.580 +
   1.581 +    QString text = e->text();
   1.582 +    QFile f( filename );
   1.583 +    if ( !f.open( IO_WriteOnly ) ) 
   1.584 +	{
   1.585 +		statusBar()->message( QString("Could not write to %1").arg(filename),
   1.586 +					  statusbarTime );
   1.587 +		return;
   1.588 +    }
   1.589 +
   1.590 +    QTextStream t( &f );
   1.591 +    t << text;
   1.592 +    f.close();
   1.593 +
   1.594 +    e->setModified( FALSE );
   1.595 +
   1.596 +    statusBar()->message( QString( "Note exported as %1" ).arg( filename ), statusbarTime );
   1.597 +}
   1.598 +
   1.599 +void TextEditor::textConvertPar()
   1.600 +{
   1.601 +	// In X11 a copy&paste generates paragraphs, 
   1.602 +	// which is not always wanted
   1.603 +	// This function replaces paragraphs by linebreaks.
   1.604 +	int parFrom, parTo, indFrom, indTo;
   1.605 +	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   1.606 +	QString t;
   1.607 +	if (parFrom>-1)
   1.608 +		t=e->selectedText();
   1.609 +	else
   1.610 +		t=e->text();
   1.611 +
   1.612 +	QRegExp re("<p.*>");
   1.613 +	re.setMinimal(true);
   1.614 +	t.replace (re,"");
   1.615 +	t.replace ("</p>","<br />");
   1.616 +	if (parFrom>-1)
   1.617 +	{
   1.618 +		e->setCursorPosition (parFrom,indFrom);
   1.619 +		e->cut();
   1.620 +		// Tried to simply insert the changed text with
   1.621 +		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   1.622 +		// but then the html would be quoted. So I use the ugly
   1.623 +		// way: insert a marker, replace it in whole text of QTextEdit
   1.624 +		QString marker="R3PlAcEMeL4teR!";
   1.625 +		e->insert (marker);
   1.626 +		e->setText (e->text().replace(marker,t));
   1.627 +	} else
   1.628 +		e->setText(t);
   1.629 +}
   1.630 +
   1.631 +void TextEditor::textJoinLines()
   1.632 +{
   1.633 +	int parFrom, parTo, indFrom, indTo;
   1.634 +	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   1.635 +	QString t;
   1.636 +	if (parFrom>-1)
   1.637 +		t=e->selectedText();
   1.638 +	else
   1.639 +		t=e->text();
   1.640 +	// In addition to textConvertPar it is sometimes
   1.641 +	// useful to join all lines of a paragraph
   1.642 +	QRegExp re("</p>\n+<p>(?!</p>)");
   1.643 +	re.setMinimal(true);
   1.644 +	t.replace (re," ");
   1.645 +
   1.646 +	// Above we may have introduced new " " at beginning of a
   1.647 +	// paragraph - remove it.
   1.648 +	re.setPattern("<p> ");
   1.649 +	t.replace (re,"<p>");
   1.650 +	if (parFrom>-1)
   1.651 +	{
   1.652 +		e->setCursorPosition (parFrom,indFrom);
   1.653 +		e->cut();
   1.654 +		// Tried to simply insert the changed text with
   1.655 +		// e->insert (t,(uint)(QTextEdit::RemoveSelected));
   1.656 +		// but then the html would be quoted. So I use the ugly
   1.657 +		// way: insert a marker, replace it in whole text of QTextEdit
   1.658 +		QString marker="R3PlAcEMeL4teR!";
   1.659 +		e->insert (marker);
   1.660 +		e->setText (e->text().replace(marker,t));
   1.661 +	} else
   1.662 +		e->setText(t);
   1.663 +}
   1.664 +
   1.665 +QString TextEditor::textConvertToASCII(const QString &t)
   1.666 +{
   1.667 +	QString r=t;
   1.668 +
   1.669 +	// convert all "<br*>" to "\n"
   1.670 +	QRegExp re("<br.*>");
   1.671 +	re.setMinimal(true);
   1.672 +	r.replace (re,"\n");
   1.673 +
   1.674 +	// convert all "</p>" to "\n"
   1.675 +	re.setPattern ("/p");
   1.676 +	r.replace (re,"\n");
   1.677 +	
   1.678 +	// remove all remaining tags 
   1.679 +	re.setPattern ("<.*>");
   1.680 +	r.replace (re,"");
   1.681 +
   1.682 +	// convert "&", "<" and ">"
   1.683 +	re.setPattern ("&gt;");
   1.684 +	r.replace (re,">");
   1.685 +	re.setPattern ("&lt;");
   1.686 +	r.replace (re,"<");
   1.687 +	re.setPattern ("&amp;");
   1.688 +	r.replace (re,"&");
   1.689 +	return r;
   1.690 +}
   1.691 +
   1.692 +void TextEditor::textExportAsASCII()
   1.693 +{
   1.694 +	QString text = textConvertToASCII( e->text());
   1.695 +    QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (ASCII) (*.txt);;All files (*)",
   1.696 +					       this,"export note dialog",tr("Export Note to single file (ASCII)") );
   1.697 +	int ret=-1;
   1.698 +
   1.699 +    if ( !fn.isEmpty() ) 
   1.700 +	{
   1.701 +		QFile file (fn);
   1.702 +		if (file.exists())
   1.703 +		{
   1.704 +			QMessageBox mb( "VYM",
   1.705 +				tr("The file ") + fn + 
   1.706 +				tr(" exists already. "
   1.707 +				"Do you want to overwrite it?"),
   1.708 +			QMessageBox::Warning,
   1.709 +			QMessageBox::Yes | QMessageBox::Default,
   1.710 +			QMessageBox::Cancel | QMessageBox::Escape,
   1.711 +			QMessageBox::NoButton );
   1.712 +			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   1.713 +			mb.setButtonText( QMessageBox::No, tr("Cancel"));
   1.714 +			ret=mb.exec();
   1.715 +		}	
   1.716 +		if (ret==QMessageBox::Cancel)
   1.717 +			return;
   1.718 +			
   1.719 +		// save 
   1.720 +		if ( !file.open( IO_WriteOnly ) ) 
   1.721 +			statusBar()->message( QString("Could not write to %1").arg(filename),
   1.722 +						  statusbarTime );
   1.723 +		else
   1.724 +		{
   1.725 +			QTextStream t( &file );
   1.726 +			t << text;
   1.727 +			file.close();
   1.728 +
   1.729 +			statusBar()->message( QString( "Note exported as %1" ).arg( fn ), statusbarTime );
   1.730 +		}
   1.731 +    }
   1.732 +}
   1.733 +
   1.734 +
   1.735 +void TextEditor::textPrint()
   1.736 +{
   1.737 +    printer->setFullPage(TRUE);
   1.738 +    if ( printer->setup( this ) ) 
   1.739 +	{
   1.740 +		QPainter p( printer );
   1.741 +		// Check that there is a valid device to print to.
   1.742 +		if ( !p.device() ) return;
   1.743 +		QPaintDeviceMetrics metrics( p.device() );
   1.744 +		int dpiy = metrics.logicalDpiY();
   1.745 +		int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
   1.746 +		QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
   1.747 +		QFont font( e->currentFont() );
   1.748 +		font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
   1.749 +
   1.750 +		QSimpleRichText richText( e->text(), font,
   1.751 +					  e->context(),
   1.752 +					  e->styleSheet(),
   1.753 +					  e->mimeSourceFactory(),
   1.754 +					  body.height() );
   1.755 +		richText.setWidth( &p, body.width() );
   1.756 +		QRect view( body );
   1.757 +		int page = 1;
   1.758 +		do 
   1.759 +		{
   1.760 +			richText.draw( &p, body.left(), body.top(), view, colorGroup() );
   1.761 +			view.moveBy( 0, body.height() );
   1.762 +			p.translate( 0 , -body.height() );
   1.763 +			p.setFont( font );
   1.764 +			p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
   1.765 +				view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
   1.766 +			if ( view.top()  >= richText.height() )
   1.767 +			break;
   1.768 +			printer->newPage();
   1.769 +			page++;
   1.770 +		} while (TRUE);
   1.771 +    }
   1.772 +}
   1.773 +
   1.774 +void TextEditor::textEditUndo()
   1.775 +{
   1.776 +}
   1.777 +
   1.778 +void TextEditor::toggleFonthint()
   1.779 +{
   1.780 +	setUpdatesEnabled (false);
   1.781 +	e->selectAll (true);
   1.782 +	if (!actionFormatUseFixedFont->isOn() ) 
   1.783 +		e->setCurrentFont (varFont);
   1.784 +	else	
   1.785 +		e->setCurrentFont (fixedFont);
   1.786 +	e->selectAll (false);
   1.787 +	setUpdatesEnabled (true);
   1.788 +	repaint();
   1.789 +}
   1.790 +
   1.791 +void TextEditor::setFixedFont()
   1.792 +{
   1.793 +	bool ok;
   1.794 +	QFont font =QFontDialog::getFont(
   1.795 +                    &ok, fixedFont, this );
   1.796 +    if ( ok ) 
   1.797 +        // font is set to the font the user selected
   1.798 +		fixedFont=font;
   1.799 +}
   1.800 +
   1.801 +void TextEditor::setVarFont()
   1.802 +{
   1.803 +	bool ok;
   1.804 +	QFont font =QFontDialog::getFont(
   1.805 +                    &ok, varFont, this );
   1.806 +    if ( ok ) 
   1.807 +        // font is set to the font the user selected
   1.808 +		varFont=font;
   1.809 +}
   1.810 +
   1.811 +void TextEditor::textBold()
   1.812 +{
   1.813 +    e->setBold( actionTextBold->isOn() );
   1.814 +}
   1.815 +
   1.816 +void TextEditor::textUnderline()
   1.817 +{
   1.818 +    e->setUnderline( actionTextUnderline->isOn() );
   1.819 +}
   1.820 +
   1.821 +void TextEditor::textItalic()
   1.822 +{
   1.823 +    e->setItalic( actionTextItalic->isOn() );
   1.824 +}
   1.825 +
   1.826 +void TextEditor::textFamily( const QString &f )
   1.827 +{
   1.828 +    e->setFamily( f );
   1.829 +}
   1.830 +
   1.831 +void TextEditor::textSize( const QString &p )
   1.832 +{
   1.833 +    e->setPointSize( p.toInt() );
   1.834 +}
   1.835 +
   1.836 +
   1.837 +void TextEditor::textColor()
   1.838 +{
   1.839 +    QColor col = QColorDialog::getColor( e->color(), this );
   1.840 +    if ( !col.isValid() )
   1.841 +	return;
   1.842 +    e->setColor( col );
   1.843 +    QPixmap pix( 16, 16 );
   1.844 +    pix.fill( black );
   1.845 +    actionTextColor->setIconSet( pix );
   1.846 +}
   1.847 +
   1.848 +void TextEditor::textAlign( QAction *a )
   1.849 +{
   1.850 +    if ( a == actionAlignLeft )
   1.851 +	e->setAlignment( AlignLeft );
   1.852 +    else if ( a == actionAlignCenter )
   1.853 +	e->setAlignment( AlignHCenter );
   1.854 +    else if ( a == actionAlignRight )
   1.855 +	e->setAlignment( AlignRight );
   1.856 +    else if ( a == actionAlignJustify )
   1.857 +	e->setAlignment( AlignJustify );
   1.858 +}
   1.859 +
   1.860 +void TextEditor::fontChanged( const QFont &f )
   1.861 +{
   1.862 +    comboFont->lineEdit()->setText( f.family() );
   1.863 +    comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
   1.864 +    actionTextBold->setOn( f.bold() );
   1.865 +    actionTextItalic->setOn( f.italic() );
   1.866 +    actionTextUnderline->setOn( f.underline() );
   1.867 +}
   1.868 +
   1.869 +void TextEditor::colorChanged( const QColor &c )
   1.870 +{
   1.871 +    QPixmap pix( 16, 16 );
   1.872 +    pix.fill( c );
   1.873 +    actionTextColor->setIconSet( pix );
   1.874 +}
   1.875 +
   1.876 +void TextEditor::alignmentChanged( int a )
   1.877 +{
   1.878 +    if ( ( a == AlignAuto ) || ( a & AlignLeft ))
   1.879 +	actionAlignLeft->setOn( true );
   1.880 +    else if ( ( a & AlignHCenter ) )
   1.881 +	actionAlignCenter->setOn( true );
   1.882 +    else if ( ( a & AlignRight ) )
   1.883 +	actionAlignRight->setOn( true );
   1.884 +    else if ( ( a & AlignJustify ) )
   1.885 +	actionAlignJustify->setOn( true );
   1.886 +}
   1.887 +
   1.888 +
   1.889 +
   1.890 +void TextEditor::enableActions()
   1.891 +{
   1.892 +	actionFileLoad->setEnabled(true);
   1.893 +	actionFileSave->setEnabled(true);
   1.894 +	actionFileSaveAs->setEnabled(true);
   1.895 +	actionFilePrint->setEnabled(true);
   1.896 +	actionEditUndo->setEnabled(true);
   1.897 +	actionEditRedo->setEnabled(true);
   1.898 +	actionEditCopy->setEnabled(true);
   1.899 +	actionEditCut->setEnabled(true);
   1.900 +	actionEditPaste->setEnabled(true);
   1.901 +	actionEditDeleteAll->setEnabled(true);
   1.902 +	actionEditConvertPar->setEnabled(true);
   1.903 +	actionEditJoinLines->setEnabled(true);
   1.904 +	actionFormatUseFixedFont->setEnabled(true);
   1.905 +}
   1.906 +
   1.907 +void TextEditor::disableActions()
   1.908 +{
   1.909 +	actionFileLoad->setEnabled(false);
   1.910 +	actionFileSave->setEnabled(false);
   1.911 +	actionFileSaveAs->setEnabled(false);
   1.912 +	actionFilePrint->setEnabled(false);
   1.913 +	actionEditUndo->setEnabled(false);
   1.914 +	actionEditRedo->setEnabled(false);
   1.915 +	actionEditCopy->setEnabled(false);
   1.916 +	actionEditCut->setEnabled(false);
   1.917 +	actionEditPaste->setEnabled(false);
   1.918 +	actionEditDeleteAll->setEnabled(false);
   1.919 +	actionEditConvertPar->setEnabled(false);
   1.920 +	actionEditJoinLines->setEnabled(false);
   1.921 +	actionFormatUseFixedFont->setEnabled(false);
   1.922 +}
   1.923 +
   1.924 +