File indexing completed on 2024-04-28 15:29:18
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 "notepad.h" 0009 #include <kparts/mainwindow.h> 0010 #include <kparts/partmanager.h> 0011 0012 #include <QAction> 0013 #include <QFile> 0014 #include <QTextEdit> 0015 #include <QTextStream> 0016 0017 #include <KAboutData> 0018 #include <KActionCollection> 0019 #include <KLocalizedString> 0020 #include <QDebug> 0021 0022 K_PLUGIN_FACTORY_WITH_JSON(NotepadFactory, "notepad.json", registerPlugin<NotepadPart>();) 0023 0024 NotepadPart::NotepadPart(QWidget *parentWidget, QObject *parent, const KPluginMetaData &metaData, const QVariantList &) 0025 : KParts::ReadWritePart(parent) 0026 { 0027 setMetaData(metaData); 0028 0029 m_edit = new QTextEdit(parentWidget); 0030 m_edit->setPlainText(QStringLiteral("NotepadPart's multiline edit")); 0031 setWidget(m_edit); 0032 0033 QAction *searchReplace = new QAction(QStringLiteral("Search and replace"), this); 0034 actionCollection()->addAction(QStringLiteral("searchreplace"), searchReplace); 0035 connect(searchReplace, &QAction::triggered, this, &NotepadPart::slotSearchReplace); 0036 0037 setXMLFile(QStringLiteral("notepadpart.rc")); // will be found in the qrc resource 0038 0039 setReadWrite(true); 0040 0041 #if KPARTS_BUILD_DEPRECATED_SINCE(5, 90) 0042 // Always write this as the last line of your constructor 0043 loadPlugins(); 0044 #endif 0045 } 0046 0047 NotepadPart::~NotepadPart() 0048 { 0049 } 0050 0051 void NotepadPart::setReadWrite(bool rw) 0052 { 0053 m_edit->setReadOnly(!rw); 0054 if (rw) { 0055 connect(m_edit, &QTextEdit::textChanged, this, qOverload<>(&KParts::ReadWritePart::setModified)); 0056 } else { 0057 disconnect(m_edit, &QTextEdit::textChanged, this, qOverload<>(&KParts::ReadWritePart::setModified)); 0058 } 0059 0060 ReadWritePart::setReadWrite(rw); 0061 } 0062 0063 bool NotepadPart::openFile() 0064 { 0065 // qDebug() << "NotepadPart: opening " << localFilePath(); 0066 QFile f(localFilePath()); 0067 QString s; 0068 if (f.open(QIODevice::ReadOnly)) { 0069 QTextStream t(&f); 0070 // The default with Qt6 is UTF-8 0071 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0072 t.setCodec("UTF-8"); 0073 #endif 0074 s = t.readAll(); 0075 f.close(); 0076 } 0077 m_edit->setPlainText(s); 0078 0079 Q_EMIT setStatusBarText(url().toString()); 0080 0081 return true; 0082 } 0083 0084 bool NotepadPart::saveFile() 0085 { 0086 if (!isReadWrite()) { 0087 return false; 0088 } 0089 QFile f(localFilePath()); 0090 if (f.open(QIODevice::WriteOnly)) { 0091 QTextStream t(&f); 0092 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0093 t.setCodec("UTF-8"); 0094 #endif 0095 t << m_edit->toPlainText(); 0096 f.close(); 0097 return true; 0098 } else { 0099 return false; 0100 } 0101 } 0102 0103 void NotepadPart::slotSearchReplace() 0104 { 0105 } 0106 0107 #include "moc_notepad.cpp" 0108 #include "notepad.moc"