forked from qt-creator/qt-creator
Designer: Move "open" implementation to document.
This avoids that reloading needs to call back to the editor, and is in preparation to a complete move of "open" to the document. Change-Id: Ic24ecf7612c311055276e81edb080ab855590df9 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
This commit is contained in:
@@ -39,11 +39,6 @@
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QDebug>
|
||||
#include <QDesignerFormWindowInterface>
|
||||
#include <QFileInfo>
|
||||
|
||||
namespace Designer {
|
||||
|
||||
using namespace Internal;
|
||||
@@ -58,49 +53,9 @@ FormWindowEditor::~FormWindowEditor()
|
||||
{
|
||||
}
|
||||
|
||||
void FormWindowEditor::finalizeInitialization()
|
||||
{
|
||||
// Revert to saved/load externally modified files.
|
||||
connect(formWindowFile(), &FormWindowFile::reloadRequested,
|
||||
[this](QString *errorString, const QString &fileName) {
|
||||
open(errorString, fileName, fileName);
|
||||
});
|
||||
}
|
||||
|
||||
bool FormWindowEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
||||
{
|
||||
if (Designer::Constants::Internal::debug)
|
||||
qDebug() << "FormWindowEditor::open" << fileName;
|
||||
|
||||
auto document = qobject_cast<FormWindowFile *>(textDocument());
|
||||
QDesignerFormWindowInterface *form = document->formWindow();
|
||||
QTC_ASSERT(form, return false);
|
||||
|
||||
if (fileName.isEmpty())
|
||||
return true;
|
||||
|
||||
const QFileInfo fi(fileName);
|
||||
const QString absfileName = fi.absoluteFilePath();
|
||||
|
||||
QString contents;
|
||||
if (document->read(absfileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess)
|
||||
return false;
|
||||
|
||||
form->setFileName(absfileName);
|
||||
const QByteArray contentsBA = contents.toUtf8();
|
||||
QBuffer str;
|
||||
str.setData(contentsBA);
|
||||
str.open(QIODevice::ReadOnly);
|
||||
if (!form->setContents(&str, errorString))
|
||||
return false;
|
||||
form->setDirty(fileName != realFileName);
|
||||
|
||||
document->syncXmlFromFormWindow();
|
||||
document->setFilePath(Utils::FileName::fromString(absfileName));
|
||||
document->setShouldAutoSave(false);
|
||||
document->resourceHandler()->updateResources(true);
|
||||
|
||||
return true;
|
||||
return formWindowFile()->open(errorString, fileName, realFileName);
|
||||
}
|
||||
|
||||
QWidget *FormWindowEditor::toolBar()
|
||||
|
||||
@@ -54,7 +54,6 @@ public:
|
||||
FormWindowEditor();
|
||||
~FormWindowEditor();
|
||||
|
||||
void finalizeInitialization();
|
||||
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
|
||||
QWidget *toolBar();
|
||||
bool isDesignModePreferred() const;
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QBuffer>
|
||||
#include <QDesignerFormWindowInterface>
|
||||
#include <QDesignerFormWindowManagerInterface>
|
||||
#include <QDesignerFormEditorInterface>
|
||||
@@ -71,6 +72,41 @@ FormWindowFile::FormWindowFile(QDesignerFormWindowInterface *form, QObject *pare
|
||||
m_resourceHandler, SLOT(updateResources()));
|
||||
}
|
||||
|
||||
bool FormWindowFile::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
||||
{
|
||||
if (Designer::Constants::Internal::debug)
|
||||
qDebug() << "FormWindowFile::open" << fileName;
|
||||
|
||||
QDesignerFormWindowInterface *form = formWindow();
|
||||
QTC_ASSERT(form, return false);
|
||||
|
||||
if (fileName.isEmpty())
|
||||
return true;
|
||||
|
||||
const QFileInfo fi(fileName);
|
||||
const QString absfileName = fi.absoluteFilePath();
|
||||
|
||||
QString contents;
|
||||
if (read(absfileName, &contents, errorString) != Utils::TextFileFormat::ReadSuccess)
|
||||
return false;
|
||||
|
||||
form->setFileName(absfileName);
|
||||
const QByteArray contentsBA = contents.toUtf8();
|
||||
QBuffer str;
|
||||
str.setData(contentsBA);
|
||||
str.open(QIODevice::ReadOnly);
|
||||
if (!form->setContents(&str, errorString))
|
||||
return false;
|
||||
form->setDirty(fileName != realFileName);
|
||||
|
||||
syncXmlFromFormWindow();
|
||||
setFilePath(Utils::FileName::fromString(absfileName));
|
||||
setShouldAutoSave(false);
|
||||
resourceHandler()->updateResources(true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FormWindowFile::save(QString *errorString, const QString &name, bool autoSave)
|
||||
{
|
||||
const FileName actualName = name.isEmpty() ? filePath() : FileName::fromString(name);
|
||||
@@ -175,8 +211,7 @@ bool FormWindowFile::reload(QString *errorString, ReloadFlag flag, ChangeType ty
|
||||
emit changed();
|
||||
} else {
|
||||
emit aboutToReload();
|
||||
emit reloadRequested(errorString, filePath().toString());
|
||||
const bool success = errorString->isEmpty();
|
||||
const bool success = open(errorString, filePath().toString(), filePath().toString());
|
||||
emit reloadFinished(success);
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -52,6 +52,8 @@ public:
|
||||
explicit FormWindowFile(QDesignerFormWindowInterface *form, QObject *parent = 0);
|
||||
~FormWindowFile() { }
|
||||
|
||||
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
|
||||
|
||||
// IDocument
|
||||
bool save(QString *errorString, const QString &fileName, bool autoSave);
|
||||
bool setContents(const QByteArray &contents);
|
||||
@@ -72,10 +74,6 @@ public:
|
||||
QString formWindowContents() const;
|
||||
ResourceHandler *resourceHandler() const;
|
||||
|
||||
signals:
|
||||
// Internal
|
||||
void reloadRequested(QString *errorString, const QString &);
|
||||
|
||||
public slots:
|
||||
void setFilePath(const Utils::FileName &);
|
||||
void setShouldAutoSave(bool sad = true) { m_shouldAutoSave = sad; }
|
||||
|
||||
Reference in New Issue
Block a user