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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
/*
varianthandler.cpp
This file is part of GammaRay, the Qt application inspection and
manipulation tool.
Copyright (C) 2013-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Volker Krause <volker.krause@kdab.com>
Licensees holding valid commercial KDAB GammaRay licenses may use this file in
accordance with GammaRay Commercial License Agreement provided with the Software.
Contact info@kdab.com if any conditions of this licensing are not clear to you.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "varianthandler.h"
#include "util.h"
#include "enumutil.h"
#include "enumrepositoryserver.h"
#include <common/metatypedeclarations.h>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QApplication>
#else
#include <QGuiApplication>
#endif
#include <QCursor>
#include <QDebug>
#include <QDir>
#include <QEasingCurve>
#include <QIcon>
#include <QMatrix4x4>
#include <QMetaEnum>
#include <QMetaObject>
#include <QObject>
#include <QPainter>
#include <QPalette>
#include <QPoint>
#include <QRect>
#include <QSize>
#include <QStringList>
#include <QVector2D>
#include <QVector3D>
#include <QVector4D>
using namespace GammaRay;
namespace GammaRay {
class VariantHandlerRepository
{
public:
VariantHandlerRepository() {}
~VariantHandlerRepository();
void clear();
QHash<int, VariantHandler::Converter<QString> *> stringConverters;
QVector<VariantHandler::GenericStringConverter> genericStringConverters;
private:
Q_DISABLE_COPY(VariantHandlerRepository)
};
VariantHandlerRepository::~VariantHandlerRepository()
{
qDeleteAll(stringConverters);
}
void VariantHandlerRepository::clear()
{
qDeleteAll(stringConverters);
stringConverters.clear();
genericStringConverters.clear();
}
static QString displayMatrix4x4(const QMatrix4x4 &matrix)
{
QStringList rows;
rows.reserve(4);
for (int i = 0; i < 4; ++i) {
QStringList cols;
cols.reserve(4);
for (int j = 0; j < 4; ++j) {
cols.push_back(QString::number(matrix(i, j)));
}
rows.push_back(cols.join(QStringLiteral(" ")));
}
return '[' + rows.join(QStringLiteral(", ")) + ']';
}
static QString displayMatrix4x4(const QMatrix4x4 *matrix)
{
if (matrix) {
return displayMatrix4x4(*matrix);
}
return QStringLiteral("<null>");
}
template<int Dim, typename T>
static QString displayVector(const T &vector)
{
QStringList v;
for (int i = 0; i < Dim; ++i) {
v.push_back(QString::number(vector[i]));
}
return '[' + v.join(QStringLiteral(", ")) + ']';
}
}
Q_GLOBAL_STATIC(VariantHandlerRepository, s_variantHandlerRepository)
QString VariantHandler::displayString(const QVariant &value)
{
switch (value.type()) {
#ifndef QT_NO_CURSOR
case QVariant::Cursor:
{
const QCursor cursor = value.value<QCursor>();
return EnumUtil::enumToString(QVariant::fromValue<int>(cursor.shape()), "Qt::CursorShape");
}
#endif
case QVariant::Icon:
{
const QIcon icon = value.value<QIcon>();
if (icon.isNull()) {
return qApp->translate("GammaRay::VariantHandler", "<no icon>");
}
const auto sizes = icon.availableSizes();
QStringList l;
l.reserve(sizes.size());
foreach (QSize size, sizes) {
l.push_back(displayString(size));
}
return l.join(QStringLiteral(", "));
}
case QVariant::Line:
{
const auto line = value.toLine();
return
QStringLiteral("%1, %2 → %3, %4").
arg(line.x1()).arg(line.y1()).
arg(line.x2()).arg(line.y2());
}
case QVariant::LineF:
{
const auto line = value.toLineF();
return
QStringLiteral("%1, %2 → %3, %4").
arg(line.x1()).arg(line.y1()).
arg(line.x2()).arg(line.y2());
}
case QVariant::Locale:
return value.toLocale().name();
case QVariant::Point:
{
const auto point = value.toPoint();
return
QStringLiteral("%1, %2").
arg(point.x()).
arg(point.y());
}
case QVariant::PointF:
{
const auto point = value.toPointF();
return
QStringLiteral("%1, %2").
arg(point.x()).
arg(point.y());
}
case QVariant::Rect:
{
const auto rect = value.toRect();
return
QStringLiteral("%1, %2 %3 x %4").
arg(rect.x()).
arg(rect.y()).
arg(rect.width()).
arg(rect.height());
}
case QVariant::RectF:
{
const auto rect = value.toRectF();
return
QStringLiteral("%1, %2 %3 x %4").
arg(rect.x()).
arg(rect.y()).
arg(rect.width()).
arg(rect.height());
}
case QVariant::Palette:
{
const QPalette pal = value.value<QPalette>();
if (pal == qApp->palette()) {
return QStringLiteral("<inherited>");
}
return QStringLiteral("<custom>");
}
case QVariant::Size:
{
const auto size = value.toSize();
return
QStringLiteral("%1 x %2").
arg(size.width()).
arg(size.height());
}
case QVariant::SizeF:
{
const auto size = value.toSizeF();
return
QStringLiteral("%1 x %2").
arg(size.width()).
arg(size.height());
}
case QVariant::StringList:
{
const auto l = value.toStringList();
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
if (l.isEmpty()) {
return QStringLiteral("<empty>");
}
if (l.size() == 1) {
return l.at(0);
}
return QStringLiteral("<%1 entries>").arg(l.size());
#else
return l.join(", ");
#endif
}
case QVariant::Transform:
{
const QTransform t = value.value<QTransform>();
return
QStringLiteral("[%1 %2 %3, %4 %5 %6, %7 %8 %9]").
arg(t.m11()).arg(t.m12()).arg(t.m13()).
arg(t.m21()).arg(t.m22()).arg(t.m23()).
arg(t.m31()).arg(t.m32()).arg(t.m33());
}
default:
break;
}
// types with dynamic type ids
if (value.userType() == qMetaTypeId<uchar>()) {
const auto v = value.value<uchar>();
return QString::number(v) + QLatin1String(" '") + QChar(v) + QLatin1Char('\'');
}
if (value.userType() == qMetaTypeId<QMargins>()) {
const QMargins margins = value.value<QMargins>();
return qApp->translate("GammaRay::VariantHandler", "left: %1, top: %2, right: %3, bottom: %4")
.arg(margins.left()).arg(margins.top())
.arg(margins.right()).arg(margins.bottom());
}
if (value.userType() == qMetaTypeId<const QMetaObject *>()) {
const auto mo = value.value<const QMetaObject *>();
if (!mo) {
return QStringLiteral("0x0");
}
return mo->className();
}
if (value.userType() == qMetaTypeId<QMatrix4x4>())
return displayMatrix4x4(value.value<QMatrix4x4>());
if (value.userType() == qMetaTypeId<const QMatrix4x4 *>())
return displayMatrix4x4(value.value<const QMatrix4x4 *>());
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
if (value.userType() == qMetaTypeId<QVector2D>()) {
return displayVector<2>(value.value<QVector2D>());
}
if (value.userType() == qMetaTypeId<QVector3D>()) {
return displayVector<3>(value.value<QVector3D>());
}
if (value.userType() == qMetaTypeId<QVector4D>()) {
return displayVector<4>(value.value<QVector4D>());
}
if (value.userType() == qMetaTypeId<QTimeZone>()) {
return value.value<QTimeZone>().id();
}
#endif
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
if (value.userType() == qMetaTypeId<QSet<QByteArray> >()) {
const QSet<QByteArray> set = value.value<QSet<QByteArray> >();
QStringList l;
l.reserve(set.size());
foreach (const QByteArray &b, set) {
l.push_back(QString::fromUtf8(b));
}
return l.join(QStringLiteral(", "));
}
#endif // Qt5
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
if (value.userType() == qMetaTypeId<QEasingCurve>()) {
const auto ec = value.toEasingCurve();
return EnumUtil::enumToString(QVariant::fromValue<QEasingCurve::Type>(ec.type()));
}
#endif
// enums
const QString enumStr = EnumUtil::enumToString(value);
if (!enumStr.isEmpty()) {
return enumStr;
}
// custom converters
auto it = s_variantHandlerRepository()->stringConverters.constFind(value.userType());
if (it != s_variantHandlerRepository()->stringConverters.constEnd()) {
return (*it.value())(value);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
if (value.canConvert<QVariantList>()) {
QSequentialIterable it = value.value<QSequentialIterable>();
if (it.size() == 0) {
return QStringLiteral("<empty>");
} else {
return QStringLiteral("<%1 entries>").arg(it.size());
}
}
if (value.canConvert<QVariantHash>()) {
auto it = value.value<QAssociativeIterable>();
if (it.size() == 0) {
return QStringLiteral("<empty>");
} else {
return QStringLiteral("<%1 entries>").arg(it.size());
}
}
#endif
// generic converters
QVector<VariantHandler::GenericStringConverter> genStrConverters
= s_variantHandlerRepository()->genericStringConverters;
foreach (auto converter, genStrConverters) {
bool ok = false;
const QString s = converter(value, &ok);
if (ok)
return s;
}
// catch-all QObject handler
// search the entire hierarchy for custom converters, so we can override this
// for entire sub-trees
if (value.canConvert<QObject*>()) {
const auto obj = value.value<QObject*>();
if (!obj || obj->metaObject() == &QObject::staticMetaObject)
return Util::displayString(obj);
auto mo = value.value<QObject*>()->metaObject();
while (mo) {
auto type = QMetaType::type(QByteArray(mo->className()) + '*');
if (type > 0) {
auto it = s_variantHandlerRepository()->stringConverters.constFind(type);
if (it != s_variantHandlerRepository()->stringConverters.constEnd())
return (*it.value())(value);
}
mo = mo->superClass();
}
return Util::displayString(obj);
}
return value.toString();
}
QVariant VariantHandler::decoration(const QVariant &value)
{
switch (value.type()) {
case QVariant::Brush:
{
const QBrush b = value.value<QBrush>();
if (b.style() != Qt::NoBrush) {
QPixmap p(16, 16);
p.fill(QColor(0, 0, 0, 0));
QPainter painter(&p);
painter.setBrush(b);
painter.drawRect(0, 0, p.width() - 1, p.height() - 1);
return p;
}
break;
}
case QVariant::Color:
{
const QColor c = value.value<QColor>();
if (c.isValid()) {
QPixmap p(16, 16);
QPainter painter(&p);
Util::drawTransparencyPattern(&painter, p.rect(), 4);
painter.setBrush(QBrush(c));
painter.drawRect(0, 0, p.width() - 1, p.height() - 1);
return p;
}
break;
}
#ifndef QT_NO_CURSOR
case QVariant::Cursor:
{
const QCursor c = value.value<QCursor>();
if (!c.pixmap().isNull()) {
return c.pixmap().scaled(16, 16, Qt::KeepAspectRatio, Qt::FastTransformation);
}
break;
}
#endif
case QVariant::Icon:
return value;
case QVariant::Pen:
{
const QPen pen = value.value<QPen>();
if (pen.style() != Qt::NoPen) {
QPixmap p(16, 16);
QPainter painter(&p);
Util::drawTransparencyPattern(&painter, p.rect(), 4);
painter.save();
painter.setPen(pen);
painter.translate(0, 8 - pen.width() / 2);
painter.drawLine(0, 0, p.width(), 0);
painter.restore();
painter.drawRect(0, 0, p.width() - 1, p.height() - 1);
return p;
}
break;
}
case QVariant::Pixmap:
{
const QPixmap p = value.value<QPixmap>();
if (p.isNull()) {
break;
}
QPixmap deco(16, 16);
QPainter painter(&deco);
Util::drawTransparencyPattern(&painter, deco.rect(), 4);
QPixmap scaled(p);
if (p.width() > deco.width() || p.height() > deco.height()) {
scaled = p.scaled(deco.width(), deco.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
painter.drawPixmap((deco.width() - scaled.width()) / 2, (deco.height() - scaled.height()) / 2, scaled);
painter.drawRect(0, 0, deco.width() - 1, deco.height() - 1);
return deco;
}
default:
break;
}
return QVariant();
}
void VariantHandler::registerStringConverter(int type, Converter<QString> *converter)
{
Q_ASSERT(!s_variantHandlerRepository()->stringConverters.contains(type));
s_variantHandlerRepository()->stringConverters.insert(type, converter);
}
void VariantHandler::registerGenericStringConverter(
VariantHandler::GenericStringConverter converter)
{
s_variantHandlerRepository()->genericStringConverters.push_back(converter);
}
QVariant VariantHandler::serializableVariant(const QVariant &value)
{
if (value.userType() == qMetaTypeId<const QMatrix4x4 *>()) {
const QMatrix4x4 *m = value.value<const QMatrix4x4 *>();
if (!m) {
return QVariant();
}
return QVariant::fromValue(QMatrix4x4(*m));
}
if (EnumRepositoryServer::isEnum(value.userType())) {
return QVariant::fromValue(EnumRepositoryServer::valueFromVariant(value));
}
return value;
}
void VariantHandler::clear()
{
s_variantHandlerRepository()->clear();
}
|