File indexing completed on 2024-07-21 12:15:43

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 Rafael Fernández López <ereslibre@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include <QAction>
0009 #include <QApplication>
0010 #include <QStandardPaths>
0011 #include <QTest>
0012 #include <QTextEdit>
0013 #include <QTimer>
0014 
0015 #include <KConfigGroup>
0016 #include <KMessageBox>
0017 #include <kactioncollection.h>
0018 #include <kxmlguiwindow.h>
0019 
0020 // BUG: if this symbol is defined the problem consists on:
0021 //      - main window is created.
0022 //      - settings are saved (and applied), but in this case no toolbars exist yet, so they don't
0023 //        apply to any toolbar.
0024 //      - after 1 second the GUI is created.
0025 //
0026 //      How to reproduce ?
0027 //          - Move one toolbar to other place (bottom, left, right, or deattach it).
0028 //          - Close the test (so settings are saved).
0029 //          - Reopen the test. The toolbar you moved is not keeping the place you specified.
0030 #define REPRODUCE_TOOLBAR_BUG
0031 
0032 class MainWindow : public KXmlGuiWindow
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     MainWindow(QWidget *parent = nullptr);
0038 
0039 public Q_SLOTS:
0040     void slotTest();
0041     void slotCreate();
0042 
0043 private:
0044     void setupActions();
0045 };
0046 
0047 void MainWindow::slotTest()
0048 {
0049     KMessageBox::information(nullptr, QStringLiteral("Test"), QStringLiteral("Test"));
0050 }
0051 
0052 void MainWindow::slotCreate()
0053 {
0054     setupGUI(ToolBar | Keys);
0055     createGUI(xmlFile());
0056 
0057     if (autoSaveConfigGroup().isValid()) {
0058         applyMainWindowSettings(autoSaveConfigGroup());
0059     }
0060 }
0061 
0062 void MainWindow::setupActions()
0063 {
0064     QAction *testAction = new QAction(this);
0065     testAction->setText(QStringLiteral("Test"));
0066     testAction->setIcon(QIcon::fromTheme(QStringLiteral("kde")));
0067     actionCollection()->addAction(QStringLiteral("test"), testAction);
0068     connect(testAction, &QAction::triggered, this, &MainWindow::slotTest);
0069 
0070     KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());
0071 
0072     setAutoSaveSettings();
0073 
0074     // BUG: if the GUI is created after an amount of time (so settings have been saved), then toolbars
0075     //      are shown misplaced. KMainWindow uses a 500 ms timer to save window settings.
0076 #ifdef REPRODUCE_TOOLBAR_BUG
0077     QTimer::singleShot(1000, this, &MainWindow::slotCreate); // more than 500 ms so the main window has saved settings.
0078     // We can think of this case on natural applications when they
0079     // load plugins and change parts. It can take 1 second perfectly.
0080 #else
0081     QTimer::singleShot(0, this, &MainWindow::slotCreate);
0082 #endif
0083 }
0084 
0085 MainWindow::MainWindow(QWidget *parent)
0086     : KXmlGuiWindow(parent)
0087 {
0088     setXMLFile(QFINDTESTDATA("kxmlguiwindowtestui.rc"), true);
0089     // Because we use a full path in setXMLFile, we need to call setLocalXMLFile too.
0090     // In your apps, just pass a relative filename to setXMLFile instead.
0091     setLocalXMLFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kxmlguiwindowtest/kxmlguiwindowtestui.rc"));
0092 
0093     setCentralWidget(new QTextEdit(this));
0094     setupActions();
0095 }
0096 
0097 int main(int argc, char **argv)
0098 {
0099     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
0100     QApplication app(argc, argv);
0101 
0102     MainWindow *mainWindow = new MainWindow;
0103     mainWindow->show();
0104 
0105     return app.exec();
0106 }
0107 
0108 #include "kxmlguiwindowtest.moc"