DocumentManager: Avoid using sender()

Change-Id: I668f602c50857dd003fe7cc67a5c1ffbc5d3311d
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2022-07-20 10:54:33 +02:00
parent ee5ffafa45
commit b6c7d29007
2 changed files with 17 additions and 27 deletions

View File

@@ -368,16 +368,22 @@ static void addFileInfos(const QList<IDocument *> &documents)
*/ */
void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool addWatcher) void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool addWatcher)
{ {
auto connectDocument = [](IDocument *document) {
connect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed);
connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
connect(document, &IDocument::filePathChanged,
m_instance, [document](const FilePath &oldName, const FilePath &newName) {
if (document == d->m_blockedIDocument)
return;
emit m_instance->documentRenamed(document, oldName, newName);
});
};
if (!addWatcher) { if (!addWatcher) {
// We keep those in a separate list // We keep those in a separate list
for (IDocument *document : documents) { for (IDocument *document : documents) {
if (document && !d->m_documentsWithoutWatch.contains(document)) { if (document && !d->m_documentsWithoutWatch.contains(document)) {
connect(document, &QObject::destroyed, connectDocument(document);
m_instance, &DocumentManager::documentDestroyed);
connect(document, &IDocument::filePathChanged,
m_instance, &DocumentManager::filePathChanged);
connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
d->m_documentsWithoutWatch.append(document); d->m_documentsWithoutWatch.append(document);
} }
} }
@@ -388,11 +394,9 @@ void DocumentManager::addDocuments(const QList<IDocument *> &documents, bool add
return document && !d->m_documentsWithWatch.contains(document); return document && !d->m_documentsWithWatch.contains(document);
}); });
for (IDocument *document : documentsToWatch) { for (IDocument *document : documentsToWatch) {
connect(document, &IDocument::changed, m_instance, &DocumentManager::checkForNewFileName); connect(document, &IDocument::changed,
connect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed); m_instance, [document] { m_instance->checkForNewFileName(document); });
connect(document, &IDocument::filePathChanged, connectDocument(document);
m_instance, &DocumentManager::filePathChanged);
connect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
} }
addFileInfos(documentsToWatch); addFileInfos(documentsToWatch);
} }
@@ -503,15 +507,6 @@ void DocumentManager::renamedFile(const Utils::FilePath &from, const Utils::File
emit m_instance->allDocumentsRenamed(from, to); emit m_instance->allDocumentsRenamed(from, to);
} }
void DocumentManager::filePathChanged(const FilePath &oldName, const FilePath &newName)
{
auto doc = qobject_cast<IDocument *>(sender());
QTC_ASSERT(doc, return);
if (doc == d->m_blockedIDocument)
return;
emit m_instance->documentRenamed(doc, oldName, newName);
}
void DocumentManager::updateSaveAll() void DocumentManager::updateSaveAll()
{ {
d->m_saveAllAction->setEnabled(!modifiedDocuments().empty()); d->m_saveAllAction->setEnabled(!modifiedDocuments().empty());
@@ -550,23 +545,19 @@ bool DocumentManager::removeDocument(IDocument *document)
if (!d->m_documentsWithoutWatch.removeOne(document)) { if (!d->m_documentsWithoutWatch.removeOne(document)) {
addWatcher = true; addWatcher = true;
removeFileInfo(document); removeFileInfo(document);
disconnect(document, &IDocument::changed, m_instance, &DocumentManager::checkForNewFileName);
} }
disconnect(document, &QObject::destroyed, m_instance, &DocumentManager::documentDestroyed); disconnect(document, nullptr, m_instance, nullptr);
disconnect(document, &IDocument::changed, m_instance, &DocumentManager::updateSaveAll);
return addWatcher; return addWatcher;
} }
/* Slot reacting on IDocument::changed. We need to check if the signal was sent /* Slot reacting on IDocument::changed. We need to check if the signal was sent
because the document was saved under different name. */ because the document was saved under different name. */
void DocumentManager::checkForNewFileName() void DocumentManager::checkForNewFileName(IDocument *document)
{ {
auto document = qobject_cast<IDocument *>(sender());
// We modified the IDocument // We modified the IDocument
// Trust the other code to also update the m_states map // Trust the other code to also update the m_states map
if (document == d->m_blockedIDocument) if (document == d->m_blockedIDocument)
return; return;
QTC_ASSERT(document, return);
QTC_ASSERT(d->m_documentsWithWatch.contains(document), return); QTC_ASSERT(d->m_documentsWithWatch.contains(document), return);
// Maybe the name has changed or file has been deleted and created again ... // Maybe the name has changed or file has been deleted and created again ...

View File

@@ -166,10 +166,9 @@ private:
~DocumentManager() override; ~DocumentManager() override;
void documentDestroyed(QObject *obj); void documentDestroyed(QObject *obj);
void checkForNewFileName(); void checkForNewFileName(IDocument *document);
void checkForReload(); void checkForReload();
void changedFile(const QString &file); void changedFile(const QString &file);
void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName);
void updateSaveAll(); void updateSaveAll();
static void registerSaveAllAction(); static void registerSaveAllAction();