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 @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -40,25 +41,18 @@ 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); - KTextEditor::View *view() const { - return m_view; - } - - static bool noWindows() { - return winList.isEmpty(); - } - private: void setupActions(); @@ -113,7 +107,15 @@ //session management public: void restore(KConfig *, int); - static void restore(); + +public: + KTextEditor::MainWindow *mainWindow() { return &m_mainWindow; } + +public Q_SLOTS: + QWidget *window() { return this; } + QList views(); + KTextEditor::View *activeView() { return m_view; } + KTextEditor::View *activateView(KTextEditor::Document *document); private: void readProperties(const KConfigGroup &) override; @@ -129,19 +131,17 @@ KToggleAction *m_paShowStatusBar; QAction *m_closeAction; KActivities::ResourceInstance *m_activityResource; - - static QList docList; - static QList winList; + KWriteApplication *m_app; + KTextEditor::MainWindow m_mainWindow; public Q_SLOTS: void documentNameChanged(); - + protected: /** * Event filter for QApplication to handle mac os like file open */ bool eventFilter(QObject *obj, QEvent *event) override; }; #endif - diff --git a/kwrite/kwrite.cpp b/kwrite/kwrite.cpp --- a/kwrite/kwrite.cpp +++ b/kwrite/kwrite.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -56,16 +57,17 @@ #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) + , m_mainWindow(this) { if (!doc) { doc = KTextEditor::Editor::instance()->createDocument(nullptr); @@ -75,7 +77,7 @@ qobject_cast(doc)->setModifiedOnDiskWarning(true); } - docList.append(doc); + m_app->addDocument(doc); } m_view = doc->createView(this); @@ -105,8 +107,6 @@ readConfig(); - winList.append(this); - documentNameChanged(); show(); @@ -121,16 +121,15 @@ KWrite::~KWrite() { + m_app->removeWindow(this); 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; } @@ -243,7 +242,7 @@ void KWrite::slotNew() { - new KWrite(); + m_app->newWindow(); } void KWrite::slotOpen() @@ -261,7 +260,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); @@ -280,7 +279,7 @@ void KWrite::newView() { - new KWrite(m_view->document()); + m_app->newWindow(m_view->document()); } void KWrite::toggleMenuBar(bool showMessage) @@ -436,62 +435,15 @@ { 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); } void KWrite::saveGlobalProperties(KConfig *config) //save documents { - config->group("Number").writeEntry("NumberOfDocuments", docList.count()); - - for (int z = 1; z <= docList.count(); z++) { - QString buf = QStringLiteral("Document %1").arg(z); - KConfigGroup cg(config, buf); - KTextEditor::Document *doc = docList.at(z - 1); - doc->writeSessionConfig(cg); - } - - for (int z = 1; z <= winList.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); - } + m_app->saveProperties(config); } void KWrite::aboutEditor() @@ -552,9 +504,26 @@ slotOpen(foe->url()); return true; } - + /** * else: pass over to default implementation */ return KParts::MainWindow::eventFilter(obj, event); } + +QList KWrite::views() +{ + QList list; + list.append(m_view); + return list; +} + + +KTextEditor::View *KWrite::activateView(KTextEditor::Document *document) +{ + if (m_view->document() == document) { + return m_view; + } + + return nullptr; +} diff --git a/kwrite/kwriteapplication.h b/kwrite/kwriteapplication.h new file mode 100644 --- /dev/null +++ b/kwrite/kwriteapplication.h @@ -0,0 +1,63 @@ +/* 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 removeWindow(KWrite *kwrite) { m_kwrites.removeAll(kwrite); } + + bool noWindows() { return m_kwrites.isEmpty(); } + + KWrite *newWindow(KTextEditor::Document *doc = nullptr); + + void restore(); + void saveProperties(KConfig *config); + +public Q_SLOTS: + QList documents() { return m_documents; } + bool quit(); + KTextEditor::MainWindow *activeMainWindow(); + QList mainWindows(); + bool closeDocument(KTextEditor::Document *document); + +private: + KTextEditor::Application *m_application; + QList m_documents; + QList m_kwrites; +}; + +#endif diff --git a/kwrite/kwriteapplication.cpp b/kwrite/kwriteapplication.cpp new file mode 100644 --- /dev/null +++ b/kwrite/kwriteapplication.cpp @@ -0,0 +1,145 @@ +/* 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_kwrites.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); + } +} + +void KWriteApplication::saveProperties(KConfig* config) +{ + config->group("Number").writeEntry("NumberOfDocuments", m_documents.count()); + + for (int z = 1; z <= m_documents.count(); z++) { + QString buf = QStringLiteral("Document %1").arg(z); + KConfigGroup cg(config, buf); + KTextEditor::Document *doc = m_documents.at(z - 1); + doc->writeSessionConfig(cg); + } + + for (int z = 1; z <= m_kwrites.count(); z++) { + QString buf = QStringLiteral("Window %1").arg(z); + KConfigGroup cg(config, buf); + cg.writeEntry("DocumentNumber", m_documents.indexOf(m_kwrites.at(z - 1)->activeView()->document()) + 1); + } +} + +bool KWriteApplication::quit() +{ + QList copy(m_kwrites); + for(auto kwrite: copy) { + if (!kwrite->close()) { + return false; + } + + m_kwrites.removeAll(kwrite); + delete kwrite; + } + return true; +} + +KTextEditor::MainWindow *KWriteApplication::activeMainWindow() +{ + for(auto kwrite: m_kwrites) { + if (kwrite->isActiveWindow()) { + return kwrite->mainWindow(); + } + } + + return nullptr; +} + +QList KWriteApplication::mainWindows() +{ + QList windows; + for(auto kwrite: m_kwrites) { + windows.append(kwrite->mainWindow()); + } + + return windows; +} + +bool KWriteApplication::closeDocument(KTextEditor::Document* document) +{ + QList copy(m_kwrites); + for(auto kwrite: copy) { + if (kwrite->activeView()->document() == document) { + if (!kwrite->close()) { + return false; + } + m_kwrites.removeAll(kwrite); + delete kwrite; + } + } + + 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); @@ -231,7 +234,7 @@ text.append(line + QLatin1Char('\n')); } while (!line.isNull()); - KTextEditor::Document *doc = t->view()->document(); + KTextEditor::Document *doc = t->activeView()->document(); if (doc) { // remember codec in document, e.g. to show the right one if (codec) { @@ -241,8 +244,8 @@ } } - if (nav && t->view()) { - t->view()->setCursorPosition(KTextEditor::Cursor(line, column)); + if (nav && t->activeView()) { + t->activeView()->setCursorPosition(KTextEditor::Cursor(line, column)); } } else { int docs_opened = 0; @@ -257,16 +260,16 @@ if (noDir) { ++docs_opened; - KWrite *t = new KWrite(); + KWrite *t = kapp.newWindow(); if (codec) { - t->view()->document()->setEncoding(QString::fromLatin1(codec->name())); + t->activeView()->document()->setEncoding(QString::fromLatin1(codec->name())); } t->loadURL(info.url); if (info.cursor.isValid()) { - t->view()->setCursorPosition(info.cursor); + t->activeView()->setCursorPosition(info.cursor); } else if (info.url.hasQuery()) { QUrlQuery q(info.url); @@ -281,7 +284,7 @@ if (column > 0) column--; - t->view()->setCursorPosition(KTextEditor::Cursor(line, column)); + t->activeView()->setCursorPosition(KTextEditor::Cursor(line, column)); } } else { KMessageBox::sorry(nullptr, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", info.url.toString())); @@ -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(); } /**