File indexing completed on 2024-05-05 05:37:10
0001 /* 0002 * SPDX-FileCopyrightText: 2008 Aaron Seigo <aseigo@kde.org> 0003 * SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org> 0004 * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "modelviewer.h" 0009 0010 #include <KLocalizedString> 0011 #include <KMessageBox> 0012 #include <KStringHandler> 0013 #include <QDebug> 0014 #include <QDialogButtonBox> 0015 0016 #include <Plasma5Support/DataEngine> 0017 #include <Plasma5Support/Service> 0018 #include <Plasma5Support/ServiceJob> 0019 0020 #include "engineexplorer.h" 0021 0022 Delegate::Delegate(QObject *parent) 0023 : QAbstractItemDelegate(parent) 0024 { 0025 } 0026 0027 Delegate::~Delegate() 0028 { 0029 } 0030 0031 void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0032 { 0033 if (!index.model()) { 0034 return; 0035 } 0036 0037 const QHash<int, QByteArray> roleNames = index.model()->roleNames(); 0038 const QList<int> roles = roleNames.keys(); 0039 0040 QFontMetrics fm(option.font); 0041 int maxWidth = 0; 0042 for (int role : roles) { 0043 const QString text = roleNames.value(role) + QLatin1String(": "); 0044 maxWidth = qMax(maxWidth, fm.boundingRect(text).width()); 0045 } 0046 0047 int i = 2; 0048 for (int role : roles) { 0049 const QString text = roleNames.value(role) + QLatin1String(": "); 0050 painter->drawText(option.rect.x() + maxWidth - fm.boundingRect(text).width(), option.rect.y() + i * fm.height(), text); 0051 0052 if (index.data(role).canConvert<QIcon>()) { 0053 index.data(role).value<QIcon>().paint(painter, option.rect.x() + maxWidth, option.rect.y() + (i - 1) * fm.height(), 16, 16); 0054 } else if (!index.data(role).isValid()) { 0055 painter->drawText(option.rect.x() + maxWidth, option.rect.y() + i * fm.height(), "null"); 0056 } else { 0057 painter->drawText(option.rect.x() + maxWidth, option.rect.y() + i * fm.height(), index.data(role).toString()); 0058 } 0059 ++i; 0060 } 0061 } 0062 0063 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0064 { 0065 if (!index.model()) { 0066 return QSize(); 0067 } 0068 0069 QFontMetrics fm(option.font); 0070 return QSize(fm.boundingRect("M").width() * 50, fm.height() * (index.model()->roleNames().count() + 2)); 0071 } 0072 0073 ModelViewer::ModelViewer(Plasma5Support::DataEngine *engine, const QString &source, QWidget *parent) 0074 : QDialog(parent) 0075 , m_engine(engine) 0076 , m_source(source) 0077 { 0078 setAttribute(Qt::WA_DeleteOnClose); 0079 m_view = new QTreeView(this); 0080 m_view->setItemDelegate(new Delegate(m_view)); 0081 QVBoxLayout *layout = new QVBoxLayout(); 0082 layout->addWidget(m_view); 0083 setLayout(layout); 0084 0085 QString engineName = i18nc("Plasma engine with unknown name", "Unknown"); 0086 0087 if (m_engine) { 0088 if (m_engine->metadata().isValid()) { 0089 engineName = KStringHandler::capwords(m_engine->metadata().name()); 0090 } 0091 qDebug() << "Requesting model for source:" << m_source; 0092 m_model = m_engine->modelForSource(m_source); 0093 0094 if (m_model != nullptr) { 0095 connect(m_engine, &QObject::destroyed, this, &ModelViewer::engineDestroyed); 0096 m_view->setModel(m_model); 0097 } else { 0098 KMessageBox::error(this, i18n("No valid model was returned. Verify that a model is available for this source.")); 0099 close(); 0100 } 0101 } 0102 0103 setWindowTitle(i18nc("%1 is a Plasma dataengine name", "%1 Model Explorer", engineName)); 0104 } 0105 0106 ModelViewer::~ModelViewer() 0107 { 0108 m_engine = nullptr; 0109 } 0110 0111 void ModelViewer::engineDestroyed() 0112 { 0113 m_model = nullptr; 0114 m_engine = nullptr; 0115 hide(); 0116 deleteLater(); 0117 } 0118 0119 #include "moc_modelviewer.cpp"