File indexing completed on 2024-05-05 07:58:22
0001 /* 0002 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org> 0003 SPDX-FileCopyrightText: 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 "normalktm.h" 0009 #include "parts.h" 0010 0011 #include <KParts/PartLoader> 0012 0013 #include <QCheckBox> 0014 #include <QCoreApplication> 0015 #include <QDebug> 0016 #include <QDir> 0017 #include <QMenu> 0018 #include <QMenuBar> 0019 #include <QSplitter> 0020 0021 #include <KActionCollection> 0022 #include <KLocalizedString> 0023 #include <KMessageBox> 0024 #include <QAction> 0025 #include <QApplication> 0026 #include <QStandardPaths> 0027 0028 TestMainWindow::TestMainWindow() 0029 : KXmlGuiWindow() 0030 { 0031 // We can do this "switch active part" because we have a splitter with 0032 // two items in it. 0033 // I wonder what kdevelop uses/will use to embed kedit, BTW. 0034 m_splitter = new QSplitter(this); 0035 0036 m_part1 = new Part1(this, m_splitter); 0037 m_part2 = new Part2(this, m_splitter); 0038 0039 QMenu *pFile = new QMenu(QStringLiteral("File"), menuBar()); 0040 KActionCollection *coll = actionCollection(); 0041 QAction *paLocal = new QAction(QStringLiteral("&View local file"), this); 0042 coll->addAction(QStringLiteral("open_local_file"), paLocal); 0043 connect(paLocal, &QAction::triggered, this, &TestMainWindow::slotFileOpen); 0044 // No XML: we need to add our actions to the menus ourselves 0045 pFile->addAction(paLocal); 0046 0047 QAction *paRemote = new QAction(QStringLiteral("&View remote file"), this); 0048 coll->addAction(QStringLiteral("open_remote_file"), paRemote); 0049 connect(paRemote, &QAction::triggered, this, &TestMainWindow::slotFileOpenRemote); 0050 pFile->addAction(paRemote); 0051 0052 m_paEditFile = new QAction(QStringLiteral("&Edit file"), this); 0053 coll->addAction(QStringLiteral("edit_file"), m_paEditFile); 0054 connect(m_paEditFile, &QAction::triggered, this, &TestMainWindow::slotFileEdit); 0055 pFile->addAction(m_paEditFile); 0056 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 pFile->addAction(m_paCloseEditor); 0062 0063 QAction *paQuit = new QAction(QStringLiteral("&Quit"), this); 0064 coll->addAction(QStringLiteral("shell_quit"), paQuit); 0065 connect(paQuit, &QAction::triggered, this, &TestMainWindow::close); 0066 paQuit->setIcon(QIcon::fromTheme(QStringLiteral("application-exit"))); 0067 pFile->addAction(paQuit); 0068 0069 setCentralWidget(m_splitter); 0070 m_splitter->setMinimumSize(400, 300); 0071 0072 m_splitter->show(); 0073 0074 m_editorpart = nullptr; 0075 } 0076 0077 TestMainWindow::~TestMainWindow() 0078 { 0079 } 0080 0081 void TestMainWindow::slotFileOpen() 0082 { 0083 const QString file = 0084 QStandardPaths::locate(QStandardPaths::GenericDataLocation, QCoreApplication::instance()->applicationName() + QStringLiteral("/kpartstest_shell.rc")); 0085 if (!m_part1->openUrl(QUrl::fromLocalFile(file))) { 0086 KMessageBox::error(this, QStringLiteral("Couldn't open file !")); 0087 } 0088 } 0089 0090 void TestMainWindow::slotFileOpenRemote() 0091 { 0092 QUrl u(QStringLiteral("http://www.kde.org/index.html")); 0093 if (!m_part1->openUrl(u)) { 0094 KMessageBox::error(this, QStringLiteral("Couldn't open file !")); 0095 } 0096 } 0097 0098 void TestMainWindow::embedEditor() 0099 { 0100 // replace part2 with the editor part 0101 delete m_part2; 0102 m_part2 = nullptr; 0103 if (auto res = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadWritePart>(QStringLiteral("text/plain"), m_splitter, this)) { 0104 m_editorpart = res.plugin; 0105 m_editorpart->setReadWrite(); // read-write mode 0106 ////// m_manager->addPart( m_editorpart ); 0107 m_editorpart->widget()->show(); //// we need to do this in a normal KTM.... 0108 m_paEditFile->setEnabled(false); 0109 m_paCloseEditor->setEnabled(true); 0110 } else { 0111 qWarning() << res.errorString; 0112 } 0113 } 0114 0115 void TestMainWindow::slotFileCloseEditor() 0116 { 0117 delete m_editorpart; 0118 m_editorpart = nullptr; 0119 m_part2 = new Part2(this, m_splitter); 0120 ////// m_manager->addPart( m_part2 ); 0121 m_part2->widget()->show(); //// we need to do this in a normal KTM.... 0122 m_paEditFile->setEnabled(true); 0123 m_paCloseEditor->setEnabled(false); 0124 } 0125 0126 void TestMainWindow::slotFileEdit() 0127 { 0128 if (!m_editorpart) { 0129 embedEditor(); 0130 } 0131 // TODO use KFileDialog to allow testing remote files 0132 if (!m_editorpart->openUrl(QUrl::fromLocalFile(QDir::current().absolutePath() + QStringLiteral("/kpartstest_shell.rc")))) { 0133 KMessageBox::error(this, QStringLiteral("Couldn't open file!")); 0134 } 0135 } 0136 0137 int main(int argc, char **argv) 0138 { 0139 // we cheat and call ourselves kpartstest for TestMainWindow::slotFileOpen() 0140 QApplication::setApplicationName(QStringLiteral("kpartstest")); 0141 QApplication app(argc, argv); 0142 0143 TestMainWindow *shell = new TestMainWindow; 0144 shell->show(); 0145 0146 app.exec(); 0147 0148 return 0; 0149 } 0150 0151 #include "moc_normalktm.cpp"