1 /*
2    SPDX-FileCopyrightText: 2010 Marco Mentasti <marcomentasti@gmail.com>
3 
4    SPDX-License-Identifier: LGPL-2.0-only
5 */
6 
7 #include "dataoutputmodel.h"
8 #include "outputstyle.h"
9 
10 #include <KColorScheme>
11 #include <KConfigGroup>
12 #include <KLocalizedString>
13 #include <KSharedConfig>
14 
15 #include <QFontDatabase>
16 #include <QLocale>
17 
isNumeric(const QVariant::Type type)18 inline bool isNumeric(const QVariant::Type type)
19 {
20     return (type > 1 && type < 7);
21 }
22 
DataOutputModel(QObject * parent)23 DataOutputModel::DataOutputModel(QObject *parent)
24     : CachedSqlQueryModel(parent, 1000)
25 {
26     m_useSystemLocale = false;
27 
28     m_styles.insert(QStringLiteral("text"), new OutputStyle());
29     m_styles.insert(QStringLiteral("number"), new OutputStyle());
30     m_styles.insert(QStringLiteral("null"), new OutputStyle());
31     m_styles.insert(QStringLiteral("blob"), new OutputStyle());
32     m_styles.insert(QStringLiteral("datetime"), new OutputStyle());
33     m_styles.insert(QStringLiteral("bool"), new OutputStyle());
34 
35     readConfig();
36 }
37 
~DataOutputModel()38 DataOutputModel::~DataOutputModel()
39 {
40     qDeleteAll(m_styles);
41 }
42 
clear()43 void DataOutputModel::clear()
44 {
45     beginResetModel();
46 
47     CachedSqlQueryModel::clear();
48 
49     endResetModel();
50 }
51 
readConfig()52 void DataOutputModel::readConfig()
53 {
54     KConfigGroup config(KSharedConfig::openConfig(), "KateSQLPlugin");
55 
56     KConfigGroup group = config.group("OutputCustomization");
57 
58     KColorScheme scheme(QPalette::Active, KColorScheme::View);
59 
60     const auto styleKeys = m_styles.keys();
61     for (const QString &k : styleKeys) {
62         OutputStyle *s = m_styles[k];
63 
64         KConfigGroup g = group.group(k);
65 
66         s->foreground = scheme.foreground();
67         s->background = scheme.background();
68         s->font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
69 
70         QFont dummy = g.readEntry("font", QFontDatabase::systemFont(QFontDatabase::GeneralFont));
71 
72         s->font.setBold(dummy.bold());
73         s->font.setItalic(dummy.italic());
74         s->font.setUnderline(dummy.underline());
75         s->font.setStrikeOut(dummy.strikeOut());
76         s->foreground.setColor(g.readEntry("foregroundColor", s->foreground.color()));
77         s->background.setColor(g.readEntry("backgroundColor", s->background.color()));
78     }
79 
80     Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
81 }
82 
useSystemLocale() const83 bool DataOutputModel::useSystemLocale() const
84 {
85     return m_useSystemLocale;
86 }
87 
setUseSystemLocale(bool useSystemLocale)88 void DataOutputModel::setUseSystemLocale(bool useSystemLocale)
89 {
90     m_useSystemLocale = useSystemLocale;
91 
92     Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
93 }
94 
data(const QModelIndex & index,int role) const95 QVariant DataOutputModel::data(const QModelIndex &index, int role) const
96 {
97     if (role == Qt::EditRole) {
98         return CachedSqlQueryModel::data(index, role);
99     }
100 
101     const QVariant value(CachedSqlQueryModel::data(index, Qt::DisplayRole));
102     const QVariant::Type type = value.type();
103 
104     if (value.isNull()) {
105         if (role == Qt::FontRole) {
106             return QVariant(m_styles.value(QStringLiteral("null"))->font);
107         }
108         if (role == Qt::ForegroundRole) {
109             return QVariant(m_styles.value(QStringLiteral("null"))->foreground);
110         }
111         if (role == Qt::BackgroundRole) {
112             return QVariant(m_styles.value(QStringLiteral("null"))->background);
113         }
114         if (role == Qt::DisplayRole) {
115             return QVariant(QLatin1String("NULL"));
116         }
117     }
118 
119     if (type == QVariant::ByteArray) {
120         if (role == Qt::FontRole) {
121             return QVariant(m_styles.value(QStringLiteral("blob"))->font);
122         }
123         if (role == Qt::ForegroundRole) {
124             return QVariant(m_styles.value(QStringLiteral("blob"))->foreground);
125         }
126         if (role == Qt::BackgroundRole) {
127             return QVariant(m_styles.value(QStringLiteral("blob"))->background);
128         }
129         if (role == Qt::DisplayRole) {
130             return QVariant(value.toByteArray().left(255));
131         }
132     }
133 
134     if (isNumeric(type)) {
135         if (role == Qt::FontRole) {
136             return QVariant(m_styles.value(QStringLiteral("number"))->font);
137         }
138         if (role == Qt::ForegroundRole) {
139             return QVariant(m_styles.value(QStringLiteral("number"))->foreground);
140         }
141         if (role == Qt::BackgroundRole) {
142             return QVariant(m_styles.value(QStringLiteral("number"))->background);
143         }
144         if (role == Qt::TextAlignmentRole) {
145             return QVariant(Qt::AlignRight | Qt::AlignVCenter);
146         }
147         if (role == Qt::DisplayRole || role == Qt::UserRole) {
148             if (useSystemLocale()) {
149                 return QVariant(value.toString()); // FIXME KF5 KGlobal::locale()->formatNumber(value.toString(), false));
150             } else {
151                 return QVariant(value.toString());
152             }
153         }
154     }
155 
156     if (type == QVariant::Bool) {
157         if (role == Qt::FontRole) {
158             return QVariant(m_styles.value(QStringLiteral("bool"))->font);
159         }
160         if (role == Qt::ForegroundRole) {
161             return QVariant(m_styles.value(QStringLiteral("bool"))->foreground);
162         }
163         if (role == Qt::BackgroundRole) {
164             return QVariant(m_styles.value(QStringLiteral("bool"))->background);
165         }
166         if (role == Qt::DisplayRole) {
167             return QVariant(value.toBool() ? QLatin1String("True") : QLatin1String("False"));
168         }
169     }
170 
171     if (type == QVariant::Date || type == QVariant::Time || type == QVariant::DateTime) {
172         if (role == Qt::FontRole) {
173             return QVariant(m_styles.value(QStringLiteral("datetime"))->font);
174         }
175         if (role == Qt::ForegroundRole) {
176             return QVariant(m_styles.value(QStringLiteral("datetime"))->foreground);
177         }
178         if (role == Qt::BackgroundRole) {
179             return QVariant(m_styles.value(QStringLiteral("datetime"))->background);
180         }
181         if (role == Qt::DisplayRole || role == Qt::UserRole) {
182             if (useSystemLocale()) {
183                 if (type == QVariant::Date) {
184                     return QVariant(QLocale().toString(value.toDate(), QLocale::ShortFormat));
185                 }
186                 if (type == QVariant::Time) {
187                     return QVariant(QLocale().toString(value.toTime()));
188                 }
189                 if (type == QVariant::DateTime) {
190                     return QVariant(QLocale().toString(value.toDateTime(), QLocale::ShortFormat));
191                 }
192             } else { // return sql server format
193                 return QVariant(value.toString());
194             }
195         }
196     }
197 
198     if (role == Qt::FontRole) {
199         return QVariant(m_styles.value(QStringLiteral("text"))->font);
200     }
201     if (role == Qt::ForegroundRole) {
202         return QVariant(m_styles.value(QStringLiteral("text"))->foreground);
203     }
204     if (role == Qt::BackgroundRole) {
205         return QVariant(m_styles.value(QStringLiteral("text"))->background);
206     }
207     if (role == Qt::TextAlignmentRole) {
208         return QVariant(Qt::AlignVCenter);
209     }
210     if (role == Qt::DisplayRole) {
211         return value.toString();
212     }
213     if (role == Qt::UserRole) {
214         return value;
215     }
216 
217     return CachedSqlQueryModel::data(index, role);
218 }
219