File indexing completed on 2024-04-21 15:02:45

0001 /*
0002     SPDX-FileCopyrightText: 1999, 2000 David Faure <faure@kde.org>
0003     SPDX-FileCopyrightText: 1999, 2000 Simon Hausmann <hausmann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "testmainwindow.h"
0009 #include "parts.h"
0010 
0011 #include <KParts/PartLoader>
0012 
0013 #include <KActionCollection>
0014 #include <KLocalizedString>
0015 #include <KMessageBox>
0016 #include <KXMLGUIFactory>
0017 #include <QAction>
0018 #include <QApplication>
0019 #include <QCheckBox>
0020 #include <QCoreApplication>
0021 #include <QDir>
0022 #include <QSplitter>
0023 #include <QStandardPaths>
0024 #include <QTest>
0025 #include <kparts/partmanager.h>
0026 
0027 TestMainWindow::TestMainWindow()
0028     : KParts::MainWindow()
0029 {
0030     setXMLFile(QFINDTESTDATA("kpartstest_shell.rc"));
0031 
0032     m_manager = new KParts::PartManager(this);
0033 
0034     // When the manager says the active part changes, the builder updates (recreates) the GUI
0035     connect(m_manager, &KParts::PartManager::activePartChanged, this, &TestMainWindow::createGUI);
0036 
0037     // We can do this "switch active part" because we have a splitter with
0038     // two items in it.
0039     // I wonder what kdevelop uses/will use to embed kedit, BTW.
0040     m_splitter = new QSplitter(this);
0041 
0042     m_part1 = new Part1(this, m_splitter);
0043     m_part2 = new Part2(this, m_splitter);
0044 
0045     KActionCollection *coll = actionCollection();
0046 
0047     QAction *paOpen = new QAction(QStringLiteral("&View local file"), this);
0048     coll->addAction(QStringLiteral("open_local_file"), paOpen);
0049     connect(paOpen, &QAction::triggered, this, &TestMainWindow::slotFileOpen);
0050     QAction *paOpenRemote = new QAction(QStringLiteral("&View remote file"), this);
0051     coll->addAction(QStringLiteral("open_remote_file"), paOpenRemote);
0052     connect(paOpenRemote, &QAction::triggered, this, &TestMainWindow::slotFileOpenRemote);
0053 
0054     m_paEditFile = new QAction(QStringLiteral("&Edit file"), this);
0055     coll->addAction(QStringLiteral("edit_file"), m_paEditFile);
0056     connect(m_paEditFile, &QAction::triggered, this, &TestMainWindow::slotFileEdit);
0057     m_paCloseEditor = new QAction(QStringLiteral("&Close file editor"), this);
0058     coll->addAction(QStringLiteral("close_editor"), m_paCloseEditor);
0059     connect(m_paCloseEditor, &QAction::triggered, this, &TestMainWindow::slotFileCloseEditor);
0060     m_paCloseEditor->setEnabled(false);
0061     QAction *paQuit = new QAction(QStringLiteral("&Quit"), this);
0062     coll->addAction(QStringLiteral("shell_quit"), paQuit);
0063     connect(paQuit, &QAction::triggered, this, &TestMainWindow::close);
0064     paQuit->setIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
0065 
0066     //  (void)new QAction( "Yet another menu item", coll, "shell_yami" );
0067     //  (void)new QAction( "Yet another submenu item", coll, "shell_yasmi" );
0068 
0069     KStandardAction::configureToolbars(this, &KParts::MainWindow::configureToolbars, actionCollection());
0070 
0071     KStandardAction::keyBindings(guiFactory(), &KXMLGUIFactory::showConfigureShortcutsDialog, actionCollection());
0072 
0073     setCentralWidget(m_splitter);
0074     m_splitter->setMinimumSize(400, 300);
0075 
0076     m_splitter->show();
0077 
0078     m_manager->addPart(m_part1, true); // sets part 1 as the active part
0079     m_manager->addPart(m_part2, false);
0080     m_editorpart = nullptr;
0081 }
0082 
0083 TestMainWindow::~TestMainWindow()
0084 {
0085     disconnect(m_manager, nullptr, this, nullptr);
0086 }
0087 
0088 void TestMainWindow::slotFileOpen()
0089 {
0090     const QString file =
0091         QStandardPaths::locate(QStandardPaths::GenericDataLocation, QCoreApplication::instance()->applicationName() + QStringLiteral("/kpartstest_shell.rc"));
0092     if (!m_part1->openUrl(QUrl::fromLocalFile(file))) {
0093         KMessageBox::error(this, QStringLiteral("Couldn't open file!"));
0094     }
0095 }
0096 
0097 void TestMainWindow::slotFileOpenRemote()
0098 {
0099     QUrl u(QStringLiteral("http://www.kde.org/index.html"));
0100     if (!m_part1->openUrl(u)) {
0101         KMessageBox::error(this, QStringLiteral("Couldn't open file!"));
0102     }
0103 }
0104 
0105 void TestMainWindow::embedEditor()
0106 {
0107     if (m_manager->activePart() == m_part2) {
0108         createGUI(nullptr);
0109     }
0110 
0111     // replace part2 with the editor part
0112     delete m_part2;
0113     m_part2 = nullptr;
0114     QString errorString;
0115     m_editorpart = KParts::PartLoader::createPartInstanceForMimeType<KParts::ReadWritePart>(QStringLiteral("text/plain"), m_splitter, this, &errorString);
0116     if (!m_editorpart) {
0117         qWarning() << errorString;
0118     } else {
0119         m_editorpart->setReadWrite(); // read-write mode
0120         m_manager->addPart(m_editorpart);
0121         m_paEditFile->setEnabled(false);
0122         m_paCloseEditor->setEnabled(true);
0123     }
0124 }
0125 
0126 void TestMainWindow::slotFileCloseEditor()
0127 {
0128     // It is very important to close the url of a read-write part
0129     // before destroying it. This allows to save the document (if modified)
0130     // and also to cancel.
0131     if (!m_editorpart->closeUrl()) {
0132         return;
0133     }
0134 
0135     // Is this necessary ? (David)
0136     if (m_manager->activePart() == m_editorpart) {
0137         createGUI(nullptr);
0138     }
0139 
0140     delete m_editorpart;
0141     m_editorpart = nullptr;
0142     m_part2 = new Part2(this, m_splitter);
0143     m_manager->addPart(m_part2);
0144     m_paEditFile->setEnabled(true);
0145     m_paCloseEditor->setEnabled(false);
0146 }
0147 
0148 void TestMainWindow::slotFileEdit()
0149 {
0150     if (!m_editorpart) {
0151         embedEditor();
0152     }
0153     // TODO use KFileDialog to allow testing remote files
0154     const QString file(QDir::current().absolutePath() + QStringLiteral("/kpartstest_shell.rc"));
0155     if (!m_editorpart->openUrl(QUrl::fromLocalFile(file))) {
0156         KMessageBox::error(this, QStringLiteral("Couldn't open file!"));
0157     }
0158 }
0159 
0160 int main(int argc, char **argv)
0161 {
0162     QApplication::setApplicationName(QStringLiteral("kpartstest"));
0163     QApplication app(argc, argv);
0164 
0165     TestMainWindow *shell = new TestMainWindow;
0166     shell->show();
0167 
0168     return app.exec();
0169 }
0170 
0171 #include "moc_testmainwindow.cpp"