source: vendor/trolltech/current/examples/textedit/textedit.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 14.6 KB
Line 
1/****************************************************************************
2** $Id: textedit.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "textedit.h"
12
13#include <qtextedit.h>
14#include <qaction.h>
15#include <qmenubar.h>
16#include <qpopupmenu.h>
17#include <qtoolbar.h>
18#include <qtabwidget.h>
19#include <qapplication.h>
20#include <qfontdatabase.h>
21#include <qcombobox.h>
22#include <qlineedit.h>
23#include <qfileinfo.h>
24#include <qfile.h>
25#include <qfiledialog.h>
26#include <qprinter.h>
27#include <qpaintdevicemetrics.h>
28#include <qsimplerichtext.h>
29#include <qcolordialog.h>
30#include <qpainter.h>
31
32TextEdit::TextEdit( QWidget *parent, const char *name )
33 : QMainWindow( parent, name )
34{
35 setupFileActions();
36 setupEditActions();
37 setupTextActions();
38
39 tabWidget = new QTabWidget( this );
40 connect( tabWidget, SIGNAL( currentChanged( QWidget * ) ),
41 this, SLOT( editorChanged( QWidget * ) ) );
42 setCentralWidget( tabWidget );
43
44 if ( qApp->argc() == 1 ) {
45 load( "example.html" );
46 } else {
47 for ( int i = 1; i < qApp->argc(); ++i )
48 load( qApp->argv()[ i ] );
49 }
50}
51
52void TextEdit::setupFileActions()
53{
54 QToolBar *tb = new QToolBar( this );
55 tb->setLabel( "File Actions" );
56 QPopupMenu *menu = new QPopupMenu( this );
57 menuBar()->insertItem( tr( "&File" ), menu );
58
59 QAction *a;
60 a = new QAction( QPixmap::fromMimeSource( "filenew.xpm" ), tr( "&New..." ), CTRL + Key_N, this, "fileNew" );
61 connect( a, SIGNAL( activated() ), this, SLOT( fileNew() ) );
62 a->addTo( tb );
63 a->addTo( menu );
64 a = new QAction( QPixmap::fromMimeSource( "fileopen.xpm" ), tr( "&Open..." ), CTRL + Key_O, this, "fileOpen" );
65 connect( a, SIGNAL( activated() ), this, SLOT( fileOpen() ) );
66 a->addTo( tb );
67 a->addTo( menu );
68 menu->insertSeparator();
69 a = new QAction( QPixmap::fromMimeSource( "filesave.xpm" ), tr( "&Save..." ), CTRL + Key_S, this, "fileSave" );
70 connect( a, SIGNAL( activated() ), this, SLOT( fileSave() ) );
71 a->addTo( tb );
72 a->addTo( menu );
73 a = new QAction( tr( "Save &As..." ), 0, this, "fileSaveAs" );
74 connect( a, SIGNAL( activated() ), this, SLOT( fileSaveAs() ) );
75 a->addTo( menu );
76 menu->insertSeparator();
77 a = new QAction( QPixmap::fromMimeSource( "fileprint.xpm" ), tr( "&Print..." ), CTRL + Key_P, this, "filePrint" );
78 connect( a, SIGNAL( activated() ), this, SLOT( filePrint() ) );
79 a->addTo( tb );
80 a->addTo( menu );
81 a = new QAction( tr( "&Close" ), 0, this, "fileClose" );
82 connect( a, SIGNAL( activated() ), this, SLOT( fileClose() ) );
83 a->addTo( menu );
84 a = new QAction( tr( "E&xit" ), 0, this, "fileExit" );
85 connect( a, SIGNAL( activated() ), this, SLOT( fileExit() ) );
86 a->addTo( menu );
87}
88
89void TextEdit::setupEditActions()
90{
91 QToolBar *tb = new QToolBar( this );
92 tb->setLabel( "Edit Actions" );
93 QPopupMenu *menu = new QPopupMenu( this );
94 menuBar()->insertItem( tr( "&Edit" ), menu );
95
96 QAction *a;
97 a = new QAction( QPixmap::fromMimeSource( "editundo.xpm" ), tr( "&Undo" ), CTRL + Key_Z, this, "editUndo" );
98 connect( a, SIGNAL( activated() ), this, SLOT( editUndo() ) );
99 a->addTo( tb );
100 a->addTo( menu );
101 a = new QAction( QPixmap::fromMimeSource( "editredo.xpm" ), tr( "&Redo" ), CTRL + Key_Y, this, "editRedo" );
102 connect( a, SIGNAL( activated() ), this, SLOT( editRedo() ) );
103 a->addTo( tb );
104 a->addTo( menu );
105 menu->insertSeparator();
106 a = new QAction( QPixmap::fromMimeSource( "editcopy.xpm" ), tr( "&Copy" ), CTRL + Key_C, this, "editCopy" );
107 connect( a, SIGNAL( activated() ), this, SLOT( editCopy() ) );
108 a->addTo( tb );
109 a->addTo( menu );
110 a = new QAction( QPixmap::fromMimeSource( "editcut.xpm" ), tr( "Cu&t" ), CTRL + Key_X, this, "editCut" );
111 connect( a, SIGNAL( activated() ), this, SLOT( editCut() ) );
112 a->addTo( tb );
113 a->addTo( menu );
114 a = new QAction( QPixmap::fromMimeSource( "editpaste.xpm" ), tr( "&Paste" ), CTRL + Key_V, this, "editPaste" );
115 connect( a, SIGNAL( activated() ), this, SLOT( editPaste() ) );
116 a->addTo( tb );
117 a->addTo( menu );
118}
119
120void TextEdit::setupTextActions()
121{
122 QToolBar *tb = new QToolBar( this );
123 tb->setLabel( "Format Actions" );
124 QPopupMenu *menu = new QPopupMenu( this );
125 menuBar()->insertItem( tr( "F&ormat" ), menu );
126
127 comboFont = new QComboBox( TRUE, tb );
128 QFontDatabase db;
129 comboFont->insertStringList( db.families() );
130 connect( comboFont, SIGNAL( activated( const QString & ) ),
131 this, SLOT( textFamily( const QString & ) ) );
132 comboFont->lineEdit()->setText( QApplication::font().family() );
133
134 comboSize = new QComboBox( TRUE, tb );
135 QValueList<int> sizes = db.standardSizes();
136 QValueList<int>::Iterator it = sizes.begin();
137 for ( ; it != sizes.end(); ++it )
138 comboSize->insertItem( QString::number( *it ) );
139 connect( comboSize, SIGNAL( activated( const QString & ) ),
140 this, SLOT( textSize( const QString & ) ) );
141 comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
142
143 actionTextBold = new QAction( QPixmap::fromMimeSource( "textbold.xpm" ), tr( "&Bold" ), CTRL + Key_B, this, "textBold" );
144 connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
145 actionTextBold->addTo( tb );
146 actionTextBold->addTo( menu );
147 actionTextBold->setToggleAction( TRUE );
148 actionTextItalic = new QAction( QPixmap::fromMimeSource( "textitalic.xpm" ), tr( "&Italic" ), CTRL + Key_I, this, "textItalic" );
149 connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
150 actionTextItalic->addTo( tb );
151 actionTextItalic->addTo( menu );
152 actionTextItalic->setToggleAction( TRUE );
153 actionTextUnderline = new QAction( QPixmap::fromMimeSource( "textunder.xpm" ), tr( "&Underline" ), CTRL + Key_U, this, "textUnderline" );
154 connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
155 actionTextUnderline->addTo( tb );
156 actionTextUnderline->addTo( menu );
157 actionTextUnderline->setToggleAction( TRUE );
158 menu->insertSeparator();
159
160 QActionGroup *grp = new QActionGroup( this );
161 connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
162
163 actionAlignLeft = new QAction( QPixmap::fromMimeSource( "textleft.xpm" ), tr( "&Left" ), CTRL + Key_L, grp, "textLeft" );
164 actionAlignLeft->setToggleAction( TRUE );
165 actionAlignCenter = new QAction( QPixmap::fromMimeSource( "textcenter.xpm" ), tr( "C&enter" ), CTRL + Key_E, grp, "textCenter" );
166 actionAlignCenter->setToggleAction( TRUE );
167 actionAlignRight = new QAction( QPixmap::fromMimeSource( "textright.xpm" ), tr( "&Right" ), CTRL + Key_R, grp, "textRight" );
168 actionAlignRight->setToggleAction( TRUE );
169 actionAlignJustify = new QAction( QPixmap::fromMimeSource( "textjustify.xpm" ), tr( "&Justify" ), CTRL + Key_J, grp, "textjustify" );
170 actionAlignJustify->setToggleAction( TRUE );
171
172 grp->addTo( tb );
173 grp->addTo( menu );
174
175 menu->insertSeparator();
176
177 QPixmap pix( 16, 16 );
178 pix.fill( black );
179 actionTextColor = new QAction( pix, tr( "&Color..." ), 0, this, "textColor" );
180 connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
181 actionTextColor->addTo( tb );
182 actionTextColor->addTo( menu );
183}
184
185void TextEdit::load( const QString &f )
186{
187 if ( !QFile::exists( f ) )
188 return;
189 QTextEdit *edit = new QTextEdit( tabWidget );
190 edit->setTextFormat( RichText );
191 doConnections( edit );
192 tabWidget->addTab( edit, QFileInfo( f ).fileName() );
193 QFile file( f );
194 if ( !file.open( IO_ReadOnly ) )
195 return;
196 QTextStream ts( &file );
197 QString txt = ts.read();
198 if ( !QStyleSheet::mightBeRichText( txt ) )
199 txt = QStyleSheet::convertFromPlainText( txt, QStyleSheetItem::WhiteSpacePre );
200 edit->setText( txt );
201 tabWidget->showPage( edit );
202 edit->viewport()->setFocus();
203 filenames.replace( edit, f );
204}
205
206QTextEdit *TextEdit::currentEditor() const
207{
208 if ( tabWidget->currentPage() &&
209 tabWidget->currentPage()->inherits( "QTextEdit" ) )
210 return (QTextEdit*)tabWidget->currentPage();
211 return 0;
212}
213
214void TextEdit::doConnections( QTextEdit *e )
215{
216 connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
217 this, SLOT( fontChanged( const QFont & ) ) );
218 connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
219 this, SLOT( colorChanged( const QColor & ) ) );
220 connect( e, SIGNAL( currentAlignmentChanged( int ) ),
221 this, SLOT( alignmentChanged( int ) ) );
222}
223
224void TextEdit::fileNew()
225{
226 QTextEdit *edit = new QTextEdit( tabWidget );
227 edit->setTextFormat( RichText );
228 doConnections( edit );
229 tabWidget->addTab( edit, tr( "noname" ) );
230 tabWidget->showPage( edit );
231 edit->viewport()->setFocus();
232}
233
234void TextEdit::fileOpen()
235{
236 QString fn = QFileDialog::getOpenFileName( QString::null, tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
237 if ( !fn.isEmpty() )
238 load( fn );
239}
240
241void TextEdit::fileSave()
242{
243 if ( !currentEditor() )
244 return;
245 QString fn;
246 if ( filenames.find( currentEditor() ) == filenames.end() ) {
247 fileSaveAs();
248 } else {
249 QFile file( *filenames.find( currentEditor() ) );
250 if ( !file.open( IO_WriteOnly ) )
251 return;
252 QTextStream ts( &file );
253 ts << currentEditor()->text();
254 }
255}
256
257void TextEdit::fileSaveAs()
258{
259 if ( !currentEditor() )
260 return;
261 QString fn = QFileDialog::getSaveFileName( QString::null, tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
262 if ( !fn.isEmpty() ) {
263 filenames.replace( currentEditor(), fn );
264 fileSave();
265 tabWidget->setTabLabel( currentEditor(), QFileInfo( fn ).fileName() );
266 }
267}
268
269void TextEdit::filePrint()
270{
271 if ( !currentEditor() )
272 return;
273#ifndef QT_NO_PRINTER
274 QPrinter printer( QPrinter::HighResolution );
275 printer.setFullPage(TRUE);
276 if ( printer.setup( this ) ) {
277 QPainter p( &printer );
278 // Check that there is a valid device to print to.
279 if ( !p.device() ) return;
280 QPaintDeviceMetrics metrics( p.device() );
281 int dpiy = metrics.logicalDpiY();
282 int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins
283 QRect body( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin );
284 QFont font( currentEditor()->QWidget::font() );
285 font.setPointSize( 10 ); // we define 10pt to be a nice base size for printing
286
287 QSimpleRichText richText( currentEditor()->text(), font,
288 currentEditor()->context(),
289 currentEditor()->styleSheet(),
290 currentEditor()->mimeSourceFactory(),
291 body.height() );
292 richText.setWidth( &p, body.width() );
293 QRect view( body );
294 int page = 1;
295 do {
296 richText.draw( &p, body.left(), body.top(), view, colorGroup() );
297 view.moveBy( 0, body.height() );
298 p.translate( 0 , -body.height() );
299 p.setFont( font );
300 p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
301 view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
302 if ( view.top() >= richText.height() )
303 break;
304 printer.newPage();
305 page++;
306 } while (TRUE);
307 }
308#endif
309}
310
311void TextEdit::fileClose()
312{
313 delete currentEditor();
314 if ( currentEditor() )
315 currentEditor()->viewport()->setFocus();
316}
317
318void TextEdit::fileExit()
319{
320 qApp->quit();
321}
322
323void TextEdit::editUndo()
324{
325 if ( !currentEditor() )
326 return;
327 currentEditor()->undo();
328}
329
330void TextEdit::editRedo()
331{
332 if ( !currentEditor() )
333 return;
334 currentEditor()->redo();
335}
336
337void TextEdit::editCut()
338{
339 if ( !currentEditor() )
340 return;
341 currentEditor()->cut();
342}
343
344void TextEdit::editCopy()
345{
346 if ( !currentEditor() )
347 return;
348 currentEditor()->copy();
349}
350
351void TextEdit::editPaste()
352{
353 if ( !currentEditor() )
354 return;
355 currentEditor()->paste();
356}
357
358void TextEdit::textBold()
359{
360 if ( !currentEditor() )
361 return;
362 currentEditor()->setBold( actionTextBold->isOn() );
363}
364
365void TextEdit::textUnderline()
366{
367 if ( !currentEditor() )
368 return;
369 currentEditor()->setUnderline( actionTextUnderline->isOn() );
370}
371
372void TextEdit::textItalic()
373{
374 if ( !currentEditor() )
375 return;
376 currentEditor()->setItalic( actionTextItalic->isOn() );
377}
378
379void TextEdit::textFamily( const QString &f )
380{
381 if ( !currentEditor() )
382 return;
383 currentEditor()->setFamily( f );
384 currentEditor()->viewport()->setFocus();
385}
386
387void TextEdit::textSize( const QString &p )
388{
389 if ( !currentEditor() )
390 return;
391 currentEditor()->setPointSize( p.toInt() );
392 currentEditor()->viewport()->setFocus();
393}
394
395void TextEdit::textColor()
396{
397 if ( !currentEditor() )
398 return;
399 QColor col = QColorDialog::getColor( currentEditor()->color(), this );
400 if ( !col.isValid() )
401 return;
402 currentEditor()->setColor( col );
403 QPixmap pix( 16, 16 );
404 pix.fill( black );
405 actionTextColor->setIconSet( pix );
406}
407
408void TextEdit::textAlign( QAction *a )
409{
410 if ( !currentEditor() )
411 return;
412 if ( a == actionAlignLeft )
413 currentEditor()->setAlignment( AlignLeft );
414 else if ( a == actionAlignCenter )
415 currentEditor()->setAlignment( AlignHCenter );
416 else if ( a == actionAlignRight )
417 currentEditor()->setAlignment( AlignRight );
418 else if ( a == actionAlignJustify )
419 currentEditor()->setAlignment( AlignJustify );
420}
421
422void TextEdit::fontChanged( const QFont &f )
423{
424 comboFont->lineEdit()->setText( f.family() );
425 comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
426 actionTextBold->setOn( f.bold() );
427 actionTextItalic->setOn( f.italic() );
428 actionTextUnderline->setOn( f.underline() );
429}
430
431void TextEdit::colorChanged( const QColor &c )
432{
433 QPixmap pix( 16, 16 );
434 pix.fill( c );
435 actionTextColor->setIconSet( pix );
436}
437
438void TextEdit::alignmentChanged( int a )
439{
440 if ( ( a == AlignAuto ) || ( a & AlignLeft ))
441 actionAlignLeft->setOn( TRUE );
442 else if ( ( a & AlignHCenter ) )
443 actionAlignCenter->setOn( TRUE );
444 else if ( ( a & AlignRight ) )
445 actionAlignRight->setOn( TRUE );
446 else if ( ( a & AlignJustify ) )
447 actionAlignJustify->setOn( TRUE );
448}
449
450void TextEdit::editorChanged( QWidget * )
451{
452 if ( !currentEditor() )
453 return;
454 fontChanged( currentEditor()->currentFont() );
455 colorChanged( currentEditor()->color() );
456 alignmentChanged( currentEditor()->alignment() );
457}
Note: See TracBrowser for help on using the repository browser.