File indexing completed on 2024-05-12 16:07:25
0001 /* This file is part of the TikZKit project 0002 * 0003 * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org> 0004 * Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org> 0005 * 0006 * This program is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU General Public License as published by 0008 * the Free Software Foundation, either version 2 or the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program; If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 #include "DocumentManager.h" 0020 0021 #include <tikz/ui/Editor.h> 0022 #include <tikz/ui/View.h> 0023 0024 #include <QUrl> 0025 0026 DocumentManager::DocumentManager(QObject *parent) 0027 : QObject(parent) 0028 { 0029 // make sure there is at least one document around 0030 createDocument(); 0031 } 0032 0033 DocumentManager::~DocumentManager() 0034 { 0035 qDeleteAll(m_documents); 0036 m_documents.clear(); 0037 } 0038 0039 tikz::ui::Document *DocumentManager::createDocument() 0040 { 0041 auto doc = tikz::ui::Editor::instance()->createDocument(this); 0042 0043 m_documents.append(doc); 0044 0045 // we have a new document, show it the world 0046 Q_EMIT documentCreated(doc); 0047 0048 // return our new document 0049 return doc; 0050 } 0051 0052 tikz::ui::Document *DocumentManager::findDocument(const QUrl &url) const 0053 { 0054 QUrl u(url.adjusted(QUrl::NormalizePathSegments)); 0055 0056 for (tikz::ui::Document * it : m_documents) { 0057 if (it->url() == u) { 0058 return it; 0059 } 0060 } 0061 0062 return nullptr; 0063 } 0064 0065 tikz::ui::Document *DocumentManager::openUrl(const QUrl &url) 0066 { 0067 // 0068 // create new document 0069 // 0070 QUrl u(url.adjusted(QUrl::NormalizePathSegments)); 0071 0072 // in case the url is already open, just return the existing document 0073 if (!u.isEmpty()) { 0074 auto doc = findDocument(u); 0075 if (doc) { 0076 return doc; 0077 } 0078 } 0079 0080 // special handling: if only one unmodified empty buffer in the list, 0081 // reuse this buffer and load the file 0082 tikz::ui::Document *doc = nullptr; 0083 if (m_documents.count() == 1 && m_documents.first()->isEmptyBuffer()) { 0084 doc = m_documents.first(); 0085 } else { 0086 doc = createDocument(); 0087 } 0088 0089 if (!u.isEmpty()) { 0090 doc->load(u); 0091 } 0092 0093 return doc; 0094 } 0095 0096 bool DocumentManager::closeDocument(tikz::ui::Document *doc, bool closeUrl) 0097 { 0098 doc->close(); 0099 0100 if (closeUrl) { 0101 // document will be deleted, soon 0102 Q_EMIT aboutToDeleteDocument(doc); 0103 0104 // really delete the document 0105 Q_ASSERT(m_documents.contains(doc)); 0106 delete m_documents.takeAt(m_documents.indexOf(doc)); 0107 0108 // document is gone, Q_EMIT our signals 0109 Q_EMIT documentDeleted(doc); 0110 } 0111 0112 /** 0113 * never ever empty the whole document list 0114 * do this before documentsDeleted is emited, to have no flicker 0115 */ 0116 if (m_documents.isEmpty()) { 0117 createDocument(); 0118 } 0119 0120 return true; 0121 } 0122 0123 0124 bool DocumentManager::closeAllDocuments(bool closeUrl) 0125 { 0126 for (auto doc : qAsConst(m_documents)) { 0127 closeDocument(doc, closeUrl); 0128 } 0129 return true; 0130 } 0131 0132 /** 0133 * Find all modified documents. 0134 * @return Return the list of all modified documents. 0135 */ 0136 QVector<tikz::ui::Document *> DocumentManager::modifiedDocumentList() 0137 { 0138 QVector<tikz::ui::Document *> modified; 0139 for (auto doc : qAsConst(m_documents)) { 0140 if (doc->isModified()) { 0141 modified.append(doc); 0142 } 0143 } 0144 return modified; 0145 } 0146 0147 bool DocumentManager::queryCloseDocuments(MainWindow * w) 0148 { 0149 #if 0 0150 int docCount = m_documents.count(); 0151 for (tikz::ui::Document * doc : qAsConst(m_documents)) { 0152 if (doc->url().isEmpty() && doc->isModified()) { 0153 int msgres = QMessageBox::warningYesNoCancel(w, 0154 tr("<p>The document '%1' has been modified, but not saved.</p>" 0155 "<p>Do you want to save your changes or discard them?</p>", doc->documentName()), 0156 tr("Close Document"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); 0157 0158 if (msgres == QMessageBox::Cancel) { 0159 return false; 0160 } 0161 0162 if (msgres == QMessageBox::Yes) { 0163 KEncodingFileDialog::Result r = KEncodingFileDialog::getSaveUrlAndEncoding(doc->encoding(), QUrl(), QString(), w, i18n("Save As")); 0164 0165 doc->setEncoding(r.encoding); 0166 0167 if (!r.URLs.isEmpty()) { 0168 QUrl tmp = r.URLs.first(); 0169 0170 if (!doc->saveAs(tmp)) { 0171 return false; 0172 } 0173 } else { 0174 return false; 0175 } 0176 } 0177 } else { 0178 if (!doc->queryClose()) { 0179 return false; 0180 } 0181 } 0182 } 0183 0184 // document count changed while queryClose, abort and notify user 0185 if (m_documents.count() > docCount) { 0186 KMessageBox::information(w, 0187 i18n("New file opened while trying to close Kate, closing aborted."), 0188 i18n("Closing Aborted")); 0189 return false; 0190 } 0191 #endif 0192 return true; 0193 } 0194 0195 void DocumentManager::saveAll() 0196 { 0197 for (tikz::ui::Document * doc : qAsConst(m_documents)) 0198 if (doc->isModified()) { 0199 doc->save(); 0200 } 0201 } 0202 0203 void DocumentManager::reloadAll() 0204 { 0205 // reload all docs that are NOT modified on disk 0206 for (tikz::ui::Document * doc : qAsConst(m_documents)) { 0207 doc->reload(); 0208 } 0209 } 0210 0211 // kate: indent-width 4; replace-tabs on;