Commit ff6e1008 authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Erik Verbruggen

C++: prevent cloning the QTextDocument on the UI thread.

Now the document contents (the text) is passed to the background thread,
which in turn will recreate the text document.

Change-Id: I7af47348fe162b53b8b440f1561a9919bf3c381a
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 53e8e0fb
...@@ -132,6 +132,8 @@ private: ...@@ -132,6 +132,8 @@ private:
// -------------------- // --------------------
const QChar CodeAssistantPrivate::m_null; const QChar CodeAssistantPrivate::m_null;
static const int AutomaticProposalTimerInterval = 400;
CodeAssistantPrivate::CodeAssistantPrivate(CodeAssistant *assistant) CodeAssistantPrivate::CodeAssistantPrivate(CodeAssistant *assistant)
: m_q(assistant) : m_q(assistant)
, m_textEditor(0) , m_textEditor(0)
...@@ -142,7 +144,7 @@ CodeAssistantPrivate::CodeAssistantPrivate(CodeAssistant *assistant) ...@@ -142,7 +144,7 @@ CodeAssistantPrivate::CodeAssistantPrivate(CodeAssistant *assistant)
, m_settings(TextEditorSettings::instance()->completionSettings()) , m_settings(TextEditorSettings::instance()->completionSettings())
{ {
m_automaticProposalTimer.setSingleShot(true); m_automaticProposalTimer.setSingleShot(true);
m_automaticProposalTimer.setInterval(400); m_automaticProposalTimer.setInterval(AutomaticProposalTimerInterval);
connect(&m_automaticProposalTimer, SIGNAL(timeout()), this, SLOT(automaticProposalTimeout())); connect(&m_automaticProposalTimer, SIGNAL(timeout()), this, SLOT(automaticProposalTimeout()));
connect(TextEditorSettings::instance(), connect(TextEditorSettings::instance(),
...@@ -248,7 +250,7 @@ void CodeAssistantPrivate::requestProposal(AssistReason reason, ...@@ -248,7 +250,7 @@ void CodeAssistantPrivate::requestProposal(AssistReason reason,
m_requestRunner = new ProcessorRunner; m_requestRunner = new ProcessorRunner;
connect(m_requestRunner, SIGNAL(finished()), this, SLOT(proposalComputed())); connect(m_requestRunner, SIGNAL(finished()), this, SLOT(proposalComputed()));
connect(m_requestRunner, SIGNAL(finished()), this, SLOT(finalizeRequest())); connect(m_requestRunner, SIGNAL(finished()), this, SLOT(finalizeRequest()));
assistInterface->detach(m_requestRunner); assistInterface->prepareForAsyncUse();
m_requestRunner->setReason(reason); m_requestRunner->setReason(reason);
m_requestRunner->setProcessor(processor); m_requestRunner->setProcessor(processor);
m_requestRunner->setAssistInterface(assistInterface); m_requestRunner->setAssistInterface(assistInterface);
......
...@@ -42,7 +42,7 @@ DefaultAssistInterface::DefaultAssistInterface(QTextDocument *textDocument, ...@@ -42,7 +42,7 @@ DefaultAssistInterface::DefaultAssistInterface(QTextDocument *textDocument,
Core::IDocument *document, Core::IDocument *document,
AssistReason reason) AssistReason reason)
: m_textDocument(textDocument) : m_textDocument(textDocument)
, m_detached(false) , m_isAsync(false)
, m_position(position) , m_position(position)
, m_document(document) , m_document(document)
, m_reason(reason) , m_reason(reason)
...@@ -50,7 +50,7 @@ DefaultAssistInterface::DefaultAssistInterface(QTextDocument *textDocument, ...@@ -50,7 +50,7 @@ DefaultAssistInterface::DefaultAssistInterface(QTextDocument *textDocument,
DefaultAssistInterface::~DefaultAssistInterface() DefaultAssistInterface::~DefaultAssistInterface()
{ {
if (m_detached) if (m_isAsync)
delete m_textDocument; delete m_textDocument;
} }
...@@ -64,11 +64,17 @@ QString DefaultAssistInterface::textAt(int pos, int length) const ...@@ -64,11 +64,17 @@ QString DefaultAssistInterface::textAt(int pos, int length) const
return Convenience::textAt(QTextCursor(m_textDocument), pos, length); return Convenience::textAt(QTextCursor(m_textDocument), pos, length);
} }
void DefaultAssistInterface::detach(QThread *destination) void DefaultAssistInterface::prepareForAsyncUse()
{ {
m_textDocument = m_textDocument->clone(); m_text = m_textDocument->toPlainText();
m_textDocument->moveToThread(destination); m_textDocument = 0;
m_detached = true; m_isAsync = true;
}
void DefaultAssistInterface::recreateTextDocument()
{
m_textDocument = new QTextDocument(m_text);
m_text = QString();
} }
AssistReason DefaultAssistInterface::reason() const AssistReason DefaultAssistInterface::reason() const
......
...@@ -48,15 +48,17 @@ public: ...@@ -48,15 +48,17 @@ public:
virtual QString textAt(int position, int length) const; virtual QString textAt(int position, int length) const;
virtual const Core::IDocument *document() const { return m_document; } virtual const Core::IDocument *document() const { return m_document; }
virtual QTextDocument *textDocument() const { return m_textDocument; } virtual QTextDocument *textDocument() const { return m_textDocument; }
virtual void detach(QThread *destination); virtual void prepareForAsyncUse();
virtual void recreateTextDocument();
virtual AssistReason reason() const; virtual AssistReason reason() const;
private: private:
QTextDocument *m_textDocument; QTextDocument *m_textDocument;
bool m_detached; bool m_isAsync;
int m_position; int m_position;
Core::IDocument *m_document; Core::IDocument *m_document;
AssistReason m_reason; AssistReason m_reason;
QString m_text;
}; };
} // TextEditor } // TextEditor
......
...@@ -58,7 +58,8 @@ public: ...@@ -58,7 +58,8 @@ public:
virtual QString textAt(int position, int length) const = 0; virtual QString textAt(int position, int length) const = 0;
virtual const Core::IDocument *document() const = 0; virtual const Core::IDocument *document() const = 0;
virtual QTextDocument *textDocument() const = 0; virtual QTextDocument *textDocument() const = 0;
virtual void detach(QThread *destination) = 0; virtual void prepareForAsyncUse() = 0;
virtual void recreateTextDocument() = 0;
virtual AssistReason reason() const = 0; virtual AssistReason reason() const = 0;
}; };
......
...@@ -60,6 +60,7 @@ void ProcessorRunner::setProcessor(IAssistProcessor *computer) ...@@ -60,6 +60,7 @@ void ProcessorRunner::setProcessor(IAssistProcessor *computer)
void ProcessorRunner::run() void ProcessorRunner::run()
{ {
m_interface->recreateTextDocument();
m_proposal = m_processor->perform(m_interface); m_proposal = m_processor->perform(m_interface);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment