home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / qfd / fontdisplayer.cpp next >
C/C++ Source or Header  |  2001-10-11  |  4KB  |  156 lines

  1. /****************************************************************************
  2. ** $Id:  qt/fontdisplayer.cpp   3.0.0   edited Jun 22 13:24 $
  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 "fontdisplayer.h"
  12. #include <qapplication.h>
  13. #include <qslider.h>
  14. #include <qspinbox.h>
  15. #include <qpainter.h>
  16. #include <qtoolbar.h>
  17. #include <qstatusbar.h>
  18. #include <qlabel.h>
  19. #include <qpushbutton.h>
  20. #include <qfontdialog.h>
  21. #include <stdlib.h>
  22.  
  23.  
  24. FontRowTable::FontRowTable( QWidget* parent, const char* name ) :
  25.     QFrame(parent,name)
  26. {
  27.     setBackgroundMode(PaletteBase);
  28.     setFrameStyle(Panel|Sunken);
  29.     setMargin(8);
  30.     setRow(0);
  31.     tablefont = QApplication::font();
  32. }
  33.  
  34. QSize FontRowTable::sizeHint() const
  35. {
  36.     return 16*cellSize()+QSize(2,2)*(margin()+frameWidth());
  37. }
  38.  
  39. QSize FontRowTable::cellSize() const
  40. {
  41.     QFontMetrics fm = fontMetrics();
  42.     return QSize( fm.maxWidth(), fm.lineSpacing()+1 );
  43. }
  44.  
  45. void FontRowTable::paintEvent( QPaintEvent* e )
  46. {
  47.     QFrame::paintEvent(e);
  48.     QPainter p(this);
  49.     p.setClipRegion(e->region());
  50.     QRect r = e->rect();
  51.     QFontMetrics fm = fontMetrics();
  52.     int ml = frameWidth()+margin() + 1 + QMAX(0,-fm.minLeftBearing());
  53.     int mt = frameWidth()+margin();
  54.     QSize cell((width()-15-ml)/16,(height()-15-mt)/16);
  55.  
  56.     if ( !cell.width() || !cell.height() )
  57.     return;
  58.  
  59.     int mini = r.left() / cell.width();
  60.     int maxi = (r.right()+cell.width()-1) / cell.width();
  61.     int minj = r.top() / cell.height();
  62.     int maxj = (r.bottom()+cell.height()-1) / cell.height();
  63.  
  64.     int h = fm.height();
  65.  
  66.     QColor body(255,255,192);
  67.     QColor negative(255,192,192);
  68.     QColor positive(192,192,255);
  69.     QColor rnegative(255,128,128);
  70.     QColor rpositive(128,128,255);
  71.  
  72.     for (int j = minj; j<=maxj; j++) {
  73.     for (int i = mini; i<=maxi; i++) {
  74.         if ( i < 16 && j < 16 ) {
  75.         int x = i*cell.width();
  76.         int y = j*cell.height();
  77.  
  78.         QChar ch = QChar(j*16+i,row);
  79.  
  80.         if ( fm.inFont(ch) ) {
  81.             int w = fm.width(ch);
  82.             int l = fm.leftBearing(ch);
  83.             int r = fm.rightBearing(ch);
  84.  
  85.             x += ml;
  86.             y += mt+h;
  87.  
  88.             p.fillRect(x,y,w,-h,body);
  89.             if ( w ) {
  90.             if ( l ) {
  91.                 p.fillRect(x+(l>0?0:l), y-h/2, abs(l),-h/2,
  92.                        l < 0 ? negative : positive);
  93.             }
  94.             if ( r ) {
  95.                 p.fillRect(x+w-(r>0?r:0),y+2, abs(r),-h/2,
  96.                        r < 0 ? rnegative : rpositive);
  97.             }
  98.             }
  99.             QString s;
  100.             s += ch;
  101.             p.setPen(QPen(Qt::black));
  102.             p.drawText(x,y,s);
  103.         }
  104.         }
  105.     }
  106.     }
  107. }
  108.  
  109. void FontRowTable::setRow(int r)
  110. {
  111.     row = r;
  112.  
  113.     QFontMetrics fm = fontMetrics();
  114.     QString str;
  115.     str.sprintf("mLB=%d mRB=%d mW=%d",
  116.     fm.minLeftBearing(),
  117.     fm.minRightBearing(),
  118.     fm.maxWidth()
  119.     );
  120.  
  121.     emit fontInformation(str);
  122.     update();
  123. }
  124.  
  125. void FontRowTable::chooseFont()
  126. {
  127.     bool ok;
  128.     QFont oldfont = tablefont;
  129.     tablefont = QFontDialog::getFont(&ok, oldfont, this);
  130.  
  131.     if (ok)
  132.     setFont(tablefont);
  133.     else
  134.     tablefont = oldfont;
  135.  
  136.  
  137. }
  138.  
  139. FontDisplayer::FontDisplayer( QWidget* parent, const char* name ) :
  140.     QMainWindow(parent,name)
  141. {
  142.     FontRowTable* table = new FontRowTable(this);
  143.     QToolBar* controls = new QToolBar(this);
  144.     (void) new QLabel(tr("Row:"), controls);
  145.     QSpinBox *row = new QSpinBox(0,255,1,controls);
  146.     controls->addSeparator();
  147.     QPushButton *fontbutton = new QPushButton(tr("Font..."), controls);
  148.  
  149.     connect(row,SIGNAL(valueChanged(int)),table,SLOT(setRow(int)));
  150.     connect(fontbutton, SIGNAL(clicked()), table, SLOT(chooseFont()));
  151.     connect(table,SIGNAL(fontInformation(const QString&)),
  152.         statusBar(),SLOT(message(const QString&)));
  153.     table->setRow(0);
  154.     setCentralWidget(table);
  155. }
  156.