summaryrefslogtreecommitdiffstats
path: root/Swift
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-18 08:40:30 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-18 08:41:23 (GMT)
commit013725b6ac01ee0351cd61701588099dc1ec1258 (patch)
tree6749a4ef2116060f5fba6d5f432e4889ee7f2489 /Swift
parentf9f84b3d960bfe92103235edef607d8db8d0b6b0 (diff)
downloadswift-013725b6ac01ee0351cd61701588099dc1ec1258.zip
swift-013725b6ac01ee0351cd61701588099dc1ec1258.tar.bz2
Wait until WebKit is ready loading the initial template before appending messages.
Fixes bug where first messages were missing from the view.
Diffstat (limited to 'Swift')
-rw-r--r--Swift/QtUI/QtChatView.cpp21
-rw-r--r--Swift/QtUI/QtChatView.h5
2 files changed, 24 insertions, 2 deletions
diff --git a/Swift/QtUI/QtChatView.cpp b/Swift/QtUI/QtChatView.cpp
index 12f6beb..62e04ce 100644
--- a/Swift/QtUI/QtChatView.cpp
+++ b/Swift/QtUI/QtChatView.cpp
@@ -1,97 +1,114 @@
#include "QtChatView.h"
#include <QtDebug>
#include <QFile>
#include <QDesktopServices>
#include <QVBoxLayout>
#include <QWebView>
#include <QWebFrame>
#include <QKeyEvent>
#include <QStackedWidget>
namespace Swift {
QtChatView::QtChatView(QWidget* parent) : QWidget(parent) {
setFocusPolicy(Qt::NoFocus);
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0,0,0,0);
webView_ = new QWebView(this);
webView_->setFocusPolicy(Qt::NoFocus);
connect(webView_, SIGNAL(linkClicked(const QUrl&)), SLOT(handleLinkClicked(const QUrl&)));
+ connect(webView_, SIGNAL(loadFinished(bool)), SLOT(handleViewLoadFinished(bool)));
#ifdef Q_WS_X11
/* To give a border on Linux, where it looks bad without */
QStackedWidget* stack = new QStackedWidget(this);
stack->addWidget(webView_);
stack->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
stack->setLineWidth(2);
mainLayout->addWidget(stack);
#else
mainLayout->addWidget(webView_);
#endif
webPage_ = new QWebPage(this);
webPage_->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
webView_->setPage(webPage_);
connect(webPage_, SIGNAL(selectionChanged()), SLOT(copySelectionToClipboard()));
QFile file(":/themes/Default/Template.html");
bool result = file.open(QIODevice::ReadOnly);
Q_ASSERT(result);
Q_UNUSED(result);
QString pageHTML = file.readAll();
pageHTML.replace("==bodyBackground==", "background-color:#e3e3e3");
pageHTML.replace(pageHTML.indexOf("%@"), 2, "qrc:/themes/Default/");
pageHTML.replace(pageHTML.indexOf("%@"), 2, "Variants/Blue on Green.css");
pageHTML.replace(pageHTML.indexOf("%@"), 2, "");
pageHTML.replace(pageHTML.indexOf("%@"), 2, "");
file.close();
+
+ viewReady_ = false;
webPage_->mainFrame()->setHtml(pageHTML);
}
void QtChatView::addMessage(const ChatSnippet& snippet) {
//bool wasScrolledToBottom = isScrolledToBottom();
QString content = snippet.getContent();
content.replace("\\", "\\\\");
content.replace("\"", "\\\"");
content.replace("\n", "\\n");
content.replace("\r", "");
+ QString command;
if (previousContinuationElementID_.isEmpty() || !snippet.getAppendToPrevious()) {
- webPage_->mainFrame()->evaluateJavaScript("appendMessage(\"" + content + "\");");
+ command = "appendMessage(\"" + content + "\");";
+ }
+ else {
+ command = "appendNextMessage(\"" + content + "\");";
+ }
+ if (viewReady_) {
+ webPage_->mainFrame()->evaluateJavaScript(command);
}
else {
- webPage_->mainFrame()->evaluateJavaScript("appendNextMessage(\"" + content + "\");");
+ queuedMessages_ += command;
}
//qDebug() << webPage_->mainFrame()->toHtml();
previousContinuationElementID_ = snippet.getContinuationElementID();
/*if (wasScrolledToBottom) {
scrollToBottom();
}*/
}
void QtChatView::copySelectionToClipboard() {
if (!webPage_->selectedText().isEmpty()) {
webPage_->triggerAction(QWebPage::Copy);
}
}
bool QtChatView::isScrolledToBottom() const {
return webPage_->mainFrame()->scrollBarValue(Qt::Vertical) == webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical);
}
void QtChatView::scrollToBottom() {
webPage_->mainFrame()->setScrollBarValue(Qt::Vertical, webPage_->mainFrame()->scrollBarMaximum(Qt::Vertical));
}
void QtChatView::handleLinkClicked(const QUrl& url) {
QDesktopServices::openUrl(url);
}
+void QtChatView::handleViewLoadFinished(bool ok) {
+ Q_ASSERT(ok);
+ viewReady_ = true;
+ webPage_->mainFrame()->evaluateJavaScript(queuedMessages_);
+ queuedMessages_.clear();
+}
+
}
diff --git a/Swift/QtUI/QtChatView.h b/Swift/QtUI/QtChatView.h
index 7340e00..d3e997a 100644
--- a/Swift/QtUI/QtChatView.h
+++ b/Swift/QtUI/QtChatView.h
@@ -1,34 +1,39 @@
#ifndef SWIFT_QtChatView_H
#define SWIFT_QtChatView_H
#include <QString>
#include <QWidget>
#include "ChatSnippet.h"
class QWebView;
class QWebPage;
class QUrl;
namespace Swift {
class QtChatView : public QWidget {
Q_OBJECT
public:
QtChatView(QWidget* parent);
void addMessage(const ChatSnippet& snippet);
bool isScrolledToBottom() const;
public slots:
void copySelectionToClipboard();
void scrollToBottom();
void handleLinkClicked(const QUrl&);
+ private slots:
+ void handleViewLoadFinished(bool);
+
private:
+ bool viewReady_;
QWebView* webView_;
QWebPage* webPage_;
QString previousContinuationElementID_;
+ QString queuedMessages_;
};
}
#endif