UicCodeModelSupport: Make the code not so eager to run uic processes

Instead mostly postpone it until the contents are needed.

Task-number: QTCREATORBUG-9149

Change-Id: I83167537e97fbd965531d990c8073409ff7b0b32
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
This commit is contained in:
Daniel Teske
2013-03-21 19:24:34 +01:00
parent 3aa424ca64
commit 3abe2a72ea
5 changed files with 47 additions and 15 deletions

View File

@@ -42,6 +42,7 @@ class CMakeProject;
class CMakeUiCodeModelSupport : public CppTools::UiCodeModelSupport class CMakeUiCodeModelSupport : public CppTools::UiCodeModelSupport
{ {
Q_OBJECT
public: public:
CMakeUiCodeModelSupport(CPlusPlus::CppModelManagerInterface *modelmanager, CMakeUiCodeModelSupport(CPlusPlus::CppModelManagerInterface *modelmanager,
CMakeProject *project, CMakeProject *project,

View File

@@ -33,6 +33,7 @@
#include "cpptools_global.h" #include "cpptools_global.h"
#include <QString> #include <QString>
#include <QObject>
namespace CPlusPlus { namespace CPlusPlus {
class CppModelManagerInterface; class CppModelManagerInterface;
@@ -40,8 +41,9 @@ class CppModelManagerInterface;
namespace CppTools { namespace CppTools {
class CPPTOOLS_EXPORT AbstractEditorSupport class CPPTOOLS_EXPORT AbstractEditorSupport : public QObject
{ {
Q_OBJECT
public: public:
explicit AbstractEditorSupport(CPlusPlus::CppModelManagerInterface *modelmanager); explicit AbstractEditorSupport(CPlusPlus::CppModelManagerInterface *modelmanager);
virtual ~AbstractEditorSupport(); virtual ~AbstractEditorSupport();

View File

@@ -44,11 +44,12 @@ UiCodeModelSupport::UiCodeModelSupport(CppModelManagerInterface *modelmanager,
: AbstractEditorSupport(modelmanager), : AbstractEditorSupport(modelmanager),
m_sourceName(source), m_sourceName(source),
m_fileName(uiHeaderFile), m_fileName(uiHeaderFile),
m_initialized(false), m_state(BARE)
m_running(false)
{ {
if (debug) if (debug)
qDebug()<<"ctor UiCodeModelSupport for"<<m_sourceName<<uiHeaderFile; qDebug()<<"ctor UiCodeModelSupport for"<<m_sourceName<<uiHeaderFile;
connect(&m_process, SIGNAL(finished(int)),
this, SLOT(finishProcess()));
} }
UiCodeModelSupport::~UiCodeModelSupport() UiCodeModelSupport::~UiCodeModelSupport()
@@ -59,7 +60,8 @@ UiCodeModelSupport::~UiCodeModelSupport()
void UiCodeModelSupport::init() const void UiCodeModelSupport::init() const
{ {
m_initialized = true; if (m_state != BARE)
return;
QDateTime sourceTime = QFileInfo(m_sourceName).lastModified(); QDateTime sourceTime = QFileInfo(m_sourceName).lastModified();
QFileInfo uiHeaderFileInfo(m_fileName); QFileInfo uiHeaderFileInfo(m_fileName);
QDateTime uiHeaderTime = uiHeaderFileInfo.exists() ? uiHeaderFileInfo.lastModified() : QDateTime(); QDateTime uiHeaderTime = uiHeaderFileInfo.exists() ? uiHeaderFileInfo.lastModified() : QDateTime();
@@ -71,6 +73,7 @@ void UiCodeModelSupport::init() const
QTextStream stream(&file); QTextStream stream(&file);
m_contents = stream.readAll().toUtf8(); m_contents = stream.readAll().toUtf8();
m_cacheTime = uiHeaderTime; m_cacheTime = uiHeaderTime;
m_state = FINISHED;
return; return;
} }
} }
@@ -91,20 +94,25 @@ void UiCodeModelSupport::init() const
qDebug()<<"uic run wasn't succesfull"; qDebug()<<"uic run wasn't succesfull";
m_cacheTime = QDateTime (); m_cacheTime = QDateTime ();
m_contents = QByteArray(); m_contents = QByteArray();
m_state = FINISHED;
return; return;
} }
} else { } else {
if (debug) if (debug)
qDebug()<<"Could open "<<m_sourceName<<"needed for the cpp model"; qDebug()<<"Could open "<<m_sourceName<<"needed for the cpp model";
m_contents = QByteArray(); m_contents = QByteArray();
m_state = FINISHED;
} }
} }
QByteArray UiCodeModelSupport::contents() const QByteArray UiCodeModelSupport::contents() const
{ {
if (!m_initialized) // Check the common case first
if (m_state == FINISHED)
return m_contents;
if (m_state == BARE)
init(); init();
if (m_running) if (m_state == RUNNING)
finishProcess(); finishProcess();
return m_contents; return m_contents;
@@ -120,13 +128,16 @@ void UiCodeModelSupport::setFileName(const QString &name)
if (m_fileName == name && m_cacheTime.isValid()) if (m_fileName == name && m_cacheTime.isValid())
return; return;
if (m_state == RUNNING)
finishProcess();
if (debug) if (debug)
qDebug() << "UiCodeModelSupport::setFileName"<<name; qDebug() << "UiCodeModelSupport::setFileName"<<name;
m_fileName = name; m_fileName = name;
m_contents.clear(); m_contents.clear();
m_cacheTime = QDateTime(); m_cacheTime = QDateTime();
init(); m_state = BARE;
} }
bool UiCodeModelSupport::runUic(const QString &ui) const bool UiCodeModelSupport::runUic(const QString &ui) const
@@ -145,19 +156,23 @@ bool UiCodeModelSupport::runUic(const QString &ui) const
if (!m_process.waitForBytesWritten(3000)) if (!m_process.waitForBytesWritten(3000))
goto error; goto error;
m_process.closeWriteChannel(); m_process.closeWriteChannel();
m_running = true; m_state = RUNNING;
return true; return true;
error: error:
if (debug) if (debug)
qDebug() << "failed" << m_process.readAllStandardError(); qDebug() << "failed" << m_process.readAllStandardError();
m_process.kill(); m_process.kill();
m_running = false; m_state = FINISHED;
return false; return false;
} }
void UiCodeModelSupport::updateFromEditor(const QString &formEditorContents) void UiCodeModelSupport::updateFromEditor(const QString &formEditorContents)
{ {
if (m_state == BARE)
init();
if (m_state == RUNNING)
finishProcess();
if (runUic(formEditorContents)) if (runUic(formEditorContents))
if (finishProcess()) if (finishProcess())
updateDocument(); updateDocument();
@@ -165,23 +180,29 @@ void UiCodeModelSupport::updateFromEditor(const QString &formEditorContents)
bool UiCodeModelSupport::finishProcess() const bool UiCodeModelSupport::finishProcess() const
{ {
if (!m_running) if (m_state != RUNNING)
return false; return false;
if (!m_process.waitForFinished(3000) if (!m_process.waitForFinished(3000)
&& m_process.exitStatus() != QProcess::NormalExit && m_process.exitStatus() != QProcess::NormalExit
&& m_process.exitCode() != 0) { && m_process.exitCode() != 0) {
if (m_state != RUNNING) // waitForFinished can recurse into finishProcess
return false;
if (debug) if (debug)
qDebug() << "failed" << m_process.readAllStandardError(); qDebug() << "failed" << m_process.readAllStandardError();
m_process.kill(); m_process.kill();
m_running = false; m_state = FINISHED;
return false; return false;
} }
if (m_state != RUNNING) // waitForFinished can recurse into finishProcess
return true;
m_contents = m_process.readAllStandardOutput(); m_contents = m_process.readAllStandardOutput();
m_cacheTime = QDateTime::currentDateTime(); m_cacheTime = QDateTime::currentDateTime();
if (debug) if (debug)
qDebug() << "ok" << m_contents.size() << "bytes."; qDebug() << "ok" << m_contents.size() << "bytes.";
m_running = false; m_state = FINISHED;
return true; return true;
} }
@@ -189,6 +210,10 @@ void UiCodeModelSupport::updateFromBuild()
{ {
if (debug) if (debug)
qDebug()<<"UiCodeModelSupport::updateFromBuild() for file"<<m_sourceName; qDebug()<<"UiCodeModelSupport::updateFromBuild() for file"<<m_sourceName;
if (m_state == BARE)
init();
if (m_state == RUNNING)
finishProcess();
// This is mostly a fall back for the cases when uic couldn't be run // This is mostly a fall back for the cases when uic couldn't be run
// it pays special attention to the case where a ui_*h was newly created // it pays special attention to the case where a ui_*h was newly created
QDateTime sourceTime = QFileInfo(m_sourceName).lastModified(); QDateTime sourceTime = QFileInfo(m_sourceName).lastModified();

View File

@@ -43,6 +43,7 @@ namespace CppTools {
class CPPTOOLS_EXPORT UiCodeModelSupport : public AbstractEditorSupport class CPPTOOLS_EXPORT UiCodeModelSupport : public AbstractEditorSupport
{ {
Q_OBJECT
public: public:
UiCodeModelSupport(CPlusPlus::CppModelManagerInterface *modelmanager, UiCodeModelSupport(CPlusPlus::CppModelManagerInterface *modelmanager,
const QString &sourceFile, const QString &sourceFile,
@@ -58,16 +59,18 @@ protected:
virtual QString uicCommand() const = 0; virtual QString uicCommand() const = 0;
virtual QStringList environment() const = 0; virtual QStringList environment() const = 0;
private: private:
enum State { BARE, RUNNING, FINISHED };
void init() const; void init() const;
bool runUic(const QString &ui) const; bool runUic(const QString &ui) const;
bool finishProcess() const; Q_SLOT bool finishProcess() const;
mutable QProcess m_process; mutable QProcess m_process;
QString m_sourceName; QString m_sourceName;
QString m_fileName; QString m_fileName;
mutable bool m_initialized; mutable State m_state;
mutable QByteArray m_contents; mutable QByteArray m_contents;
mutable QDateTime m_cacheTime; mutable QDateTime m_cacheTime;
mutable bool m_running; static QList<UiCodeModelSupport *> m_waitingForStart;
}; };
} // CppTools } // CppTools

View File

@@ -42,6 +42,7 @@ namespace Internal {
class Qt4UiCodeModelSupport : public CppTools::UiCodeModelSupport class Qt4UiCodeModelSupport : public CppTools::UiCodeModelSupport
{ {
Q_OBJECT
public: public:
Qt4UiCodeModelSupport(CPlusPlus::CppModelManagerInterface *modelmanager, Qt4UiCodeModelSupport(CPlusPlus::CppModelManagerInterface *modelmanager,
Qt4Project *project, Qt4Project *project,