qdbusviewer.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 530 行 · 第 1/2 页
CPP
530 行
sig.mName = model->dBusMethodName(item); QMenu menu; menu.addAction(refreshAction); switch (model->itemType(item)) { case QDBusModel::SignalItem: { QAction *action = new QAction("&Connect", &menu); action->setData(1); menu.addAction(action); break; } case QDBusModel::MethodItem: { QAction *action = new QAction("&Call", &menu); action->setData(2); menu.addAction(action); break; } case QDBusModel::PropertyItem: { QAction *actionSet = new QAction("&Set value", &menu); actionSet->setData(3); QAction *actionGet = new QAction("&Get value", &menu); actionGet->setData(4); menu.addAction(actionSet); menu.addAction(actionGet); break; } default: break; } QAction *selectedAction = menu.exec(tree->viewport()->mapToGlobal(point)); if (!selectedAction) return; switch (selectedAction->data().toInt()) { case 1: connectionRequested(sig); break; case 2: callMethod(sig); break; case 3: setProperty(sig); break; case 4: getProperty(sig); break; }}void QDBusViewer::connectionRequested(const BusSignature &sig){ if (!c.connect(sig.mService, QString(), sig.mInterface, sig.mName, this, SLOT(dumpMessage(QDBusMessage)))) { logError(QString("Unable to connect to service %1, path %2, interface %3, signal %4").arg( sig.mService).arg(sig.mPath).arg(sig.mInterface).arg(sig.mName)); }}void QDBusViewer::dumpMessage(const QDBusMessage &message){ QList<QVariant> args = message.arguments(); QString out = "Received "; switch (message.type()) { case QDBusMessage::SignalMessage: out += "signal "; break; case QDBusMessage::ErrorMessage: out += "error message "; break; case QDBusMessage::ReplyMessage: out += "reply "; break; default: out += "message "; break; } out += "from "; out += message.service(); if (!message.path().isEmpty()) out += ", path " + message.path(); if (!message.interface().isEmpty()) out += ", interface <i>" + message.interface() + "</i>"; if (!message.member().isEmpty()) out += ", member " + message.member(); out += "<br>"; if (args.isEmpty()) { out += " (no arguments)"; } else { out += " Arguments: "; foreach (QVariant arg, args) { if (arg.canConvert(QVariant::StringList)) { out += "<b>{</b>"; QStringList list = arg.toStringList(); foreach (QString item, list) out += "<b>\"</b>" + Qt::escape(item) + "<b>\"</b>, "; if (!list.isEmpty()) out.chop(2); out += "<b>}</b>"; } else if (arg.canConvert(QVariant::Rect)) { QRect r = arg.toRect(); out += QString::fromLatin1("QRect(%1, %2, %3, %4)").arg(r.left()).arg(r.top()).arg( r.right()).arg(r.bottom()); } else if (qVariantCanConvert<QDBusArgument>(arg)) { out += "[Argument: " + qvariant_cast<QDBusArgument>(arg).currentSignature(); out += "]"; } else if (qVariantCanConvert<QDBusObjectPath>(arg)) { const QString path = qvariant_cast<QDBusObjectPath>(arg).path(); out += "[ObjectPath: <a href=\"qdbus://bus" + path; out += "\">" + path; out += "</a>]"; } else if (qVariantCanConvert<QDBusSignature>(arg)) { out += "[Signature: " + qvariant_cast<QDBusSignature>(arg).signature(); out += "]"; } else if (qVariantCanConvert<QDBusVariant>(arg)) { QVariant v = qvariant_cast<QDBusVariant>(arg).variant(); out += "[Variant("; out += v.typeName(); out += "): "; out += Qt::escape(v.toString()); out += "]"; } else if (arg.canConvert(QVariant::String)) { out += "<b>\"</b>" + Qt::escape(arg.toString()) + "<b>\"</b>"; } else { out += "["; out += arg.typeName(); out += "]"; } out += ", "; } out.chop(2); } log->append(out);}void QDBusViewer::serviceChanged(QTreeWidgetItem *item){ delete tree->model(); currentService.clear(); if (!item) return; currentService = item->text(0); tree->setModel(new QDBusViewModel(currentService, c)); connect(tree->model(), SIGNAL(busError(QString)), this, SLOT(logError(QString)));}void QDBusViewer::serviceRegistered(const QString &service){ if (service == c.baseService()) return; new QTreeWidgetItem(services, QStringList(service));}static QTreeWidgetItem *findItem(const QTreeWidget *services, const QString &name){ for (int i = 0; i < services->topLevelItemCount(); ++i) { if (services->topLevelItem(i)->text(0) == name) return services->topLevelItem(i); } return 0;}void QDBusViewer::serviceUnregistered(const QString &name){ delete findItem(services, name);}void QDBusViewer::serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner){ QTreeWidgetItem *item = findItem(services, name); if (!item && oldOwner.isEmpty() && !newOwner.isEmpty()) serviceRegistered(name); else if (item && !oldOwner.isEmpty() && newOwner.isEmpty()) delete item; else if (item && !oldOwner.isEmpty() && !newOwner.isEmpty()) { delete item; serviceRegistered(name); }}void QDBusViewer::refreshChildren(){ QDBusModel *model = qobject_cast<QDBusModel *>(tree->model()); if (!model) return; model->refresh(tree->currentIndex());}void QDBusViewer::about(){ QMessageBox box(this);#if QT_EDITION == QT_EDITION_OPENSOURCE QString edition = tr("Open Source Edition"); QString info = tr("This version of Qt's D-Bus Viewer is part of the Qt Open Source Edition. " "Qt is a comprehensive C++ framework for cross-platform application " "development."); QString moreInfo = tr("You need a commercial Qt license for development of proprietary (closed " "source) applications. Please see <a href=\"http://www.trolltech.com/company/model" ".html\">www.trolltech.com/company/model.html</a> for an overview of Qt licensing.");#else QString edition; QString info; QString moreInfo(tr("This program is licensed to you under the terms of the " "Qt Commercial License Agreement. For details, see the file LICENSE " "that came with this software distribution."));#endif box.setText(QString::fromLatin1("<center><img src=\":/trolltech/qdbusviewer/images/qdbusviewer-128.png\">" "<h3>%1</h3>" "<p>Version %2 %3</p></center>" "<p>%4</p>" "<p>%5</p>" "<p>Copyright (C) 2000-2007 Trolltech ASA. All rights reserved.</p>" "<p>The program is provided AS IS with NO WARRANTY OF ANY KIND," " INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A" " PARTICULAR PURPOSE.<p/>") .arg(tr("D-Bus Viewer")).arg(QLatin1String(QT_VERSION_STR)).arg(edition).arg(info).arg(moreInfo)); box.setWindowTitle(tr("D-Bus Viewer")); box.exec();}void QDBusViewer::anchorClicked(const QUrl &url){ if (url.scheme() != "qdbus") // not ours return; // swallow the click without setting a new document log->setSource(QUrl()); QDBusModel *model = qobject_cast<QDBusModel *>(tree->model()); if (!model) return; QModelIndex idx = model->findObject(QDBusObjectPath(url.path())); if (!idx.isValid()) return; tree->scrollTo(idx); tree->setCurrentIndex(idx);}/*! \page qdbusviewer.html \title D-Bus Viewer \keyword qdbusviewer The Qt D-Bus Viewer is a tool that lets you introspect D-Bus objects and messages. You can choose between the system bus and the session bus. Click on any service on the list on the left side to see all the exported objects. You can invoke methods by double-clicking on them. If a method takes one or more IN parameters, a property editor opens. Right-click on a signal to connect to it. All emitted signals including their parameters are output in the message view on the lower side of the window.*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?