X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/846b21edf57e7aeb83e84ee68e9f61c5981fbacd..ae17139546eedbc1e7cc1e887b06a07122ee3a2a:/src/selectiontoggle.cpp diff --git a/src/selectiontoggle.cpp b/src/selectiontoggle.cpp index 7a3f0ef56..6608b5821 100644 --- a/src/selectiontoggle.cpp +++ b/src/selectiontoggle.cpp @@ -19,9 +19,11 @@ #include "selectiontoggle.h" +#include #include #include #include +#include #include #include @@ -32,15 +34,19 @@ SelectionToggle::SelectionToggle(QWidget* parent) : QAbstractButton(parent), m_isHovered(false), + m_leftMouseButtonPressed(false), m_fadingValue(0), m_icon(), m_fadingTimeLine(0) { + setFocusPolicy(Qt::NoFocus); parent->installEventFilter(this); resize(sizeHint()); - m_icon = KIconLoader::global()->loadIcon("dialog-ok", - KIconLoader::NoGroup, - KIconLoader::SizeSmall); + setIconOverlay(isChecked()); + connect(this, SIGNAL(toggled(bool)), + this, SLOT(setIconOverlay(bool))); + connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)), + this, SLOT(refreshIcon())); } SelectionToggle::~SelectionToggle() @@ -54,21 +60,21 @@ QSize SelectionToggle::sizeHint() const void SelectionToggle::reset() { - m_item = KFileItem(); + m_url = KUrl(); hide(); } -void SelectionToggle::setFileItem(const KFileItem& item) +void SelectionToggle::setUrl(const KUrl& url) { - m_item = item; - if (!item.isNull()) { + m_url = url; + if (!url.isEmpty()) { startFading(); } } -KFileItem SelectionToggle::fileItem() const +KUrl SelectionToggle::url() const { - return m_item; + return m_url; } void SelectionToggle::setVisible(bool visible) @@ -84,9 +90,27 @@ void SelectionToggle::setVisible(bool visible) bool SelectionToggle::eventFilter(QObject* obj, QEvent* event) { - if ((obj == parent()) && (event->type() == QEvent::Leave)) { - hide(); + if (obj == parent()) { + switch (event->type()) { + case QEvent::Leave: + hide(); + break; + + case QEvent::MouseMove: + if (m_leftMouseButtonPressed) { + // Don't forward mouse move events to the viewport, + // otherwise a rubberband selection will be shown when + // clicking on the selection toggle and moving the mouse + // above the viewport. + return true; + } + break; + + default: + break; + } } + return QAbstractButton::eventFilter(obj, event); } @@ -101,6 +125,8 @@ void SelectionToggle::enterEvent(QEvent* event) m_fadingTimeLine->stop(); } m_fadingValue = 255; + setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") : + i18nc("@info:tooltip", "Select Item")); update(); } @@ -111,11 +137,30 @@ void SelectionToggle::leaveEvent(QEvent* event) update(); } +void SelectionToggle::mousePressEvent(QMouseEvent* event) +{ + QAbstractButton::mousePressEvent(event); + m_leftMouseButtonPressed = (event->buttons() & Qt::LeftButton); +} + +void SelectionToggle::mouseReleaseEvent(QMouseEvent* event) +{ + QAbstractButton::mouseReleaseEvent(event); + m_leftMouseButtonPressed = (event->buttons() & Qt::LeftButton); +} + +void SelectionToggle::resizeEvent(QResizeEvent* event) +{ + QAbstractButton::resizeEvent(event); + setIconOverlay(isChecked()); +} + void SelectionToggle::paintEvent(QPaintEvent* event) { QPainter painter(this); painter.setClipRect(event->rect()); + // draw the icon overlay if (m_isHovered) { KIconEffect iconEffect; QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState); @@ -146,11 +191,28 @@ void SelectionToggle::setFadingValue(int value) update(); } +void SelectionToggle::setIconOverlay(bool checked) +{ + const char* icon = checked ? "list-remove" : "list-add"; + m_icon = KIconLoader::global()->loadIcon(icon, + KIconLoader::NoGroup, + qMin(width(), height())); + update(); +} + +void SelectionToggle::refreshIcon() +{ + setIconOverlay(isChecked()); +} + void SelectionToggle::startFading() { Q_ASSERT(m_fadingTimeLine == 0); - m_fadingTimeLine = new QTimeLine(2000, this); + const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects; + const int duration = animate ? 600 : 1; + + m_fadingTimeLine = new QTimeLine(duration, this); connect(m_fadingTimeLine, SIGNAL(frameChanged(int)), this, SLOT(setFadingValue(int))); m_fadingTimeLine->setFrameRange(0, 255);