2016-06-03 16:06:11 +08:00
|
|
|
#include "appitem.h"
|
|
|
|
|
2016-06-14 16:01:01 +08:00
|
|
|
#include "util/themeappicon.h"
|
|
|
|
|
2016-06-03 16:06:11 +08:00
|
|
|
#include <QPainter>
|
2016-06-07 14:40:45 +08:00
|
|
|
#include <QDrag>
|
|
|
|
#include <QMouseEvent>
|
2016-06-03 16:06:11 +08:00
|
|
|
|
2016-06-07 14:40:45 +08:00
|
|
|
#define APP_DRAG_THRESHOLD 20
|
|
|
|
|
2016-06-21 17:24:03 +08:00
|
|
|
int AppItem::IconBaseSize;
|
2016-06-15 16:17:51 +08:00
|
|
|
QPoint AppItem::MousePressPos;
|
2016-06-06 14:57:07 +08:00
|
|
|
|
2016-06-03 16:06:11 +08:00
|
|
|
AppItem::AppItem(const QDBusObjectPath &entry, QWidget *parent)
|
2016-06-06 11:37:09 +08:00
|
|
|
: DockItem(App, parent),
|
2016-06-07 16:01:37 +08:00
|
|
|
m_itemEntry(new DBusDockEntry(entry.path(), this)),
|
|
|
|
m_draging(false)
|
2016-06-03 16:06:11 +08:00
|
|
|
{
|
2016-06-23 14:45:57 +08:00
|
|
|
setAcceptDrops(true);
|
2016-06-06 14:28:28 +08:00
|
|
|
|
2016-06-14 11:22:19 +08:00
|
|
|
m_id = m_itemEntry->id();
|
2016-06-23 14:28:47 +08:00
|
|
|
m_active = m_itemEntry->active();
|
2016-06-06 14:28:28 +08:00
|
|
|
|
2016-06-23 14:28:47 +08:00
|
|
|
connect(m_itemEntry, &DBusDockEntry::ActiveChanged, this, &AppItem::activeChanged);
|
2016-06-15 11:20:05 +08:00
|
|
|
connect(m_itemEntry, &DBusDockEntry::TitlesChanged, this, &AppItem::updateTitle);
|
2016-06-14 11:22:19 +08:00
|
|
|
connect(m_itemEntry, &DBusDockEntry::ActiveChanged, this, static_cast<void (AppItem::*)()>(&AppItem::update));
|
2016-06-14 16:01:01 +08:00
|
|
|
|
2016-06-15 11:20:05 +08:00
|
|
|
updateTitle();
|
|
|
|
updateIcon();
|
2016-06-14 11:22:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString AppItem::appId() const
|
|
|
|
{
|
|
|
|
return m_id;
|
2016-06-03 16:06:11 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 17:24:03 +08:00
|
|
|
void AppItem::setIconBaseSize(const int size)
|
|
|
|
{
|
|
|
|
IconBaseSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AppItem::iconBaseSize()
|
|
|
|
{
|
|
|
|
return IconBaseSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AppItem::itemBaseWidth()
|
|
|
|
{
|
2016-06-23 14:28:47 +08:00
|
|
|
if (DockDisplayMode == Dock::Fashion)
|
|
|
|
return itemBaseHeight() * 1.1;
|
|
|
|
else
|
|
|
|
return itemBaseHeight() * 1.4;
|
2016-06-21 17:24:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int AppItem::itemBaseHeight()
|
|
|
|
{
|
2016-06-21 19:25:30 +08:00
|
|
|
return IconBaseSize * 1.5;
|
2016-06-21 17:24:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 16:06:11 +08:00
|
|
|
void AppItem::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
DockItem::paintEvent(e);
|
|
|
|
|
2016-06-23 14:28:47 +08:00
|
|
|
if (m_draging)
|
2016-06-07 16:01:37 +08:00
|
|
|
return;
|
|
|
|
|
2016-06-03 16:06:11 +08:00
|
|
|
QPainter painter(this);
|
2016-06-21 19:25:30 +08:00
|
|
|
if (!painter.isActive())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QRect itemRect = rect();
|
2016-06-06 14:28:28 +08:00
|
|
|
|
2016-06-14 16:01:01 +08:00
|
|
|
// draw background
|
2016-06-23 10:50:26 +08:00
|
|
|
const QRect backgroundRect = itemRect.marginsRemoved(QMargins(1, 2, 1, 2));
|
2016-06-23 14:28:47 +08:00
|
|
|
if (DockDisplayMode == Efficient)
|
2016-06-23 10:50:26 +08:00
|
|
|
{
|
2016-06-23 14:28:47 +08:00
|
|
|
if (m_active)
|
2016-06-23 10:50:26 +08:00
|
|
|
{
|
2016-06-23 14:28:47 +08:00
|
|
|
painter.fillRect(backgroundRect, QColor(70, 100, 200, 120));
|
|
|
|
|
|
|
|
const int activeLineWidth = 3;
|
|
|
|
QRect activeRect = backgroundRect;
|
|
|
|
switch (DockPosition)
|
|
|
|
{
|
|
|
|
case Top: activeRect.setBottom(activeRect.top() + activeLineWidth); break;
|
|
|
|
case Bottom: activeRect.setTop(activeRect.bottom() - activeLineWidth); break;
|
|
|
|
case Left: activeRect.setRight(activeRect.left() + activeLineWidth); break;
|
|
|
|
case Right: activeRect.setLeft(activeRect.right() - activeLineWidth); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.fillRect(activeRect, QColor(47, 168, 247));
|
|
|
|
}
|
|
|
|
else if (!m_titles.isEmpty())
|
|
|
|
painter.fillRect(backgroundRect, QColor(255, 255, 255, 50));
|
|
|
|
// else
|
|
|
|
// painter.fillRect(backgroundRect, Qt::gray);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_titles.isEmpty())
|
|
|
|
{
|
|
|
|
const int activeLineWidth = 1;
|
|
|
|
const int activeLineLength = 20;
|
|
|
|
QRect activeRect = itemRect;
|
|
|
|
switch (DockPosition)
|
|
|
|
{
|
|
|
|
case Top:
|
|
|
|
activeRect.setBottom(activeRect.top() + activeLineWidth);
|
|
|
|
activeRect.setWidth(activeLineLength);
|
|
|
|
activeRect.moveLeft((itemRect.width() - activeRect.width()) / 2);
|
|
|
|
break;
|
|
|
|
case Bottom:
|
|
|
|
activeRect.setTop(activeRect.bottom() - activeLineWidth);
|
|
|
|
activeRect.setWidth(activeLineLength);
|
|
|
|
activeRect.moveLeft((itemRect.width() - activeRect.width()) / 2);
|
|
|
|
break;
|
|
|
|
case Left:
|
|
|
|
activeRect.setRight(activeRect.left() + activeLineWidth);
|
|
|
|
activeRect.setHeight(activeLineLength);
|
|
|
|
activeRect.moveTop((itemRect.height() - activeRect.height()) / 2);
|
|
|
|
break;
|
|
|
|
case Right:
|
|
|
|
activeRect.setLeft(activeRect.right() - activeLineWidth);
|
|
|
|
activeRect.setHeight(activeLineLength);
|
|
|
|
activeRect.moveTop((itemRect.height() - activeRect.height()) / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.fillRect(activeRect, QColor(163, 167, 166));
|
2016-06-23 10:50:26 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 14:28:28 +08:00
|
|
|
|
|
|
|
// draw icon
|
2016-06-23 14:28:47 +08:00
|
|
|
if (DockDisplayMode == Efficient)
|
|
|
|
painter.drawPixmap(itemRect.center() - m_smallIcon.rect().center(), m_smallIcon);
|
|
|
|
else
|
|
|
|
painter.drawPixmap(itemRect.center() - m_largeIcon.rect().center(), m_largeIcon);
|
2016-06-03 16:06:11 +08:00
|
|
|
}
|
2016-06-06 14:28:28 +08:00
|
|
|
|
|
|
|
void AppItem::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2016-06-20 14:28:25 +08:00
|
|
|
if (e->button() != Qt::LeftButton)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QPoint distance = MousePressPos - e->pos();
|
|
|
|
if (distance.manhattanLength() < APP_DRAG_THRESHOLD)
|
2016-06-17 09:42:06 +08:00
|
|
|
m_itemEntry->Activate();
|
2016-06-07 14:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
2016-06-22 11:02:52 +08:00
|
|
|
if (e->button() == Qt::RightButton)
|
|
|
|
{
|
|
|
|
if (perfectIconRect().contains(e->pos()))
|
|
|
|
return showContextMenu();
|
|
|
|
else
|
|
|
|
return QWidget::mousePressEvent(e);
|
|
|
|
}
|
2016-06-15 16:17:51 +08:00
|
|
|
|
|
|
|
if (e->button() == Qt::LeftButton)
|
|
|
|
MousePressPos = e->pos();
|
2016-06-07 14:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::mouseMoveEvent(QMouseEvent *e)
|
|
|
|
{
|
2016-06-14 15:17:25 +08:00
|
|
|
e->accept();
|
|
|
|
|
2016-06-07 14:40:45 +08:00
|
|
|
// handle drag
|
|
|
|
if (e->buttons() != Qt::LeftButton)
|
|
|
|
return;
|
|
|
|
|
2016-06-15 16:17:51 +08:00
|
|
|
const QPoint distance = e->pos() - MousePressPos;
|
2016-06-07 14:40:45 +08:00
|
|
|
if (distance.manhattanLength() < APP_DRAG_THRESHOLD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
startDrag();
|
|
|
|
}
|
|
|
|
|
2016-06-15 11:20:05 +08:00
|
|
|
void AppItem::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
|
|
|
DockItem::resizeEvent(e);
|
|
|
|
|
|
|
|
updateIcon();
|
|
|
|
}
|
|
|
|
|
2016-06-23 14:45:57 +08:00
|
|
|
void AppItem::dragEnterEvent(QDragEnterEvent *e)
|
|
|
|
{
|
2016-06-23 15:30:06 +08:00
|
|
|
// ignore drag from panel
|
|
|
|
if (e->source())
|
|
|
|
return;
|
|
|
|
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::dropEvent(QDropEvent *e)
|
|
|
|
{
|
|
|
|
QStringList uriList;
|
|
|
|
for (auto uri : e->mimeData()->urls())
|
|
|
|
uriList << uri.toString();
|
|
|
|
|
|
|
|
// qDebug() << uriList;
|
|
|
|
m_itemEntry->HandleDragDrop(uriList);
|
2016-06-23 14:45:57 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 16:17:51 +08:00
|
|
|
void AppItem::invokedMenuItem(const QString &itemId, const bool checked)
|
|
|
|
{
|
2016-06-21 10:12:43 +08:00
|
|
|
Q_UNUSED(checked);
|
|
|
|
|
|
|
|
m_itemEntry->HandleMenuItem(itemId);
|
2016-06-15 16:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString AppItem::contextMenu() const
|
|
|
|
{
|
|
|
|
return m_itemEntry->menu();
|
|
|
|
}
|
|
|
|
|
2016-06-07 14:40:45 +08:00
|
|
|
void AppItem::startDrag()
|
|
|
|
{
|
2016-06-07 16:01:37 +08:00
|
|
|
m_draging = true;
|
|
|
|
update();
|
|
|
|
|
2016-06-07 14:40:45 +08:00
|
|
|
QDrag *drag = new QDrag(this);
|
2016-06-23 17:53:20 +08:00
|
|
|
drag->setPixmap(m_smallIcon);
|
|
|
|
drag->setHotSpot(m_smallIcon.rect().center());
|
2016-06-07 14:40:45 +08:00
|
|
|
drag->setMimeData(new QMimeData);
|
|
|
|
|
2016-06-20 14:28:25 +08:00
|
|
|
emit dragStarted();
|
2016-06-07 14:40:45 +08:00
|
|
|
const Qt::DropAction result = drag->exec(Qt::MoveAction);
|
2016-06-21 19:25:30 +08:00
|
|
|
Q_UNUSED(result);
|
2016-06-20 16:46:53 +08:00
|
|
|
|
|
|
|
// drag out of dock panel
|
|
|
|
if (!drag->target())
|
|
|
|
m_itemEntry->RequestUndock();
|
2016-06-07 16:01:37 +08:00
|
|
|
|
|
|
|
m_draging = false;
|
2016-06-20 15:42:31 +08:00
|
|
|
setVisible(true);
|
2016-06-07 16:01:37 +08:00
|
|
|
update();
|
2016-06-06 14:28:28 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 11:20:05 +08:00
|
|
|
void AppItem::updateTitle()
|
2016-06-06 14:28:28 +08:00
|
|
|
{
|
2016-06-14 15:01:27 +08:00
|
|
|
m_titles = m_itemEntry->titles();
|
|
|
|
|
|
|
|
update();
|
2016-06-06 14:28:28 +08:00
|
|
|
}
|
2016-06-14 16:01:01 +08:00
|
|
|
|
2016-06-15 11:20:05 +08:00
|
|
|
void AppItem::updateIcon()
|
2016-06-14 16:01:01 +08:00
|
|
|
{
|
|
|
|
const QString icon = m_itemEntry->icon();
|
2016-06-23 14:28:47 +08:00
|
|
|
const int iconSize = qMin(width(), height());
|
|
|
|
|
|
|
|
m_smallIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.6);
|
|
|
|
m_largeIcon = ThemeAppIcon::getIcon(icon, iconSize * 0.8);
|
|
|
|
}
|
2016-06-14 16:01:01 +08:00
|
|
|
|
2016-06-23 14:28:47 +08:00
|
|
|
void AppItem::activeChanged()
|
|
|
|
{
|
|
|
|
m_active = !m_active;
|
2016-06-14 16:01:01 +08:00
|
|
|
}
|