add hover effects

Change-Id: I4bcdceb7c3a8b41e6c8b64c0e72ade11a1e4fd81
This commit is contained in:
石博文 2016-06-27 20:16:35 +08:00 committed by Hualet Wang
parent 218ad40609
commit 8b479bbf14
8 changed files with 94 additions and 12 deletions

View File

@ -28,7 +28,8 @@ SOURCES += main.cpp \
dbus/dbusmenumanager.cpp \
dbus/dbusmenu.cpp \
item/pluginsitem.cpp \
controller/dockpluginscontroller.cpp
controller/dockpluginscontroller.cpp \
util/imagefactory.cpp
HEADERS += \
window/mainwindow.h \
@ -48,7 +49,8 @@ HEADERS += \
dbus/dbusmenumanager.h \
dbus/dbusmenu.h \
item/pluginsitem.h \
controller/dockpluginscontroller.h
controller/dockpluginscontroller.h \
util/imagefactory.h
dbus_service.files += com.deepin.dde.dock.service
dbus_service.path = /usr/share/dbus-1/services

View File

@ -1,6 +1,7 @@
#include "appitem.h"
#include "util/themeappicon.h"
#include "util/imagefactory.h"
#include <QPainter>
#include <QDrag>
@ -130,11 +131,14 @@ void AppItem::paintEvent(QPaintEvent *e)
}
}
// icon
QPixmap pixmap = DockDisplayMode == Efficient ? m_smallIcon : m_largeIcon;
// ligher icon
if (m_hover)
pixmap = ImageFactory::lighter(pixmap);
// draw icon
if (DockDisplayMode == Efficient)
painter.drawPixmap(itemRect.center() - m_smallIcon.rect().center(), m_smallIcon);
else
painter.drawPixmap(itemRect.center() - m_largeIcon.rect().center(), m_largeIcon);
painter.drawPixmap(itemRect.center() - pixmap.rect().center(), pixmap);
}
void AppItem::mouseReleaseEvent(QMouseEvent *e)

View File

@ -12,6 +12,7 @@ DisplayMode DockItem::DockDisplayMode = DisplayMode::Efficient;
DockItem::DockItem(const ItemType type, QWidget *parent)
: QWidget(parent),
m_type(type),
m_hover(false),
m_menuManagerInter(new DBusMenuManager(this))
{
@ -43,6 +44,24 @@ void DockItem::mousePressEvent(QMouseEvent *e)
return showContextMenu();
}
void DockItem::enterEvent(QEvent *e)
{
m_hover = true;
update();
return QWidget::enterEvent(e);
}
void DockItem::leaveEvent(QEvent *e)
{
m_hover = false;
update();
return QWidget::leaveEvent(e);
}
const QRect DockItem::perfectIconRect() const
{
const QRect itemRect = rect();

View File

@ -33,6 +33,8 @@ signals:
protected:
void paintEvent(QPaintEvent *e);
void mousePressEvent(QMouseEvent *e);
void enterEvent(QEvent *e);
void leaveEvent(QEvent *e);
const QRect perfectIconRect() const;
@ -42,6 +44,7 @@ protected:
protected:
ItemType m_type;
bool m_hover;
DBusMenuManager *m_menuManagerInter;

View File

@ -1,5 +1,6 @@
#include "launcheritem.h"
#include "util/themeappicon.h"
#include "util/imagefactory.h"
#include <QPainter>
#include <QProcess>
@ -18,10 +19,11 @@ void LauncherItem::paintEvent(QPaintEvent *e)
return;
QPainter painter(this);
if (DockDisplayMode == Fashion)
painter.drawPixmap(rect().center() - m_largeIcon.rect().center(), m_largeIcon);
else
painter.drawPixmap(rect().center() - m_smallIcon.rect().center(), m_smallIcon);
QPixmap pixmap = DockDisplayMode == Fashion ? m_largeIcon : m_smallIcon;
if (m_hover)
pixmap = ImageFactory::lighter(pixmap);
painter.drawPixmap(rect().center() - pixmap.rect().center(), pixmap);
}
void LauncherItem::resizeEvent(QResizeEvent *e)

View File

@ -1,7 +1,8 @@
#include "pluginsitem.h"
#include "pluginsiteminterface.h"
#include "util/imagefactory.h"
#include <QPainter>
#include <QBoxLayout>
#include <QMouseEvent>
@ -92,7 +93,8 @@ void PluginsItem::paintEvent(QPaintEvent *e)
const QIcon icon = m_pluginInter->itemIcon(m_itemKey);
const QRect iconRect = perfectIconRect();
const QPixmap pixmap = icon.pixmap(iconRect.size());
painter.drawPixmap(iconRect, pixmap);
painter.drawPixmap(iconRect, m_hover ? ImageFactory::lighter(pixmap) : pixmap);
}
bool PluginsItem::eventFilter(QObject *o, QEvent *e)

View File

@ -0,0 +1,32 @@
#include "imagefactory.h"
#include <QDebug>
ImageFactory::ImageFactory(QObject *parent)
: QObject(parent)
{
}
QPixmap ImageFactory::lighter(const QPixmap pixmap, const int delta)
{
QImage image = pixmap.toImage();
const int width = image.width();
const int height = image.height();
const int bytesPerPixel = image.bytesPerLine() / image.width();
for (int i(0); i != height; ++i)
{
uchar *scanLine = image.scanLine(i);
for (int j(0); j != width; ++j)
{
QRgb &rgba = *(QRgb*)scanLine;
if (qAlpha(rgba) && (qRed(rgba) || qGreen(rgba) || qBlue(rgba)))
rgba = QColor::fromRgba(rgba).lighter(delta).rgba();
scanLine += bytesPerPixel;
}
}
return QPixmap::fromImage(image);
}

18
frame/util/imagefactory.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef IMAGEFACTORY_H
#define IMAGEFACTORY_H
#include <QObject>
#include <QPixmap>
#include <QImage>
class ImageFactory : public QObject
{
Q_OBJECT
public:
explicit ImageFactory(QObject *parent = 0);
static QPixmap lighter(const QPixmap pixmap, const int delta = 120);
};
#endif // IMAGEFACTORY_H