19#include "ObjectViewer_p.h"
21#include <QStandardItem>
22#include <QApplication>
25#include <QContextMenuEvent>
26#include <QItemSelectionModel>
29#include <QMetaProperty>
39using namespace KDDockWidgets::Debug;
46ObjectViewer::ObjectViewer(
QWidget *parent)
52 lay->addWidget(&m_treeView);
53 m_treeView.setModel(&m_model);
55 this, &ObjectViewer::onSelectionChanged);
57 QAction *action = m_menu.addAction(QStringLiteral(
"Refresh"));
59 action = m_menu.addAction(QStringLiteral(
"Dump Windows"));
62 action = m_menu.addAction(QStringLiteral(
"Update"));
65 action = m_menu.addAction(QStringLiteral(
"Print to png"));
68 action = m_menu.addAction(QStringLiteral(
"Toggle Visible"));
72 action = m_menu.addAction(QStringLiteral(
"Send WM_NCHITTEST"));
77 setWindowTitle(QStringLiteral(
"ObjectViewer"));
80void ObjectViewer::refresh()
82 const auto hashCopy = m_itemMap;
83 for (
auto it = hashCopy.cbegin(), e = hashCopy.cend(); it != e; ++it)
88 const auto &topLevelWidgets = qApp->topLevelWidgets();
89 for (
QWidget *window : topLevelWidgets) {
90 add(window, m_model.invisibleRootItem());
94void ObjectViewer::dumpSelectedWidgetToPng()
96 if (
auto w = selectedWidget()) {
99 px.save(QStringLiteral(
"px.png"));
104void ObjectViewer::updateSelectedWidget()
106 if (
auto w = selectedWidget())
110void ObjectViewer::toggleVisible()
112 if (
auto w = selectedWidget())
113 w->setVisible(!w->isVisible());
117void ObjectViewer::sendHitTest()
119 if (
auto w = selectedWidget()) {
120 qDebug() <<
"Sending hit test to" << w;
121 ::SendMessage(HWND(w->winId()), WM_NCHITTEST, 0, 0);
126void ObjectViewer::dumpWindows()
128 qDebug() <<
"Top Level QWidgets:";
129 const auto &topLevelWidgets = qApp->topLevelWidgets();
130 for (
QWidget *w : topLevelWidgets) {
131 if (qobject_cast<QMenu *>(w))
134 qDebug() <<
" QWidget=" << w;
137 qDebug() <<
"Top Level Windows:";
138 const auto &topLevelWindows = qApp->topLevelWindows();
139 for (
QWindow *w : topLevelWindows) {
140 qDebug() <<
" QWindow=" << w <<
"; parent=" << w->parent() <<
"; transientParent=" << w->transientParent() <<
"; hwnd=" << w->winId();
150 if (
auto w = qobject_cast<QWidget *>(o)) {
151 name += QStringLiteral(
" - %1,%2 %3x%4").
arg(w->x()).
arg(w->y()).
arg(w->width()).
arg(w->height());
154 name += QStringLiteral(
" ;W");
155 if (w->windowHandle() !=
nullptr)
156 name += QStringLiteral(
" ;N");
164 if (obj ==
this || obj == &m_menu || obj == parentWidget() || !obj)
167 if (m_ignoreMenus && qobject_cast<QMenu *>(obj))
170 if (m_ignoreShortcuts && qobject_cast<QShortcut *>(obj))
173 if (m_ignoreToolBars && qobject_cast<QToolBar *>(obj))
180 m_itemMap.insert(obj, item);
182 updateItemAppearence(item);
184 for (
auto child : obj->children())
188void ObjectViewer::remove(
QObject *obj)
192 m_itemMap.remove(obj);
195void ObjectViewer::onSelectionChanged()
198 if (m_selectedObject == o)
201 if (m_selectedObject) {
202 m_selectedObject->removeEventFilter(
this);
203 if (
auto w = qobject_cast<QWidget *>(m_selectedObject))
207 m_selectedObject = o;
209 if (m_selectedObject) {
211 m_selectedObject->installEventFilter(
this);
212 if (m_highlightsWidget) {
213 if (
auto w = qobject_cast<QWidget *>(o))
219void ObjectViewer::printProperties(
QObject *obj)
const
221 qDebug() <<
"Printing properties for" << obj;
225 for (
int i = 0; i < count; ++i) {
227 qDebug() <<
" " << prop.
name() << prop.
read(obj);
230 if (
auto w = qobject_cast<QWidget *>(obj)) {
231 qDebug() <<
"Is a widget!";
232 qDebug() <<
"Window=" << w->window();
233 qDebug() <<
"flags=" << w->windowFlags();
234 qDebug() <<
"is native?" << (w->windowHandle() !=
nullptr);
238QObject *ObjectViewer::selectedObject()
const
240 auto indexes = m_treeView.selectionModel()->selectedIndexes();
241 if (indexes.isEmpty())
248QWidget *ObjectViewer::selectedWidget()
const
250 return qobject_cast<QWidget *>(selectedObject());
256 QWidget *widget = widgetForItem(item);
274 return qobject_cast<QWidget *>(objectForItem(item));
282bool ObjectViewer::eventFilter(
QObject *watched,
QEvent *event)
284 auto widget =
static_cast<QWidget *
>(watched);
286 updateItemAppearence(m_itemMap.value(watched));
290 if (m_selectedObject != watched)
void triggered(bool checked)
QEvent::Type type() const const
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
QVariant data(int role) const const
void destroyed(QObject *obj)
void installEventFilter(QObject *filterObj)
void removeEventFilter(QObject *obj)
void appendRow(const QList< QStandardItem * > &items)
virtual QVariant data(int role) const const
void setForeground(const QBrush &brush)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QVariant fromValue(const T &value)