diff --git a/src/Document/VivyDocument.cc b/src/Document/VivyDocument.cc index 60df47cba9a1ed7fe506db88f7589856078a60cf..1d077b833ecbfae2fe4bd39fd9847605f9d8d8e2 100644 --- a/src/Document/VivyDocument.cc +++ b/src/Document/VivyDocument.cc @@ -149,12 +149,6 @@ VivyDocument::fromPath(const QString &path) noexcept return ret; } -VivyDocument * -VivyDocument::newEmpty([[maybe_unused]] const QString &name) noexcept -{ - return nullptr; -} - QString VivyDocument::getName() const noexcept { diff --git a/src/Document/VivyDocument.hh b/src/Document/VivyDocument.hh index cad06e31827225c54c3e14b3c44a4529e04d1b5c..970610d5614e5ae4c03bd1ef7a2db291204d91cf 100644 --- a/src/Document/VivyDocument.hh +++ b/src/Document/VivyDocument.hh @@ -35,14 +35,12 @@ private: std::unique_ptr<VideoDocument> videoDocument{}; std::unique_ptr<AssDocument> assDocument{}; - /* Create an empty document */ - explicit VivyDocument(const QString &name); - static bool detectDocumentType(const QFileInfo &file, SubDocument *type); public: + /* Create an empty document */ + explicit VivyDocument(const QString &name); [[nodiscard("allocated")]] static VivyDocument *fromPath(const QString &path) noexcept; - [[nodiscard("allocated")]] static VivyDocument *newEmpty(const QString &name) noexcept; bool rename(const QString &) noexcept; bool loadSubDocument(const QString &) noexcept; diff --git a/src/Document/VivyDocumentStore.cc b/src/Document/VivyDocumentStore.cc index 361a4b4c2ff44e6bc29c4d2330ea19ba182fc385..d7f6946c4cdabfb3e9c2ea668618570944548bc2 100644 --- a/src/Document/VivyDocumentStore.cc +++ b/src/Document/VivyDocumentStore.cc @@ -15,7 +15,7 @@ VivyDocumentStore::loadDocument(const QString &file) } if (VivyDocument *ret = VivyDocument::fromPath(file)) { - qDebug() << "Register ument " << baseName; + qDebug() << "Register document " << baseName; documents[baseName] = std::shared_ptr<VivyDocument>(ret); return std::weak_ptr{ documents[baseName] }; } else { @@ -30,10 +30,28 @@ VivyDocumentStore::isDocumentPresent(const QString &name) noexcept return documents.count(name) >= 1; } + +#define NEW_DOCUMENT_BASENAME "Untitled " std::weak_ptr<VivyDocument> -VivyDocumentStore::newDocument(const QString & /* name */) +VivyDocumentStore::newDocument(QString &name) { - throw std::runtime_error("Not implemented"); + uint docNumber{1}; + QString newDocName; + + do { + newDocName = NEW_DOCUMENT_BASENAME + QString::number(docNumber); + docNumber++; + } while (documents.contains(newDocName)); + + name = newDocName; + if (auto ret = std::make_shared<VivyDocument>(newDocName)) { + qDebug() << "Create new document " << newDocName; + documents[newDocName] = ret; + return std::weak_ptr{ documents[newDocName] }; + } else { + qDebug() << "Failed to create new document " << newDocName; + throw std::runtime_error("Failed to create the document"); + } } void diff --git a/src/Document/VivyDocumentStore.hh b/src/Document/VivyDocumentStore.hh index ea419b7858c1d0bb0327db3726df8490f6a0017b..e4495ab65dca3902921957314213926bf8217bb0 100644 --- a/src/Document/VivyDocumentStore.hh +++ b/src/Document/VivyDocumentStore.hh @@ -20,7 +20,7 @@ public: /* Create/load documents */ std::weak_ptr<VivyDocument> loadDocument(const QString &file); - std::weak_ptr<VivyDocument> newDocument(const QString &name); + std::weak_ptr<VivyDocument> newDocument(QString &name); /* Get to see if a document is already present or not */ [[nodiscard("handle-it")]] bool isDocumentPresent(const QString &name) noexcept; diff --git a/src/MainWindow.cc b/src/MainWindow.cc index f028b208e99c3e1b8a4eebb4733fc256bb9febed..361299f336baebd44ecd1c9eae96e9810296a8ca 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc @@ -86,6 +86,22 @@ MainWindow::closeDocument(int index) noexcept documents->removeTab(index); } +void +MainWindow::newDocument() noexcept +{ + QString baseName; + std::weak_ptr<VivyDocument> document = vivyApp->documentStore.newDocument(baseName); + + 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 the new document " << baseName; + throw; + } +} + void MainWindow::openDocument() noexcept { diff --git a/src/MainWindow.hh b/src/MainWindow.hh index e826c319fac92f456cfcd61a6a8c350cd8e215eb..32c7ae4609fa60636e538730d7c53d140bae4281 100644 --- a/src/MainWindow.hh +++ b/src/MainWindow.hh @@ -26,6 +26,7 @@ public: std::weak_ptr<VivyDocument> getCurrentDocument() const; private slots: + void newDocument() noexcept; void openDocument() noexcept; void closeDocument(int index) noexcept; diff --git a/src/MainWindow.xdef b/src/MainWindow.xdef index a1354835c9dc41ac872437d8ad0f8b42279a78e1..9275efae5bb65947c03e8b88e35d892df8834054 100644 --- a/src/MainWindow.xdef +++ b/src/MainWindow.xdef @@ -45,16 +45,19 @@ MAIN_WINDOW_MENU(help, "&Help") MAIN_WINDOW_TOOLBAR(file, "File") MAIN_WINDOW_TOOLBAR(other, "Other") -MAIN_WINDOW_ACTION_ICON(openDocument, "&Open document", "Open a a document", file, "document-open") +MAIN_WINDOW_ACTION_ICON(newDocument, "&New document", "Create a new document", file, "document-new") +MAIN_WINDOW_ACTION_ICON(openDocument, "&Open document", "Open a document", file, "document-open") MAIN_WINDOW_ACTION_ICON(saveFile, "&Save file", "Save the current document", file, "document-save") MAIN_WINDOW_ACTION_ICON(saveFileAs, "&Save file as", "Save the current document as", file, "document-save-as") MAIN_WINDOW_ACTION_ICON(openDialogHelp, "&About", "Open the help dialog", help, "help-about") +MAIN_WINDOW_TOOLBAR_ADD_ACTION(file, newDocument) MAIN_WINDOW_TOOLBAR_ADD_ACTION(file, openDocument) MAIN_WINDOW_TOOLBAR_ADD_ACTION(file, saveFile) MAIN_WINDOW_TOOLBAR_ADD_ACTION(file, saveFileAs) MAIN_WINDOW_TOOLBAR_ADD_ACTION(other, openDialogHelp) +MAIN_WINDOW_ACTION_ADD_SHORTCUT(newDocument, QKeySequence::New) MAIN_WINDOW_ACTION_ADD_SHORTCUT(openDocument, QKeySequence::Open) MAIN_WINDOW_ACTION_ADD_SHORTCUT(saveFile, QKeySequence::Save) diff --git a/src/VivyDocumentView.cc b/src/VivyDocumentView.cc index 45cb9f7dca6a950d3ff47a8338c0c34eac247e2c..b11df389d6b6f8e5d494882e04215da2e7723371 100644 --- a/src/VivyDocumentView.cc +++ b/src/VivyDocumentView.cc @@ -9,17 +9,22 @@ VivyDocumentView::VivyDocumentView(std::weak_ptr<VivyDocument> docWeak, QWidget , document(docWeak) { if (auto doc = document.lock()) { - AudioDocument *const audioDocument = doc->getAudioSubDocument(); - qDebug() << "Create an audio vizualizer for the audio sub document " << audioDocument->getFilePath(); - if (audioDocument != nullptr) { - visualizer = AudioVisualizer::fromFile(audioDocument->getFilePath()); - assert(visualizer != nullptr); + int capabilities = doc->getDocumentCapabilities(); + + if (capabilities & VivyDocument::AudioAble){ + AudioDocument *const audioDocument = doc->getAudioSubDocument(); + qDebug() << "Create an audio vizualizer for the audio sub document " << audioDocument->getFilePath(); + if (audioDocument != nullptr) { + visualizer = AudioVisualizer::fromFile(audioDocument->getFilePath()); + assert(visualizer != nullptr); + } + + QVBoxLayout *layout = new QVBoxLayout; + if (visualizer != nullptr) + layout->addWidget(visualizer); + setLayout(layout); } - QVBoxLayout *layout = new QVBoxLayout; - if (visualizer != nullptr) - layout->addWidget(visualizer); - setLayout(layout); } }