From 069a889e205c5cc93f93d8b8530a1375da9945d0 Mon Sep 17 00:00:00 2001
From: Andrea Diamantini <adjam7@gmail.com>
Date: Wed, 18 May 2011 23:28:18 +0200
Subject: Fixes the size of a tab preview to a constant size, decorate it a bit
 and clean up the code around it.

Patch by Tirtha Chatterjee, thanks :D

Reviewed by me ;)
---
 src/tabbar.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++-------------
 src/tabbar.h   |  4 ++++
 2 files changed, 52 insertions(+), 13 deletions(-)

(limited to 'src')

diff --git a/src/tabbar.cpp b/src/tabbar.cpp
index d2e0ff17..dee49eaf 100644
--- a/src/tabbar.cpp
+++ b/src/tabbar.cpp
@@ -59,10 +59,6 @@
 #include <QStyleOptionFrameV3>
 
 
-#define BASE_WIDTH_DIVISOR    4
-#define MIN_WIDTH_DIVISOR     8
-
-
 static inline QByteArray highlightPropertyName(int index)
 {
     return QByteArray("hAnim").append(QByteArray::number(index));
@@ -102,8 +98,8 @@ QSize TabBar::tabSizeHint(int index) const
 
     int buttonSize = view->addTabButton()->size().width();
     int tabBarWidth = view->size().width() - buttonSize;
-    int baseWidth =  view->sizeHint().width() / BASE_WIDTH_DIVISOR;
-    int minWidth =  view->sizeHint().width() / MIN_WIDTH_DIVISOR;
+    int baseWidth =  view->sizeHint().width() / baseWidthDivisor;
+    int minWidth =  view->sizeHint().width() / minWidthDivisor;
 
     int w;
     if (baseWidth*count() < tabBarWidth)
@@ -186,22 +182,61 @@ void TabBar::showTabPreview()
     if (indexedTab->isPageLoading())
         return;
 
-    int w = tabSizeHint(m_currentTabPreviewIndex).width();
-    int h = w * ((0.0 + currentTab->height()) / currentTab->width());
+    int w = (mv->sizeHint().width() / baseWidthDivisor);
+    int h = w * 0.75;
 
     m_previewPopup = new KPassivePopup(this);
     m_previewPopup.data()->setFrameShape(QFrame::StyledPanel);
     m_previewPopup.data()->setFrameShadow(QFrame::Plain);
-    m_previewPopup.data()->setFixedSize(w, h);
 
-    QLabel *l = new QLabel();
-    l->setPixmap(WebSnap::renderTabPreview(*indexedTab->page(), w, h));
+    QWidget *widget = new QWidget();
+    QLabel *thumbnail = new QLabel(widget);
+    QLabel *title = new QLabel(widget);
+    QLabel *url = new QLabel(widget);
+    thumbnail->setPixmap(WebSnap::renderTabPreview(*indexedTab->page(), w, h));
+    thumbnail->setAlignment(Qt::AlignHCenter);
+
+    QString text = indexedTab->view()->title();
+    if (text.length() > 20)
+    {
+        text = text.left(17) + "...";
+    }
+
+    title->setText(text);
+    title->setAlignment(Qt::AlignHCenter);
+
+    text = indexedTab->url().prettyUrl();
+    if (text.length() > 20)
+    {
+        text = text.left(17) + "...";
+    }
+
+    url->setText(text);
+    url->setAlignment(Qt::AlignHCenter);
+    QVBoxLayout *vb = new QVBoxLayout(widget);
+    vb->addWidget(title);
+    vb->addWidget(thumbnail);
+    vb->addWidget(url);
+    widget->setLayout(vb);
 
-    m_previewPopup.data()->setView(l);
+    m_previewPopup.data()->setFixedSize(w, h + url->heightForWidth(w) + title->heightForWidth(w));
+    m_previewPopup.data()->setView(widget);
     m_previewPopup.data()->layout()->setAlignment(Qt::AlignTop);
     m_previewPopup.data()->layout()->setMargin(0);
 
-    QPoint pos(tabRect(m_currentTabPreviewIndex).x() , tabRect(m_currentTabPreviewIndex).y() + tabRect(m_currentTabPreviewIndex).height());
+    int tabBarWidth = mv->size().width();
+    int leftIndex = tabRect(m_currentTabPreviewIndex).x() + (tabRect(m_currentTabPreviewIndex).width() - w)/2;
+
+    if (leftIndex < 0)
+    {
+        leftIndex = 0;
+    }
+    else if (leftIndex + w > tabBarWidth)
+    {
+        leftIndex = tabBarWidth - w;
+    }
+
+    QPoint pos(leftIndex, tabRect(m_currentTabPreviewIndex).y() + tabRect(m_currentTabPreviewIndex).height());
     m_previewPopup.data()->show(mapToGlobal(pos));
 }
 
diff --git a/src/tabbar.h b/src/tabbar.h
index 5d4c6d63..c1b0ba5c 100644
--- a/src/tabbar.h
+++ b/src/tabbar.h
@@ -101,6 +101,10 @@ private slots:
     void removeAnimation(int index);
 
 private:
+    //constants
+    static const int baseWidthDivisor = 4;
+    static const int minWidthDivisor = 8;
+
     void setupHistoryActions();
     friend class MainView;
 
-- 
cgit v1.2.1