File indexing completed on 2024-05-05 10:11:55
0001 /* 0002 SPDX-FileCopyrightText: 2010 Marco Mentasti <marcomentasti@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "dataoutputmodel.h" 0008 #include "outputstyle.h" 0009 0010 #include <KColorScheme> 0011 #include <KConfigGroup> 0012 #include <KLocalizedString> 0013 #include <KSharedConfig> 0014 0015 #include <QFontDatabase> 0016 #include <QLocale> 0017 0018 inline bool isNumeric(const QVariant::Type type) 0019 { 0020 return (type > 1 && type < 7); 0021 } 0022 0023 DataOutputModel::DataOutputModel(QObject *parent) 0024 : CachedSqlQueryModel(parent, 1000) 0025 { 0026 m_useSystemLocale = false; 0027 0028 m_styles.insert(QStringLiteral("text"), new OutputStyle()); 0029 m_styles.insert(QStringLiteral("number"), new OutputStyle()); 0030 m_styles.insert(QStringLiteral("null"), new OutputStyle()); 0031 m_styles.insert(QStringLiteral("blob"), new OutputStyle()); 0032 m_styles.insert(QStringLiteral("datetime"), new OutputStyle()); 0033 m_styles.insert(QStringLiteral("bool"), new OutputStyle()); 0034 0035 readConfig(); 0036 } 0037 0038 DataOutputModel::~DataOutputModel() 0039 { 0040 qDeleteAll(m_styles); 0041 } 0042 0043 void DataOutputModel::clear() 0044 { 0045 beginResetModel(); 0046 0047 CachedSqlQueryModel::clear(); 0048 0049 endResetModel(); 0050 } 0051 0052 void DataOutputModel::readConfig() 0053 { 0054 KConfigGroup config(KSharedConfig::openConfig(), QStringLiteral("KateSQLPlugin")); 0055 0056 KConfigGroup group = config.group("OutputCustomization"); 0057 0058 KColorScheme scheme(QPalette::Active, KColorScheme::View); 0059 0060 const auto styleKeys = m_styles.keys(); 0061 for (const QString &k : styleKeys) { 0062 OutputStyle *s = m_styles[k]; 0063 0064 KConfigGroup g = group.group(k); 0065 0066 s->foreground = scheme.foreground(); 0067 s->background = scheme.background(); 0068 s->font = QFontDatabase::systemFont(QFontDatabase::GeneralFont); 0069 0070 QFont dummy = g.readEntry("font", QFontDatabase::systemFont(QFontDatabase::GeneralFont)); 0071 0072 s->font.setBold(dummy.bold()); 0073 s->font.setItalic(dummy.italic()); 0074 s->font.setUnderline(dummy.underline()); 0075 s->font.setStrikeOut(dummy.strikeOut()); 0076 s->foreground.setColor(g.readEntry("foregroundColor", s->foreground.color())); 0077 s->background.setColor(g.readEntry("backgroundColor", s->background.color())); 0078 } 0079 0080 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); 0081 } 0082 0083 bool DataOutputModel::useSystemLocale() const 0084 { 0085 return m_useSystemLocale; 0086 } 0087 0088 void DataOutputModel::setUseSystemLocale(bool useSystemLocale) 0089 { 0090 m_useSystemLocale = useSystemLocale; 0091 0092 Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1)); 0093 } 0094 0095 QVariant DataOutputModel::data(const QModelIndex &index, int role) const 0096 { 0097 if (role == Qt::EditRole) { 0098 return CachedSqlQueryModel::data(index, role); 0099 } 0100 0101 const QVariant value(CachedSqlQueryModel::data(index, Qt::DisplayRole)); 0102 const QVariant::Type type = value.type(); 0103 0104 if (value.isNull()) { 0105 if (role == Qt::FontRole) { 0106 return QVariant(m_styles.value(QStringLiteral("null"))->font); 0107 } 0108 if (role == Qt::ForegroundRole) { 0109 return QVariant(m_styles.value(QStringLiteral("null"))->foreground); 0110 } 0111 if (role == Qt::BackgroundRole) { 0112 return QVariant(m_styles.value(QStringLiteral("null"))->background); 0113 } 0114 if (role == Qt::DisplayRole) { 0115 return QVariant(QLatin1String("NULL")); 0116 } 0117 } 0118 0119 if (type == QVariant::ByteArray) { 0120 if (role == Qt::FontRole) { 0121 return QVariant(m_styles.value(QStringLiteral("blob"))->font); 0122 } 0123 if (role == Qt::ForegroundRole) { 0124 return QVariant(m_styles.value(QStringLiteral("blob"))->foreground); 0125 } 0126 if (role == Qt::BackgroundRole) { 0127 return QVariant(m_styles.value(QStringLiteral("blob"))->background); 0128 } 0129 if (role == Qt::DisplayRole) { 0130 return QVariant(value.toByteArray().left(255)); 0131 } 0132 } 0133 0134 if (isNumeric(type)) { 0135 if (role == Qt::FontRole) { 0136 return QVariant(m_styles.value(QStringLiteral("number"))->font); 0137 } 0138 if (role == Qt::ForegroundRole) { 0139 return QVariant(m_styles.value(QStringLiteral("number"))->foreground); 0140 } 0141 if (role == Qt::BackgroundRole) { 0142 return QVariant(m_styles.value(QStringLiteral("number"))->background); 0143 } 0144 if (role == Qt::TextAlignmentRole) { 0145 return QVariant(Qt::AlignRight | Qt::AlignVCenter); 0146 } 0147 if (role == Qt::DisplayRole || role == Qt::UserRole) { 0148 if (useSystemLocale()) { 0149 return QVariant(value.toString()); // FIXME KF5 KGlobal::locale()->formatNumber(value.toString(), false)); 0150 } else { 0151 return QVariant(value.toString()); 0152 } 0153 } 0154 } 0155 0156 if (type == QVariant::Bool) { 0157 if (role == Qt::FontRole) { 0158 return QVariant(m_styles.value(QStringLiteral("bool"))->font); 0159 } 0160 if (role == Qt::ForegroundRole) { 0161 return QVariant(m_styles.value(QStringLiteral("bool"))->foreground); 0162 } 0163 if (role == Qt::BackgroundRole) { 0164 return QVariant(m_styles.value(QStringLiteral("bool"))->background); 0165 } 0166 if (role == Qt::DisplayRole) { 0167 return QVariant(value.toBool() ? QLatin1String("True") : QLatin1String("False")); 0168 } 0169 } 0170 0171 if (type == QVariant::Date || type == QVariant::Time || type == QVariant::DateTime) { 0172 if (role == Qt::FontRole) { 0173 return QVariant(m_styles.value(QStringLiteral("datetime"))->font); 0174 } 0175 if (role == Qt::ForegroundRole) { 0176 return QVariant(m_styles.value(QStringLiteral("datetime"))->foreground); 0177 } 0178 if (role == Qt::BackgroundRole) { 0179 return QVariant(m_styles.value(QStringLiteral("datetime"))->background); 0180 } 0181 if (role == Qt::DisplayRole || role == Qt::UserRole) { 0182 if (useSystemLocale()) { 0183 if (type == QVariant::Date) { 0184 return QVariant(QLocale().toString(value.toDate(), QLocale::ShortFormat)); 0185 } 0186 if (type == QVariant::Time) { 0187 return QVariant(QLocale().toString(value.toTime())); 0188 } 0189 if (type == QVariant::DateTime) { 0190 return QVariant(QLocale().toString(value.toDateTime(), QLocale::ShortFormat)); 0191 } 0192 } else { // return sql server format 0193 return QVariant(value.toString()); 0194 } 0195 } 0196 } 0197 0198 if (role == Qt::FontRole) { 0199 return QVariant(m_styles.value(QStringLiteral("text"))->font); 0200 } 0201 if (role == Qt::ForegroundRole) { 0202 return QVariant(m_styles.value(QStringLiteral("text"))->foreground); 0203 } 0204 if (role == Qt::BackgroundRole) { 0205 return QVariant(m_styles.value(QStringLiteral("text"))->background); 0206 } 0207 if (role == Qt::TextAlignmentRole) { 0208 return QVariant(Qt::AlignVCenter); 0209 } 0210 if (role == Qt::DisplayRole) { 0211 return value.toString(); 0212 } 0213 if (role == Qt::UserRole) { 0214 return value; 0215 } 0216 0217 return CachedSqlQueryModel::data(index, role); 0218 } 0219 0220 #include "moc_dataoutputmodel.cpp"