Move generated binaries back in the root directory
[quassel.git] / src / qtui / chatitem.cpp
index 758de8d..072ae1b 100644 (file)
@@ -26,7 +26,9 @@
 #include <QPainter>
 #include <QPalette>
 #include <QTextLayout>
+#ifdef HAVE_WEBKIT
 #include <QWebView>
+#endif
 #include <QGraphicsProxyWidget>
 
 #include "chatitem.h"
@@ -378,10 +380,14 @@ QVector<QTextLayout::FormatRange> ContentsChatItem::additionalFormats() const {
 }
 
 void ContentsChatItem::endHoverMode() {
-  if(hasLayout() && privateData()->currentClickable.isValid()) {
-    setCursor(Qt::ArrowCursor);
-    privateData()->currentClickable = Clickable();
-    privateData()->clearPreview();
+  if(hasLayout()) {
+    if(privateData()->currentClickable.isValid()) {
+      setCursor(Qt::ArrowCursor);
+      privateData()->currentClickable = Clickable();
+    }
+#ifdef HAVE_WEBKIT
+    clearWebPreview();
+#endif
     update();
   }
 }
@@ -437,20 +443,9 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
     if(idx >= click.start && idx < click.start + click.length) {
       if(click.type == Clickable::Url) {
         onClickable = true;
-
-       if(!hasLayout())
-         updateLayout();
-
-       QTextLine line = layout()->lineForTextPosition(click.start);
-       qreal x = line.cursorToX(click.start);
-       qreal width = line.cursorToX(click.start + click.length) - x;
-       qreal height = line.height();
-       qreal y = height * line.lineNumber();
-       QRectF urlRect(x, y, width, height);
-       QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
-       if(!url.contains("://"))
-         url = "http://" + url;
-       privateData()->loadWebPreview(url, urlRect);
+#ifdef HAVE_WEBKIT
+       showWebPreview(click);
+#endif
       } else if(click.type == Clickable::Channel) {
         // TODO: don't make clickable if it's our own name
         //onClickable = true; //FIXME disabled for now
@@ -467,56 +462,73 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
   event->accept();
 }
 
+#ifdef HAVE_WEBKIT
+void ContentsChatItem::showWebPreview(const Clickable &click) {
+  if(!hasLayout())
+    updateLayout();
+
+  QTextLine line = layout()->lineForTextPosition(click.start);
+  qreal x = line.cursorToX(click.start);
+  qreal width = line.cursorToX(click.start + click.length) - x;
+  qreal height = line.height();
+  qreal y = height * line.lineNumber();
+  QRectF urlRect(x, y, width, height);
+  QString url = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
+  if(!url.contains("://"))
+    url = "http://" + url;
+  privateData()->loadWebPreview(url, urlRect);
+}
+
+void ContentsChatItem::clearWebPreview() {
+  if(hasLayout())
+    privateData()->clearWebPreview();
+}
+#endif
+
 // ****************************************
 // ContentsChatItemPrivate
 // ****************************************
+#ifdef HAVE_WEBKIT
 ContentsChatItemPrivate::~ContentsChatItemPrivate() {
-  clearPreview();
+  clearWebPreview();
 }
 
 void ContentsChatItemPrivate::loadWebPreview(const QString &url, const QRectF &urlRect) {
-  if(!controller)
-    controller = new PreviewController(contentsItem);
-  controller->loadPage(url, urlRect);
-}
-
-void ContentsChatItemPrivate::clearPreview() {
-  delete controller;
-  controller = 0;
+  if(url != previewUrl) {
+    previewUrl = url;
+    // load a new web view and delete the old one (if exists)
+    if(previewItem) {
+      contentsItem->scene()->removeItem(previewItem);
+      delete previewItem;
+    }
+    QWebView *view = new QWebView;
+    view->load(url);
+    previewItem = new ContentsChatItemPrivate::PreviewItem(view);
+    contentsItem->scene()->addItem(previewItem);
+  }
+  if(urlRect != previewUrlRect) {
+    previewUrlRect = urlRect;
+    QPointF sPos = contentsItem->scenePos();
+    qreal previewY = sPos.y() + urlRect.y() + urlRect.height(); // bottom of url;
+    qreal previewX = sPos.x() + urlRect.x();
+    if(previewY + previewItem->boundingRect().height() > contentsItem->scene()->sceneRect().bottom())
+      previewY = sPos.y() + urlRect.y() - previewItem->boundingRect().height();
+    
+    if(previewX + previewItem->boundingRect().width() > contentsItem->scene()->sceneRect().width())
+      previewX = contentsItem->scene()->sceneRect().right() - previewItem->boundingRect().width();
+  
+    previewItem->setPos(previewX, previewY);
+  }
 }
 
-ContentsChatItemPrivate::PreviewController::~PreviewController() {
+void ContentsChatItemPrivate::clearWebPreview() {
   if(previewItem) {
     contentsItem->scene()->removeItem(previewItem);
     delete previewItem;
+    previewItem = 0;
   }
-}
-
-void ContentsChatItemPrivate::PreviewController::loadPage(const QString &newUrl, const QRectF &urlRect) {
-  if(newUrl.isEmpty() || newUrl == url)
-    return;
-
-  url = newUrl;
-  QWebView *view = new QWebView;
-  connect(view, SIGNAL(loadFinished(bool)), this, SLOT(pageLoaded(bool)));
-  view->load(url);
-  previewItem = new ContentsChatItemPrivate::PreviewItem(view);
-
-  QPointF sPos = contentsItem->scenePos();
-  qreal previewY = sPos.y() + urlRect.y() + urlRect.height(); // bottom of url;
-  qreal previewX = sPos.x() + urlRect.x();
-  if(previewY + previewItem->boundingRect().height() > contentsItem->scene()->sceneRect().bottom())
-    previewY = sPos.y() + urlRect.y() - previewItem->boundingRect().height();
-
-  if(previewX + previewItem->boundingRect().width() > contentsItem->scene()->sceneRect().width())
-    previewX = contentsItem->scene()->sceneRect().right() - previewItem->boundingRect().width();
-
-  previewItem->setPos(previewX, previewY);
-  contentsItem->scene()->addItem(previewItem);
-}
-
-void ContentsChatItemPrivate::PreviewController::pageLoaded(bool success) {
-  Q_UNUSED(success)
+  previewUrl = QString();
+  previewUrlRect = QRectF();
 }
 
 ContentsChatItemPrivate::PreviewItem::PreviewItem(QWebView *webView)
@@ -549,6 +561,7 @@ void ContentsChatItemPrivate::PreviewItem::paint(QPainter *painter, const QStyle
   QString text = QString::number(zValue());
   painter->drawText(_boundingRect.center(), text);
 }
+#endif // #ifdef HAVE_WEBKIT
 
 /*************************************************************************************************/