Changeset View
Changeset View
Standalone View
Standalone View
kwrite/kwriteapplication.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> | ||||
3 | Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org> | ||||
4 | Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk> | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Library General Public | ||||
8 | License version 2 as published by the Free Software Foundation. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "kwriteapplication.h" | ||||
22 | #include "kwrite.h" | ||||
23 | | ||||
24 | #include <QApplication> | ||||
25 | #include <KConfigGui> | ||||
26 | | ||||
27 | KWriteApplication::KWriteApplication::KWriteApplication() | ||||
28 | { | ||||
29 | m_application = new KTextEditor::Application(this); | ||||
30 | KTextEditor::Editor::instance()->setApplication(m_application); | ||||
31 | } | ||||
32 | | ||||
33 | KWriteApplication::~KWriteApplication() | ||||
34 | { | ||||
35 | delete m_application; | ||||
36 | } | ||||
37 | | ||||
38 | KWrite * KWriteApplication::newWindow(KTextEditor::Document* doc) | ||||
39 | { | ||||
40 | KWrite *k = new KWrite(doc, this); | ||||
41 | m_kwrites.append(k); | ||||
42 | return k; | ||||
43 | } | ||||
44 | | ||||
45 | void KWriteApplication::restore() | ||||
46 | { | ||||
47 | KConfig *config = KConfigGui::sessionConfig(); | ||||
48 | | ||||
49 | if (!config) { | ||||
50 | return; | ||||
51 | } | ||||
52 | | ||||
53 | int docs, windows; | ||||
54 | QString buf; | ||||
55 | KTextEditor::Document *doc; | ||||
56 | KWrite *t; | ||||
57 | | ||||
58 | KConfigGroup numberConfig(config, "Number"); | ||||
59 | docs = numberConfig.readEntry("NumberOfDocuments", 0); | ||||
60 | windows = numberConfig.readEntry("NumberOfWindows", 0); | ||||
61 | | ||||
62 | for (int z = 1; z <= docs; z++) { | ||||
63 | buf = QStringLiteral("Document %1").arg(z); | ||||
64 | KConfigGroup cg(config, buf); | ||||
65 | doc = KTextEditor::Editor::instance()->createDocument(nullptr); | ||||
66 | doc->readSessionConfig(cg); | ||||
67 | addDocument(doc); | ||||
68 | } | ||||
69 | | ||||
70 | for (int z = 1; z <= windows; z++) { | ||||
71 | buf = QStringLiteral("Window %1").arg(z); | ||||
72 | KConfigGroup cg(config, buf); | ||||
73 | t = newWindow(m_documents.at(cg.readEntry("DocumentNumber", 0) - 1)); | ||||
74 | t->restore(config, z); | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | void KWriteApplication::saveProperties(KConfig* config) | ||||
79 | { | ||||
80 | config->group("Number").writeEntry("NumberOfDocuments", m_documents.count()); | ||||
81 | | ||||
meven: Unused I believe. | |||||
82 | for (int z = 1; z <= m_documents.count(); z++) { | ||||
Avoid foreach, you can use for future proofing : for (KWrite *w: qAsConst(m_windows)) { Reference: meven: Avoid foreach, you can use for future proofing :
```
for (KWrite *w: qAsConst(m_windows)) {… | |||||
83 | QString buf = QStringLiteral("Document %1").arg(z); | ||||
84 | KConfigGroup cg(config, buf); | ||||
85 | KTextEditor::Document *doc = m_documents.at(z - 1); | ||||
86 | doc->writeSessionConfig(cg); | ||||
anthonyfieroni: don't removed in same loop, just call clear afterwards. | |||||
87 | } | ||||
88 | | ||||
89 | for (int z = 1; z <= m_kwrites.count(); z++) { | ||||
90 | QString buf = QStringLiteral("Window %1").arg(z); | ||||
91 | KConfigGroup cg(config, buf); | ||||
92 | cg.writeEntry("DocumentNumber", m_documents.indexOf(m_kwrites.at(z - 1)->activeView()->document()) + 1); | ||||
93 | } | ||||
94 | } | ||||
95 | | ||||
96 | bool KWriteApplication::quit() | ||||
97 | { | ||||
98 | QList<KWrite *> copy(m_kwrites); | ||||
99 | for(auto kwrite: copy) { | ||||
100 | if (!kwrite->close()) { | ||||
101 | return false; | ||||
102 | } | ||||
103 | | ||||
104 | m_kwrites.removeAll(kwrite); | ||||
105 | delete kwrite; | ||||
106 | } | ||||
107 | return true; | ||||
108 | } | ||||
109 | | ||||
110 | KTextEditor::MainWindow *KWriteApplication::activeMainWindow() | ||||
111 | { | ||||
112 | for(auto kwrite: m_kwrites) { | ||||
113 | if (kwrite->isActiveWindow()) { | ||||
114 | return kwrite->mainWindow(); | ||||
115 | } | ||||
116 | } | ||||
117 | | ||||
118 | return nullptr; | ||||
119 | } | ||||
120 | | ||||
121 | QList<KTextEditor::MainWindow *> KWriteApplication::mainWindows() | ||||
122 | { | ||||
123 | QList<KTextEditor::MainWindow *> windows; | ||||
124 | for(auto kwrite: m_kwrites) { | ||||
125 | windows.append(kwrite->mainWindow()); | ||||
126 | } | ||||
127 | | ||||
128 | return windows; | ||||
129 | } | ||||
130 | | ||||
131 | bool KWriteApplication::closeDocument(KTextEditor::Document* document) | ||||
132 | { | ||||
133 | QList<KWrite *> copy(m_kwrites); | ||||
134 | for(auto kwrite: copy) { | ||||
135 | if (kwrite->activeView()->document() == document) { | ||||
136 | if (!kwrite->close()) { | ||||
137 | return false; | ||||
138 | } | ||||
139 | m_kwrites.removeAll(kwrite); | ||||
140 | delete kwrite; | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
144 | return true; | ||||
145 | } |
Unused I believe.