optimize item paint

Change-Id: Iad05a1967417cf6a3dc4f2ab47e8de0f0d1d5e50
This commit is contained in:
石博文 2016-08-19 10:19:38 +08:00
parent 7024805a0f
commit 327984199c
Notes: Deepin Code Review 2016-08-19 02:26:43 +00:00
Verified+1: Anonymous Coward #1000004
Code-Review+2: 石博文 <sbw@sbw.so>
Submitted-by: 石博文 <sbw@sbw.so>
Submitted-at: Fri, 19 Aug 2016 02:26:43 +0000
Reviewed-on: https://cr.deepin.io/15363
Project: dde/dde-dock
Branch: refs/heads/master
10 changed files with 145 additions and 115 deletions

View File

@ -182,6 +182,8 @@ DockItemController::DockItemController(QObject *parent)
connect(m_pluginsInter, &DockPluginsController::pluginItemInserted, this, &DockItemController::pluginItemInserted, Qt::QueuedConnection); connect(m_pluginsInter, &DockPluginsController::pluginItemInserted, this, &DockItemController::pluginItemInserted, Qt::QueuedConnection);
connect(m_pluginsInter, &DockPluginsController::pluginItemRemoved, this, &DockItemController::pluginItemRemoved, Qt::QueuedConnection); connect(m_pluginsInter, &DockPluginsController::pluginItemRemoved, this, &DockItemController::pluginItemRemoved, Qt::QueuedConnection);
QMetaObject::invokeMethod(this, "refershItemsIcon", Qt::QueuedConnection);
} }
void DockItemController::appItemAdded(const QDBusObjectPath &path, const int index) void DockItemController::appItemAdded(const QDBusObjectPath &path, const int index)

View File

@ -194,49 +194,16 @@ void AppItem::paintEvent(QPaintEvent *e)
painter.drawPixmap(p, activePixmap); painter.drawPixmap(p, activePixmap);
else else
painter.drawPixmap(p, pixmap); painter.drawPixmap(p, pixmap);
// const int activeLineWidth = 2;
// const int activeLineLength = 20;
// QRect activeRect = itemRect;
// switch (DockPosition)
// {
// case Top:
// activeRect.setBottom(activeRect.top() + activeLineWidth);
// activeRect.moveBottom(activeRect.bottom() + 1);
// activeRect.setWidth(activeLineLength);
// activeRect.moveLeft((itemRect.width() - activeRect.width()) / 2);
// break;
// case Bottom:
// activeRect.setTop(activeRect.bottom() - activeLineWidth);
// activeRect.moveTop(activeRect.top() - 1);
// activeRect.setWidth(activeLineLength);
// activeRect.moveLeft((itemRect.width() - activeRect.width()) / 2);
// break;
// case Left:
// activeRect.setRight(activeRect.left() + activeLineWidth);
// activeRect.moveRight(activeRect.right() + 1);
// activeRect.setHeight(activeLineLength);
// activeRect.moveTop((itemRect.height() - activeRect.height()) / 2);
// break;
// case Right:
// activeRect.setLeft(activeRect.right() - activeLineWidth);
// activeRect.moveLeft(activeRect.left() - 1);
// activeRect.setHeight(activeLineLength);
// activeRect.moveTop((itemRect.height() - activeRect.height()) / 2);
// break;
// }
// painter.fillRect(activeRect, QColor(163, 167, 166));
} }
} }
// icon // icon
const QPixmap pixmap = DockDisplayMode == Efficient ? m_smallIcon : m_largeIcon; const QPixmap pixmap = DockDisplayMode == Efficient ? m_smallIcon : m_largeIcon;
// draw icon // draw ligher/normal icon
painter.drawPixmap(itemRect.center() - pixmap.rect().center(), pixmap);
// draw ligher
if (m_hover) if (m_hover)
painter.drawPixmap(itemRect.center() - pixmap.rect().center(), ImageFactory::lighterEffect(pixmap)); painter.drawPixmap(itemRect.center() - pixmap.rect().center(), ImageFactory::lighterEffect(pixmap));
else
painter.drawPixmap(itemRect.center() - pixmap.rect().center(), pixmap);
// Update the window icon geometry when the icon is changed. // Update the window icon geometry when the icon is changed.
m_updateIconGeometryTimer->start(); m_updateIconGeometryTimer->start();

View File

@ -32,6 +32,21 @@ bool ContainerItem::contains(DockItem * const item)
return false; return false;
} }
void ContainerItem::refershIcon()
{
QPixmap icon;
switch (DockPosition)
{
case Top: icon = QPixmap(":/icons/resources/arrow-down.svg"); break;
case Left: icon = QPixmap(":/icons/resources/arrow-right.svg"); break;
case Bottom: icon = QPixmap(":/icons/resources/arrow-up.svg"); break;
case Right: icon = QPixmap(":/icons/resources/arrow-left.svg"); break;
default: Q_UNREACHABLE();
}
m_icon = icon;
}
void ContainerItem::dragEnterEvent(QDragEnterEvent *e) void ContainerItem::dragEnterEvent(QDragEnterEvent *e)
{ {
if (m_containerWidget->allowDragEnter(e)) if (m_containerWidget->allowDragEnter(e))
@ -52,18 +67,8 @@ void ContainerItem::paintEvent(QPaintEvent *e)
if (DockDisplayMode == Dock::Fashion) if (DockDisplayMode == Dock::Fashion)
return; return;
QPixmap icon;
switch (DockPosition)
{
case Top: icon = QPixmap(":/icons/resources/arrow-down.svg"); break;
case Left: icon = QPixmap(":/icons/resources/arrow-right.svg"); break;
case Bottom: icon = QPixmap(":/icons/resources/arrow-up.svg"); break;
case Right: icon = QPixmap(":/icons/resources/arrow-left.svg"); break;
default: Q_UNREACHABLE();
}
QPainter painter(this); QPainter painter(this);
painter.drawPixmap(rect().center() - icon.rect().center(), icon); painter.drawPixmap(rect().center() - m_icon.rect().center(), m_icon);
} }
void ContainerItem::mouseReleaseEvent(QMouseEvent *e) void ContainerItem::mouseReleaseEvent(QMouseEvent *e)

View File

@ -19,6 +19,9 @@ public:
void removeItem(DockItem * const item); void removeItem(DockItem * const item);
bool contains(DockItem * const item); bool contains(DockItem * const item);
public slots:
void refershIcon();
protected: protected:
void dragEnterEvent(QDragEnterEvent *e); void dragEnterEvent(QDragEnterEvent *e);
void dragMoveEvent(QDragMoveEvent *e); void dragMoveEvent(QDragMoveEvent *e);
@ -28,6 +31,7 @@ protected:
private: private:
ContainerWidget *m_containerWidget; ContainerWidget *m_containerWidget;
QPixmap m_icon;
}; };
#endif // CONTAINERITEM_H #endif // CONTAINERITEM_H

View File

@ -29,10 +29,11 @@ void LauncherItem::paintEvent(QPaintEvent *e)
QPainter painter(this); QPainter painter(this);
const QPixmap pixmap = DockDisplayMode == Fashion ? m_largeIcon : m_smallIcon; const QPixmap pixmap = DockDisplayMode == Fashion ? m_largeIcon : m_smallIcon;
painter.drawPixmap(rect().center() - pixmap.rect().center(), pixmap);
if (m_hover) if (m_hover)
painter.drawPixmap(rect().center() - pixmap.rect().center(), ImageFactory::lighterEffect(pixmap)); painter.drawPixmap(rect().center() - pixmap.rect().center(), ImageFactory::lighterEffect(pixmap));
else
painter.drawPixmap(rect().center() - pixmap.rect().center(), pixmap);
} }
void LauncherItem::resizeEvent(QResizeEvent *e) void LauncherItem::resizeEvent(QResizeEvent *e)

View File

@ -20,6 +20,13 @@ QSize DatetimeWidget::sizeHint() const
return fm.boundingRect("88:88").size() + QSize(20, 10); return fm.boundingRect("88:88").size() + QSize(20, 10);
} }
void DatetimeWidget::resizeEvent(QResizeEvent *e)
{
m_cachedTime.clear();
QWidget::resizeEvent(e);
}
void DatetimeWidget::paintEvent(QPaintEvent *e) void DatetimeWidget::paintEvent(QPaintEvent *e)
{ {
Q_UNUSED(e); Q_UNUSED(e);
@ -36,44 +43,59 @@ void DatetimeWidget::paintEvent(QPaintEvent *e)
return; return;
} }
// draw fashion mode datetime plugin const QString currentTimeString = current.toString("HHmm");
const int perfectIconSize = qMin(width(), height()) * 0.8; // check cache valid
const QString currentTimeString = current.toString("HHmmss"); if (m_cachedTime != currentTimeString)
const QRect r = rect(); {
m_cachedTime = currentTimeString;
// draw background // draw new pixmap
const QPixmap background = loadSvg(":/icons/resources/icons/background.svg", QSize(perfectIconSize, perfectIconSize)); m_cachedTime = currentTimeString;
const QPoint backgroundOffset = r.center() - background.rect().center(); m_cachedIcon = QPixmap(size());
painter.drawPixmap(backgroundOffset, background); m_cachedIcon.fill(Qt::transparent);
QPainter p(&m_cachedIcon);
const int bigNumHeight = perfectIconSize / 2.5; // draw fashion mode datetime plugin
const int bigNumWidth = double(bigNumHeight) * 8 / 18; const int perfectIconSize = qMin(width(), height()) * 0.8;
const int smallNumHeight = bigNumHeight / 2; const QRect r = rect();
const int smallNumWidth = double(smallNumHeight) * 5 / 9;
// draw big num 1 // draw background
const QString bigNum1Path = QString(":/icons/resources/icons/big%1.svg").arg(currentTimeString[0]); const QPixmap background = loadSvg(":/icons/resources/icons/background.svg", QSize(perfectIconSize, perfectIconSize));
const QPixmap bigNum1 = loadSvg(bigNum1Path, QSize(bigNumWidth, bigNumHeight)); const QPoint backgroundOffset = r.center() - background.rect().center();
const QPoint bigNum1Offset = backgroundOffset + QPoint(perfectIconSize / 2 - bigNumWidth * 2 + 1, perfectIconSize / 2 - bigNumHeight / 2); p.drawPixmap(backgroundOffset, background);
painter.drawPixmap(bigNum1Offset, bigNum1);
// draw big num 2 const int bigNumHeight = perfectIconSize / 2.5;
const QString bigNum2Path = QString(":/icons/resources/icons/big%1.svg").arg(currentTimeString[1]); const int bigNumWidth = double(bigNumHeight) * 8 / 18;
const QPixmap bigNum2 = loadSvg(bigNum2Path, QSize(bigNumWidth, bigNumHeight)); const int smallNumHeight = bigNumHeight / 2;
const QPoint bigNum2Offset = bigNum1Offset + QPoint(bigNumWidth + 1, 0); const int smallNumWidth = double(smallNumHeight) * 5 / 9;
painter.drawPixmap(bigNum2Offset, bigNum2);
// draw small num 1 // draw big num 1
const QString smallNum1Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[2]); const QString bigNum1Path = QString(":/icons/resources/icons/big%1.svg").arg(currentTimeString[0]);
const QPixmap smallNum1 = loadSvg(smallNum1Path, QSize(smallNumWidth, smallNumHeight)); const QPixmap bigNum1 = loadSvg(bigNum1Path, QSize(bigNumWidth, bigNumHeight));
const QPoint smallNum1Offset = bigNum2Offset + QPoint(bigNumWidth + 2, smallNumHeight); const QPoint bigNum1Offset = backgroundOffset + QPoint(perfectIconSize / 2 - bigNumWidth * 2 + 1, perfectIconSize / 2 - bigNumHeight / 2);
painter.drawPixmap(smallNum1Offset, smallNum1); p.drawPixmap(bigNum1Offset, bigNum1);
// draw small num 2 // draw big num 2
const QString smallNum2Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[3]); const QString bigNum2Path = QString(":/icons/resources/icons/big%1.svg").arg(currentTimeString[1]);
const QPixmap smallNum2 = loadSvg(smallNum2Path, QSize(smallNumWidth, smallNumHeight)); const QPixmap bigNum2 = loadSvg(bigNum2Path, QSize(bigNumWidth, bigNumHeight));
const QPoint smallNum2Offset = smallNum1Offset + QPoint(smallNumWidth + 1, 0); const QPoint bigNum2Offset = bigNum1Offset + QPoint(bigNumWidth + 1, 0);
painter.drawPixmap(smallNum2Offset, smallNum2); p.drawPixmap(bigNum2Offset, bigNum2);
// draw small num 1
const QString smallNum1Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[2]);
const QPixmap smallNum1 = loadSvg(smallNum1Path, QSize(smallNumWidth, smallNumHeight));
const QPoint smallNum1Offset = bigNum2Offset + QPoint(bigNumWidth + 2, smallNumHeight);
p.drawPixmap(smallNum1Offset, smallNum1);
// draw small num 2
const QString smallNum2Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[3]);
const QPixmap smallNum2 = loadSvg(smallNum2Path, QSize(smallNumWidth, smallNumHeight));
const QPoint smallNum2Offset = smallNum1Offset + QPoint(smallNumWidth + 1, 0);
p.drawPixmap(smallNum2Offset, smallNum2);
}
// draw cached fashion mode time item
painter.drawPixmap(rect().center() - m_cachedIcon.rect().center(), m_cachedIcon);
} }
void DatetimeWidget::mousePressEvent(QMouseEvent *e) void DatetimeWidget::mousePressEvent(QMouseEvent *e)

View File

@ -12,10 +12,15 @@ public:
private: private:
QSize sizeHint() const; QSize sizeHint() const;
void resizeEvent(QResizeEvent *e);
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e);
void mousePressEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);
const QPixmap loadSvg(const QString &fileName, const QSize size); const QPixmap loadSvg(const QString &fileName, const QSize size);
private:
QPixmap m_cachedIcon;
QString m_cachedTime;
}; };
#endif // DATETIMEWIDGET_H #endif // DATETIMEWIDGET_H

View File

@ -5,8 +5,11 @@ TipsWidget::TipsWidget(QWidget *parent)
: QWidget(parent), : QWidget(parent),
m_mainLayout(new QHBoxLayout) m_mainLayout(new QHBoxLayout)
{ {
m_mainLayout->setMargin(0);
m_mainLayout->setSpacing(0);
setLayout(m_mainLayout); setLayout(m_mainLayout);
setFixedHeight(32); setFixedHeight(26);
} }
void TipsWidget::clear() void TipsWidget::clear()
@ -27,5 +30,5 @@ void TipsWidget::addWidgets(QList<TrayWidget *> widgets)
w->setVisible(true); w->setVisible(true);
m_mainLayout->addWidget(w); m_mainLayout->addWidget(w);
} }
setFixedWidth(widgets.size() * 20 + 20); setFixedWidth(widgets.size() * 26);
} }

View File

@ -34,6 +34,8 @@ TrayWidget::TrayWidget(quint32 winId, QWidget *parent)
m_updateTimer->start(); m_updateTimer->start();
connect(m_updateTimer, &QTimer::timeout, this, &TrayWidget::updateIcon); connect(m_updateTimer, &QTimer::timeout, this, &TrayWidget::updateIcon);
setFixedSize(26, 26);
} }
TrayWidget::~TrayWidget() TrayWidget::~TrayWidget()
@ -52,9 +54,18 @@ QSize TrayWidget::sizeHint() const
void TrayWidget::showEvent(QShowEvent *e) void TrayWidget::showEvent(QShowEvent *e)
{ {
m_image = getImageNonComposite();
QWidget::showEvent(e); QWidget::showEvent(e);
setX11PassMouseEvent(false); setX11PassMouseEvent(false);
// auto c = QX11Info::connection();
// QPoint globalPos = mapToGlobal(QPoint(0, 0));
// const uint32_t windowMoveConfigVals[2] = { uint32_t(globalPos.x()), uint32_t(globalPos.y()) };
// xcb_configure_window(c, m_containerWid,
// XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
// windowMoveConfigVals);
} }
void TrayWidget::hideEvent(QHideEvent *e) void TrayWidget::hideEvent(QHideEvent *e)
@ -74,17 +85,18 @@ void TrayWidget::paintEvent(QPaintEvent *e)
painter.begin(this); painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
m_image = getImageNonComposite(); // m_image = getImageNonComposite();
if (!m_image.isNull()) { // if (!m_image.isNull()) {
if (true) { // if (true) {
QPainterPath path; // QPainterPath path;
path.addRoundedRect(p.x(), p.y(), iconSize, iconSize, iconSize / 2, iconSize / 2); // path.addRoundedRect(p.x(), p.y(), iconSize, iconSize, iconSize / 2, iconSize / 2);
painter.setClipPath(path); // painter.setClipPath(path);
} // }
painter.drawImage(p.x(), p.y(), m_image.scaled(iconSize, iconSize)); // painter.drawImage(p.x(), p.y(), m_image.scaled(iconSize, iconSize));
} // }
painter.drawImage(rect().center() - m_image.rect().center(), m_image);
painter.end(); painter.end();
} }
@ -226,39 +238,40 @@ void TrayWidget::updateIcon()
{ {
if (!isVisible()) return; if (!isVisible()) return;
auto c = QX11Info::connection(); // auto c = QX11Info::connection();
const uint32_t stackAboveData[] = {XCB_STACK_MODE_ABOVE}; // const uint32_t stackAboveData[] = {XCB_STACK_MODE_ABOVE};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData); // xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData);
QPoint globalPos = mapToGlobal(QPoint(0, 0)); // QPoint globalPos = mapToGlobal(QPoint(0, 0));
const uint32_t windowMoveConfigVals[2] = { uint32_t(globalPos.x()), uint32_t(globalPos.y()) }; // const uint32_t windowMoveConfigVals[2] = { uint32_t(globalPos.x()), uint32_t(globalPos.y()) };
xcb_configure_window(c, m_containerWid, // xcb_configure_window(c, m_containerWid,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, // XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
windowMoveConfigVals); // windowMoveConfigVals);
const uint32_t windowResizeConfigVals[2] = { iconSize, iconSize }; // const uint32_t windowResizeConfigVals[2] = { iconSize, iconSize };
xcb_configure_window(c, m_windowId, // xcb_configure_window(c, m_windowId,
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, // XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
windowResizeConfigVals); // windowResizeConfigVals);
m_image = getImageNonComposite();
update(); update();
} }
void TrayWidget::hideIcon() //void TrayWidget::hideIcon()
{ //{
auto c = QX11Info::connection(); // auto c = QX11Info::connection();
const uint32_t stackAboveData[] = {XCB_STACK_MODE_BELOW}; // const uint32_t stackAboveData[] = {XCB_STACK_MODE_BELOW};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData); // xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData);
const uint32_t windowMoveConfigVals[2] = {0, 0}; // const uint32_t windowMoveConfigVals[2] = {0, 0};
xcb_configure_window(c, m_containerWid, // xcb_configure_window(c, m_containerWid,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, // XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
windowMoveConfigVals); // windowMoveConfigVals);
hide(); // hide();
} //}
void TrayWidget::sendClick(uint8_t mouseButton, int x, int y) void TrayWidget::sendClick(uint8_t mouseButton, int x, int y)
{ {
@ -372,9 +385,14 @@ QImage TrayWidget::getImageNonComposite() const
void TrayWidget::setX11PassMouseEvent(const bool pass) void TrayWidget::setX11PassMouseEvent(const bool pass)
{ {
auto c = QX11Info::connection();
if (pass) if (pass)
{ {
XShapeCombineRectangles(QX11Info::display(), m_containerWid, ShapeInput, 0, 0, nullptr, 0, ShapeSet, YXBanded); XShapeCombineRectangles(QX11Info::display(), m_containerWid, ShapeInput, 0, 0, nullptr, 0, ShapeSet, YXBanded);
const uint32_t stackAboveData[] = {XCB_STACK_MODE_BELOW};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData);
} }
else else
{ {
@ -385,5 +403,8 @@ void TrayWidget::setX11PassMouseEvent(const bool pass)
rectangle.height = iconSize; rectangle.height = iconSize;
XShapeCombineRectangles(QX11Info::display(), m_containerWid, ShapeInput, 0, 0, &rectangle, 1, ShapeSet, YXBanded); XShapeCombineRectangles(QX11Info::display(), m_containerWid, ShapeInput, 0, 0, &rectangle, 1, ShapeSet, YXBanded);
const uint32_t stackAboveData[] = {XCB_STACK_MODE_ABOVE};
xcb_configure_window(c, m_containerWid, XCB_CONFIG_WINDOW_STACK_MODE, stackAboveData);
} }
} }

View File

@ -25,7 +25,7 @@ private:
void wrapWindow(); void wrapWindow();
void updateIcon(); void updateIcon();
void hideIcon(); // void hideIcon();
QImage getImageNonComposite() const; QImage getImageNonComposite() const;
private slots: private slots: