diff --git a/kwrite/CMakeLists.txt b/kwrite/CMakeLists.txt --- a/kwrite/CMakeLists.txt +++ b/kwrite/CMakeLists.txt @@ -19,7 +19,7 @@ ) # collect the sources -set (KWRITE_APPLICATION_SRCS main.cpp kwrite.cpp) +set (KWRITE_APPLICATION_SRCS main.cpp kwrite.cpp kwriteapplication.cpp) qt5_add_resources(KWRITE_APPLICATION_SRCS data/kwrite.qrc) # add icons to application sources, to have them bundled diff --git a/kwrite/kwrite.h b/kwrite/kwrite.h --- a/kwrite/kwrite.h +++ b/kwrite/kwrite.h @@ -37,20 +37,17 @@ class ResourceInstance; } -namespace KTextEditor { - class Application; -} - class KToggleAction; class KRecentFilesAction; class KSqueezedTextLabel; +class KWriteApplication; class KWrite : public KParts::MainWindow { Q_OBJECT public: - KWrite(KTextEditor::Document * = nullptr); + KWrite(KTextEditor::Document * = nullptr, KWriteApplication *app = nullptr); ~KWrite() override; void loadURL(const QUrl &url); @@ -59,10 +56,6 @@ return m_view; } - static bool noWindows() { - return winList.isEmpty(); - } - private: void setupActions(); @@ -117,7 +110,6 @@ //session management public: void restore(KConfig *, int); - static void restore(); private: void readProperties(const KConfigGroup &) override; @@ -133,20 +125,11 @@ KToggleAction *m_paShowStatusBar; QAction *m_closeAction; KActivities::ResourceInstance *m_activityResource; - - static QList docList; - static QList winList; - KTextEditor::Application *m_application; + KWriteApplication *m_app; public Q_SLOTS: void documentNameChanged(); - /** - * KTextEditor::Application extensions - */ - bool quit(); - QList documents() { return docList; } - protected: /** * Event filter for QApplication to handle mac os like file open @@ -155,4 +138,3 @@ }; #endif - diff --git a/kwrite/kwrite.cpp b/kwrite/kwrite.cpp --- a/kwrite/kwrite.cpp +++ b/kwrite/kwrite.cpp @@ -57,16 +57,16 @@ #include #include -QList KWrite::docList; -QList KWrite::winList; +#include "kwriteapplication.h" -KWrite::KWrite(KTextEditor::Document *doc) +KWrite::KWrite(KTextEditor::Document *doc, KWriteApplication *app) : m_view(nullptr) , m_recentFiles(nullptr) , m_paShowPath(nullptr) , m_paShowMenuBar(nullptr) , m_paShowStatusBar(nullptr) , m_activityResource(nullptr) + , m_app(app) { if (!doc) { doc = KTextEditor::Editor::instance()->createDocument(nullptr); @@ -76,12 +76,9 @@ qobject_cast(doc)->setModifiedOnDiskWarning(true); } - docList.append(doc); + m_app->addDocument(doc); } - m_application = new KTextEditor::Application(this); - KTextEditor::Editor::instance()->setApplication(m_application); - m_view = doc->createView(this); setCentralWidget(m_view); @@ -109,8 +106,6 @@ readConfig(); - winList.append(this); - documentNameChanged(); show(); @@ -127,14 +122,12 @@ { guiFactory()->removeClient(m_view); - winList.removeAll(this); - KTextEditor::Document *doc = m_view->document(); delete m_view; // kill document, if last view is closed if (doc->views().isEmpty()) { - docList.removeAll(doc); + m_app->removeDocument(doc); delete doc; } @@ -247,7 +240,7 @@ void KWrite::slotNew() { - new KWrite(); + m_app->newWindow(); } void KWrite::slotOpen() @@ -265,7 +258,7 @@ } if (m_view->document()->isModified() || !m_view->document()->url().isEmpty()) { - KWrite *t = new KWrite(); + KWrite *t = m_app->newWindow(); t->loadURL(url); } else { loadURL(url); @@ -284,7 +277,7 @@ void KWrite::newView() { - new KWrite(m_view->document()); + m_app->newWindow(m_view->document()); } void KWrite::toggleMenuBar(bool showMessage) @@ -440,7 +433,7 @@ { writeConfig(); - config.writeEntry("DocumentNumber", docList.indexOf(m_view->document()) + 1); + config.writeEntry("DocumentNumber", m_app->documents().indexOf(m_view->document()) + 1); KConfigGroup cg(&config, QStringLiteral("General Options")); m_view->writeSessionConfig(cg); @@ -448,53 +441,19 @@ void KWrite::saveGlobalProperties(KConfig *config) //save documents { - config->group("Number").writeEntry("NumberOfDocuments", docList.count()); + config->group("Number").writeEntry("NumberOfDocuments", m_app->documents().count()); - for (int z = 1; z <= docList.count(); z++) { + for (int z = 1; z <= m_app->documents().count(); z++) { QString buf = QStringLiteral("Document %1").arg(z); KConfigGroup cg(config, buf); - KTextEditor::Document *doc = docList.at(z - 1); + KTextEditor::Document *doc = m_app->documents().at(z - 1); doc->writeSessionConfig(cg); } - for (int z = 1; z <= winList.count(); z++) { + for (int z = 1; z <= m_app->windows().count(); z++) { QString buf = QStringLiteral("Window %1").arg(z); KConfigGroup cg(config, buf); - cg.writeEntry("DocumentNumber", docList.indexOf(winList.at(z - 1)->view()->document()) + 1); - } -} - -//restore session -void KWrite::restore() -{ - KConfig *config = KConfigGui::sessionConfig(); - - if (!config) { - return; - } - - int docs, windows; - QString buf; - KTextEditor::Document *doc; - KWrite *t; - - KConfigGroup numberConfig(config, "Number"); - docs = numberConfig.readEntry("NumberOfDocuments", 0); - windows = numberConfig.readEntry("NumberOfWindows", 0); - - for (int z = 1; z <= docs; z++) { - buf = QStringLiteral("Document %1").arg(z); - KConfigGroup cg(config, buf); - doc = KTextEditor::Editor::instance()->createDocument(nullptr); - doc->readSessionConfig(cg); - docList.append(doc); - } - - for (int z = 1; z <= windows; z++) { - buf = QStringLiteral("Window %1").arg(z); - KConfigGroup cg(config, buf); - t = new KWrite(docList.at(cg.readEntry("DocumentNumber", 0) - 1)); - t->restore(config, z); + cg.writeEntry("DocumentNumber", m_app->documents().indexOf(m_app->windows().at(z - 1)->view()->document()) + 1); } } @@ -562,8 +521,3 @@ */ return KParts::MainWindow::eventFilter(obj, event); } - -bool KWrite::quit() -{ - return close(); -} diff --git a/kwrite/kwriteapplication.h b/kwrite/kwriteapplication.h new file mode 100644 --- /dev/null +++ b/kwrite/kwriteapplication.h @@ -0,0 +1,62 @@ +/* This file is part of the KDE project + Copyright (C) 2001 Christoph Cullmann + Copyright (C) 2001 Joseph Wenninger + Copyright (C) 2001 Anders Lund + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KWRITE_APPLICATION_H +#define KWRITE_APPLICATION_H + +#include +#include +#include +#include + +class KWrite; + +class KWriteApplication : public QObject +{ + Q_OBJECT + +public: + KWriteApplication(); + ~KWriteApplication(); + + void addDocument(KTextEditor::Document *doc) { m_documents.append(doc); } + void removeDocument(KTextEditor::Document *doc) { m_documents.removeAll(doc); } + void addWindow(KWrite *window) { m_windows.append(window); } + void removeWindow(KWrite *window) { m_windows.removeAll(window); } + + QList windows() { return m_windows; } + + bool noWindows() { return m_windows.isEmpty(); } + + KWrite *newWindow(KTextEditor::Document *doc = nullptr); + void restore(); + +public Q_SLOTS: + QList documents() { return m_documents; } + bool quit(); + KTextEditor::MainWindow *activeMainWindow() { return nullptr; } + +private: + KTextEditor::Application *m_application; + QList m_documents; + QList m_windows; +}; + +#endif diff --git a/kwrite/kwriteapplication.cpp b/kwrite/kwriteapplication.cpp new file mode 100644 --- /dev/null +++ b/kwrite/kwriteapplication.cpp @@ -0,0 +1,90 @@ +/* This file is part of the KDE project + Copyright (C) 2001 Christoph Cullmann + Copyright (C) 2001 Joseph Wenninger + Copyright (C) 2001 Anders Lund + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kwriteapplication.h" +#include "kwrite.h" + +#include +#include + +KWriteApplication::KWriteApplication::KWriteApplication() +{ + m_application = new KTextEditor::Application(this); + KTextEditor::Editor::instance()->setApplication(m_application); +} + +KWriteApplication::~KWriteApplication() +{ + delete m_application; +} + +KWrite * KWriteApplication::newWindow(KTextEditor::Document* doc) +{ + KWrite *k = new KWrite(doc, this); + m_windows.append(k); + return k; +} + +void KWriteApplication::restore() +{ + KConfig *config = KConfigGui::sessionConfig(); + + if (!config) { + return; + } + + int docs, windows; + QString buf; + KTextEditor::Document *doc; + KWrite *t; + + KConfigGroup numberConfig(config, "Number"); + docs = numberConfig.readEntry("NumberOfDocuments", 0); + windows = numberConfig.readEntry("NumberOfWindows", 0); + + for (int z = 1; z <= docs; z++) { + buf = QStringLiteral("Document %1").arg(z); + KConfigGroup cg(config, buf); + doc = KTextEditor::Editor::instance()->createDocument(nullptr); + doc->readSessionConfig(cg); + addDocument(doc); + } + + for (int z = 1; z <= windows; z++) { + buf = QStringLiteral("Window %1").arg(z); + KConfigGroup cg(config, buf); + t = newWindow(m_documents.at(cg.readEntry("DocumentNumber", 0) - 1)); + t->restore(config, z); + } +} + +bool KWriteApplication::quit() +{ + QList::iterator i; + foreach(KWrite *w, m_windows) { + if (!w->close()) { + return false; + } + m_windows.removeAll(w); + delete w; + } + QApplication::quit(); + return true; +} diff --git a/kwrite/main.cpp b/kwrite/main.cpp --- a/kwrite/main.cpp +++ b/kwrite/main.cpp @@ -19,6 +19,7 @@ */ #include "kwrite.h" +#include "kwriteapplication.h" #include #include @@ -194,8 +195,10 @@ */ aboutData.processCommandLine(&parser); + KWriteApplication kapp; + if (app.isSessionRestored()) { - KWrite::restore(); + kapp.restore(); } else { bool nav = false; int line = 0, column = 0; @@ -213,7 +216,7 @@ } if (parser.positionalArguments().count() == 0) { - KWrite *t = new KWrite; + KWrite *t = kapp.newWindow(); if (parser.isSet(QStringLiteral("stdin"))) { QTextStream input(stdin, QIODevice::ReadOnly); @@ -257,7 +260,7 @@ if (noDir) { ++docs_opened; - KWrite *t = new KWrite(); + KWrite *t = kapp.newWindow(); if (codec) { t->view()->document()->setEncoding(QString::fromLatin1(codec->name())); @@ -295,8 +298,8 @@ // no window there, uh, ohh, for example borked session config !!! // create at least one !! - if (KWrite::noWindows()) { - new KWrite(); + if (kapp.noWindows()) { + kapp.newWindow(); } /**