not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kfontinst / viewpart / FontPreview.cpp
blob17da2c0c01cce8db6d1848b07cf170eb399ff572
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
6 * ----
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "FontPreview.h"
25 #include "FcEngine.h"
26 #include "CharTip.h"
27 #include <KDE/KApplication>
28 #include <KDE/KLocale>
29 #include <QtGui/QPainter>
30 #include <QtGui/QPaintEvent>
31 #include <QtGui/QMouseEvent>
32 #include <QtGui/QWheelEvent>
33 #include <stdlib.h>
35 namespace KFI
38 static const int constBorder=4;
39 static const int constStepSize=16;
41 CFontPreview::CFontPreview(QWidget *parent)
42 : QWidget(parent),
43 itsCurrentFace(1),
44 itsLastWidth(0),
45 itsLastHeight(0),
46 itsStyleInfo(KFI_NO_STYLE_INFO),
47 itsTip(NULL)
49 itsEngine=new CFcEngine;
52 CFontPreview::~CFontPreview()
54 delete itsTip;
57 void CFontPreview::showFont(const KUrl &url, const QString &name, unsigned long styleInfo,
58 int face)
60 itsFontName=name;
61 itsCurrentUrl=url;
62 itsStyleInfo=styleInfo;
63 showFace(face);
66 void CFontPreview::showFace(int face)
68 itsCurrentFace=face;
69 showFont();
72 void CFontPreview::showFont()
74 itsLastWidth=width()+constStepSize;
75 itsLastHeight=height()+constStepSize;
77 if(!itsCurrentUrl.isEmpty() &&
78 itsEngine->draw(itsCurrentUrl, itsLastWidth, itsLastHeight, itsPixmap,
79 itsCurrentFace, false, itsRange, &itsChars, itsFontName,
80 itsStyleInfo))
82 itsLastChar=CFcEngine::TChar();
83 setMouseTracking(itsChars.count()>0);
84 update();
85 emit status(true);
87 else
89 QPixmap nullPix;
91 itsPixmap=nullPix;
92 itsLastChar=CFcEngine::TChar();
93 setMouseTracking(false);
94 update();
95 emit status(false);
99 void CFontPreview::zoomIn()
101 itsEngine->zoomIn();
102 showFont();
105 void CFontPreview::zoomOut()
107 itsEngine->zoomOut();
108 showFont();
111 void CFontPreview::setUnicodeRange(const QList<CFcEngine::TRange> &r)
113 itsRange=r;
114 showFont();
117 void CFontPreview::paintEvent(QPaintEvent *)
119 QPainter paint(this);
121 paint.fillRect(rect(), palette().base());
122 if(!itsPixmap.isNull())
125 if(abs(width()-itsLastWidth)>constStepSize || abs(height()-itsLastHeight)>constStepSize)
126 showFont();
127 else
128 paint.drawPixmap(QPoint(constBorder, constBorder), itsPixmap,
129 QRect(0, 0, width()-(constBorder*2), height()-(constBorder*2)));
133 void CFontPreview::mouseMoveEvent(QMouseEvent *event)
135 if(itsChars.size())
137 QList<CFcEngine::TChar>::ConstIterator end(itsChars.end());
139 if(itsLastChar.isNull() || !itsLastChar.contains(event->pos()))
140 for(QList<CFcEngine::TChar>::ConstIterator it(itsChars.begin()); it!=end; ++it)
141 if((*it).contains(event->pos()))
143 if(!itsTip)
144 itsTip=new CCharTip(this);
146 itsTip->setItem(*it);
147 itsLastChar=*it;
148 break;
153 void CFontPreview::wheelEvent(QWheelEvent *e)
155 if(e->delta()>0)
156 emit doZoomIn();
157 else if(e->delta()<0)
158 emit doZoomOut();
160 e->accept();
163 QSize CFontPreview::sizeHint() const
165 return QSize(132, 132);
168 QSize CFontPreview::minimumSizeHint() const
170 return QSize(32, 32);
175 #include "FontPreview.moc"