File: fontdisplayer.cpp

package info (click to toggle)
qt-x11 3%3A2.3.1-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48,524 kB
  • ctags: 46,337
  • sloc: cpp: 260,077; ansic: 32,457; makefile: 31,131; yacc: 2,444; sh: 1,513; lex: 480; perl: 422; xml: 68; lisp: 15
file content (131 lines) | stat: -rw-r--r-- 3,307 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/****************************************************************************
** $Id: qt/examples/qfd/fontdisplayer.cpp   2.3.1   edited 2001-01-26 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "fontdisplayer.h"
#include <qslider.h>
#include <qspinbox.h>
#include <qpainter.h>
#include <qtoolbar.h>
#include <qstatusbar.h>
#include <stdlib.h>


FontRowTable::FontRowTable( QWidget* parent, const char* name ) :
    QFrame(parent,name)
{
    setBackgroundMode(PaletteBase);
    setFrameStyle(Panel|Sunken);
    setMargin(8);
    setRow(0);
}

QSize FontRowTable::sizeHint() const
{
    return 16*cellSize()+QSize(2,2)*(margin()+frameWidth());
}

QSize FontRowTable::cellSize() const
{
    QFontMetrics fm = fontMetrics();
    return QSize( fm.maxWidth(), fm.lineSpacing()+1 );
}

void FontRowTable::paintEvent( QPaintEvent* e )
{
    QFrame::paintEvent(e);
    QPainter p(this);
    p.setClipRegion(e->region());
    QRect r = e->rect();
    QFontMetrics fm = fontMetrics();
    int ml = frameWidth()+margin() + 1 + QMAX(0,-fm.minLeftBearing());
    int mt = frameWidth()+margin();
    QSize cell((width()-15-ml)/16,(height()-15-mt)/16);

    if ( !cell.width() || !cell.height() )
	return;

    int mini = r.left() / cell.width();
    int maxi = (r.right()+cell.width()-1) / cell.width();
    int minj = r.top() / cell.height();
    int maxj = (r.bottom()+cell.height()-1) / cell.height();

    int h = fm.height();

    QColor body(255,255,192);
    QColor negative(255,192,192);
    QColor positive(192,192,255);
    QColor rnegative(255,128,128);
    QColor rpositive(128,128,255);

    for (int j = minj; j<=maxj; j++) {
	for (int i = mini; i<=maxi; i++) {
	    if ( i < 16 && j < 16 ) {
		int x = i*cell.width();
		int y = j*cell.height();

		QChar ch = QChar(j*16+i,row);

		if ( fm.inFont(ch) ) {
		    int w = fm.width(ch);
		    int l = fm.leftBearing(ch);
		    int r = fm.rightBearing(ch);

		    x += ml;
		    y += mt+h;

		    p.fillRect(x,y,w,-h,body);
		    if ( w ) {
			if ( l ) {
			    p.fillRect(x+(l>0?0:l), y-h/2, abs(l),-h/2,
				       l < 0 ? negative : positive);
			}
			if ( r ) {
			    p.fillRect(x+w-(r>0?r:0),y+2, abs(r),-h/2,
				       r < 0 ? rnegative : rpositive);
			}
		    }
		    QString s;
		    s += ch;
		    p.setPen(QPen(Qt::black));
		    p.drawText(x,y,s);
		}
	    }
	}
    }
}

void FontRowTable::setRow(int r)
{
    row = r;

    QFontMetrics fm = fontMetrics();
    QString str;
    str.sprintf("mLB=%d mRB=%d mW=%d",
	fm.minLeftBearing(),
	fm.minRightBearing(),
	fm.maxWidth()
	);
	
    emit fontInformation(str);
    update();
}

FontDisplayer::FontDisplayer( QWidget* parent, const char* name ) :
    QMainWindow(parent,name)
{
    FontRowTable* table = new FontRowTable(this);
    QToolBar* controls = new QToolBar(this);
    QSpinBox *row = new QSpinBox(0,255,1,controls);
    connect(row,SIGNAL(valueChanged(int)),table,SLOT(setRow(int)));
    connect(table,SIGNAL(fontInformation(const QString&)),
	statusBar(),SLOT(message(const QString&)));
    table->setRow(0);
    setCentralWidget(table);
}