Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
mainwindow.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd and/or its subsidiary(-ies).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "mainwindow.h"
5
6#include "qdbusviewer.h"
7
8#include <QtWidgets/QApplication>
9#include <QtWidgets/QTabWidget>
10#include <QtWidgets/QMenuBar>
11#include <QtWidgets/QMenu>
12#include <QtGui/QAction>
13#include <QtWidgets/QMessageBox>
14
15#include <QtDBus/QDBusConnection>
16#include <QtCore/QSettings>
17
18using namespace Qt::StringLiterals;
19
20MainWindow::MainWindow(QWidget *parent)
22 , systemBusViewer(nullptr)
23{
24 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
25 QAction *quitAction = fileMenu->addAction(tr("&Quit"), this, &QWidget::close);
26 quitAction->setShortcut(QKeySequence::Quit);
27 quitAction->setMenuRole(QAction::QuitRole);
28
29 QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
30 QAction *aboutAction = helpMenu->addAction(tr("&About"));
31 aboutAction->setMenuRole(QAction::AboutRole);
32 QObject::connect(aboutAction, &QAction::triggered, this, &MainWindow::about);
33
34 QAction *aboutQtAction = helpMenu->addAction(tr("About &Qt"));
35 aboutQtAction->setMenuRole(QAction::AboutQtRole);
36 QObject::connect(aboutQtAction, &QAction::triggered, qApp, &QApplication::aboutQt);
37
38 tabWidget = new QTabWidget;
39 setCentralWidget(tabWidget);
40
41 sessionBusViewer = new QDBusViewer(QDBusConnection::sessionBus());
42 tabWidget->addTab(sessionBusViewer, tr("Session Bus"));
43
44 QDBusConnection connection = QDBusConnection::systemBus();
45 if (connection.isConnected()) {
46 systemBusViewer = new QDBusViewer(connection);
47 tabWidget->addTab(systemBusViewer, tr("System Bus"));
48 }
49
50 restoreSettings();
51}
52
54{
55 saveSettings();
56}
57
58void MainWindow::addCustomBusTab(const QString &busAddress)
59{
60 QDBusConnection connection = QDBusConnection::connectToBus(busAddress, "QDBusViewer");
61 if (connection.isConnected()) {
62 QDBusViewer *customBusViewer = new QDBusViewer(connection);
63 tabWidget->addTab(customBusViewer, tr("Custom Bus"));
64 }
65}
66
67void MainWindow::about()
68{
69 QMessageBox box(this);
70
71 box.setText(tr("<center><img src=\":/qt-project.org/qdbusviewer/images/qdbusviewer-128.png\">"
72 "<h3>%1</h3>"
73 "<p>Version %2</p></center>"
74 "<p>Copyright (C) The Qt Company Ltd. and other contributors.</p>")
75 .arg(tr("D-Bus Viewer"), QLatin1String(QT_VERSION_STR)));
76 box.setWindowTitle(tr("D-Bus Viewer"));
77 box.exec();
78}
79
81{
82 return u"WindowGeometry"_s;
83}
84
85static inline QString sessionTabGroup()
86{
87 return u"SessionTab"_s;
88}
89
90static inline QString systemTabGroup()
91{
92 return u"SystemTab"_s;
93}
94
95void MainWindow::saveSettings()
96{
97 QSettings settings;
98
99 settings.setValue(windowGeometryKey(), saveGeometry());
100
101 settings.beginGroup(sessionTabGroup());
102 sessionBusViewer->saveState(&settings);
103 settings.endGroup();
104
105 if (systemBusViewer) {
106 settings.beginGroup(systemTabGroup());
107 systemBusViewer->saveState(&settings);
108 settings.endGroup();
109 }
110}
111
112void MainWindow::restoreSettings()
113{
114 QSettings settings;
115
116 restoreGeometry(settings.value(windowGeometryKey()).toByteArray());
117
118 settings.beginGroup(sessionTabGroup());
119 sessionBusViewer->restoreState(&settings);
120 settings.endGroup();
121
122 if (systemBusViewer) {
123 settings.beginGroup(systemTabGroup());
124 systemBusViewer->restoreState(&settings);
125 settings.endGroup();
126 }
127}
~MainWindow() override
void addCustomBusTab(const QString &bus)
MainWindow(QWidget *parent=nullptr)
static QString sessionTabGroup()
static QString systemTabGroup()
static QString windowGeometryKey()