19 #include "ObjectViewer_p.h"
21 #include <QStandardItem>
22 #include <QApplication>
23 #include <QHBoxLayout>
25 #include <QContextMenuEvent>
26 #include <QItemSelectionModel>
29 #include <QMetaProperty>
39 using namespace KDDockWidgets::Debug;
47 ObjectViewer::ObjectViewer(
QWidget *parent)
53 lay->addWidget(&m_treeView);
54 m_treeView.setModel(&m_model);
56 this, &ObjectViewer::onSelectionChanged);
58 QAction *action = m_menu.addAction(QStringLiteral(
"Refresh"));
60 action = m_menu.addAction(QStringLiteral(
"Dump Windows"));
63 action = m_menu.addAction(QStringLiteral(
"Update"));
66 action = m_menu.addAction(QStringLiteral(
"Print to png"));
69 action = m_menu.addAction(QStringLiteral(
"Toggle Visible"));
73 action = m_menu.addAction(QStringLiteral(
"Send WM_NCHITTEST"));
78 setWindowTitle(QStringLiteral(
"ObjectViewer"));
81 void ObjectViewer::refresh()
83 const auto hashCopy = m_itemMap;
84 for (
auto it = hashCopy.cbegin(), e = hashCopy.cend(); it != e; ++it)
89 const auto &topLevelWidgets = qApp->topLevelWidgets();
90 for (
QWidget *window : topLevelWidgets) {
91 add(window, m_model.invisibleRootItem());
95 void ObjectViewer::dumpSelectedWidgetToPng()
97 if (
auto w = selectedWidget()) {
100 px.save(QStringLiteral(
"px.png"));
105 void ObjectViewer::updateSelectedWidget()
107 if (
auto w = selectedWidget())
111 void ObjectViewer::toggleVisible()
113 if (
auto w = selectedWidget())
114 w->setVisible(!w->isVisible());
118 void ObjectViewer::sendHitTest()
120 if (
auto w = selectedWidget()) {
121 qDebug() <<
"Sending hit test to" << w;
122 ::SendMessage(HWND(w->winId()), WM_NCHITTEST, 0, 0);
127 void ObjectViewer::dumpWindows()
129 qDebug() <<
"Top Level QWidgets:";
130 const auto &topLevelWidgets = qApp->topLevelWidgets();
131 for (
QWidget *w : topLevelWidgets) {
132 if (qobject_cast<QMenu *>(w))
135 qDebug() <<
" QWidget=" << w;
138 qDebug() <<
"Top Level Windows:";
139 const auto &topLevelWindows = qApp->topLevelWindows();
140 for (
QWindow *w : topLevelWindows) {
141 qDebug() <<
" QWindow=" << w <<
"; parent=" << w->parent() <<
"; transientParent=" << w->transientParent() <<
"; hwnd=" << w->winId();
151 if (
auto w = qobject_cast<QWidget *>(o)) {
152 name += QStringLiteral(
" - %1,%2 %3x%4").
arg(w->x()).
arg(w->y()).
arg(w->width()).
arg(w->height());
155 name += QStringLiteral(
" ;W");
156 if (w->windowHandle() !=
nullptr)
157 name += QStringLiteral(
" ;N");
165 if (obj ==
this || obj == &m_menu || obj == parentWidget() || !obj)
168 if (m_ignoreMenus && qobject_cast<QMenu *>(obj))
171 if (m_ignoreShortcuts && qobject_cast<QShortcut *>(obj))
174 if (m_ignoreToolBars && qobject_cast<QToolBar *>(obj))
181 m_itemMap.insert(obj, item);
183 updateItemAppearence(item);
189 void ObjectViewer::remove(
QObject *obj)
193 m_itemMap.remove(obj);
196 void ObjectViewer::onSelectionChanged()
199 if (m_selectedObject == o)
202 if (m_selectedObject) {
203 m_selectedObject->removeEventFilter(
this);
204 if (
auto w = qobject_cast<QWidget *>(m_selectedObject))
208 m_selectedObject = o;
210 if (m_selectedObject) {
212 m_selectedObject->installEventFilter(
this);
213 if (m_highlightsWidget) {
214 if (
auto w = qobject_cast<QWidget *>(o))
220 void ObjectViewer::printProperties(
QObject *obj)
const
222 qDebug() <<
"Printing properties for" << obj;
226 for (
int i = 0; i < count; ++i) {
228 qDebug() <<
" " << prop.
name() << prop.
read(obj);
231 if (
auto w = qobject_cast<QWidget *>(obj)) {
232 qDebug() <<
"Is a widget!";
233 qDebug() <<
"Window=" << w->window();
234 qDebug() <<
"flags=" << w->windowFlags();
235 qDebug() <<
"is native?" << (w->windowHandle() !=
nullptr);
239 QObject *ObjectViewer::selectedObject()
const
241 auto indexes = m_treeView.selectionModel()->selectedIndexes();
242 if (indexes.isEmpty())
249 QWidget *ObjectViewer::selectedWidget()
const
251 return qobject_cast<QWidget *>(selectedObject());
257 QWidget *widget = widgetForItem(item);
275 return qobject_cast<QWidget *>(objectForItem(item));
283 bool ObjectViewer::eventFilter(
QObject *watched,
QEvent *event)
285 auto widget =
static_cast<QWidget *
>(watched);
287 updateItemAppearence(m_itemMap.value(watched));
291 if (m_selectedObject != watched)