home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / fonts / simple-qfont-demo / viewer.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  5KB  |  162 lines

  1. /* $Id$ */
  2.  
  3. #include "viewer.h"
  4. #include <qstring.h>
  5. #include <qstringlist.h>
  6. #include <qtextview.h>
  7. #include <qpushbutton.h>
  8. #include <qlayout.h>
  9.  
  10. Viewer::Viewer()
  11.        :QWidget()
  12. {
  13.     setFontSubstitutions();
  14.  
  15.     QString greeting_heb = QString::fromUtf8( "\327\251\327\234\327\225\327\235" );
  16.     QString greeting_ru = QString::fromUtf8( "\320\227\320\264\321\200\320\260\320\262\321\201\321\202\320\262\321\203\320\271\321\202\320\265" );
  17.     QString greeting_en( "Hello" );
  18.  
  19.     greetings = new QTextView( this, "textview" );
  20.  
  21.     greetings->setText( greeting_en + "\n" +
  22.                        greeting_ru + "\n" +
  23.                        greeting_heb );
  24.  
  25.     fontInfo = new QTextView( this, "fontinfo" );
  26.  
  27.     setDefault();
  28.  
  29.     defaultButton = new QPushButton( "Default", this,
  30.                                                    "pushbutton1" );
  31.     defaultButton->setFont( QFont( "times" ) );
  32.     connect( defaultButton, SIGNAL( clicked() ),
  33.              this, SLOT( setDefault() ) );
  34.  
  35.     sansSerifButton = new QPushButton( "Sans Serif", this,
  36.                                                      "pushbutton2" );
  37.     sansSerifButton->setFont( QFont( "Helvetica", 12 ) );
  38.     connect( sansSerifButton, SIGNAL( clicked() ),
  39.              this, SLOT( setSansSerif() ) );
  40.  
  41.     italicsButton = new QPushButton( "Italics", this,
  42.                                                    "pushbutton3" );
  43.     italicsButton->setFont( QFont( "lucida", 12, QFont::Bold, TRUE ) );
  44.     connect( italicsButton, SIGNAL( clicked() ),
  45.              this, SLOT( setItalics() ) );
  46.  
  47.     layout();
  48. }
  49.  
  50. void Viewer::setDefault()
  51. {
  52.     QFont font( "Bavaria" );
  53.     font.setPointSize( 24 );
  54.  
  55.     font.setWeight( QFont::Bold );
  56.     font.setUnderline( TRUE );
  57.  
  58.     greetings->setFont( font );
  59.  
  60.     showFontInfo( font );
  61. }
  62.  
  63. void Viewer::setSansSerif()
  64. {
  65.     QFont font( "Newyork", 18 );
  66.     font.setStyleHint( QFont::SansSerif );
  67.     greetings->setFont( font );
  68.  
  69.     showFontInfo( font );
  70. }
  71.  
  72. void Viewer::setItalics()
  73. {
  74.     QFont font( "Tokyo" );
  75.     font.setPointSize( 32 );
  76.     font.setWeight( QFont::Bold );
  77.     font.setItalic( TRUE );
  78.  
  79.     greetings->setFont( font );
  80.  
  81.     showFontInfo( font );
  82. }
  83.  
  84. void Viewer::showFontInfo( QFont & font )
  85. {
  86.     QFontInfo info( font );
  87.  
  88.     QString messageText;
  89.     messageText = "Font requested: \"" +
  90.                   font.family() + "\" " +
  91.                   QString::number( font.pointSize() ) + "pt<BR>" +
  92.                   "Font used: \"" +
  93.                   info.family() + "\" " +
  94.                   QString::number( info.pointSize() ) + "pt<P>";
  95.  
  96.     QStringList substitutions = QFont::substitutes( font.family() );
  97.  
  98.     if ( ! substitutions.isEmpty() ){
  99.     messageText += "The following substitutions exist for " + \
  100.                font.family() + ":<UL>";
  101.  
  102.     QStringList::Iterator i = substitutions.begin();
  103.     while ( i != substitutions.end() ){
  104.         messageText += "<LI>\"" + (* i) + "\"";
  105.         i++;
  106.     }
  107.      messageText += "</UL>";
  108.     } else {
  109.     messageText += "No substitutions exist for " + \
  110.                font.family() + ".";
  111.     }
  112.  
  113.     fontInfo->setText( messageText );
  114. }
  115.  
  116. void Viewer::setFontSubstitutions()
  117. {
  118.     QStringList substitutes;
  119.     substitutes.append( "Times" );
  120.     substitutes +=  "Mincho",
  121.     substitutes << "Arabic Newspaper" << "crox";
  122.  
  123.     QFont::insertSubstitutions( "Bavaria", substitutes );
  124.  
  125.     QFont::insertSubstitution( "Tokyo", "Lucida" );
  126. }
  127.  
  128.  
  129. // For those who prefer to use Qt Designer for creating GUIs
  130. // the following function might not be of particular interest:
  131. // all it does is creating the widget layout.
  132.  
  133. void Viewer::layout()
  134. {
  135.     QHBoxLayout * textViewContainer = new QHBoxLayout();
  136.     textViewContainer->addWidget( greetings );
  137.     textViewContainer->addWidget( fontInfo );
  138.  
  139.     QHBoxLayout * buttonContainer = new QHBoxLayout();
  140.  
  141.     buttonContainer->addWidget( defaultButton );
  142.     buttonContainer->addWidget( sansSerifButton );
  143.     buttonContainer->addWidget( italicsButton );
  144.  
  145.     int maxButtonHeight = defaultButton->height();
  146.  
  147.     if ( sansSerifButton->height() > maxButtonHeight )
  148.     maxButtonHeight = sansSerifButton->height();
  149.     if ( italicsButton->height() > maxButtonHeight )
  150.         maxButtonHeight = italicsButton->height();
  151.  
  152.     defaultButton->setFixedHeight( maxButtonHeight );
  153.     sansSerifButton->setFixedHeight( maxButtonHeight );
  154.     italicsButton->setFixedHeight( maxButtonHeight );
  155.  
  156.     QVBoxLayout * container = new QVBoxLayout( this );
  157.     container->addLayout( textViewContainer );
  158.     container->addLayout( buttonContainer );
  159.  
  160.     resize( 700, 250 );
  161. }
  162.