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

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     QString errorString;
0104     m_editorpart = KParts::PartLoader::createPartInstanceForMimeType<KParts::ReadWritePart>(QStringLiteral("text/plain"), m_splitter, this, &errorString);
0105     if (!m_editorpart) {
0106         qWarning() << errorString;
0107     } else {
0108         m_editorpart->setReadWrite(); // read-write mode
0109         ////// m_manager->addPart( m_editorpart );
0110         m_editorpart->widget()->show(); //// we need to do this in a normal KTM....
0111         m_paEditFile->setEnabled(false);
0112         m_paCloseEditor->setEnabled(true);
0113     }
0114 }
0115 
0116 void TestMainWindow::slotFileCloseEditor()
0117 {
0118     delete m_editorpart;
0119     m_editorpart = nullptr;
0120     m_part2 = new Part2(this, m_splitter);
0121     ////// m_manager->addPart( m_part2 );
0122     m_part2->widget()->show(); //// we need to do this in a normal KTM....
0123     m_paEditFile->setEnabled(true);
0124     m_paCloseEditor->setEnabled(false);
0125 }
0126 
0127 void TestMainWindow::slotFileEdit()
0128 {
0129     if (!m_editorpart) {
0130         embedEditor();
0131     }
0132     // TODO use KFileDialog to allow testing remote files
0133     if (!m_editorpart->openUrl(QUrl::fromLocalFile(QDir::current().absolutePath() + QStringLiteral("/kpartstest_shell.rc")))) {
0134         KMessageBox::error(this, QStringLiteral("Couldn't open file!"));
0135     }
0136 }
0137 
0138 int main(int argc, char **argv)
0139 {
0140     // we cheat and call ourselves kpartstest for TestMainWindow::slotFileOpen()
0141     QApplication::setApplicationName(QStringLiteral("kpartstest"));
0142     QApplication app(argc, argv);
0143 
0144     TestMainWindow *shell = new TestMainWindow;
0145     shell->show();
0146 
0147     app.exec();
0148 
0149     return 0;
0150 }
0151 
0152 #include "moc_normalktm.cpp"