Skip to content

Commit

Permalink
Highlight on hover for dock items.
Browse files Browse the repository at this point in the history
  • Loading branch information
MadFishTheOne committed Apr 23, 2011
1 parent 3a1c330 commit 75f12c5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
2 changes: 1 addition & 1 deletion applet.cpp
Expand Up @@ -69,7 +69,7 @@ void Applet::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QW

void Applet::animateHighlight()
{
static const qreal highlightAnimationSpeed = 0.1;
static const qreal highlightAnimationSpeed = 0.15;
if(isHighlighted())
{
m_highlightIntensity += highlightAnimationSpeed;
Expand Down
55 changes: 49 additions & 6 deletions dockapplet.cpp
@@ -1,5 +1,6 @@
#include "dockapplet.h"

#include <QtCore/QTimer>
#include <QtGui/QPainter>
#include <QtGui/QFontMetrics>
#include <QtGui/QGraphicsScene>
Expand All @@ -12,7 +13,9 @@ DockItem::DockItem(DockApplet* dockApplet)
{
m_dockApplet = dockApplet;

this->setParentItem(m_dockApplet);
setParentItem(m_dockApplet);
setAcceptsHoverEvents(true);
setAcceptedMouseButtons(Qt::LeftButton);

m_textItem = new TextGraphicsItem(this);
m_textItem->setColor(Qt::white);
Expand All @@ -39,7 +42,7 @@ void DockItem::updateContent()
QFontMetrics fontMetrics(m_textItem->font());
QString shortName = fontMetrics.elidedText(m_clients[0]->name(), Qt::ElideRight, m_size.width() - 36);
m_textItem->setText(shortName);
m_textItem->setPos(28.0, m_dockApplet->panelWindow()->textBaseLine() - 4.0);
m_textItem->setPos(28.0, m_dockApplet->panelWindow()->textBaseLine());

m_iconItem->setPixmap(m_clients[0]->icon().pixmap(16));
m_iconItem->setPos(8.0, m_size.height()/2 - 8);
Expand Down Expand Up @@ -88,10 +91,50 @@ void DockItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
painter->setPen(Qt::NoPen);
QPointF center(m_size.width()/2.0, m_size.height() + 32.0);
QRadialGradient gradient(center, 200.0, center);
gradient.setColorAt(0, QColor(255, 255, 255, 80));
gradient.setColorAt(0, QColor(255, 255, 255, 80 + static_cast<int>(80*m_highlightIntensity)));
gradient.setColorAt(1, QColor(255, 255, 255, 0));
painter->setBrush(QBrush(gradient));
painter->drawRoundedRect(boundingRect(), 3.0, 3.0);
painter->drawRoundedRect(QRectF(0.0, 4.0, m_size.width(), m_size.height() - 8.0), 3.0, 3.0);
}

void DockItem::animateHighlight()
{
static const qreal highlightAnimationSpeed = 0.15;
if(isUnderMouse())
{
m_highlightIntensity += highlightAnimationSpeed;
if(m_highlightIntensity > 1.0)
m_highlightIntensity = 1.0;
else
QTimer::singleShot(20, this, SLOT(animateHighlight()));
}
else
{
m_highlightIntensity -= highlightAnimationSpeed;
if(m_highlightIntensity < 0.0)
m_highlightIntensity = 0.0;
else
QTimer::singleShot(20, this, SLOT(animateHighlight()));
}
update();
}

void DockItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
animateHighlight();
}

void DockItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
animateHighlight();
}

void DockItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
}

void DockItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
}

Client::Client(DockApplet* dockApplet, unsigned long handle)
Expand Down Expand Up @@ -185,8 +228,8 @@ void DockApplet::updateLayout()
int spaceForThisClient = spaceForOneClient;
if(spaceForThisClient > 256)
spaceForThisClient = 256;
m_dockItems[i]->setPosition(QPoint(currentPosition, 4));
m_dockItems[i]->setSize(QSize(spaceForThisClient - 4, m_size.height() - 8));
m_dockItems[i]->setPosition(QPoint(currentPosition, 0));
m_dockItems[i]->setSize(QSize(spaceForThisClient - 4, m_size.height()));
currentPosition += spaceForThisClient;
}

Expand Down
14 changes: 13 additions & 1 deletion dockapplet.h
Expand Up @@ -16,8 +16,10 @@ class Client;
// There isn't one to one relationship between window (client) and dock item, that's why
// it's separate entity. One dock item can represent pinned launcher and one or more opened
// windows of that application.
class DockItem: public QGraphicsItem
class DockItem: public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
public:
DockItem(DockApplet* dockApplet);
~DockItem();
Expand All @@ -38,12 +40,22 @@ class DockItem: public QGraphicsItem
QRectF boundingRect() const;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);

public slots:
void animateHighlight();

protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent* event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent* event);
void mousePressEvent(QGraphicsSceneMouseEvent* event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);

private:
DockApplet* m_dockApplet;
TextGraphicsItem* m_textItem;
QGraphicsPixmapItem* m_iconItem;
QVector<Client*> m_clients;
QSize m_size;
qreal m_highlightIntensity;
};

// Used for tracking connected windows (X11 clients).
Expand Down

0 comments on commit 75f12c5

Please sign in to comment.