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 "parts.h"
0009 
0010 #include "guiactivateevent.h"
0011 
0012 #include <KActionCollection>
0013 #include <KActionMenu>
0014 #include <KPluginMetaData>
0015 
0016 #include <QAction>
0017 #include <QCheckBox>
0018 #include <QFile>
0019 #include <QJsonDocument>
0020 #include <QLineEdit>
0021 #include <QTest>
0022 #include <QTextEdit>
0023 #include <QTextStream>
0024 
0025 #include <KLocalizedString>
0026 #include <QDebug>
0027 #include <qstringliteral.h>
0028 
0029 KPluginMetaData fakeMetadata(const QString &id, const QString &name)
0030 {
0031     const QString json = QLatin1String(
0032                              "{ \"KPlugin\": {\n"
0033                              " \"Name\": \"%1\",\n"
0034                              " \"Version\": \"0.1\"\n"
0035                              "}\n}")
0036                              .arg(name);
0037     QJsonObject jo = QJsonDocument::fromJson(json.toLocal8Bit()).object();
0038     return KPluginMetaData(jo, id);
0039 }
0040 
0041 Part1::Part1(QObject *parent, QWidget *parentWidget)
0042     : KParts::ReadOnlyPart(parent, fakeMetadata(QStringLiteral("kpartstestpart"), QStringLiteral("KPart test part")))
0043 {
0044     m_edit = new QTextEdit(parentWidget);
0045     setWidget(m_edit);
0046 
0047     setXMLFile(QFINDTESTDATA("kpartstest_part1.rc"));
0048 
0049     // An action and an action menu (test code for #70459)
0050 
0051     QAction *testAction = actionCollection()->addAction(QStringLiteral("p1_blah"));
0052     testAction->setText(QStringLiteral("Part1's action"));
0053     actionCollection()->setDefaultShortcut(testAction, Qt::CTRL | Qt::Key_B);
0054     connect(testAction, &QAction::triggered, this, &Part1::slotBlah);
0055 
0056     KActionMenu *menu = new KActionMenu(QIcon::fromTheme(QStringLiteral("mail_forward")), QStringLiteral("Foo"), this);
0057     actionCollection()->addAction(QStringLiteral("p1_foo"), menu);
0058 
0059     QAction *mailForward = new QAction(QIcon::fromTheme(QStringLiteral("mail_forward")), QStringLiteral("Bar"), this);
0060     actionCollection()->setDefaultShortcut(mailForward, Qt::CTRL | Qt::Key_F);
0061     connect(mailForward, &QAction::triggered, this, &Part1::slotFooBar);
0062     actionCollection()->addAction(QStringLiteral("p1_foo_bar"), mailForward);
0063     menu->addAction(mailForward);
0064 }
0065 
0066 Part1::~Part1()
0067 {
0068 }
0069 
0070 void Part1::slotBlah()
0071 {
0072     m_edit->setText(QStringLiteral("Blah"));
0073 }
0074 
0075 void Part1::slotFooBar()
0076 {
0077     m_edit->setText(QStringLiteral("FooBar"));
0078 }
0079 
0080 bool Part1::openFile()
0081 {
0082     // qDebug() << "Part1: opening " << QFile::encodeName(localFilePath());
0083     // Hehe this is from a tutorial I did some time ago :)
0084     QFile f(localFilePath());
0085     QString s;
0086     if (f.open(QIODevice::ReadOnly)) {
0087         QTextStream t(&f);
0088         while (!t.atEnd()) {
0089             s += t.readLine() + QStringLiteral("\n");
0090         }
0091         f.close();
0092     } else {
0093         return false;
0094     }
0095     m_edit->setPlainText(s);
0096 
0097     Q_EMIT setStatusBarText(url().toString());
0098 
0099     return true;
0100 }
0101 
0102 Part2::Part2(QObject *parent, QWidget *parentWidget)
0103     : KParts::Part(parent, fakeMetadata(QStringLiteral("part2"), QStringLiteral("Part2")))
0104 {
0105     QWidget *w = new QWidget(parentWidget);
0106     w->setObjectName(QStringLiteral("Part2Widget"));
0107     setWidget(w);
0108 
0109     setXMLFile(QFINDTESTDATA("kpartstest_part2.rc"));
0110 
0111     /*QCheckBox * cb =*/new QCheckBox(QStringLiteral("something"), w);
0112 
0113     // QLineEdit * l = new QLineEdit( "something", widget() );
0114     // l->move(0,50);
0115     // Since the main widget is a dummy one, we HAVE to set
0116     // strong focus for it, otherwise we get the
0117     // the famous activating-file-menu-switches-part bug.
0118     w->setFocusPolicy(Qt::ClickFocus);
0119 }
0120 
0121 Part2::~Part2()
0122 {
0123 }
0124 
0125 void Part2::guiActivateEvent(KParts::GUIActivateEvent *event)
0126 {
0127     if (event->activated()) {
0128         Q_EMIT setWindowCaption(QStringLiteral("[part2 activated]"));
0129     }
0130 }
0131 
0132 #include "moc_parts.cpp"