]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tooltips/tooltipmanager.cpp
* Adjust code to use the improved KUrlNavigator API.
[dolphin.git] / src / tooltips / tooltipmanager.cpp
index ae514f896dd53ee9c9d6f4826ab1af6937466ffc..d8a9e190cd3a29c60f95d9dffc45cd43ba112058 100644 (file)
 
 #include "tooltipmanager.h"
 
-#include "dolphintooltip.h"
 #include "dolphinmodel.h"
 #include "dolphinsortfilterproxymodel.h"
 
 #include <kicon.h>
-#include <tooltips/ktooltip.h>
 #include <kio/previewjob.h>
+#include <kseparator.h>
+
+#include "panels/information/kmetadatawidget.h"
+#include "tooltips/ktooltip.h"
 
 #include <QApplication>
 #include <QDesktopWidget>
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QScrollArea>
 #include <QScrollBar>
 #include <QTimer>
-#include <QToolTip>
-
-const int ICON_WIDTH = 128;
-const int ICON_HEIGHT = 128;
-const int PREVIEW_DELAY = 250;
-
-K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate, g_delegate)
+#include <QVBoxLayout>
 
 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
                                DolphinSortFilterProxyModel* model) :
@@ -54,8 +53,6 @@ ToolTipManager::ToolTipManager(QAbstractItemView* parent,
     m_hasDefaultIcon(false),
     m_previewPixmap()
 {
-    KToolTip::setToolTipDelegate(g_delegate);
-
     m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
     connect(parent, SIGNAL(entered(const QModelIndex&)),
             this, SLOT(requestToolTip(const QModelIndex&)));
@@ -86,6 +83,7 @@ ToolTipManager::ToolTipManager(QAbstractItemView* parent,
             this, SLOT(hideTip()));
 
     m_view->viewport()->installEventFilter(this);
+    m_view->installEventFilter(this);
 }
 
 ToolTipManager::~ToolTipManager()
@@ -99,7 +97,16 @@ void ToolTipManager::hideTip()
 
 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
 {
-    if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
+    if (watched == m_view->viewport()) {
+        switch (event->type()) {
+        case QEvent::Leave:
+        case QEvent::MouseButtonPress:
+            hideToolTip();
+            break;
+        default:
+            break;
+        }
+    } else if ((watched == m_view) && (event->type() == QEvent::KeyPress)) {
         hideToolTip();
     }
 
@@ -147,36 +154,55 @@ void ToolTipManager::prepareToolTip()
         m_waitOnPreviewTimer->start(250);
     }
 
-    const QString text = m_item.getToolTipText();
     if (!m_previewPixmap.isNull()) {
-        showToolTip(KIcon(m_previewPixmap), text);
+        showToolTip(m_previewPixmap);
     } else if (!m_hasDefaultIcon) {
-        const QPixmap image(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
-        showToolTip(image, text);
+        const QPixmap image(KIcon(m_item.iconName()).pixmap(128, 128));
+        showToolTip(image);
         m_hasDefaultIcon = true;
     }
 }
 
-void ToolTipManager::showToolTip(const QIcon& icon, const QString& text)
+void ToolTipManager::startPreviewJob()
+{
+    m_generatingPreview = true;
+    KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item, 256, 256);
+
+    connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
+            this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
+    connect(job, SIGNAL(failed(const KFileItem&)),
+            this, SLOT(previewFailed()));
+}
+
+
+void ToolTipManager::setPreviewPix(const KFileItem& item,
+                                   const QPixmap& pixmap)
+{
+    if ((m_item.url() != item.url()) || pixmap.isNull()) {
+        // an old preview or an invalid preview has been received
+        previewFailed();
+    } else {
+        m_previewPixmap = pixmap;
+        m_generatingPreview = false;
+    }
+}
+
+void ToolTipManager::previewFailed()
+{
+    m_generatingPreview = false;
+}
+
+
+void ToolTipManager::showToolTip(const QPixmap& pixmap)
 {
     if (QApplication::mouseButtons() & Qt::LeftButton) {
         return;
     }
 
-    KToolTipItem* tip = new KToolTipItem(icon, text);
-
-    KStyleOptionToolTip option;
-    // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
-    option.direction      = QApplication::layoutDirection();
-    option.fontMetrics    = QFontMetrics(QToolTip::font());
-    option.activeCorner   = KStyleOptionToolTip::TopLeftCorner;
-    option.palette        = QToolTip::palette();
-    option.font           = QToolTip::font();
-    option.rect           = QRect();
-    option.state          = QStyle::State_None;
-    option.decorationSize = QSize(32, 32);
+    QWidget* tip = createTipContent(pixmap);
 
-    const QSize size = g_delegate->sizeHint(option, *tip);
+    // calculate the x- and y-position of the tooltip
+    const QSize size = tip->sizeHint();
     const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
 
     // m_itemRect defines the area of the item, where the tooltip should be
@@ -215,37 +241,50 @@ void ToolTipManager::showToolTip(const QIcon& icon, const QString& text)
     KToolTip::showTip(QPoint(x, y), tip);
 }
 
-
-
-void ToolTipManager::startPreviewJob()
+QWidget* ToolTipManager::createTipContent(const QPixmap& pixmap) const
 {
-    m_generatingPreview = true;
-    KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item,
-                                            PREVIEW_WIDTH,
-                                            PREVIEW_HEIGHT);
-
-    connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
-            this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
-    connect(job, SIGNAL(failed(const KFileItem&)),
-            this, SLOT(previewFailed()));
-}
-
-
-void ToolTipManager::setPreviewPix(const KFileItem& item,
-                                   const QPixmap& pixmap)
-{
-    if ((m_item.url() != item.url()) || pixmap.isNull()) {
-        // an old preview or an invalid preview has been received
-        previewFailed();
-    } else {
-        m_previewPixmap = pixmap;
-        m_generatingPreview = false;
-    }
-}
-
-void ToolTipManager::previewFailed()
-{
-    m_generatingPreview = false;
+    QWidget* tipContent = new QWidget();
+
+    // add pixmap
+    QLabel* pixmapLabel = new QLabel(tipContent);
+    pixmapLabel->setPixmap(pixmap);
+    pixmapLabel->setFixedSize(pixmap.size());
+
+    // add item name
+    QLabel* nameLabel = new QLabel(tipContent);
+    nameLabel->setText(m_item.text());
+    nameLabel->setWordWrap(true);
+    QFont font = nameLabel->font();
+    font.setBold(true);
+    nameLabel->setFont(font);
+    nameLabel->setAlignment(Qt::AlignHCenter);
+    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
+
+    // add meta data
+    KMetaDataWidget* metaDataWidget = new KMetaDataWidget(tipContent);
+    metaDataWidget->setForegroundRole(QPalette::ToolTipText);
+    metaDataWidget->setItem(m_item);
+    metaDataWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+    metaDataWidget->setReadOnly(true);
+
+    // the stretchwidget allows the metadata widget to be top aligned and fills
+    // the remaining vertical space
+    QWidget* stretchWidget = new QWidget(tipContent);
+    stretchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
+
+    QWidget* textContainer = new QWidget(tipContent);
+    QVBoxLayout* textLayout = new QVBoxLayout(textContainer);
+    textLayout->addWidget(nameLabel);
+    textLayout->addWidget(new KSeparator());
+    textLayout->addWidget(metaDataWidget);
+    textLayout->addWidget(stretchWidget);
+
+    QHBoxLayout* tipLayout = new QHBoxLayout(tipContent);
+    tipLayout->setMargin(0);
+    tipLayout->addWidget(pixmapLabel);
+    tipLayout->addWidget(textContainer);
+
+    return tipContent;
 }
 
 #include "tooltipmanager.moc"