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

DOCUMENT: The newDocument method in the document store handles the name

The new document's name is handled internally, if the user want to
consult it he should the returned weak pointer.
parent 8c0f817a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!3Add documents
......@@ -31,19 +31,11 @@ VivyDocumentStore::isDocumentPresent(const QString &name) noexcept
}
#define NEW_DOCUMENT_BASENAME "Untitled "
std::weak_ptr<VivyDocument>
VivyDocumentStore::newDocument(QString &name)
VivyDocumentStore::newDocument()
{
uint docNumber{1};
QString newDocName;
QString newDocName = newDocumentBaseName + QString::number(newDocumentNumber++);
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;
......
......@@ -20,7 +20,7 @@ public:
/* Create/load documents */
std::weak_ptr<VivyDocument> loadDocument(const QString &file);
std::weak_ptr<VivyDocument> newDocument(QString &name);
std::weak_ptr<VivyDocument> newDocument();
/* Get to see if a document is already present or not */
[[nodiscard("handle-it")]] bool isDocumentPresent(const QString &name) noexcept;
......@@ -34,6 +34,8 @@ public:
private:
QMap<QString, std::shared_ptr<VivyDocument>> documents;
uint newDocumentNumber{1};
static inline const QString newDocumentBaseName = "Untitled ";
};
#endif // VIVY_DOCUMENTSTORE_H
......@@ -89,16 +89,17 @@ MainWindow::closeDocument(int index) noexcept
void
MainWindow::newDocument() noexcept
{
QString baseName;
std::weak_ptr<VivyDocument> document = vivyApp->documentStore.newDocument(baseName);
std::weak_ptr<VivyDocument> document = vivyApp->documentStore.newDocument();
try {
VivyDocumentView *documentView = new VivyDocumentView(document);
qDebug() << "View constructed successfully";
documents->addTab(documentView, document.lock()->getName());
qDebug() << "View constructed successfully";
} catch (const std::runtime_error &e) {
qCritical() << "Failed to create the document view for the new document " << baseName;
throw;
qCritical() << "Failed to create a new empty document";
if (auto locked = document.lock()) {
vivyApp->documentStore.closeDocument(locked->getName());
}
}
}
......@@ -128,7 +129,7 @@ MainWindow::openDocument() noexcept
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 std::runtime_error("failed to create a document view");
vivyApp->documentStore.closeDocument(baseName);
}
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter