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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
fontmodel.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Stephen Kelly <stephen.kelly@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "fontmodel.h"
#include <QFontMetrics>
#include <QPixmap>
#include <QPainter>
using namespace GammaRay;
FontModel::FontModel(QObject *parent)
: QAbstractTableModel(parent)
, m_size(12)
, m_bold(false)
, m_italic(false)
, m_underline(false)
{
}
QVector<QFont> FontModel::currentFonts() const
{
return m_fonts;
}
void FontModel::updateFonts(const QVector<QFont> &fonts)
{
if (!m_fonts.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, m_fonts.size() - 1);
m_fonts.clear();
endRemoveRows();
}
if (fonts.isEmpty())
return;
beginInsertRows(QModelIndex(), 0, fonts.size() - 1);
m_fonts = fonts;
for (int i = 0; i < m_fonts.size(); ++i) {
QFont &font = m_fonts[i];
font.setPointSize(m_size);
font.setBold(m_bold);
font.setItalic(m_italic);
font.setUnderline(m_underline);
}
endInsertRows();
}
void FontModel::updateText(const QString &text)
{
if (text == m_text)
return;
m_text = text;
fontDataChanged();
}
QVariant FontModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0)
return "Font Family";
else if (section == 1)
return "Style Name";
else if (section == 2)
return "Text Preview";
}
return QAbstractTableModel::headerData(section, orientation, role);
}
int FontModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_fonts.size();
}
int FontModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant FontModel::data(const QModelIndex &index, int role) const
{
if (index.column() == 0) {
if (role == Qt::DisplayRole)
return m_fonts.at(index.row()).family();
} else if (index.column() == 1) {
if (role == Qt::DisplayRole)
return m_fonts.at(index.row()).styleName();
} else if (index.column() == 2) {
if (role == Qt::DecorationRole || role == Qt::SizeHintRole) {
const QFont &font = m_fonts.at(index.row());
QFontMetrics metrics(font);
const QString text = m_text.isEmpty() ? tr("<no text>") : m_text;
const QRect rect = metrics.boundingRect(text.left(100));
if (role == Qt::SizeHintRole)
return rect.size();
QPixmap pixmap(rect.size());
pixmap.fill(m_background);
QPainter painter(&pixmap);
painter.setPen(m_foreground);
painter.setFont(font);
painter.drawText(0, -rect.y(), text);
return pixmap;
}
}
return QVariant();
}
void FontModel::setPointSize(int size)
{
if (size == m_size)
return;
m_size = size;
for (int i = 0; i < m_fonts.size(); ++i)
m_fonts[i].setPointSize(size);
fontDataChanged();
}
void FontModel::toggleItalicFont(bool italic)
{
if (italic == m_italic)
return;
m_italic = italic;
for (int i = 0; i < m_fonts.size(); ++i)
m_fonts[i].setItalic(italic);
fontDataChanged();
}
void FontModel::toggleUnderlineFont(bool underline)
{
if (underline == m_underline)
return;
m_underline = underline;
for (int i = 0; i < m_fonts.size(); ++i)
m_fonts[i].setUnderline(underline);
fontDataChanged();
}
void FontModel::toggleBoldFont(bool bold)
{
if (bold == m_bold)
return;
m_bold = bold;
for (int i = 0; i < m_fonts.size(); ++i)
m_fonts[i].setBold(bold);
fontDataChanged();
}
void FontModel::setColors(const QColor &foreground, const QColor &background)
{
if (foreground == m_foreground && background == m_background)
return;
m_foreground = foreground;
m_background = background;
fontDataChanged();
}
void FontModel::fontDataChanged()
{
if (m_fonts.isEmpty())
return;
emit dataChanged(index(0, 2), index(rowCount() - 1, 2));
}
|