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
|
/*
* SPDX-FileCopyrightText: 2008 Aaron Seigo <aseigo@kde.org>
* SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "modelviewer.h"
#include <KLocalizedString>
#include <KMessageBox>
#include <KStringHandler>
#include <QDebug>
#include <QDialogButtonBox>
#include <Plasma5Support/DataEngine>
#include <Plasma5Support/Service>
#include <Plasma5Support/ServiceJob>
#include "engineexplorer.h"
Delegate::Delegate(QObject *parent)
: QAbstractItemDelegate(parent)
{
}
Delegate::~Delegate()
{
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.model()) {
return;
}
const QHash<int, QByteArray> roleNames = index.model()->roleNames();
const QList<int> roles = roleNames.keys();
QFontMetrics fm(option.font);
int maxWidth = 0;
for (int role : roles) {
const QString text = roleNames.value(role) + QLatin1String(": ");
maxWidth = qMax(maxWidth, fm.boundingRect(text).width());
}
int i = 2;
for (int role : roles) {
const QString text = roleNames.value(role) + QLatin1String(": ");
painter->drawText(option.rect.x() + maxWidth - fm.boundingRect(text).width(), option.rect.y() + i * fm.height(), text);
if (index.data(role).canConvert<QIcon>()) {
index.data(role).value<QIcon>().paint(painter, option.rect.x() + maxWidth, option.rect.y() + (i - 1) * fm.height(), 16, 16);
} else if (!index.data(role).isValid()) {
painter->drawText(option.rect.x() + maxWidth, option.rect.y() + i * fm.height(), "null");
} else {
painter->drawText(option.rect.x() + maxWidth, option.rect.y() + i * fm.height(), index.data(role).toString());
}
++i;
}
}
QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.model()) {
return QSize();
}
QFontMetrics fm(option.font);
return QSize(fm.boundingRect("M").width() * 50, fm.height() * (index.model()->roleNames().count() + 2));
}
ModelViewer::ModelViewer(Plasma5Support::DataEngine *engine, const QString &source, QWidget *parent)
: QDialog(parent)
, m_engine(engine)
, m_source(source)
{
setAttribute(Qt::WA_DeleteOnClose);
m_view = new QTreeView(this);
m_view->setItemDelegate(new Delegate(m_view));
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(m_view);
setLayout(layout);
QString engineName = i18nc("Plasma engine with unknown name", "Unknown");
if (m_engine) {
if (m_engine->metadata().isValid()) {
engineName = KStringHandler::capwords(m_engine->metadata().name());
}
qDebug() << "Requesting model for source:" << m_source;
m_model = m_engine->modelForSource(m_source);
if (m_model != nullptr) {
connect(m_engine, &QObject::destroyed, this, &ModelViewer::engineDestroyed);
m_view->setModel(m_model);
} else {
KMessageBox::error(this, i18n("No valid model was returned. Verify that a model is available for this source."));
close();
}
}
setWindowTitle(i18nc("%1 is a Plasma dataengine name", "%1 Model Explorer", engineName));
}
ModelViewer::~ModelViewer()
{
m_engine = nullptr;
}
void ModelViewer::engineDestroyed()
{
m_model = nullptr;
m_engine = nullptr;
hide();
deleteLater();
}
#include "moc_modelviewer.cpp"
|