]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tooltips/ktooltip.cpp
Forward port for SVN commit 996129: Fix possible crash when no current item is given...
[dolphin.git] / src / tooltips / ktooltip.cpp
index d5aa0f7b77202ddece8ed16944197a1eb65375a8..52a4d4a5f5beb5e010859d3339d03510d2158cab 100644 (file)
  ***************************************************************************/
 
 #include "ktooltip.h"
+#include "ktooltip_p.h"
+#include "ktooltipdelegate.h"
 
 #include <QApplication>
-#include <QMap>
-#include <QPixmap>
 #include <QPainter>
-#include <QVariant>
-#include <QIcon>
 #include <QWidget>
 #include <QToolTip>
-#include <QDebug>
 
 #ifdef Q_WS_X11
 #  include <QX11Info>
 #  include <X11/Xlib.h>
-#  include <X11/extensions/Xrender.h>
 #  include <X11/extensions/shape.h>
 #endif
 
-#include "ktooltip_p.h"
-
-
 // compile with XShape older than 1.0
 #ifndef ShapeInput
 const int ShapeInput = 2;
 #endif
 
 
-class KToolTipItemPrivate
-{
-public:
-    QMap<int, QVariant> map;
-    int type;
-};
-
-KToolTipItem::KToolTipItem(const QString &text, int type)
-    : d(new KToolTipItemPrivate)
-{
-    d->map[Qt::DisplayRole] = text;
-    d->type = type;
-}
-
-KToolTipItem::KToolTipItem(const QIcon &icon, const QString &text, int type)
-    : d(new KToolTipItemPrivate)
-{
-    d->map[Qt::DecorationRole] = icon;
-    d->map[Qt::DisplayRole]    = text;
-    d->type = type;
-}
-
-KToolTipItem::~KToolTipItem()
-{
-    delete d;
-}
-
-int KToolTipItem::type() const
-{
-    return d->type;
-}
-
-QString KToolTipItem::text() const
-{
-    return data(Qt::DisplayRole).toString();
-}
-
-QIcon KToolTipItem::icon() const
-{
-    return qvariant_cast<QIcon>(data(Qt::DecorationRole));
-}
-
-QVariant KToolTipItem::data(int role) const
-{
-    return d->map.value(role);
-}
-
-void KToolTipItem::setData(int role, const QVariant &data)
-{
-    d->map[role] = data;
-    KToolTipManager::instance()->update();
-}
-
-
-
-// ----------------------------------------------------------------------------
-
-
-KStyleOptionToolTip::KStyleOptionToolTip()
-    : fontMetrics(QApplication::font())
-{
-}
-
-
-// ----------------------------------------------------------------------------
-
-
-
-KToolTipDelegate::KToolTipDelegate() : QObject()
-{
-}
-
-KToolTipDelegate::~KToolTipDelegate()
-{
-}
-
-QSize KToolTipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const
-{
-    QSize size;
-    size.rwidth() = option->fontMetrics.width(item->text());
-    size.rheight() = option->fontMetrics.lineSpacing();
-
-    QIcon icon = item->icon();
-    if (!icon.isNull()) {
-        const QSize iconSize = icon.actualSize(option->decorationSize);
-        size.rwidth() += iconSize.width() + 4;
-        size.rheight() = qMax(size.height(), iconSize.height());
-    }
-
-    return size + QSize(20, 20);
-
-}
-
-void KToolTipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option,
-                             const KToolTipItem *item) const
-{
-    bool haveAlpha = haveAlphaChannel();
-    painter->setRenderHint(QPainter::Antialiasing);
-
-    QPainterPath path;
-    if (haveAlpha)
-        path.addRoundRect(option->rect.adjusted(0, 0, -1, -1), 25);
-    else
-        path.addRect(option->rect.adjusted(0, 0, -1, -1));
-
-    QColor color = option->palette.color(QPalette::ToolTipBase);
-    QColor from = color.lighter(105);
-    QColor to   = color.darker(120);
-
-    QLinearGradient gradient(0, 0, 0, 1);
-    gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
-    gradient.setColorAt(0, from);
-    gradient.setColorAt(1, to);
-
-    painter->translate(.5, .5);
-    painter->setPen(QPen(Qt::black, 1));
-    painter->setBrush(gradient);
-    painter->drawPath(path);
-    painter->translate(-.5, -.5);
-
-    if (haveAlpha) {
-        QLinearGradient mask(0, 0, 0, 1);
-        gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
-        gradient.setColorAt(0, QColor(0, 0, 0, 192));
-        gradient.setColorAt(1, QColor(0, 0, 0, 72));
-        painter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
-        painter->fillRect(option->rect, gradient);
-        painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
-    }
-
-    QRect textRect = option->rect.adjusted(10, 10, -10, -10);
-
-    QIcon icon = item->icon();
-    if (!icon.isNull()) {
-        const QSize iconSize = icon.actualSize(option->decorationSize);
-        painter->drawPixmap(textRect.topLeft(), icon.pixmap(iconSize));
-        textRect.adjust(iconSize.width() + 4, 0, 0, 0);
-    }
-    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, item->text());
-}
-
-QRegion KToolTipDelegate::inputShape(const KStyleOptionToolTip *option) const
-{
-    return QRegion(option->rect);
-}
-
-QRegion KToolTipDelegate::shapeMask(const KStyleOptionToolTip *option) const
-{
-    return QRegion(option->rect);
-}
-
-bool KToolTipDelegate::haveAlphaChannel() const
-{
-    return KToolTipManager::instance()->haveAlphaChannel();
-}
-
-
-
 // ----------------------------------------------------------------------------
 
 
-
-class KAbstractToolTipLabel
+class KTipLabel : public QWidget
 {
 public:
-    KAbstractToolTipLabel() {}
-    virtual ~KAbstractToolTipLabel() {}
-
-    virtual void showTip(const QPoint &pos, const KToolTipItem *item) = 0;
-    virtual void moveTip(const QPoint &pos) = 0;
-    virtual void hideTip() = 0;
-
-protected:
-    KStyleOptionToolTip styleOption() const;
-    KToolTipDelegate *delegate() const;
-};
-
-KStyleOptionToolTip KAbstractToolTipLabel::styleOption() const
-{
-     KStyleOptionToolTip option;
-     KToolTipManager::instance()->initStyleOption(&option);
-     return option;
-}
-
-KToolTipDelegate *KAbstractToolTipLabel::delegate() const
-{
-    return KToolTipManager::instance()->delegate();
-}
-
-
-// ----------------------------------------------------------------------------
-
-
-
-class QWidgetLabel : public QWidget, public KAbstractToolTipLabel
-{
-public:
-    QWidgetLabel();
+    KTipLabel();
     void showTip(const QPoint &pos, const KToolTipItem *item);
     void moveTip(const QPoint &pos);
     void hideTip();
@@ -250,239 +52,113 @@ public:
 private:
     void paintEvent(QPaintEvent*);
     QSize sizeHint() const;
+    KStyleOptionToolTip styleOption() const;
+    KToolTipDelegate *delegate() const;
 
 private:
-    const KToolTipItem *currentItem;
+    const KToolTipItem *m_currentItem;
 };
 
-QWidgetLabel::QWidgetLabel() : QWidget(0, Qt::ToolTip)
+KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip)
 {
-    if (KToolTipManager::instance()->haveAlphaChannel()) {
+#ifdef Q_WS_X11
+    if (QX11Info::isCompositingManagerRunning()) {
         setAttribute(Qt::WA_TranslucentBackground);
     }
+#endif
 }
 
-void QWidgetLabel::showTip(const QPoint &pos, const KToolTipItem *item)
+void KTipLabel::showTip(const QPoint &pos, const KToolTipItem *item)
 {
-    currentItem = item;
+    m_currentItem = item;
     move(pos);
     show();
 }
 
-void QWidgetLabel::hideTip()
+void KTipLabel::hideTip()
 {
     hide();
-    currentItem = 0;
+    m_currentItem = 0;
 }
 
-void QWidgetLabel::moveTip(const QPoint &pos)
+void KTipLabel::moveTip(const QPoint &pos)
 {
     move(pos);
 }
 
-void QWidgetLabel::paintEvent(QPaintEvent*)
+void KTipLabel::paintEvent(QPaintEvent*)
 {
+    if (!m_currentItem)
+        return;
+    
     KStyleOptionToolTip option = styleOption();
     option.rect = rect();
 
-    setMask(delegate()->shapeMask(&option));
+#ifdef Q_WS_X11
+    if (QX11Info::isCompositingManagerRunning())
+        XShapeCombineRegion(x11Info().display(), winId(), ShapeInput, 0, 0,
+                            delegate()->inputShape(option).handle(), ShapeSet);
+    else
+#endif
+    setMask(delegate()->shapeMask(option));
 
     QPainter p(this);
     p.setFont(option.font);
     p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
-    delegate()->paint(&p, &option, currentItem);
+    delegate()->paint(&p, option, *m_currentItem);
 }
 
-QSize QWidgetLabel::sizeHint() const
+QSize KTipLabel::sizeHint() const
 {
-    if (!currentItem)
+    if (!m_currentItem)
         return QSize();
 
     KStyleOptionToolTip option = styleOption();
-    return delegate()->sizeHint(&option, currentItem);
+    return delegate()->sizeHint(option, *m_currentItem);
 }
 
-
-
-// ----------------------------------------------------------------------------
-
-
-
-#ifdef Q_WS_X11
-
-// X11 specific label that displays the tip in an ARGB window.
-class ArgbLabel : public KAbstractToolTipLabel
-{
-public:
-    ArgbLabel(Visual *visual, int depth);
-    ~ArgbLabel();
-
-    void showTip(const QPoint &pos, const KToolTipItem *item);
-    void moveTip(const QPoint &pos);
-    void hideTip();
-
-private:
-    Window    window;
-    Colormap  colormap;
-    Picture   picture;
-    bool      mapped;
-};
-
-ArgbLabel::ArgbLabel(Visual *visual, int depth)
+KStyleOptionToolTip KTipLabel::styleOption() const
 {
-    Display *dpy = QX11Info::display();
-    Window root = QX11Info::appRootWindow();
-    colormap = XCreateColormap(dpy, QX11Info::appRootWindow(), visual, AllocNone);
-
-    XSetWindowAttributes attr;
-    attr.border_pixel      = 0;
-    attr.background_pixel  = 0;
-    attr.colormap          = colormap;
-    attr.override_redirect = True;
-
-    window = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, depth, InputOutput, visual,
-                           CWBorderPixel | CWBackPixel | CWColormap |
-                           CWOverrideRedirect, &attr);
-
-    // ### TODO: Set the WM hints so KWin can identify this window as a
-    //           tooltip.
-
-    XRenderPictFormat *format = XRenderFindVisualFormat(dpy, visual);
-    picture = XRenderCreatePicture(dpy, window, format, 0, 0);
-
-    mapped = false;
-}
-
-ArgbLabel::~ArgbLabel()
-{
-    Display *dpy = QX11Info::display();
-    XRenderFreePicture(dpy, picture);
-    XDestroyWindow(dpy, window);
-    XFreeColormap(dpy, colormap);
-}
-
-void ArgbLabel::showTip(const QPoint &pos, const KToolTipItem *item)
-{
-    Display *dpy = QX11Info::display();
-    KStyleOptionToolTip option = styleOption();
-    const QSize size = delegate()->sizeHint(&option, item);
-    option.rect = QRect(QPoint(), size);
-
-    QPixmap pixmap(size);
-    pixmap.fill(Qt::transparent);
-
-    QPainter p(&pixmap);
-    p.setFont(option.font);
-    p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
-    delegate()->paint(&p, &option, item);
-
-    // Resize, position and show the window.
-    XMoveResizeWindow(dpy, window, pos.x(), pos.y(), size.width(), size.height());
-
-    if (KToolTipManager::instance()->haveAlphaChannel()) {
-        const QRegion region = delegate()->inputShape(&option);
-        XShapeCombineRegion(dpy, window, ShapeInput, 0, 0, region.handle(), ShapeSet);
-    } else {
-        const QRegion region = delegate()->shapeMask(&option);
-        XShapeCombineRegion(dpy, window, ShapeBounding, 0, 0, region.handle(), ShapeSet);
-    }
-
-    XMapWindow(dpy, window);
-
-    // Blit the pixmap with the tip contents to the window.
-    // Since the window is override-redirect and an ARGB32 window,
-    // which always has an offscreen pixmap, there's no need to
-    // wait for an Expose event, or to process those.
-    XRenderComposite(dpy, PictOpSrc, pixmap.x11PictureHandle(), None,
-                     picture, 0, 0, 0, 0, 0, 0, size.width(), size.height());
-
-    mapped = true;
-}
-
-void ArgbLabel::moveTip(const QPoint &pos)
-{
-    if (mapped)
-        XMoveWindow(QX11Info::display(), window, pos.x(), pos.y());
+     KStyleOptionToolTip option;
+     KToolTipManager::instance()->initStyleOption(&option);
+     return option;
 }
 
-void ArgbLabel::hideTip()
+KToolTipDelegate *KTipLabel::delegate() const
 {
-    if (mapped) {
-        Display *dpy = QX11Info::display();
-        XUnmapWindow(dpy, window);
-       mapped = false;
-    }
+    return KToolTipManager::instance()->delegate();
 }
 
-#endif // Q_WS_X11
-
-
-
 
 // ----------------------------------------------------------------------------
 
 
-
-
 KToolTipManager *KToolTipManager::s_instance = 0;
 
 KToolTipManager::KToolTipManager()
-       : label(0), currentItem(0), m_delegate(0)
+    : m_label(new KTipLabel), m_currentItem(0), m_delegate(0)
 {
-#ifdef Q_WS_X11
-    Display *dpy      = QX11Info::display();
-    int screen        = DefaultScreen(dpy);
-    int depth         = DefaultDepth(dpy, screen);
-    Visual *visual    = DefaultVisual(dpy, screen);
-    net_wm_cm_s0      = XInternAtom(dpy, "_NET_WM_CM_S0", False);
-    haveArgbVisual    = false;
-
-    int nvi;
-    XVisualInfo templ;
-    templ.screen  = screen;
-    templ.depth   = 32;
-    templ.c_class = TrueColor;
-    XVisualInfo *xvi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
-                                      VisualClassMask, &templ, &nvi);
-
-    for (int i = 0; i < nvi; ++i)
-    {
-        XRenderPictFormat *format = XRenderFindVisualFormat(dpy, xvi[i].visual);
-        if (format->type == PictTypeDirect && format->direct.alphaMask)
-        {
-            visual   = xvi[i].visual;
-            depth    = xvi[i].depth;
-            haveArgbVisual = true;
-            break;
-        }
-    }
-
-    if (haveArgbVisual)
-        label = new ArgbLabel(visual, depth);
-    else
-#endif
-        label = new QWidgetLabel();
 }
 
 KToolTipManager::~KToolTipManager()
 {
-    delete label;
-    delete currentItem;
+    delete m_label;
+    delete m_currentItem;
 }
 
 void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item)
 {
     hideTip();
-    label->showTip(pos, item);
-    currentItem = item;
+    m_label->showTip(pos, item);
+    m_currentItem = item;
     m_tooltipPos = pos;
 }
 
 void KToolTipManager::hideTip()
 {
-    label->hideTip();
-    delete currentItem;
-    currentItem = 0;
+    m_label->hideTip();
+    delete m_currentItem;
+    m_currentItem = 0;
 }
 
 void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
@@ -497,18 +173,6 @@ void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
     option->decorationSize = QSize(32, 32);
 }
 
-bool KToolTipManager::haveAlphaChannel() const
-{
-#ifdef Q_WS_X11
-    // ### This is a synchronous call - ideally we'd use a selection
-    //     watcher to avoid it.
-    return haveArgbVisual &&
-        XGetSelectionOwner(QX11Info::display(), net_wm_cm_s0) != None;
-#else
-    return false;
-#endif
-}
-
 void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
 {
     m_delegate = delegate;
@@ -516,9 +180,9 @@ void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
 
 void KToolTipManager::update()
 {
-    if (currentItem == 0)
+    if (m_currentItem == 0)
         return;
-    label->showTip(m_tooltipPos, currentItem);
+    m_label->showTip(m_tooltipPos, m_currentItem);
 }
 
 KToolTipDelegate *KToolTipManager::delegate() const
@@ -527,11 +191,9 @@ KToolTipDelegate *KToolTipManager::delegate() const
 }
 
 
-
 // ----------------------------------------------------------------------------
 
 
-
 namespace KToolTip
 {
     void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect)