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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*
* SPDX-FileCopyrightText: 2007 Aaron Seigo <aseigo@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "engineexplorer.h"
#include <QApplication>
#include <QBitArray>
#include <QBitmap>
#include <QDialogButtonBox>
#include <QMenu>
#include <QStandardItemModel>
#include <QUrl>
#include <KLocalizedString>
#include <KPluginMetaData>
#include <KStandardActions>
#include <KStringHandler>
#include <QAction>
#include <QDateTime>
#include <Plasma5Support/PluginLoader>
Q_DECLARE_METATYPE(Plasma5Support::DataEngine::Data)
#include "modelviewer.h"
#include "serviceviewer.h"
#include "titlecombobox.h"
enum {
ColumnDataSourceAndKey = 0,
ColumnType = 1,
ColumnValue = 2,
ColumnCount,
};
EngineExplorer::EngineExplorer(QWidget *parent)
: QDialog(parent)
, m_engine(nullptr)
, m_sourceCount(0)
, m_requestingSource(false)
, m_expandButton(new QPushButton(i18n("Expand All"), this))
, m_collapseButton(new QPushButton(i18n("Collapse All"), this))
{
setWindowTitle(i18n("Plasma Engine Explorer"));
QWidget *mainWidget = new QWidget(this);
QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
buttonBox->addButton(m_expandButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(m_collapseButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(QDialogButtonBox::Close);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(mainWidget);
layout->addWidget(buttonBox);
setLayout(layout);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
setupUi(mainWidget);
m_dataModel = new QStandardItemModel(this);
const int size = m_title->style()->pixelMetric(QStyle::PM_LargeIconSize);
m_title->setIconSize(QSize(size, size));
m_title->setIcon(QIcon::fromTheme("plasma"));
connect(m_engines, &QComboBox::textActivated, this, &EngineExplorer::showEngine);
connect(m_sourceRequesterButton, SIGNAL(clicked(bool)), this, SLOT(requestSource()));
connect(m_serviceRequesterButton, &QAbstractButton::clicked, this, &EngineExplorer::requestServiceForSource);
m_data->setModel(m_dataModel);
m_data->setWordWrap(true);
m_searchLine->setTreeView(m_data);
m_searchLine->setPlaceholderText(i18n("Search"));
listEngines();
m_engines->setFocus();
connect(m_collapseButton, &QAbstractButton::clicked, m_data, &QTreeView::collapseAll);
connect(m_expandButton, &QAbstractButton::clicked, m_data, &QTreeView::expandAll);
enableButtons(false);
addAction(KStandardActions::quit(qApp, &QApplication::quit, this));
connect(m_data, &QWidget::customContextMenuRequested, this, &EngineExplorer::showDataContextMenu);
m_data->setContextMenuPolicy(Qt::CustomContextMenu);
connect(qApp, &QCoreApplication::aboutToQuit, this, &EngineExplorer::cleanUp);
}
EngineExplorer::~EngineExplorer()
{
}
void EngineExplorer::cleanUp()
{
}
void EngineExplorer::setApp(const QString &app)
{
m_app = app;
if (m_engines->count() > 0) {
listEngines();
}
}
void EngineExplorer::setEngine(const QString &engine)
{
// find the engine in the combo box
const int index = m_engines->findText(engine);
if (index != -1) {
qDebug() << QString("Engine %1 found!").arg(engine);
m_engines->setCurrentIndex(index);
showEngine(engine);
}
}
void EngineExplorer::setInterval(const int interval)
{
m_updateInterval->setValue(interval);
}
void EngineExplorer::removeExtraRows(QStandardItem *parent, int preserve)
{
if (parent->rowCount() > preserve) {
parent->removeRows(preserve, parent->rowCount() - preserve);
}
}
void EngineExplorer::dataUpdated(const QString &source, const Plasma5Support::DataEngine::Data &data)
{
QList<QStandardItem *> items = m_dataModel->findItems(source, Qt::MatchExactly);
if (items.isEmpty()) {
return;
}
QStandardItem *parent = items.first();
int rows = showData(parent, data);
removeExtraRows(parent, rows);
}
void EngineExplorer::listEngines()
{
m_engines->clear();
QVector<KPluginMetaData> engines = Plasma5Support::PluginLoader::listDataEngineMetaData(m_app);
std::sort(engines.begin(), engines.end(), [](auto lhs, auto rhs) {
if (lhs.category() < rhs.category()) {
return true;
}
if (lhs.category() == rhs.category()) {
return lhs.name() < rhs.name();
}
return false;
});
for (const KPluginMetaData &engine : std::as_const(engines)) {
m_engines->addItem(QIcon::fromTheme(engine.iconName()), engine.pluginId());
}
m_engines->setCurrentIndex(-1);
}
void EngineExplorer::showEngine(const QString &name)
{
m_sourceRequester->setEnabled(false);
m_sourceRequesterButton->setEnabled(false);
m_serviceRequester->setEnabled(false);
m_serviceRequesterButton->setEnabled(false);
enableButtons(false);
m_dataModel->clear();
m_dataModel->setColumnCount(ColumnCount);
QStringList headers{i18n("DataSource/Key"), i18n("Type"), i18n("Value")};
m_dataModel->setHorizontalHeaderLabels(headers);
m_data->setColumnWidth(ColumnDataSourceAndKey, 200);
m_engine = nullptr;
m_sourceCount = 0;
m_engineName = name;
if (m_engineName.isEmpty()) {
updateTitle();
return;
}
if (auto res = KPluginFactory::instantiatePlugin<Plasma5Support::DataEngine>(
KPluginMetaData::findPluginById(QStringLiteral("plasma5support/dataengine"), m_engineName))) {
m_engine = res.plugin;
} else {
m_engineName.clear();
updateTitle();
return;
}
// qDebug() << "showing engine " << m_engine->objectName();
// qDebug() << "we have " << sources.count() << " data sources";
connect(m_engine, &Plasma5Support::DataEngine::sourceAdded, this, &EngineExplorer::addSource);
connect(m_engine, &Plasma5Support::DataEngine::sourceRemoved, this, &EngineExplorer::removeSource);
const QStringList &sources = m_engine->sources();
for (const QString &source : sources) {
// qDebug() << "adding " << source;
addSource(source);
}
m_sourceRequesterButton->setEnabled(true);
m_updateInterval->setEnabled(true);
m_sourceRequester->setEnabled(true);
m_sourceRequester->setFocus();
m_serviceRequester->setEnabled(true);
m_serviceRequesterButton->setEnabled(true);
updateTitle();
}
void EngineExplorer::addSource(const QString &source)
{
// qDebug() << "adding" << source;
QList<QStandardItem *> items = m_dataModel->findItems(source, Qt::MatchExactly);
if (!items.isEmpty()) {
// qDebug() << "er... already there?";
return;
}
QStandardItem *parent = new QStandardItem(source);
parent->setToolTip(source);
m_dataModel->appendRow(parent);
// qDebug() << "getting data for source " << source;
if (!m_requestingSource || m_sourceRequester->text() != source) {
// qDebug() << "connecting up now";
m_engine->connectSource(source, this);
}
++m_sourceCount;
updateTitle();
enableButtons(true);
}
void EngineExplorer::removeSource(const QString &source)
{
const QList<QStandardItem *> items = m_dataModel->findItems(source, Qt::MatchExactly);
if (items.count() < 1) {
return;
}
for (QStandardItem *item : items) {
m_dataModel->removeRow(item->row());
}
--m_sourceCount;
m_engine->disconnectSource(source, this);
updateTitle();
}
void EngineExplorer::requestSource()
{
requestSource(m_sourceRequester->text());
}
void EngineExplorer::requestServiceForSource()
{
ServiceViewer *viewer = new ServiceViewer(m_engine, m_serviceRequester->text());
viewer->show();
}
void EngineExplorer::requestSource(const QString &source)
{
if (!m_engine || source.isEmpty()) {
return;
}
qDebug() << "request source" << source;
m_requestingSource = true;
m_engine->connectSource(source, this, (uint)m_updateInterval->value());
m_requestingSource = false;
}
void EngineExplorer::showDataContextMenu(const QPoint &point)
{
QModelIndex index = m_data->indexAt(point);
if (index.isValid()) {
while (index.parent().isValid()) {
index = index.parent();
}
if (index.column() != 0) {
index = m_dataModel->index(index.row(), 0);
}
const QString source = index.data().toString();
QMenu menu;
menu.addSection(source);
QAction *service = menu.addAction(i18n("Get associated service"));
QAction *model = menu.addAction(i18n("Get associated model"));
QAction *update = menu.addAction(i18n("Update source now"));
QAction *remove = menu.addAction(i18n("Remove source"));
QAction *activated = menu.exec(m_data->viewport()->mapToGlobal(point));
if (activated == service) {
ServiceViewer *viewer = new ServiceViewer(m_engine, source);
viewer->show();
} else if (activated == model) {
ModelViewer *viewer = new ModelViewer(m_engine, source);
viewer->show();
} else if (activated == update) {
m_engine->connectSource(source, this);
// Plasma5Support::DataEngine::Data data = m_engine->query(source);
} else if (activated == remove) {
removeSource(source);
}
}
}
QString EngineExplorer::convertToString(const QVariant &value)
{
switch (value.type()) {
case QVariant::BitArray: {
return i18np("<1 bit>", "<%1 bits>", value.toBitArray().size());
}
case QVariant::Bitmap: {
QBitmap bitmap = value.value<QBitmap>();
return QString("<%1x%2px - %3bpp>").arg(bitmap.width()).arg(bitmap.height()).arg(bitmap.depth());
}
case QVariant::ByteArray: {
// Return the array size if it is not displayable
if (value.toString().isEmpty()) {
return i18np("<1 byte>", "<%1 bytes>", value.toByteArray().size());
} else {
return value.toString();
}
}
case QVariant::Image: {
QImage image = value.value<QImage>();
return QString("<%1x%2px - %3bpp>").arg(image.width()).arg(image.height()).arg(image.depth());
}
case QVariant::Line: {
QLine line = value.toLine();
return QString("<x1:%1, y1:%2, x2:%3, y2:%4>").arg(line.x1()).arg(line.y1()).arg(line.x2()).arg(line.y2());
}
case QVariant::LineF: {
QLineF lineF = value.toLineF();
return QString("<x1:%1, y1:%2, x2:%3, y2:%4>").arg(lineF.x1()).arg(lineF.y1()).arg(lineF.x2()).arg(lineF.y2());
}
case QVariant::Locale: {
return QString("%1").arg(value.toLocale().name());
}
case QVariant::Map: {
QVariantMap map = value.toMap();
QString str = i18np("<1 item>", "<%1 items>", map.size());
for (auto it = map.constBegin(); it != map.constEnd(); it++) {
str += "\n" + it.key() + ": " + convertToString(it.value());
}
return str;
}
case QVariant::Pixmap: {
QPixmap pixmap = value.value<QPixmap>();
return QString("<%1x%2px - %3bpp>").arg(pixmap.width()).arg(pixmap.height()).arg(pixmap.depth());
}
case QVariant::Point: {
QPoint point = value.toPoint();
return QString("<x:%1, y:%2>").arg(point.x()).arg(point.y());
}
case QVariant::PointF: {
QPointF pointF = value.toPointF();
return QString("<x:%1, y:%2>").arg(pointF.x()).arg(pointF.y());
}
case QVariant::Rect: {
QRect rect = value.toRect();
return QString("<x:%1, y:%2, w:%3, h:%4>").arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height());
}
case QVariant::RectF: {
QRectF rectF = value.toRectF();
return QString("<x:%1, y:%2, w:%3, h:%4>").arg(rectF.x()).arg(rectF.y()).arg(rectF.width()).arg(rectF.height());
}
case QVariant::RegularExpression: {
return value.toRegularExpression().pattern();
}
case QVariant::Region: {
QRect region = value.value<QRegion>().boundingRect();
return QString("<x:%1, y:%2, w:%3, h:%4>").arg(region.x()).arg(region.y()).arg(region.width()).arg(region.height());
}
case QVariant::Size: {
QSize size = value.toSize();
return QString("<w:%1, h:%2>").arg(size.width()).arg(size.height());
}
case QVariant::SizeF: {
QSizeF sizeF = value.toSizeF();
return QString("<w:%1, h:%2>").arg(sizeF.width()).arg(sizeF.height());
}
case QVariant::Url: {
return QString("%1").arg(value.toUrl().toString());
}
case QVariant::StringList: {
return QString("%1").arg(value.toStringList().join(", "));
}
case QVariant::Date: {
return QString("%1").arg(value.toDate().toString());
}
case QVariant::DateTime: {
return QString("%1").arg(value.toDateTime().toString());
}
case QVariant::Time: {
return QString("%1").arg(value.toTime().toString());
}
default: {
if (QLatin1String(value.typeName()) == "QDateTime") {
return QString("%1").arg(value.value<QDateTime>().toString());
}
Plasma5Support::DataEngine::Data data = value.value<Plasma5Support::DataEngine::Data>();
if (!data.isEmpty()) {
QStringList result;
for (auto it = data.constBegin(); it != data.constEnd(); it++) {
result << (it.key() + ": " + convertToString(it.value()));
}
return result.join("\n");
} else if (value.canConvert<QString>()) {
QString str = value.toString();
if (str.isEmpty()) {
return i18nc("The user did a query to a dataengine and it returned empty data", "<empty>");
} else {
return str;
}
}
return i18nc("A the dataengine returned something that the humble view on the engineexplorer can't display, like a picture", "<not displayable>");
}
}
}
int EngineExplorer::showData(QStandardItem *parent, Plasma5Support::DataEngine::Data data)
{
int rowCount = 0;
for (auto it = data.constBegin(); it != data.constEnd(); it++) {
showData(parent, rowCount++, it.key(), it.value());
}
return rowCount;
}
void EngineExplorer::showData(QStandardItem *parent, int row, const QString &key, const QVariant &value)
{
static_assert(ColumnDataSourceAndKey == 0);
// QTreeView only expands tree for children of column #zero.
QStandardItem *current = new QStandardItem(key);
current->setToolTip(key);
parent->setChild(row, ColumnDataSourceAndKey, current);
const char *typeName = value.typeName();
int rowCount = 0;
if (value.userType() == qMetaTypeId<QList<QVariantMap>>()) {
// this case is a bit special, and has to be handled before generic QVariantList
const QList<QVariantMap> list = value.value<QList<QVariantMap>>();
rowCount = showContainerData(parent, current, row, typeName, list);
} else if (value.canConvert<QVariantList>() && value.type() != QVariant::String && value.type() != QVariant::ByteArray) {
const QVariantList list = value.toList();
rowCount = showContainerData(parent, current, row, typeName, list);
} else if (value.canConvert<QVariantMap>()) {
const QVariantMap map = value.toMap();
rowCount = showContainerData(parent, current, row, typeName, map);
} else {
parent->setChild(row, ColumnType, new QStandardItem(typeName));
// clang-format off
QStandardItem *item = value.canConvert<QIcon>()
? new QStandardItem(value.value<QIcon>(), "")
: new QStandardItem(convertToString(value));
// clang-format on
item->setToolTip(item->text());
parent->setChild(row, ColumnValue, item);
// leave rowCount at value 0
}
removeExtraRows(current, rowCount);
}
int EngineExplorer::showContainerData(QStandardItem *parent, QStandardItem *current, int row, const char *typeName, const QList<QVariantMap> &list)
{
QStandardItem *typeItem = new QStandardItem(typeName);
typeItem->setToolTip(typeItem->text());
parent->setChild(row, ColumnType, typeItem);
parent->setChild(row, ColumnValue, new QStandardItem(ki18ncp("Length of the list", "<%1 item>", "<%1 items>").subs(list.length()).toString()));
int rowCount = 0;
for (const QVariantMap &map : list) {
showData(current, rowCount, QString::number(rowCount), map);
rowCount++;
}
return rowCount;
}
int EngineExplorer::showContainerData(QStandardItem *parent, QStandardItem *current, int row, const char *typeName, const QVariantList &list)
{
QStandardItem *typeItem = new QStandardItem(typeName);
typeItem->setToolTip(typeItem->text());
parent->setChild(row, ColumnType, typeItem);
parent->setChild(row, ColumnValue, new QStandardItem(ki18ncp("Length of the list", "<%1 item>", "<%1 items>").subs(list.length()).toString()));
int rowCount = 0;
for (const QVariant &var : list) {
showData(current, rowCount, QString::number(rowCount), var);
rowCount++;
}
return rowCount;
}
int EngineExplorer::showContainerData(QStandardItem *parent, QStandardItem *current, int row, const char *typeName, const QVariantMap &map)
{
QStandardItem *typeItem = new QStandardItem(typeName);
typeItem->setToolTip(typeItem->text());
parent->setChild(row, ColumnType, typeItem);
parent->setChild(row, ColumnValue, new QStandardItem(ki18ncp("Size of the map", "<%1 pair>", "<%1 pairs>").subs(map.size()).toString()));
int rowCount = 0;
for (auto it = map.constBegin(); it != map.constEnd(); it++) {
showData(current, rowCount++, it.key(), it.value());
}
return rowCount;
}
void EngineExplorer::updateTitle()
{
if (!m_engine || !m_engine->metadata().isValid()) {
m_title->setIcon(QIcon::fromTheme("plasma"));
m_title->setText(i18n("Plasma DataEngine Explorer"));
return;
}
m_title->setText(ki18ncp("The name of the engine followed by the number of data sources", "%1 Engine - 1 data source", "%1 Engine - %2 data sources")
.subs(KStringHandler::capwords(m_engine->metadata().name()))
.subs(m_sourceCount)
.toString());
if (m_engine->metadata().iconName().isEmpty()) {
m_title->setIcon(QIcon::fromTheme("plasma"));
} else {
m_title->setIcon(QIcon::fromTheme(m_engine->metadata().iconName()));
}
}
void EngineExplorer::enableButtons(bool enable)
{
if (m_expandButton) {
m_expandButton->setEnabled(enable);
}
if (m_collapseButton) {
m_collapseButton->setEnabled(enable);
}
}
#include "moc_engineexplorer.cpp"
|