texteditor.cpp
branchqt4-port
changeset 9 f94317a94db1
parent 5 5cfbba1dc2f8
child 10 e2a80d9b5858
     1.1 --- a/texteditor.cpp	Wed Jul 05 10:29:54 2006 +0000
     1.2 +++ b/texteditor.cpp	Thu Jul 06 14:47:01 2006 +0000
     1.3 @@ -1,24 +1,17 @@
     1.4  #include "texteditor.h"
     1.5  
     1.6 -#include <q3canvas.h>
     1.7 -#include <qpainter.h>
     1.8 -#include <qprinter.h>
     1.9 -#include <qfile.h>
    1.10 -#include <q3filedialog.h>
    1.11 -#include <q3toolbar.h>
    1.12 -#include <q3popupmenu.h>
    1.13 +#include <QPainter>
    1.14 +#include <QPrinter>
    1.15 +#include <QFile>
    1.16 +#include <QFileDialog>
    1.17  #include <qmenubar.h>
    1.18 -#include <q3textedit.h>
    1.19  #include <qtextstream.h>
    1.20 -#include <q3paintdevicemetrics.h>
    1.21 -#include <qsettings.h>
    1.22 -#include <qfontdialog.h>
    1.23 -#include <qmessagebox.h>
    1.24 -#include <qcolordialog.h>
    1.25 -#include <qregexp.h>
    1.26 -#include <q3simplerichtext.h>
    1.27 -//Added by qt3to4:
    1.28 -#include <Q3ValueList>
    1.29 +#include <QSettings>
    1.30 +#include <QFontDialog>
    1.31 +#include <QMessageBox>
    1.32 +#include <QColorDialog>
    1.33 +#include <QRegExp>
    1.34 +#include <QList>
    1.35  #include <QPixmap>
    1.36  #include <QCloseEvent>
    1.37  
    1.38 @@ -29,7 +22,6 @@
    1.39  #include "noteobj.h"
    1.40  #include "version.h"
    1.41  
    1.42 -extern Q3Canvas* actCanvas;
    1.43  extern int statusbarTime;
    1.44  extern QSettings settings;
    1.45  
    1.46 @@ -56,6 +48,7 @@
    1.47  	e->setTextFormat(Qt::RichText);		// default
    1.48  	e->setTabStopWidth (20);		// unit is pixel
    1.49  	e->setColor (Qt::black);
    1.50 +	e->setAutoFillBackground (true);
    1.51  	connect (e, SIGNAL( textChanged() ), this, SLOT( editorChanged() ) );
    1.52      setCentralWidget( e );
    1.53      statusBar()->message( "Ready", statusbarTime);
    1.54 @@ -68,9 +61,6 @@
    1.55  	setupSettingsActions();
    1.56  	
    1.57  	// Various states
    1.58 -	emptyPaper   = QBrush(Qt::gray);
    1.59 -	filledPaper  = QBrush(Qt::white);
    1.60 -	inactivePaper= QBrush(Qt::black);
    1.61  	setInactive();
    1.62  
    1.63  	// Load Settings
    1.64 @@ -137,7 +127,7 @@
    1.65  
    1.66  bool TextEditor::isEmpty()
    1.67  {
    1.68 -	if (e->text().length())
    1.69 +	if (e->toPlainText().length()>0)
    1.70  		return false;
    1.71  	else
    1.72  		return true;
    1.73 @@ -208,10 +198,9 @@
    1.74  	return filenameHint;
    1.75  }
    1.76  
    1.77 -bool TextEditor::findText(const QString &t, const bool &cs)
    1.78 +bool TextEditor::findText(const QString &t, const QTextDocument::FindFlags &flags)
    1.79  {
    1.80 -	bool wo=false;	// word matches
    1.81 -	if (e->find (t, cs, wo, true, 0, 0 ))
    1.82 +	if (e->find (t,flags))
    1.83  		return true;
    1.84  	else	
    1.85  		return false;
    1.86 @@ -358,18 +347,20 @@
    1.87  //    comboFont = new QComboBox( true, tb );
    1.88      comboFont = new QComboBox;
    1.89  	tb->addWidget (comboFont);
    1.90 -    QFontDatabase db;
    1.91 -    comboFont->insertStringList( db.families() );
    1.92 +    QFontDatabase fontDB;
    1.93 +    comboFont->insertStringList( fontDB.families() );
    1.94      connect( comboFont, SIGNAL( activated( const QString & ) ),
    1.95  	     this, SLOT( textFamily( const QString & ) ) );
    1.96      comboFont->addItem( QApplication::font().family() );
    1.97 -//    comboSize = new QComboBox( true, tb );
    1.98      comboSize = new QComboBox;
    1.99  	tb->addWidget (comboSize);
   1.100 -    Q3ValueList<int> sizes = db.standardSizes();
   1.101 -    Q3ValueList<int>::Iterator it = sizes.begin();
   1.102 -    for ( ; it != sizes.end(); ++it )
   1.103 -	comboSize->insertItem( QString::number( *it ) );
   1.104 +	QList<int> sizes=fontDB.standardSizes();
   1.105 +	QList<int>::iterator i = sizes.begin();
   1.106 +	while (i != sizes.end()) 
   1.107 +	{
   1.108 +		++i; // increment i before using it
   1.109 +		comboSize->insertItem ( QString::number(*i));
   1.110 +	}	
   1.111      connect( comboSize, SIGNAL( activated( const QString & ) ),
   1.112  	     this, SLOT( textSize( const QString & ) ) );
   1.113      comboSize->addItem ( QString::number( QApplication::font().pointSize() ) );
   1.114 @@ -461,10 +452,12 @@
   1.115  	     this, SLOT( fontChanged( const QFont & ) ) );
   1.116      connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
   1.117  	     this, SLOT( colorChanged( const QColor & ) ) );
   1.118 +		 /*
   1.119      connect( e, SIGNAL( currentAlignmentChanged( int ) ),
   1.120  	     this, SLOT( alignmentChanged( int ) ) );
   1.121      connect( e, SIGNAL( currentVerticalAlignmentChanged(int)),
   1.122  	     this, SLOT( verticalAlignmentChanged(int)));
   1.123 +		 */
   1.124  
   1.125  }
   1.126  
   1.127 @@ -497,7 +490,7 @@
   1.128  {
   1.129  	if (state!=inactiveEditor)
   1.130  	{
   1.131 -		if (e->length()) 
   1.132 +		if (e->text().length()) 
   1.133  		{
   1.134  			QMessageBox mb( __VYM " - " +tr("Note Editor"),
   1.135  				"Loading will overwrite the existing note",
   1.136 @@ -513,9 +506,13 @@
   1.137  			}
   1.138  		} 
   1.139  		// Load note
   1.140 -		Q3FileDialog *fd=new Q3FileDialog( this);
   1.141 -		fd->addFilter ("ASCII texts (*.txt)");
   1.142 -		fd->addFilter ("VYM notes (*.html)");
   1.143 +		QFileDialog *fd=new QFileDialog( this);
   1.144 +		QStringList types;
   1.145 +		types<< "VYM notes (*.html)" <<
   1.146 +			"ASCII texts (*.txt)" <<
   1.147 +			"All filed (*)";
   1.148 +		fd->setFilters (types);
   1.149 +		fd->setDirectory (QDir().current());
   1.150  		fd->show();
   1.151  		QString fn;
   1.152  		if ( fd->exec() == QDialog::Accepted )
   1.153 @@ -544,7 +541,10 @@
   1.154  
   1.155  QString TextEditor::getText()
   1.156  {
   1.157 -	return e->text();
   1.158 +	if (e->toPlainText().isEmpty())
   1.159 +		return "";
   1.160 +	else	
   1.161 +		return e->text();
   1.162  }
   1.163  
   1.164  void TextEditor::editorChanged()
   1.165 @@ -555,9 +555,9 @@
   1.166  		state=filledEditor;
   1.167  
   1.168  		if (state==emptyEditor)
   1.169 -			e->setPaper (emptyPaper);
   1.170 +			setState (emptyEditor);
   1.171  		else
   1.172 -			e->setPaper (filledPaper);
   1.173 +			setState (filledEditor);
   1.174  	// SLOT is LinkableMapObj, which will update systemFlag
   1.175  	emit (textHasChanged() );
   1.176  }
   1.177 @@ -565,12 +565,8 @@
   1.178  
   1.179  void TextEditor::setText(QString t)
   1.180  {
   1.181 -	if ( !Q3StyleSheet::mightBeRichText( t ) )
   1.182 -		t = Q3StyleSheet::convertFromPlainText( t, Q3StyleSheetItem::WhiteSpaceNormal );
   1.183  	e->setReadOnly(false);
   1.184  	e->setText(t);
   1.185 -//	editorChanged();	//not called automagically  FIXME or is it?
   1.186 -
   1.187  	enableActions();
   1.188  }
   1.189  
   1.190 @@ -578,7 +574,7 @@
   1.191  {
   1.192  	state=inactiveEditor;
   1.193  	setText("");
   1.194 -	e->setPaper (inactivePaper);
   1.195 +	setState (inactiveEditor);
   1.196  	e->setReadOnly (true);
   1.197  
   1.198  	disableActions();
   1.199 @@ -592,7 +588,7 @@
   1.200  
   1.201  void TextEditor::textSaveAs()
   1.202  {
   1.203 -    QString fn = Q3FileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   1.204 +    QString fn = QFileDialog::getSaveFileName( QString::null, "VYM Note (HTML) (*.html);;All files (*)",
   1.205  					       this,"export note dialog",tr("Export Note to single file") );
   1.206  
   1.207      if ( !fn.isEmpty() ) 
   1.208 @@ -662,9 +658,21 @@
   1.209  	// In X11 a copy&paste generates paragraphs, 
   1.210  	// which is not always wanted
   1.211  	// This function replaces paragraphs by linebreaks.
   1.212 +
   1.213 +	e->setBackgroundRole (QPalette::Text);
   1.214 +	cout << "dark\n";
   1.215 +
   1.216 +	QString t;
   1.217 +	t=e->text();
   1.218 +	QRegExp re("<p.*>");
   1.219 +	re.setMinimal(true);
   1.220 +	t.replace (re,"");
   1.221 +	t.replace ("</p>","<br />");
   1.222 +	e->setText(t);
   1.223 +
   1.224 +	/* FIXME QT3 use seletion ()
   1.225  	int parFrom, parTo, indFrom, indTo;
   1.226  	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   1.227 -	QString t;
   1.228  	if (parFrom>-1)
   1.229  		t=e->selectedText();
   1.230  	else
   1.231 @@ -687,10 +695,15 @@
   1.232  		e->setText (e->text().replace(marker,t));
   1.233  	} else
   1.234  		e->setText(t);
   1.235 +	*/	
   1.236  }
   1.237  
   1.238  void TextEditor::textJoinLines()
   1.239  {
   1.240 +	e->setBackgroundRole (QPalette::Light);
   1.241 +	cout << "light\n";
   1.242 +
   1.243 +/* FIXME QT3
   1.244  	int parFrom, parTo, indFrom, indTo;
   1.245  	e->getSelection (&parFrom,&indFrom,&parTo,&indTo);
   1.246  	QString t;
   1.247 @@ -721,6 +734,7 @@
   1.248  		e->setText (e->text().replace(marker,t));
   1.249  	} else
   1.250  		e->setText(t);
   1.251 +*/		
   1.252  }
   1.253  
   1.254  void TextEditor::textExportAsASCII()
   1.255 @@ -735,7 +749,7 @@
   1.256  			s=filenameHint;
   1.257  	} else	
   1.258  		s=QString::null;
   1.259 -	fn = Q3FileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   1.260 +	fn = QFileDialog::getSaveFileName( s, "VYM Note (ASCII) (*.txt);;All files (*)", this,"export note dialog",tr("Export Note to single file (ASCII)") );
   1.261  	int ret=-1;
   1.262  
   1.263      if ( !fn.isEmpty() ) 
   1.264 @@ -776,41 +790,16 @@
   1.265  
   1.266  void TextEditor::textPrint()
   1.267  {
   1.268 -    printer->setFullPage(TRUE);
   1.269 -    if ( printer->setup( this ) ) 
   1.270 -	{
   1.271 -		QPainter p( printer );
   1.272 -		// Check that there is a valid device to print to.
   1.273 -		if ( !p.device() ) return;
   1.274 -		Q3PaintDeviceMetrics metrics( p.device() );
   1.275 -		int dpiy = metrics.logicalDpiY();
   1.276 -		int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
   1.277 -		QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
   1.278 -		QFont font( e->currentFont() );
   1.279 -		font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
   1.280  
   1.281 -		Q3SimpleRichText richText( e->text(), font,
   1.282 -					  e->context(),
   1.283 -					  e->styleSheet(),
   1.284 -					  e->mimeSourceFactory(),
   1.285 -					  body.height() );
   1.286 -		richText.setWidth( &p, body.width() );
   1.287 -		QRect view( body );
   1.288 -		int page = 1;
   1.289 -		do 
   1.290 -		{
   1.291 -			richText.draw( &p, body.left(), body.top(), view, colorGroup() );
   1.292 -			view.moveBy( 0, body.height() );
   1.293 -			p.translate( 0 , -body.height() );
   1.294 -			p.setFont( font );
   1.295 -			p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
   1.296 -				view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
   1.297 -			if ( view.top()  >= richText.height() )
   1.298 -			break;
   1.299 -			printer->newPage();
   1.300 -			page++;
   1.301 -		} while (TRUE);
   1.302 -    }
   1.303 +    QTextDocument *document = e->document();
   1.304 +    QPrinter printer;
   1.305 +
   1.306 +    QPrintDialog *dialog = new QPrintDialog(&printer, this);
   1.307 +    dialog->setWindowTitle(tr("Print Document"));
   1.308 +    if (dialog->exec() != QDialog::Accepted)
   1.309 +        return;
   1.310 +
   1.311 +    document->print(&printer);
   1.312  }
   1.313  
   1.314  void TextEditor::textEditUndo()
   1.315 @@ -820,12 +809,12 @@
   1.316  void TextEditor::toggleFonthint()
   1.317  {
   1.318  	setUpdatesEnabled (false);
   1.319 -	e->selectAll (true);
   1.320 +	e->selectAll ();
   1.321  	if (!actionFormatUseFixedFont->isOn() ) 
   1.322  		e->setCurrentFont (varFont);
   1.323  	else	
   1.324  		e->setCurrentFont (fixedFont);
   1.325 -	e->selectAll (false);
   1.326 +	e->selectAll ();
   1.327  	setUpdatesEnabled (true);
   1.328  	repaint();
   1.329  }
   1.330 @@ -901,13 +890,15 @@
   1.331  
   1.332  void TextEditor::textVAlign()
   1.333  {
   1.334 +/* FIXME QT3
   1.335      if ( sender() == actionAlignSuperScript && actionAlignSuperScript->isOn()) {
   1.336 -	e->setVerticalAlignment( Q3TextEdit::AlignSuperScript);
   1.337 +	e->setVerticalAlignment( QTextEdit::AlignSuperScript);
   1.338      } else if (sender() == actionAlignSubScript && actionAlignSubScript->isOn()) {
   1.339 -	e->setVerticalAlignment( Q3TextEdit::AlignSubScript);
   1.340 +	e->setVerticalAlignment( QTextEdit::AlignSubScript);
   1.341      } else {
   1.342 -	e->setVerticalAlignment( Q3TextEdit::AlignNormal);
   1.343 +	e->setVerticalAlignment( QTextEdit::AlignNormal);
   1.344      }
   1.345 +*/	
   1.346  }
   1.347  
   1.348  
   1.349 @@ -943,16 +934,18 @@
   1.350  
   1.351  void TextEditor::verticalAlignmentChanged(int a) 
   1.352  {
   1.353 -    if (a == Q3TextEdit::AlignSuperScript ) {
   1.354 +	/* FIXME QT3
   1.355 +    if (a == QTextEdit::AlignSuperScript ) {
   1.356  	actionAlignSuperScript->setOn(true);
   1.357  	actionAlignSubScript->setOn(false);
   1.358 -    } else if (a == Q3TextEdit::AlignSubScript ) {
   1.359 +    } else if (a == QTextEdit::AlignSubScript ) {
   1.360  	actionAlignSuperScript->setOn(false);
   1.361  	actionAlignSubScript->setOn(true);
   1.362      } else {
   1.363  	actionAlignSuperScript->setOn(false);
   1.364  	actionAlignSubScript->setOn(false);
   1.365      }
   1.366 +	*/
   1.367  }
   1.368  
   1.369  
   1.370 @@ -991,4 +984,20 @@
   1.371  	actionFormatUseFixedFont->setEnabled(false);
   1.372  }
   1.373  
   1.374 +void TextEditor::setState (EditorState s)
   1.375 +{
   1.376 +	
   1.377 +	QPalette p=palette();
   1.378 +	QColor c;
   1.379 +	switch (s)
   1.380 +	{
   1.381 +		case emptyEditor:    c=QColor (150,150,150); break;
   1.382 +		case filledEditor:   c=QColor (255,255,255); break;
   1.383 +		case inactiveEditor: c=QColor (0,0,0);
   1.384 +	}
   1.385 +    p.setColor(QPalette::Active, static_cast<QPalette::ColorRole>(9), c);
   1.386 +    p.setColor(QPalette::Inactive, static_cast<QPalette::ColorRole>(9), c);
   1.387 +    e->setPalette(p);
   1.388 +}
   1.389  
   1.390 +