From b020fe9c7f94a2a603504e73238fd9a438a221a5 Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 26 Jun 2023 19:33:00 -0700 Subject: [PATCH] implement the rest of the basic text editor --- src/TextEditor/CMakeLists.txt | 2 + src/TextEditor/MainWindow.cc | 86 ++++++++++++++++++++++++++++++++-- src/TextEditor/MainWindow.hh | 17 ++++++- src/TextEditor/TextEditorUI.rc | 4 ++ src/TextEditor/main.cc | 1 + 5 files changed, 106 insertions(+), 4 deletions(-) diff --git a/src/TextEditor/CMakeLists.txt b/src/TextEditor/CMakeLists.txt index cdd9e98..466c77c 100644 --- a/src/TextEditor/CMakeLists.txt +++ b/src/TextEditor/CMakeLists.txt @@ -1,6 +1,7 @@ find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS XmlGui TextWidgets + KIO ) add_executable(TextEditor main.cc MainWindow.cc) @@ -10,6 +11,7 @@ target_link_libraries(TextEditor KF5::I18n KF5::XmlGui KF5::TextWidgets + KF5::KIOCore ) install(TARGETS TextEditor ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/src/TextEditor/MainWindow.cc b/src/TextEditor/MainWindow.cc index 89b12cc..59bdb09 100644 --- a/src/TextEditor/MainWindow.cc +++ b/src/TextEditor/MainWindow.cc @@ -1,12 +1,17 @@ #include "MainWindow.hh" -#include -#include #include +#include +#include #include +#include #include +#include +#include +#include MainWindow::MainWindow(QWidget* parent) : KXmlGuiWindow(parent) + , fileName(QString()) { // setup the TextArea textArea = new KTextEdit(); @@ -16,7 +21,7 @@ MainWindow::MainWindow(QWidget* parent) setupActions(); } -void MainWindow::setupActions() +void MainWindow::setupClearAction() { QAction* clearAction = new QAction(this); clearAction->setText(i18n("&Clear")); @@ -24,8 +29,83 @@ void MainWindow::setupActions() actionCollection()->addAction("clear", clearAction); actionCollection()->setDefaultShortcut(clearAction, Qt::CTRL + Qt::Key_W); connect(clearAction, &QAction::triggered, textArea, &KTextEdit::clear); +} + +void MainWindow::setupActions() +{ + setupClearAction(); KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection()); + KStandardAction::open(this, &MainWindow::openFile, actionCollection()); + KStandardAction::save(this, &MainWindow::saveFile, actionCollection()); + KStandardAction::saveAs(this, &MainWindow::saveFileAs, actionCollection()); + KStandardAction::openNew(this, &MainWindow::newFile, actionCollection()); setupGUI(Default, "TextEditorUI.rc"); } + +void MainWindow::saveFileToDisk(const QString& outputFileName) +{ + if (outputFileName.isNull()) + return; + + QSaveFile file(outputFileName); + // open the file as write only + file.open(QIODevice::WriteOnly); + + QByteArray outputByteArray; + outputByteArray.append(textArea->toPlainText().toUtf8()); + + file.write(outputByteArray); + file.commit(); + + fileName = outputFileName; +} + +void MainWindow::newFile() +{ + fileName.clear(); + textArea->clear(); +} + +void MainWindow::openFile() +{ + const QUrl fileNameFromDialog = QFileDialog::getOpenFileUrl(this, i18n("Open File")); + + if (fileNameFromDialog.isEmpty()) + return; + KIO::Job* job = KIO::storedGet(fileNameFromDialog); + fileName = fileNameFromDialog.toLocalFile(); + + connect(job, &KJob::result, this, &MainWindow::downloadFinished); + job->exec(); +} + +void MainWindow::saveFile() +{ + if (!fileName.isEmpty()) { + saveFileToDisk(fileName); + return; + } + + saveFileAs(); +} + +void MainWindow::saveFileAs() +{ + saveFileToDisk(QFileDialog::getSaveFileName(this, i18n("Save File As"))); +} + +void MainWindow::downloadFinished(KJob* job) +{ + if (job->error()) { + KMessageBox::error(this, job->errorString()); + fileName.clear(); + return; + } + + const KIO::StoredTransferJob* storedJob = qobject_cast(job); + if (storedJob) { + textArea->setPlainText(QTextStream(storedJob->data(), QIODevice::ReadOnly).readAll()); + } +} diff --git a/src/TextEditor/MainWindow.hh b/src/TextEditor/MainWindow.hh index d41414e..40609e4 100644 --- a/src/TextEditor/MainWindow.hh +++ b/src/TextEditor/MainWindow.hh @@ -3,14 +3,29 @@ #include #include +#include class MainWindow : public KXmlGuiWindow { + Q_OBJECT public: explicit MainWindow(QWidget* parent = nullptr); private: - KTextEdit* textArea; void setupActions(); + void saveFileToDisk(const QString& outputFileName); + void setupClearAction(); + +private Q_SLOTS: + void newFile(); + void openFile(); + void saveFile(); + void saveFileAs(); + + void downloadFinished(KJob *job); + +private: + KTextEdit* textArea; + QString fileName; }; #endif diff --git a/src/TextEditor/TextEditorUI.rc b/src/TextEditor/TextEditorUI.rc index 0a4837f..0e4433c 100644 --- a/src/TextEditor/TextEditorUI.rc +++ b/src/TextEditor/TextEditorUI.rc @@ -6,6 +6,10 @@ + + Another Meownu + + Main Toolbar diff --git a/src/TextEditor/main.cc b/src/TextEditor/main.cc index 0604d10..b3bbf30 100644 --- a/src/TextEditor/main.cc +++ b/src/TextEditor/main.cc @@ -23,6 +23,7 @@ int main(int argc, char* argv[]) // Copyright Statement i18n("2023")); + // Set the AboutData to the aboutData variable KAboutData::setApplicationData(aboutData); QCommandLineParser parser;