]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/selectiontoggle.cpp
Use the actions "new_tab" and "new_window" for the viewport-contextmenu instead of...
[dolphin.git] / src / views / selectiontoggle.cpp
index 6608b582126f072e2e2c0b6d26794f1dfe64ef08..cbd273b11924f577db2a899a286775bc9987a782 100644 (file)
@@ -25,6 +25,7 @@
 #include <kiconeffect.h>
 #include <klocale.h>
 
+#include <QApplication>
 #include <QPainter>
 #include <QPaintEvent>
 #include <QRect>
@@ -35,7 +36,9 @@ SelectionToggle::SelectionToggle(QWidget* parent) :
     QAbstractButton(parent),
     m_isHovered(false),
     m_leftMouseButtonPressed(false),
+    m_appliedArrowCursor(false),
     m_fadingValue(0),
+    m_margin(0),
     m_icon(),
     m_fadingTimeLine(0)
 {
@@ -72,6 +75,19 @@ void SelectionToggle::setUrl(const KUrl& url)
     }
 }
 
+void SelectionToggle::setMargin(int margin)
+{
+    if (margin != m_margin) {
+        m_margin = margin;
+        update();
+    }
+}
+
+int SelectionToggle::margin() const
+{
+    return m_margin;
+}
+
 KUrl SelectionToggle::url() const
 {
     return m_url;
@@ -118,6 +134,19 @@ void SelectionToggle::enterEvent(QEvent* event)
 {
     QAbstractButton::enterEvent(event);
 
+    if (!m_appliedArrowCursor) {
+        m_appliedArrowCursor = true;
+        // Apply the arrow asynchronously. This is required for
+        // the following usecase:
+        // 1. Cursor is above the viewport left beside an item
+        // 2. Cursor is moved above the item, so that the selection-toggle
+        //    and the item are entered equally.
+        // In this situation it is not defined who gets the enter-event first.
+        // As the selection-toggle is above the item, it should overwrite possible
+        // cursor changes done by the item.
+        QTimer::singleShot(0, this, SLOT(applyArrowCursor()));
+    }
+
     // if the mouse cursor is above the selection toggle, display
     // it immediately without fading timer
     m_isHovered = true;
@@ -133,6 +162,14 @@ void SelectionToggle::enterEvent(QEvent* event)
 void SelectionToggle::leaveEvent(QEvent* event)
 {
     QAbstractButton::leaveEvent(event);
+
+    if (m_appliedArrowCursor) {
+        // Reset the cursor asynchronously. See SelectionToggle::enterEvent()
+        // for a more information.
+        m_appliedArrowCursor = false;
+        QTimer::singleShot(0, this, SLOT(restoreCursor()));
+    }
+
     m_isHovered = false;
     update();
 }
@@ -161,10 +198,11 @@ void SelectionToggle::paintEvent(QPaintEvent* event)
     painter.setClipRect(event->rect());
 
     // draw the icon overlay
+    const QPoint pos(m_margin, m_margin);
     if (m_isHovered) {
-        KIconEffect iconEffect;
-        QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
-        painter.drawPixmap(0, 0, activeIcon);
+        KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
+        QPixmap activeIcon = iconEffect->apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
+        painter.drawPixmap(pos, activeIcon);
     } else {
         if (m_fadingValue < 255) {
             // apply an alpha mask respecting the fading value to the icon
@@ -173,12 +211,13 @@ void SelectionToggle::paintEvent(QPaintEvent* event)
             const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
             alphaMask.fill(color);
             icon.setAlphaChannel(alphaMask);
-            painter.drawPixmap(0, 0, icon);
+            painter.drawPixmap(pos, icon);
         } else {
             // no fading is required
-            painter.drawPixmap(0, 0, m_icon);
+            painter.drawPixmap(pos, m_icon);
         }
     }
+
 }
 
 void SelectionToggle::setFadingValue(int value)
@@ -194,9 +233,10 @@ void SelectionToggle::setFadingValue(int value)
 void SelectionToggle::setIconOverlay(bool checked)
 {
     const char* icon = checked ? "list-remove" : "list-add";
+    const int size = qMin(width() - 2 * m_margin, height() - 2 * m_margin);
     m_icon = KIconLoader::global()->loadIcon(icon,
                                              KIconLoader::NoGroup,
-                                             qMin(width(), height()));
+                                             size);
     update();
 }
 
@@ -205,6 +245,16 @@ void SelectionToggle::refreshIcon()
     setIconOverlay(isChecked());
 }
 
+void SelectionToggle::applyArrowCursor()
+{
+    QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
+}
+
+void SelectionToggle::restoreCursor()
+{
+    QApplication::restoreOverrideCursor();
+}
+
 void SelectionToggle::startFading()
 {
     Q_ASSERT(m_fadingTimeLine == 0);