Skip to content
Extraits de code Groupes Projets
Vérifiée Valider f7865381 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

DOCUMENT: The document store should not be in charge of constructing and caching the document views

parent 8510caae
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!3Add documents
......@@ -3,14 +3,6 @@
#include <stdexcept>
VivyDocumentStore::~VivyDocumentStore() noexcept
{
for (VivyDocumentView *ptr : documentsViews)
delete ptr;
documents.clear();
documentsViews.clear();
}
std::weak_ptr<VivyDocument>
VivyDocumentStore::loadDocument(const QString &file)
{
......@@ -49,8 +41,6 @@ VivyDocumentStore::closeDocument(const QString &name) noexcept
{
qDebug() << "Store is closing the document " << name;
documents.remove(name);
if (VivyDocumentView *__restrict docView = documentsViews.take(name))
delete docView;
}
std::weak_ptr<VivyDocument>
......@@ -62,15 +52,3 @@ VivyDocumentStore::getDocument(const QString &name) const
}
return std::weak_ptr<VivyDocument>{documents.value(name)};
}
VivyDocumentView *
VivyDocumentStore::getDocumentView(const QString &name) const
{
if (!documents.contains(name)) {
qCritical() << "Can't get a view to a non-existant document " << name;
throw std::runtime_error("Can't find the document view");
}
VivyDocumentView *ret = new VivyDocumentView(documents.value(name));
documentsViews[name] = ret;
return ret;
}
......@@ -11,7 +11,7 @@
class VivyDocumentStore final {
public:
explicit VivyDocumentStore() noexcept = default;
~VivyDocumentStore() noexcept;
~VivyDocumentStore() noexcept = default;
/* Don't move this object around */
VivyDocumentStore(const VivyDocumentStore &) = delete; // Copy
......@@ -32,11 +32,9 @@ public:
/* Get stored documents */
std::weak_ptr<VivyDocument> getDocument(const QString &name) const;
[[nodiscard("raw-pointer")]] VivyDocumentView* getDocumentView(const QString &name) const;
private:
QMap<QString, std::shared_ptr<VivyDocument>> documents;
mutable QMap<QString, VivyDocumentView*> documentsViews;
};
#endif // VIVY_DOCUMENTSTORE_H
......@@ -4,8 +4,8 @@
#include <QVBoxLayout>
VivyDocumentView::VivyDocumentView(std::weak_ptr<VivyDocument> docWeak) noexcept
: document(docWeak)
VivyDocumentView::VivyDocumentView(std::weak_ptr<VivyDocument> docWeak, QWidget *parent) noexcept
: QWidget(parent), document(docWeak)
{
if (auto doc = document.lock()) {
AudioDocument *const audioDocument = doc->getAudioSubDocument();
......
......@@ -14,7 +14,7 @@ class VivyDocumentView final : public QWidget {
Q_OBJECT
public:
explicit VivyDocumentView(std::weak_ptr<VivyDocument>) noexcept;
explicit VivyDocumentView(std::weak_ptr<VivyDocument>, QWidget *parent = nullptr) noexcept;
std::weak_ptr<VivyDocument> getDocument() const noexcept;
......
......@@ -59,8 +59,6 @@ MainWindow::closeDocument(int index) noexcept
const QWidget *widgetToClose = documents->widget(index);
const VivyDocumentView *documentToClose = reinterpret_cast<const VivyDocumentView *>(widgetToClose);
documents->removeTab(index);
if (auto doc = documentToClose->getDocument().lock()) {
const QString documentName = doc->getName();
documentStore.closeDocument(documentName);
......@@ -68,6 +66,8 @@ MainWindow::closeDocument(int index) noexcept
} else {
qCritical() << "The document at index" << index << "was already deleted from the store";
}
documents->removeTab(index);
}
void
......@@ -79,31 +79,25 @@ MainWindow::openDocument() noexcept
return;
}
qDebug() << "Trying to open file" << filename;
QFileInfo fileInfo(filename);
const QString baseName = fileInfo.baseName();
if (documentStore.isDocumentPresent(baseName)) {
qWarning() << "The document" << baseName << "is already loaded";
return;
}
qDebug() << "Trying to open file" << filename;
std::weak_ptr<VivyDocument> documentWeak = documentStore.loadDocument(filename);
std::weak_ptr<VivyDocument> document = documentStore.loadDocument(filename);
if (auto document = documentWeak.lock()) {
qDebug() << "Document" << filename << "was opened successfully, getting its view";
try {
VivyDocumentView *documentView = documentStore.getDocumentView(document->getName());
VivyDocumentView *documentView = new VivyDocumentView(document);
qDebug() << "View constructed successfully";
documents->addTab(documentView, document->getName());
documents->addTab(documentView, document.lock()->getName());
} catch (const std::runtime_error &e) {
qCritical() << "Failed to create the document view for" << document->getName();
qCritical() << "Failed to create the document view for" << baseName << "with path" << filename;
throw;
}
} else {
qCritical() << "Failed to load the document" << filename << "!";
return;
}
}
void
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter