Commit e129f5d4 authored by Michal Humpula's avatar Michal Humpula Committed by Michal Humpula

kwrite: implement kTextEditor::Application,MainWindow

BUG: 392858
Differential Revision: https://phabricator.kde.org/D21409
parent 967e5b01
......@@ -19,7 +19,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/icons/sc-apps-kwrite.svgz
)
# 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
......
......@@ -24,6 +24,7 @@
#include <ktexteditor/view.h>
#include <ktexteditor/modificationinterface.h>
#include <ktexteditor/editor.h>
#include <ktexteditor/application.h>
#include <KAboutApplicationDialog>
#include <KActionCollection>
......@@ -56,16 +57,17 @@
#include <QFileDialog>
#include <QFileOpenEvent>
QList<KTextEditor::Document *> KWrite::docList;
QList<KWrite *> 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 @@ KWrite::KWrite(KTextEditor::Document *doc)
qobject_cast<KTextEditor::ModificationInterface *>(doc)->setModifiedOnDiskWarning(true);
}
docList.append(doc);
m_app->addDocument(doc);
}
m_view = doc->createView(this);
......@@ -105,8 +107,6 @@ KWrite::KWrite(KTextEditor::Document *doc)
readConfig();
winList.append(this);
documentNameChanged();
show();
......@@ -121,16 +121,15 @@ KWrite::KWrite(KTextEditor::Document *doc)
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::modifiedChanged()
void KWrite::slotNew()
{
new KWrite();
m_app->newWindow();
}
void KWrite::slotOpen()
......@@ -261,7 +260,7 @@ void KWrite::slotOpen(const QUrl &url)
}
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::urlChanged()
void KWrite::newView()
{
new KWrite(m_view->document());
m_app->newWindow(m_view->document());
}
void KWrite::toggleMenuBar(bool showMessage)
......@@ -436,7 +435,7 @@ void KWrite::saveProperties(KConfigGroup &config)
{
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);
......@@ -444,54 +443,7 @@ void KWrite::saveProperties(KConfigGroup &config)
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 @@ bool KWrite::eventFilter(QObject *obj, QEvent *event)
slotOpen(foe->url());
return true;
}
/**
* else: pass over to default implementation
*/
return KParts::MainWindow::eventFilter(obj, event);
}
QList<KTextEditor::View *> KWrite::views()
{
QList<KTextEditor::View *> list;
list.append(m_view);
return list;
}
KTextEditor::View *KWrite::activateView(KTextEditor::Document *document)
{
if (m_view->document() == document) {
return m_view;
}
return nullptr;
}
......@@ -23,6 +23,7 @@
#include <ktexteditor/view.h>
#include <ktexteditor/document.h>
#include <ktexteditor/mainwindow.h>
#include <KParts/MainWindow>
#include <KConfigGroup>
......@@ -40,25 +41,18 @@ namespace KActivities
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 @@ public:
//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<KTextEditor::View *> views();
KTextEditor::View *activeView() { return m_view; }
KTextEditor::View *activateView(KTextEditor::Document *document);
private:
void readProperties(const KConfigGroup &) override;
......@@ -129,13 +131,12 @@ private:
KToggleAction *m_paShowStatusBar;
QAction *m_closeAction;
KActivities::ResourceInstance *m_activityResource;
static QList<KTextEditor::Document *> docList;
static QList<KWrite *> 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
......@@ -144,4 +145,3 @@ protected:
};
#endif
/* This file is part of the KDE project
Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
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 <QApplication>
#include <KConfigGui>
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<KWrite *> 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<KTextEditor::MainWindow *> KWriteApplication::mainWindows()
{
QList<KTextEditor::MainWindow *> windows;
for(auto kwrite: m_kwrites) {
windows.append(kwrite->mainWindow());
}
return windows;
}
bool KWriteApplication::closeDocument(KTextEditor::Document* document)
{
QList<KWrite *> 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;
}
/* This file is part of the KDE project
Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
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 <ktexteditor/view.h>
#include <ktexteditor/document.h>
#include <ktexteditor/application.h>
#include <ktexteditor/editor.h>
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<KTextEditor::Document *> documents() { return m_documents; }
bool quit();
KTextEditor::MainWindow *activeMainWindow();
QList<KTextEditor::MainWindow *> mainWindows();
bool closeDocument(KTextEditor::Document *document);
private:
KTextEditor::Application *m_application;
QList<KTextEditor::Document *> m_documents;
QList<KWrite *> m_kwrites;
};
#endif
......@@ -19,6 +19,7 @@
*/
#include "kwrite.h"
#include "kwriteapplication.h"
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
......@@ -194,8 +195,10 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
*/
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 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
}
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 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
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 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
}
}
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 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
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 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
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 @@ extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
// no window there, uh, ohh, for example borked session config !!!
// create at least one !!
if (KWrite::noWindows()) {
new KWrite();
if (kapp.noWindows()) {
kapp.newWindow();
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment