File indexing completed on 2024-05-05 03:57:07
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 if (auto res = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadWritePart>(QStringLiteral("text/plain"), m_splitter, this)) { 0115 m_editorpart = res.plugin; 0116 m_editorpart->setReadWrite(); // read-write mode 0117 m_manager->addPart(m_editorpart); 0118 m_paEditFile->setEnabled(false); 0119 m_paCloseEditor->setEnabled(true); 0120 } else { 0121 qWarning() << res.errorString; 0122 } 0123 } 0124 0125 void TestMainWindow::slotFileCloseEditor() 0126 { 0127 // It is very important to close the url of a read-write part 0128 // before destroying it. This allows to save the document (if modified) 0129 // and also to cancel. 0130 if (!m_editorpart->closeUrl()) { 0131 return; 0132 } 0133 0134 // Is this necessary ? (David) 0135 if (m_manager->activePart() == m_editorpart) { 0136 createGUI(nullptr); 0137 } 0138 0139 delete m_editorpart; 0140 m_editorpart = nullptr; 0141 m_part2 = new Part2(this, m_splitter); 0142 m_manager->addPart(m_part2); 0143 m_paEditFile->setEnabled(true); 0144 m_paCloseEditor->setEnabled(false); 0145 } 0146 0147 void TestMainWindow::slotFileEdit() 0148 { 0149 if (!m_editorpart) { 0150 embedEditor(); 0151 } 0152 // TODO use KFileDialog to allow testing remote files 0153 const QString file(QDir::current().absolutePath() + QStringLiteral("/kpartstest_shell.rc")); 0154 if (!m_editorpart->openUrl(QUrl::fromLocalFile(file))) { 0155 KMessageBox::error(this, QStringLiteral("Couldn't open file!")); 0156 } 0157 } 0158 0159 int main(int argc, char **argv) 0160 { 0161 QApplication::setApplicationName(QStringLiteral("kpartstest")); 0162 QApplication app(argc, argv); 0163 0164 TestMainWindow *shell = new TestMainWindow; 0165 shell->show(); 0166 0167 return app.exec(); 0168 } 0169 0170 #include "moc_testmainwindow.cpp"