forked from qt-creator/qt-creator
Fix marking files modified externally as modified
It replaces59c90e00c1
andd0c537ca75
Task-number: QTCREATORBUG-17048 Change-Id: Ief4b1b72f2e5e7b1711be05d4ea8c03bbbf48fdf Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
113
src/libs/utils/guard.cpp
Normal file
113
src/libs/utils/guard.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "guard.h"
|
||||
#include "qtcassert.h"
|
||||
|
||||
/*! \class Utils::Guard
|
||||
|
||||
\brief The Guard class implements a recursive guard with locking mechanism.
|
||||
|
||||
It may be used as an alternative to QSignalBlocker.
|
||||
QSignalBlocker blocks all signals of the object
|
||||
which is usually not desirable. It may also block signals
|
||||
which are needed internally by the object itself.
|
||||
The Guard and GuardLocker classes don't block signals at all.
|
||||
|
||||
When calling a object's method which may in turn emit a signal
|
||||
which you are connected to, and you want to ignore
|
||||
this notification, you should keep the Guard object
|
||||
as your class member and declare the GuardLocker object
|
||||
just before calling the mentioned method, like:
|
||||
|
||||
\code
|
||||
class MyClass : public QObject
|
||||
{
|
||||
\dots
|
||||
private:
|
||||
Guard updateGuard; // member of your class
|
||||
};
|
||||
|
||||
\dots
|
||||
|
||||
void MyClass::updateOtherObject()
|
||||
{
|
||||
GuardLocker updatelocker(updateGuard);
|
||||
otherObject->update(); // this may trigger a signal
|
||||
}
|
||||
\endcode
|
||||
|
||||
Inside a slot which is connected to the other's object signal
|
||||
you may check if the guard is locked and ignore the further
|
||||
operations in this case:
|
||||
|
||||
\code
|
||||
void MyClass::otherObjectUpdated()
|
||||
{
|
||||
if (updateGuard.isLocked)
|
||||
return;
|
||||
|
||||
// we didn't trigger the update
|
||||
// so do update now
|
||||
\dots
|
||||
}
|
||||
\endcode
|
||||
|
||||
The GuardLock unlocks the Guard in it's destructor.
|
||||
|
||||
The Guard object is recursive, you may declare many GuardLocker
|
||||
objects for the same Guard instance and the Guard will be locked
|
||||
as long as at least one GuardLocker object created for the Guard
|
||||
is in scope.
|
||||
*/
|
||||
|
||||
namespace Utils {
|
||||
|
||||
Guard::Guard()
|
||||
{
|
||||
}
|
||||
|
||||
Guard::~Guard()
|
||||
{
|
||||
QTC_CHECK(m_lockCount == 0);
|
||||
}
|
||||
|
||||
bool Guard::isLocked() const
|
||||
{
|
||||
return m_lockCount;
|
||||
}
|
||||
|
||||
GuardLocker::GuardLocker(Guard &guard)
|
||||
: m_guard(guard)
|
||||
{
|
||||
++m_guard.m_lockCount;
|
||||
}
|
||||
|
||||
GuardLocker::~GuardLocker()
|
||||
{
|
||||
--m_guard.m_lockCount;
|
||||
}
|
||||
|
||||
} // namespace Utils
|
53
src/libs/utils/guard.h
Normal file
53
src/libs/utils/guard.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "utils_global.h"
|
||||
|
||||
namespace Utils {
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT Guard
|
||||
{
|
||||
public:
|
||||
Guard();
|
||||
~Guard();
|
||||
bool isLocked() const;
|
||||
private:
|
||||
int m_lockCount = 0;
|
||||
friend class GuardLocker;
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT GuardLocker
|
||||
{
|
||||
public:
|
||||
GuardLocker(Guard &guard);
|
||||
~GuardLocker();
|
||||
|
||||
private:
|
||||
Guard &m_guard;
|
||||
};
|
||||
|
||||
} // namespace Utils
|
@@ -100,7 +100,8 @@ SOURCES += $$PWD/environment.cpp \
|
||||
$$PWD/icon.cpp \
|
||||
$$PWD/port.cpp \
|
||||
$$PWD/runextensions.cpp \
|
||||
$$PWD/utilsicons.cpp
|
||||
$$PWD/utilsicons.cpp \
|
||||
$$PWD/guard.cpp
|
||||
|
||||
win32:SOURCES += $$PWD/consoleprocess_win.cpp
|
||||
else:SOURCES += $$PWD/consoleprocess_unix.cpp
|
||||
@@ -217,7 +218,8 @@ HEADERS += \
|
||||
$$PWD/smallstringvector.h \
|
||||
$$PWD/smallstringlayout.h \
|
||||
$$PWD/sizedarray.h \
|
||||
$$PWD/smallstringio.h
|
||||
$$PWD/smallstringio.h \
|
||||
$$PWD/guard.h
|
||||
|
||||
FORMS += $$PWD/filewizardpage.ui \
|
||||
$$PWD/projectintropage.ui \
|
||||
|
@@ -114,6 +114,8 @@ Project {
|
||||
"flowlayout.cpp",
|
||||
"flowlayout.h",
|
||||
"functiontraits.h",
|
||||
"guard.cpp",
|
||||
"guard.h",
|
||||
"historycompleter.cpp",
|
||||
"historycompleter.h",
|
||||
"hostosinfo.h",
|
||||
|
@@ -183,6 +183,9 @@ void FormWindowFile::setFilePath(const FileName &newName)
|
||||
|
||||
void FormWindowFile::updateIsModified()
|
||||
{
|
||||
if (m_modificationChangedGuard.isLocked())
|
||||
return;
|
||||
|
||||
bool value = m_formWindow && m_formWindow->isDirty();
|
||||
if (value)
|
||||
emit contentsChanged();
|
||||
@@ -209,8 +212,20 @@ bool FormWindowFile::isSaveAsAllowed() const
|
||||
|
||||
bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
|
||||
{
|
||||
if (flag == FlagIgnore)
|
||||
if (flag == FlagIgnore) {
|
||||
if (!m_formWindow || type != TypeContents)
|
||||
return true;
|
||||
const bool wasModified = m_formWindow->isDirty();
|
||||
{
|
||||
Utils::GuardLocker locker(m_modificationChangedGuard);
|
||||
// hack to ensure we clean the clear state in form window
|
||||
m_formWindow->setDirty(false);
|
||||
m_formWindow->setDirty(true);
|
||||
}
|
||||
if (!wasModified)
|
||||
updateIsModified();
|
||||
return true;
|
||||
}
|
||||
if (type == TypePermissions) {
|
||||
emit changed();
|
||||
} else {
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <texteditor/textdocument.h>
|
||||
#include <utils/guard.h>
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
@@ -84,6 +85,7 @@ private:
|
||||
QPointer<QDesignerFormWindowInterface> m_formWindow;
|
||||
bool m_isModified = false;
|
||||
ResourceHandler *m_resourceHandler = nullptr;
|
||||
Utils::Guard m_modificationChangedGuard;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
@@ -39,6 +39,7 @@
|
||||
#include <texteditor/generichighlighter/highlighter.h>
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/editormanager/documentmodel.h>
|
||||
#include <utils/guard.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
|
||||
#include <QApplication>
|
||||
@@ -106,6 +107,7 @@ public:
|
||||
int m_autoSaveRevision;
|
||||
|
||||
TextMarks m_marksCache; // Marks not owned
|
||||
Utils::Guard m_modificationChangedGuard;
|
||||
};
|
||||
|
||||
QTextCursor TextDocumentPrivate::indentOrUnindent(const QTextCursor &textCursor, bool doIndent,
|
||||
@@ -236,14 +238,8 @@ void TextDocumentPrivate::updateRevisions()
|
||||
TextDocument::TextDocument(Id id)
|
||||
: d(new TextDocumentPrivate)
|
||||
{
|
||||
QObject::connect(&d->m_document, &QTextDocument::modificationChanged, [this](bool modified) {
|
||||
// we only want to update the block revisions when going back to the saved version,
|
||||
// e.g. with undo
|
||||
if (!modified)
|
||||
d->updateRevisions();
|
||||
emit changed();
|
||||
});
|
||||
|
||||
connect(&d->m_document, &QTextDocument::modificationChanged,
|
||||
this, &TextDocument::modificationChanged);
|
||||
connect(&d->m_document, &QTextDocument::contentsChanged,
|
||||
this, &Core::IDocument::contentsChanged);
|
||||
connect(&d->m_document, &QTextDocument::contentsChange,
|
||||
@@ -723,8 +719,21 @@ bool TextDocument::setPlainText(const QString &text)
|
||||
|
||||
bool TextDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type)
|
||||
{
|
||||
if (flag == FlagIgnore)
|
||||
if (flag == FlagIgnore) {
|
||||
if (type != TypeContents)
|
||||
return true;
|
||||
|
||||
const bool wasModified = document()->isModified();
|
||||
{
|
||||
Utils::GuardLocker locker(d->m_modificationChangedGuard);
|
||||
// hack to ensure we clean the clear state in QTextDocument
|
||||
document()->setModified(false);
|
||||
document()->setModified(true);
|
||||
}
|
||||
if (!wasModified)
|
||||
modificationChanged(true);
|
||||
return true;
|
||||
}
|
||||
if (type == TypePermissions) {
|
||||
checkPermissions();
|
||||
return true;
|
||||
@@ -808,6 +817,17 @@ void TextDocument::ensureFinalNewLine(QTextCursor& cursor)
|
||||
}
|
||||
}
|
||||
|
||||
void TextDocument::modificationChanged(bool modified)
|
||||
{
|
||||
if (d->m_modificationChangedGuard.isLocked())
|
||||
return;
|
||||
// we only want to update the block revisions when going back to the saved version,
|
||||
// e.g. with undo
|
||||
if (!modified)
|
||||
d->updateRevisions();
|
||||
emit changed();
|
||||
}
|
||||
|
||||
TextMarks TextDocument::marks() const
|
||||
{
|
||||
return d->m_marksCache;
|
||||
|
@@ -151,6 +151,7 @@ private:
|
||||
bool reload);
|
||||
void cleanWhitespace(QTextCursor &cursor, bool cleanIndentation, bool inEntireDocument);
|
||||
void ensureFinalNewLine(QTextCursor &cursor);
|
||||
void modificationChanged(bool modified);
|
||||
|
||||
TextDocumentPrivate *d;
|
||||
};
|
||||
|
Reference in New Issue
Block a user