diff --git a/src/Document/VivyDocumentStore.cc b/src/Document/VivyDocumentStore.cc index badaa983bd75551d404f2fec9b0ebeb47e74f1a8..92342328c585151f8baea9f95b1246025a6ebc58 100644 --- a/src/Document/VivyDocumentStore.cc +++ b/src/Document/VivyDocumentStore.cc @@ -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; -} diff --git a/src/Document/VivyDocumentStore.hh b/src/Document/VivyDocumentStore.hh index de00b9a432c9e10126300056e4530bf87e262675..a4965c27b6342ecf818cf24964e39465759b6537 100644 --- a/src/Document/VivyDocumentStore.hh +++ b/src/Document/VivyDocumentStore.hh @@ -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 diff --git a/src/Document/VivyDocumentView.cc b/src/Document/VivyDocumentView.cc index e21845316b62844eacb838fbcb1af602ae0e199b..2a459ab568a4bd378e3daee57d6180c5c3c1245f 100644 --- a/src/Document/VivyDocumentView.cc +++ b/src/Document/VivyDocumentView.cc @@ -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(); diff --git a/src/Document/VivyDocumentView.hh b/src/Document/VivyDocumentView.hh index 27dc2e8838198d4950843478c5a0d2af6efd3e13..266d600489050c135fe49ba20c752707361658c4 100644 --- a/src/Document/VivyDocumentView.hh +++ b/src/Document/VivyDocumentView.hh @@ -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; diff --git a/src/MainWindow.cc b/src/MainWindow.cc index 5c9b4bffe2e3be0528f8b978dd677a2942c2b7a6..4641010cb65ce5107074d0bd120527668ade1249 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc @@ -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); - - if (auto document = documentWeak.lock()) { - qDebug() << "Document" << filename << "was opened successfully, getting its view"; - try { - VivyDocumentView *documentView = documentStore.getDocumentView(document->getName()); - qDebug() << "View constructed successfully"; - documents->addTab(documentView, document->getName()); - } catch (const std::runtime_error &e) { - qCritical() << "Failed to create the document view for" << document->getName(); - throw; - } - } else { - qCritical() << "Failed to load the document" << filename << "!"; - return; - } + std::weak_ptr<VivyDocument> document = documentStore.loadDocument(filename); + try { + VivyDocumentView *documentView = new VivyDocumentView(document); + qDebug() << "View constructed successfully"; + documents->addTab(documentView, document.lock()->getName()); + } catch (const std::runtime_error &e) { + qCritical() << "Failed to create the document view for" << baseName << "with path" << filename; + throw; + } } void