home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / textedit / textedit.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  16KB  |  488 lines

  1. /****************************************************************************
  2. ** $Id$
  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.  
  32. TextEdit::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.  
  52. void 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( tr( "New" ), QPixmap( "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( tr( "Open" ), QPixmap( "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( tr( "Save" ), QPixmap( "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" ), QPixmap(), 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( tr( "Print" ), QPixmap( "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" ), QPixmap(), tr( "&Close" ), 0, this, "fileClose" );
  82.     connect( a, SIGNAL( activated() ), this, SLOT( fileClose() ) );
  83.     a->addTo( menu );
  84.     a = new QAction( tr( "Exit" ), QPixmap(), tr( "E&xit" ), 0, this, "fileExit" );
  85.     connect( a, SIGNAL( activated() ), this, SLOT( fileExit() ) );
  86.     a->addTo( menu );
  87. }
  88.  
  89. void 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( tr( "Undo" ), QPixmap( "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( tr( "Redo" ), QPixmap( "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( tr( "Copy" ), QPixmap( "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( tr( "Cut" ), QPixmap( "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( tr( "Paste" ), QPixmap( "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.  
  120. void 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.     comboStyle = new QComboBox( FALSE, tb );
  128.     comboStyle->insertItem( "Standard" );
  129.     comboStyle->insertItem( "Bullet List (Disc)" );
  130.     comboStyle->insertItem( "Bullet List (Circle)" );
  131.     comboStyle->insertItem( "Bullet List (Square)" );
  132.     comboStyle->insertItem( "Ordered List (Decimal)" );
  133.     comboStyle->insertItem( "Ordered List (Alpha lower)" );
  134.     comboStyle->insertItem( "Ordered List (Alpha upper)" );
  135.     connect( comboStyle, SIGNAL( activated( int ) ),
  136.          this, SLOT( textStyle( int ) ) );
  137.  
  138.     comboFont = new QComboBox( TRUE, tb );
  139.     QFontDatabase db;
  140.     comboFont->insertStringList( db.families() );
  141.     connect( comboFont, SIGNAL( activated( const QString & ) ),
  142.          this, SLOT( textFamily( const QString & ) ) );
  143.     comboFont->lineEdit()->setText( QApplication::font().family() );
  144.  
  145.     comboSize = new QComboBox( TRUE, tb );
  146.     QValueList<int> sizes = db.standardSizes();
  147.     QValueList<int>::Iterator it = sizes.begin();
  148.     for ( ; it != sizes.end(); ++it )
  149.     comboSize->insertItem( QString::number( *it ) );
  150.     connect( comboSize, SIGNAL( activated( const QString & ) ),
  151.          this, SLOT( textSize( const QString & ) ) );
  152.     comboSize->lineEdit()->setText( QString::number( QApplication::font().pointSize() ) );
  153.  
  154.     actionTextBold = new QAction( tr( "Bold" ), QPixmap( "textbold.xpm" ), tr( "&Bold" ), CTRL + Key_B, this, "textBold" );
  155.     connect( actionTextBold, SIGNAL( activated() ), this, SLOT( textBold() ) );
  156.     actionTextBold->addTo( tb );
  157.     actionTextBold->addTo( menu );
  158.     actionTextBold->setToggleAction( TRUE );
  159.     actionTextItalic = new QAction( tr( "Italic" ), QPixmap( "textitalic.xpm" ), tr( "&Italic" ), CTRL + Key_I, this, "textItalic" );
  160.     connect( actionTextItalic, SIGNAL( activated() ), this, SLOT( textItalic() ) );
  161.     actionTextItalic->addTo( tb );
  162.     actionTextItalic->addTo( menu );
  163.     actionTextItalic->setToggleAction( TRUE );
  164.     actionTextUnderline = new QAction( tr( "Underline" ), QPixmap( "textunder.xpm" ), tr( "&Underline" ), CTRL + Key_U, this, "textUnderline" );
  165.     connect( actionTextUnderline, SIGNAL( activated() ), this, SLOT( textUnderline() ) );
  166.     actionTextUnderline->addTo( tb );
  167.     actionTextUnderline->addTo( menu );
  168.     actionTextUnderline->setToggleAction( TRUE );
  169.     menu->insertSeparator();
  170.  
  171.     QActionGroup *grp = new QActionGroup( this );
  172.     grp->setExclusive( TRUE );
  173.     connect( grp, SIGNAL( selected( QAction* ) ), this, SLOT( textAlign( QAction* ) ) );
  174.  
  175.     actionAlignLeft = new QAction( tr( "Left" ), QPixmap( "textleft.xpm" ), tr( "&Left" ), CTRL + Key_L, grp, "textLeft" );
  176.     actionAlignLeft->addTo( tb );
  177.     actionAlignLeft->addTo( menu );
  178.     actionAlignLeft->setToggleAction( TRUE );
  179.     actionAlignCenter = new QAction( tr( "Center" ), QPixmap( "textcenter.xpm" ), tr( "C&enter" ), CTRL + Key_E, grp, "textCenter" );
  180.     actionAlignCenter->addTo( tb );
  181.     actionAlignCenter->addTo( menu );
  182.     actionAlignCenter->setToggleAction( TRUE );
  183.     actionAlignRight = new QAction( tr( "Right" ), QPixmap( "textright.xpm" ), tr( "&Right" ), CTRL + Key_R, grp, "textRight" );
  184.     actionAlignRight->addTo( tb );
  185.     actionAlignRight->addTo( menu );
  186.     actionAlignRight->setToggleAction( TRUE );
  187.     actionAlignJustify = new QAction( tr( "Justify" ), QPixmap( "textjustify.xpm" ), tr( "&Justify" ), CTRL + Key_J, grp, "textjustify" );
  188.     actionAlignJustify->addTo( tb );
  189.     actionAlignJustify->addTo( menu );
  190.     actionAlignJustify->setToggleAction( TRUE );
  191.  
  192.     menu->insertSeparator();
  193.  
  194.     QPixmap pix( 16, 16 );
  195.     pix.fill( black );
  196.     actionTextColor = new QAction( tr( "Color" ), pix, tr( "&Color..." ), 0, this, "textColor" );
  197.     connect( actionTextColor, SIGNAL( activated() ), this, SLOT( textColor() ) );
  198.     actionTextColor->addTo( tb );
  199.     actionTextColor->addTo( menu );
  200. }
  201.  
  202. void TextEdit::load( const QString &f )
  203. {
  204.     if ( !QFile::exists( f ) )
  205.     return;
  206.     QTextEdit *edit = new QTextEdit( tabWidget );
  207.     doConnections( edit );
  208.     tabWidget->addTab( edit, QFileInfo( f ).fileName() );
  209.     QFile file( f );
  210.     if ( !file.open( IO_ReadOnly ) )
  211.     return;
  212.     QTextStream ts( &file );
  213.     edit->setText( ts.read() );
  214.     tabWidget->showPage( edit );
  215.     edit->viewport()->setFocus();
  216.     filenames.replace( edit, f );
  217. }
  218.  
  219. QTextEdit *TextEdit::currentEditor() const
  220. {
  221.     if ( tabWidget->currentPage() &&
  222.      tabWidget->currentPage()->inherits( "QTextEdit" ) )
  223.     return (QTextEdit*)tabWidget->currentPage();
  224.     return 0;    
  225. }
  226.  
  227. void TextEdit::doConnections( QTextEdit *e )
  228. {
  229.     connect( e, SIGNAL( currentFontChanged( const QFont & ) ),
  230.          this, SLOT( fontChanged( const QFont & ) ) );
  231.     connect( e, SIGNAL( currentColorChanged( const QColor & ) ),
  232.          this, SLOT( colorChanged( const QColor & ) ) );
  233.     connect( e, SIGNAL( currentAlignmentChanged( int ) ),
  234.          this, SLOT( alignmentChanged( int ) ) );
  235. }
  236.  
  237. void TextEdit::fileNew()
  238. {
  239.     QTextEdit *edit = new QTextEdit( tabWidget );
  240.     edit->setTextFormat( RichText );
  241.     doConnections( edit );
  242.     tabWidget->addTab( edit, tr( "noname" ) );
  243.     tabWidget->showPage( edit );
  244.     edit->viewport()->setFocus();
  245. }
  246.  
  247. void TextEdit::fileOpen()
  248. {
  249.     QString fn = QFileDialog::getOpenFileName( QString::null, tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
  250.     if ( !fn.isEmpty() )
  251.     load( fn );
  252. }
  253.  
  254. void TextEdit::fileSave()
  255. {
  256.     if ( !currentEditor() )
  257.     return;
  258.     QString fn;
  259.     if ( filenames.find( currentEditor() ) == filenames.end() ) {
  260.     fileSaveAs();
  261.     } else {
  262.     QFile file( *filenames.find( currentEditor() ) );
  263.     if ( !file.open( IO_WriteOnly ) )
  264.         return;
  265.     QTextStream ts( &file );
  266.     ts << currentEditor()->text();
  267.     }
  268. }
  269.  
  270. void TextEdit::fileSaveAs()
  271. {
  272.     if ( !currentEditor() )
  273.     return;
  274.     QString fn = QFileDialog::getSaveFileName( QString::null, tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
  275.     if ( !fn.isEmpty() ) {
  276.     filenames.replace( currentEditor(), fn );
  277.     fileSave();
  278.     tabWidget->setTabLabel( currentEditor(), QFileInfo( fn ).fileName() );
  279.     }
  280. }
  281.  
  282. void TextEdit::filePrint()
  283. {
  284.     if ( !currentEditor() )
  285.     return;
  286. #ifndef QT_NO_PRINTER
  287.     QPrinter printer;
  288.     printer.setFullPage(TRUE);
  289.     if ( printer.setup( this ) ) {
  290.     QPainter p( &printer );
  291.     QPaintDeviceMetrics metrics( p.device() );
  292.     int dpix = metrics.logicalDpiX();
  293.     int dpiy = metrics.logicalDpiY();
  294.     const int margin = 72; // pt
  295.     QRect body( margin * dpix / 72, margin * dpiy / 72,
  296.             metrics.width() - margin * dpix / 72 * 2,
  297.             metrics.height() - margin * dpiy / 72 * 2 );
  298.     QFont font( "times", 10 );
  299.     QSimpleRichText richText( currentEditor()->text(), font, currentEditor()->context(), currentEditor()->styleSheet(),
  300.                   currentEditor()->mimeSourceFactory(), body.height() );
  301.     richText.setWidth( &p, body.width() );
  302.     QRect view( body );
  303.     int page = 1;
  304.     do {
  305.         richText.draw( &p, body.left(), body.top(), view, colorGroup() );
  306.         view.moveBy( 0, body.height() );
  307.         p.translate( 0 , -body.height() );
  308.         p.setFont( font );
  309.         p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ),
  310.             view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) );
  311.         if ( view.top()  >= richText.height() )
  312.         break;
  313.         printer.newPage();
  314.         page++;
  315.     } while (TRUE);
  316.     }
  317. #endif
  318. }
  319.  
  320. void TextEdit::fileClose()
  321. {
  322.     delete currentEditor();
  323.     if ( currentEditor() )
  324.     currentEditor()->viewport()->setFocus();                
  325. }
  326.  
  327. void TextEdit::fileExit()
  328. {
  329.     qApp->quit();
  330. }
  331.  
  332. void TextEdit::editUndo()
  333. {
  334.     if ( !currentEditor() )
  335.     return;
  336.     currentEditor()->undo();
  337. }
  338.  
  339. void TextEdit::editRedo()
  340. {
  341.     if ( !currentEditor() )
  342.     return;
  343.     currentEditor()->redo();
  344. }
  345.  
  346. void TextEdit::editCut()
  347. {
  348.     if ( !currentEditor() )
  349.     return;
  350.     currentEditor()->cut();
  351. }
  352.  
  353. void TextEdit::editCopy()
  354. {
  355.     if ( !currentEditor() )
  356.     return;
  357.     currentEditor()->copy();
  358. }
  359.  
  360. void TextEdit::editPaste()
  361. {
  362.     if ( !currentEditor() )
  363.     return;
  364.     currentEditor()->paste();
  365. }
  366.  
  367. void TextEdit::textBold()
  368. {
  369.     if ( !currentEditor() )
  370.     return;
  371.     currentEditor()->setBold( actionTextBold->isOn() );
  372. }
  373.  
  374. void TextEdit::textUnderline()
  375. {
  376.     if ( !currentEditor() )
  377.     return;
  378.     currentEditor()->setUnderline( actionTextUnderline->isOn() );
  379. }
  380.  
  381. void TextEdit::textItalic()
  382. {
  383.     if ( !currentEditor() )
  384.     return;
  385.     currentEditor()->setItalic( actionTextItalic->isOn() );
  386. }
  387.  
  388. void TextEdit::textFamily( const QString &f )
  389. {
  390.     if ( !currentEditor() )
  391.     return;
  392.     currentEditor()->setFamily( f );
  393.     currentEditor()->viewport()->setFocus();
  394. }
  395.  
  396. void TextEdit::textSize( const QString &p )
  397. {
  398.     if ( !currentEditor() )
  399.     return;
  400.     currentEditor()->setPointSize( p.toInt() );
  401.     currentEditor()->viewport()->setFocus();
  402. }
  403.  
  404. void TextEdit::textStyle( int i )
  405. {
  406.     if ( !currentEditor() )
  407.     return;
  408.     if ( i == 0 )
  409.     currentEditor()->setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc );
  410.     else if ( i == 1 )
  411.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc );
  412.     else if ( i == 2 )
  413.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListCircle );
  414.     else if ( i == 3 )
  415.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListSquare );
  416.     else if ( i == 4 )
  417.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDecimal );
  418.     else if ( i == 5 )
  419.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListLowerAlpha );
  420.     else if ( i == 6 )
  421.     currentEditor()->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListUpperAlpha );
  422.     currentEditor()->viewport()->setFocus();
  423. }
  424.  
  425. void TextEdit::textColor()
  426. {
  427.     if ( !currentEditor() )
  428.     return;
  429.     QColor col = QColorDialog::getColor( currentEditor()->color(), this );
  430.     if ( !col.isValid() )
  431.     return;
  432.     currentEditor()->setColor( col );
  433.     QPixmap pix( 16, 16 );
  434.     pix.fill( black );
  435.     actionTextColor->setIconSet( pix );
  436. }
  437.  
  438. void TextEdit::textAlign( QAction *a )
  439. {
  440.     if ( !currentEditor() )
  441.     return;
  442.     if ( a == actionAlignLeft )
  443.     currentEditor()->setAlignment( AlignLeft );
  444.     else if ( a == actionAlignCenter )
  445.     currentEditor()->setAlignment( AlignHCenter );
  446.     else if ( a == actionAlignRight )
  447.     currentEditor()->setAlignment( AlignRight );
  448.     else if ( a == actionAlignJustify )
  449.     currentEditor()->setAlignment( AlignJustify );
  450. }
  451.  
  452. void TextEdit::fontChanged( const QFont &f )
  453. {
  454.     comboFont->lineEdit()->setText( f.family() );
  455.     comboSize->lineEdit()->setText( QString::number( f.pointSize() ) );
  456.     actionTextBold->setOn( f.bold() );
  457.     actionTextItalic->setOn( f.italic() );
  458.     actionTextUnderline->setOn( f.underline() );
  459. }
  460.  
  461. void TextEdit::colorChanged( const QColor &c )
  462. {
  463.     QPixmap pix( 16, 16 );
  464.     pix.fill( c );
  465.     actionTextColor->setIconSet( pix );
  466. }
  467.  
  468. void TextEdit::alignmentChanged( int a )
  469. {
  470.     if ( ( a == AlignAuto ) || ( a & AlignLeft ))
  471.     actionAlignLeft->setOn( TRUE );
  472.     else if ( ( a & AlignHCenter ) )
  473.     actionAlignCenter->setOn( TRUE );
  474.     else if ( ( a & AlignRight ) )
  475.     actionAlignRight->setOn( TRUE );
  476.     else if ( ( a & AlignJustify ) )
  477.     actionAlignJustify->setOn( TRUE );
  478. }
  479.  
  480. void TextEdit::editorChanged( QWidget * )
  481. {
  482.     if ( !currentEditor() )
  483.     return;
  484.     fontChanged( currentEditor()->font() );
  485.     colorChanged( currentEditor()->color() );
  486.     alignmentChanged( currentEditor()->alignment() );
  487. }
  488.