File indexing completed on 2025-01-12 05:02:08
0001 /* 0002 SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "FontPreview.h" 0007 #include "CharTip.h" 0008 #include <QMouseEvent> 0009 #include <QPainter> 0010 #include <QWheelEvent> 0011 #include <stdlib.h> 0012 0013 namespace KFI 0014 { 0015 static const int constBorder = 4; 0016 static const int constStepSize = 16; 0017 0018 CFontPreview::CFontPreview(QWidget *parent) 0019 : QWidget(parent) 0020 , m_currentFace(1) 0021 , m_lastWidth(0) 0022 , m_lastHeight(0) 0023 , m_styleInfo(KFI_NO_STYLE_INFO) 0024 , m_tip(nullptr) 0025 { 0026 m_engine = new CFcEngine; 0027 } 0028 0029 CFontPreview::~CFontPreview() 0030 { 0031 delete m_tip; 0032 delete m_engine; 0033 } 0034 0035 void CFontPreview::showFont(const QString &name, unsigned long styleInfo, int face) 0036 { 0037 m_fontName = name; 0038 m_styleInfo = styleInfo; 0039 showFace(face); 0040 } 0041 0042 void CFontPreview::showFace(int face) 0043 { 0044 m_currentFace = face; 0045 showFont(); 0046 } 0047 0048 void CFontPreview::showFont() 0049 { 0050 m_lastWidth = width() + constStepSize; 0051 m_lastHeight = height() + constStepSize; 0052 0053 m_image = m_engine->draw(m_fontName, 0054 m_styleInfo, 0055 m_currentFace, 0056 palette().text().color(), 0057 palette().base().color(), 0058 m_lastWidth, 0059 m_lastHeight, 0060 false, 0061 m_range, 0062 &m_chars); 0063 0064 if (!m_image.isNull()) { 0065 m_lastChar = CFcEngine::TChar(); 0066 setMouseTracking(m_chars.count() > 0); 0067 update(); 0068 Q_EMIT status(true); 0069 Q_EMIT atMax(m_engine->atMax()); 0070 Q_EMIT atMin(m_engine->atMin()); 0071 } else { 0072 m_lastChar = CFcEngine::TChar(); 0073 setMouseTracking(false); 0074 update(); 0075 Q_EMIT status(false); 0076 Q_EMIT atMax(true); 0077 Q_EMIT atMin(true); 0078 } 0079 } 0080 0081 void CFontPreview::zoomIn() 0082 { 0083 m_engine->zoomIn(); 0084 showFont(); 0085 Q_EMIT atMax(m_engine->atMax()); 0086 } 0087 0088 void CFontPreview::zoomOut() 0089 { 0090 m_engine->zoomOut(); 0091 showFont(); 0092 Q_EMIT atMin(m_engine->atMin()); 0093 } 0094 0095 void CFontPreview::setUnicodeRange(const QList<CFcEngine::TRange> &r) 0096 { 0097 m_range = r; 0098 showFont(); 0099 } 0100 0101 void CFontPreview::paintEvent(QPaintEvent *) 0102 { 0103 QPainter paint(this); 0104 0105 paint.fillRect(rect(), palette().base()); 0106 if (!m_image.isNull()) { 0107 if (abs(width() - m_lastWidth) > constStepSize || abs(height() - m_lastHeight) > constStepSize) { 0108 showFont(); 0109 } else { 0110 paint.drawImage( 0111 QPointF(constBorder, constBorder), 0112 m_image, 0113 QRectF(0, 0, (width() - (constBorder * 2)) * m_image.devicePixelRatioF(), (height() - (constBorder * 2)) * m_image.devicePixelRatioF())); 0114 } 0115 } 0116 } 0117 0118 void CFontPreview::mouseMoveEvent(QMouseEvent *event) 0119 { 0120 if (!m_chars.isEmpty()) { 0121 QList<CFcEngine::TChar>::ConstIterator end(m_chars.end()); 0122 0123 if (m_lastChar.isNull() || !m_lastChar.contains(event->pos())) { 0124 for (QList<CFcEngine::TChar>::ConstIterator it(m_chars.begin()); it != end; ++it) { 0125 if ((*it).contains(event->pos())) { 0126 if (!m_tip) { 0127 m_tip = new CCharTip(this); 0128 } 0129 0130 m_tip->setItem(*it); 0131 m_lastChar = *it; 0132 break; 0133 } 0134 } 0135 } 0136 } 0137 } 0138 0139 void CFontPreview::wheelEvent(QWheelEvent *e) 0140 { 0141 if (e->angleDelta().y() > 0) { 0142 zoomIn(); 0143 } else if (e->angleDelta().y() < 0) { 0144 zoomOut(); 0145 } 0146 0147 e->accept(); 0148 } 0149 0150 QSize CFontPreview::sizeHint() const 0151 { 0152 return QSize(132, 132); 0153 } 0154 0155 QSize CFontPreview::minimumSizeHint() const 0156 { 0157 return QSize(32, 32); 0158 } 0159 0160 }