#include "tooltipmanager.h"
-#include "dolphintooltip.h"
#include "dolphinmodel.h"
#include "dolphinsortfilterproxymodel.h"
#include <kicon.h>
-#include <tooltips/ktooltip.h>
#include <kio/previewjob.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)
ToolTipManager::ToolTipManager(QAbstractItemView* parent,
DolphinSortFilterProxyModel* model) :
m_waitOnPreviewTimer(0),
m_item(),
m_itemRect(),
- m_preview(false),
m_generatingPreview(false),
- m_previewPass(0),
- m_pix()
+ 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&)));
this, SLOT(hideTip()));
m_view->viewport()->installEventFilter(this);
+ m_view->installEventFilter(this);
}
ToolTipManager::~ToolTipManager()
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();
}
// only start the previewJob when the mouse has been over this item for 200 milliseconds,
// this prevents a lot of useless preview jobs when passing rapidly over a lot of items
m_previewTimer->start(200);
- m_preview = false;
- m_previewPass = 0;
+ m_previewPixmap = QPixmap();
+ m_hasDefaultIcon = false;
m_timer->start(500);
} else {
void ToolTipManager::prepareToolTip()
{
if (m_generatingPreview) {
- if (m_previewPass == 1) {
- // We waited 250msec and the preview is still not finished,
- // so show default icon as fallback.
- QPixmap image(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
- showToolTip(image, m_item.getToolTipText());
- }
-
- ++m_previewPass;
m_waitOnPreviewTimer->start(250);
- } else {
- KIcon icon;
- if (m_preview) {
- // We got a preview.
- icon = KIcon(m_pix);
- } else {
- // No preview, so use an icon.
- // Force a 128x128 icon, a 256x256 one is far too big.
- const QPixmap pixmap = KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT);
- icon = KIcon(pixmap);
- }
+ }
- showToolTip(icon, m_item.getToolTipText());
+ if (!m_previewPixmap.isNull()) {
+ showToolTip(m_previewPixmap);
+ } else if (!m_hasDefaultIcon) {
+ 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
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(const KFileItem&)));
-}
+ QWidget* tipContent = new QWidget();
+ QLabel* pixmapLabel = new QLabel(tipContent);
+ pixmapLabel->setPixmap(pixmap);
+ pixmapLabel->setFixedSize(pixmap.size());
-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
- m_generatingPreview = false;
- return;
- }
+ KMetaDataWidget* metaDataWidget = new KMetaDataWidget(tipContent);
+ metaDataWidget->setItem(m_item);
+ metaDataWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- m_pix = pixmap;
- m_preview = true;
- m_generatingPreview = false;
-}
+ QHBoxLayout* tipLayout = new QHBoxLayout(tipContent);
+ tipLayout->setMargin(0);
+ tipLayout->addWidget(pixmapLabel);
+ tipLayout->addWidget(metaDataWidget);
-void ToolTipManager::previewFailed(const KFileItem& item)
-{
- Q_UNUSED(item);
- m_generatingPreview = false;
+ return tipContent;
}
#include "tooltipmanager.moc"