From: Alexander Volkov Date: Mon, 22 Sep 2014 12:12:45 +0000 Subject: qdbusviewer: Restore the window size and state on startup --- qdbusviewer: Restore the window size and state on startup Save the window size, maximization state and state of splitters on exit and restore these settings on startup. Change-Id: Ibe26b09cc97ffc2fef17e6e8e2b804324dae7002 Reviewed-by: Thiago Macieira Reviewed-by: Friedemann Kleint --- --- a/src/qdbus/qdbusviewer/main.cpp +++ b/src/qdbus/qdbusviewer/main.cpp @@ -40,6 +40,10 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); + + QCoreApplication::setOrganizationName(QStringLiteral("QtProject")); + QCoreApplication::setApplicationName(QStringLiteral("QDBusViewer")); + MainWindow mw; #ifndef Q_OS_MAC app.setWindowIcon(QIcon(QLatin1String(":/qt-project.org/qdbusviewer/images/qdbusviewer.png"))); --- a/src/qdbus/qdbusviewer/mainwindow.cpp +++ b/src/qdbus/qdbusviewer/mainwindow.cpp @@ -51,6 +51,7 @@ #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -72,10 +73,17 @@ MainWindow::MainWindow(QWidget *parent) tabWidget = new QTabWidget; setCentralWidget(tabWidget); - QDBusViewer *sessionBusViewer = new QDBusViewer(QDBusConnection::sessionBus()); - QDBusViewer *systemBusViewer = new QDBusViewer(QDBusConnection::systemBus()); + sessionBusViewer = new QDBusViewer(QDBusConnection::sessionBus()); + systemBusViewer = new QDBusViewer(QDBusConnection::systemBus()); tabWidget->addTab(sessionBusViewer, tr("Session Bus")); tabWidget->addTab(systemBusViewer, tr("System Bus")); + + restoreSettings(); +} + +MainWindow::~MainWindow() +{ + saveSettings(); } void MainWindow::addCustomBusTab(const QString &busAddress) @@ -99,3 +107,37 @@ void MainWindow::about() box.setWindowTitle(tr("D-Bus Viewer")); box.exec(); } + +static inline QString windowGeometryKey() { return QStringLiteral("WindowGeometry"); } +static inline QString sessionTabGroup() { return QStringLiteral("SessionTab"); } +static inline QString systemTabGroup() { return QStringLiteral("SystemTab"); } + +void MainWindow::saveSettings() +{ + QSettings settings; + + settings.setValue(windowGeometryKey(), saveGeometry()); + + settings.beginGroup(sessionTabGroup()); + sessionBusViewer->saveState(&settings); + settings.endGroup(); + + settings.beginGroup(systemTabGroup()); + systemBusViewer->saveState(&settings); + settings.endGroup(); +} + +void MainWindow::restoreSettings() +{ + QSettings settings; + + restoreGeometry(settings.value(windowGeometryKey()).toByteArray()); + + settings.beginGroup(sessionTabGroup()); + sessionBusViewer->restoreState(&settings); + settings.endGroup(); + + settings.beginGroup(systemTabGroup()); + systemBusViewer->restoreState(&settings); + settings.endGroup(); +} --- a/src/qdbus/qdbusviewer/mainwindow.h +++ b/src/qdbus/qdbusviewer/mainwindow.h @@ -46,11 +46,14 @@ QT_FORWARD_DECLARE_CLASS(QTabWidget) +class QDBusViewer; + class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); void addCustomBusTab(const QString &bus); @@ -58,7 +61,12 @@ void about(); private: + void saveSettings(); + void restoreSettings(); + QTabWidget *tabWidget; + QDBusViewer *sessionBusViewer; + QDBusViewer *systemBusViewer; }; #endif // MAINWINDOW_H --- a/src/qdbus/qdbusviewer/qdbusviewer.cpp +++ b/src/qdbus/qdbusviewer/qdbusviewer.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -103,13 +104,13 @@ connect(refreshShortcut, SIGNAL(activated()), this, SLOT(refreshChildren())); QVBoxLayout *layout = new QVBoxLayout(this); - QSplitter *topSplitter = new QSplitter(Qt::Vertical, this); + topSplitter = new QSplitter(Qt::Vertical, this); layout->addWidget(topSplitter); log = new LogViewer; connect(log, SIGNAL(anchorClicked(QUrl)), this, SLOT(anchorClicked(QUrl))); - QSplitter *splitter = new QSplitter(topSplitter); + splitter = new QSplitter(topSplitter); splitter->addWidget(servicesView); QWidget *servicesWidget = new QWidget; @@ -144,6 +145,21 @@ objectPathRegExp.setMinimal(true); +} + +static inline QString topSplitterStateKey() { return QStringLiteral("topSplitterState"); } +static inline QString splitterStateKey() { return QStringLiteral("splitterState"); } + +void QDBusViewer::saveState(QSettings *settings) const +{ + settings->setValue(topSplitterStateKey(), topSplitter->saveState()); + settings->setValue(splitterStateKey(), splitter->saveState()); +} + +void QDBusViewer::restoreState(const QSettings *settings) +{ + topSplitter->restoreState(settings->value(topSplitterStateKey()).toByteArray()); + splitter->restoreState(settings->value(splitterStateKey()).toByteArray()); } void QDBusViewer::logMessage(const QString &msg) --- a/src/qdbus/qdbusviewer/qdbusviewer.h +++ b/src/qdbus/qdbusviewer/qdbusviewer.h @@ -46,6 +46,8 @@ QT_FORWARD_DECLARE_CLASS(QTextBrowser) QT_FORWARD_DECLARE_CLASS(QDomDocument) QT_FORWARD_DECLARE_CLASS(QDomElement) +QT_FORWARD_DECLARE_CLASS(QSplitter) +QT_FORWARD_DECLARE_CLASS(QSettings) struct BusSignature { @@ -58,6 +60,9 @@ Q_OBJECT public: QDBusViewer(const QDBusConnection &connection, QWidget *parent = 0); + + void saveState(QSettings *settings) const; + void restoreState(const QSettings *settings); public slots: void refresh(); @@ -94,6 +99,8 @@ QLineEdit *serviceFilterLine; QListView *servicesView; QTextBrowser *log; + QSplitter *topSplitter; + QSplitter *splitter; QRegExp objectPathRegExp; };