2016-06-03 16:06:11 +08:00
|
|
|
#include "appitem.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
2016-06-07 14:40:45 +08:00
|
|
|
#include <QDrag>
|
|
|
|
#include <QMouseEvent>
|
2016-06-03 16:06:11 +08:00
|
|
|
|
2016-06-06 14:28:28 +08:00
|
|
|
#define APP_ICON_KEY "icon"
|
|
|
|
#define APP_MENU_KEY "menu"
|
|
|
|
#define APP_XIDS_KEY "app-xids"
|
|
|
|
|
2016-06-07 14:40:45 +08:00
|
|
|
#define APP_DRAG_THRESHOLD 20
|
|
|
|
|
2016-06-06 14:57:07 +08:00
|
|
|
DBusClientManager *AppItem::ClientInter = nullptr;
|
2016-06-14 11:22:19 +08:00
|
|
|
//uint AppItem::ActiveWindowId = 0;
|
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-06 14:57:07 +08:00
|
|
|
initClientManager();
|
2016-06-06 14:28:28 +08:00
|
|
|
|
2016-06-14 11:22:19 +08:00
|
|
|
m_titles = m_itemEntry->titles();
|
|
|
|
m_id = m_itemEntry->id();
|
|
|
|
qDebug() << m_titles;
|
2016-06-06 14:28:28 +08:00
|
|
|
|
2016-06-14 11:22:19 +08:00
|
|
|
connect(m_itemEntry,&DBusDockEntry::TitlesChanged, this, &AppItem::entryDataChanged);
|
|
|
|
connect(m_itemEntry, &DBusDockEntry::ActiveChanged, this, static_cast<void (AppItem::*)()>(&AppItem::update));
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString AppItem::appId() const
|
|
|
|
{
|
|
|
|
return m_id;
|
2016-06-03 16:06:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
DockItem::paintEvent(e);
|
|
|
|
|
2016-06-07 16:01:37 +08:00
|
|
|
if (m_draging)
|
|
|
|
return;
|
|
|
|
|
2016-06-06 10:59:29 +08:00
|
|
|
const QRect itemRect = rect();
|
|
|
|
const int iconSize = std::min(itemRect.width(), itemRect.height());
|
|
|
|
|
|
|
|
QRect iconRect;
|
|
|
|
iconRect.setWidth(iconSize);
|
|
|
|
iconRect.setHeight(iconSize);
|
|
|
|
iconRect.moveTopLeft(itemRect.center() - iconRect.center());
|
|
|
|
|
2016-06-03 16:06:11 +08:00
|
|
|
QPainter painter(this);
|
2016-06-06 14:28:28 +08:00
|
|
|
|
2016-06-14 11:22:19 +08:00
|
|
|
if (m_itemEntry->active())
|
|
|
|
{
|
|
|
|
painter.fillRect(rect(), Qt::blue);
|
|
|
|
} else {
|
|
|
|
painter.fillRect(rect(), Qt::gray);
|
|
|
|
}
|
|
|
|
|
2016-06-13 10:33:07 +08:00
|
|
|
// // draw current active background
|
|
|
|
// if (m_windows.contains(ActiveWindowId))
|
|
|
|
// {
|
|
|
|
// painter.fillRect(rect(), Qt::blue);
|
|
|
|
// } else if (m_data[APP_STATUS_KEY] == APP_ACTIVE_STATUS)
|
|
|
|
// {
|
|
|
|
// // draw active background
|
|
|
|
// painter.fillRect(rect(), Qt::cyan);
|
|
|
|
// } else {
|
|
|
|
// // draw normal background
|
|
|
|
// painter.fillRect(rect(), Qt::gray);
|
|
|
|
// }
|
2016-06-06 14:28:28 +08:00
|
|
|
|
|
|
|
// draw icon
|
2016-06-06 10:59:29 +08:00
|
|
|
painter.fillRect(iconRect, Qt::yellow);
|
2016-06-06 14:28:28 +08:00
|
|
|
|
|
|
|
// draw text
|
2016-06-14 11:22:19 +08:00
|
|
|
painter.setPen(Qt::red);
|
|
|
|
painter.drawText(rect(), m_itemEntry->title());
|
2016-06-03 16:06:11 +08:00
|
|
|
}
|
2016-06-06 14:28:28 +08:00
|
|
|
|
|
|
|
void AppItem::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2016-06-07 14:40:45 +08:00
|
|
|
// activate
|
2016-06-06 14:28:28 +08:00
|
|
|
// TODO: dbus signature changed
|
2016-06-07 14:40:45 +08:00
|
|
|
if (e->button() == Qt::LeftButton)
|
2016-06-14 11:22:19 +08:00
|
|
|
m_itemEntry->Activate1();
|
2016-06-07 14:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
m_mousePressPos = e->pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::mouseMoveEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
// handle drag
|
|
|
|
if (e->buttons() != Qt::LeftButton)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QPoint distance = e->pos() - m_mousePressPos;
|
|
|
|
if (distance.manhattanLength() < APP_DRAG_THRESHOLD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
startDrag();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppItem::startDrag()
|
|
|
|
{
|
2016-06-07 16:01:37 +08:00
|
|
|
m_draging = true;
|
|
|
|
update();
|
|
|
|
|
2016-06-07 14:40:45 +08:00
|
|
|
QPixmap pixmap(25, 25);
|
|
|
|
pixmap.fill(Qt::red);
|
|
|
|
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
drag->setPixmap(pixmap);
|
|
|
|
drag->setHotSpot(pixmap.rect().center());
|
|
|
|
drag->setMimeData(new QMimeData);
|
|
|
|
|
|
|
|
const Qt::DropAction result = drag->exec(Qt::MoveAction);
|
|
|
|
|
2016-06-14 11:22:19 +08:00
|
|
|
qDebug() << "dnd result: " << result;
|
2016-06-07 16:01:37 +08:00
|
|
|
|
|
|
|
m_draging = false;
|
|
|
|
update();
|
2016-06-06 14:28:28 +08:00
|
|
|
}
|
|
|
|
|
2016-06-06 14:57:07 +08:00
|
|
|
void AppItem::initClientManager()
|
|
|
|
{
|
|
|
|
if (ClientInter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ClientInter = new DBusClientManager(this);
|
2016-06-14 11:22:19 +08:00
|
|
|
// connect(ClientInter, &DBusClientManager::ActiveWindowChanged, [&] (const uint wid) {
|
|
|
|
// ActiveWindowId = wid;
|
|
|
|
// });
|
2016-06-06 14:57:07 +08:00
|
|
|
}
|
|
|
|
|
2016-06-14 11:22:19 +08:00
|
|
|
void AppItem::entryDataChanged(const quint32 xid, const QString &title)
|
2016-06-06 14:28:28 +08:00
|
|
|
{
|
2016-06-14 11:22:19 +08:00
|
|
|
qDebug() << "title changed: " << xid << title;
|
2016-06-06 14:28:28 +08:00
|
|
|
}
|