mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 09:23:03 +00:00
add context menu
Change-Id: I403dc3c1338650b58e3e7941120aa99fc6efde44
This commit is contained in:
parent
2e0094bca2
commit
61fd7e1cb3
Notes:
Deepin Code Review
2016-09-19 15:14:57 +08:00
Verified+1: Anonymous Coward #1000004 Code-Review+2: 石博文 <sbw@sbw.so> Submitted-by: 石博文 <sbw@sbw.so> Submitted-at: Mon, 19 Sep 2016 15:14:57 +0800 Reviewed-on: https://cr.deepin.io/16166 Project: dde/dde-dock Branch: refs/heads/master
@ -16,20 +16,10 @@ PopupControlWidget::PopupControlWidget(QWidget *parent)
|
||||
|
||||
m_empty(false),
|
||||
|
||||
// m_openBtn(new DLinkButton(tr("Run"), this)),
|
||||
// m_clearBtn(new DLinkButton(tr("Empty Trash"), this)),
|
||||
|
||||
m_fsWatcher(new QFileSystemWatcher(this))
|
||||
{
|
||||
// QVBoxLayout *centeralLayout = new QVBoxLayout;
|
||||
// centeralLayout->addWidget(m_openBtn);
|
||||
// centeralLayout->addWidget(m_clearBtn);
|
||||
|
||||
// connect(m_openBtn, &DLinkButton::clicked, this, &PopupControlWidget::openTrashFloder);
|
||||
// connect(m_clearBtn, &DLinkButton::clicked, this, &PopupControlWidget::clearTrashFloder);
|
||||
connect(m_fsWatcher, &QFileSystemWatcher::directoryChanged, this, &PopupControlWidget::trashStatusChanged, Qt::QueuedConnection);
|
||||
|
||||
// setLayout(centeralLayout);
|
||||
setObjectName("trash");
|
||||
setFixedWidth(80);
|
||||
|
||||
|
@ -46,7 +46,14 @@ const QString TrashPlugin::itemContextMenu(const QString &itemKey)
|
||||
{
|
||||
Q_UNUSED(itemKey);
|
||||
|
||||
return QString();
|
||||
return m_trashWidget->contextMenu();
|
||||
}
|
||||
|
||||
void TrashPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
|
||||
{
|
||||
Q_UNUSED(itemKey);
|
||||
|
||||
m_trashWidget->invokeMenuItem(menuId, checked);
|
||||
}
|
||||
|
||||
int TrashPlugin::itemSortKey(const QString &itemKey)
|
||||
|
@ -20,6 +20,7 @@ public:
|
||||
QWidget *itemPopupApplet(const QString &itemKey);
|
||||
const QString itemCommand(const QString &itemKey);
|
||||
const QString itemContextMenu(const QString &itemKey);
|
||||
void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked);
|
||||
|
||||
int itemSortKey(const QString &itemKey);
|
||||
void displayModeChanged(const Dock::DisplayMode displayMode);
|
||||
|
@ -6,16 +6,14 @@
|
||||
#include <QIcon>
|
||||
#include <QApplication>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QJsonDocument>
|
||||
|
||||
DWIDGET_USE_NAMESPACE
|
||||
|
||||
TrashWidget::TrashWidget(QWidget *parent)
|
||||
: QWidget(parent),
|
||||
|
||||
m_popupApplet(new PopupControlWidget(this)),
|
||||
|
||||
m_openAct(tr("Run"), this),
|
||||
m_clearAct(tr("Empty"), this)
|
||||
m_popupApplet(new PopupControlWidget(this))
|
||||
{
|
||||
QIcon::setThemeName("deepin");
|
||||
|
||||
@ -37,6 +35,44 @@ QSize TrashWidget::sizeHint() const
|
||||
return QSize(26, 26);
|
||||
}
|
||||
|
||||
const QString TrashWidget::contextMenu() const
|
||||
{
|
||||
QList<QVariant> items;
|
||||
items.reserve(2);
|
||||
|
||||
QMap<QString, QVariant> open;
|
||||
open["itemId"] = "open";
|
||||
open["itemText"] = tr("Open");
|
||||
open["isActive"] = true;
|
||||
items.push_back(open);
|
||||
|
||||
if (!m_popupApplet->empty())
|
||||
{
|
||||
QMap<QString, QVariant> empty;
|
||||
empty["itemId"] = "empty";
|
||||
empty["itemText"] = tr("Empty");
|
||||
empty["isActive"] = true;
|
||||
items.push_back(empty);
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> menu;
|
||||
menu["items"] = items;
|
||||
menu["checkableMenu"] = false;
|
||||
menu["singleCheck"] = false;
|
||||
|
||||
return QJsonDocument::fromVariant(menu).toJson();
|
||||
}
|
||||
|
||||
void TrashWidget::invokeMenuItem(const QString &menuId, const bool checked)
|
||||
{
|
||||
Q_UNUSED(checked);
|
||||
|
||||
if (menuId == "open")
|
||||
m_popupApplet->openTrashFloder();
|
||||
else if (menuId == "empty")
|
||||
m_popupApplet->clearTrashFloder();
|
||||
}
|
||||
|
||||
void TrashWidget::dragEnterEvent(QDragEnterEvent *e)
|
||||
{
|
||||
if (e->mimeData()->hasFormat("RequestDock"))
|
||||
@ -80,31 +116,9 @@ void TrashWidget::mousePressEvent(QMouseEvent *e)
|
||||
if (e->button() != Qt::RightButton || dis.manhattanLength() > std::min(width(), height()) * 0.8 * 0.5)
|
||||
return QWidget::mousePressEvent(e);
|
||||
|
||||
// showMenu();
|
||||
emit requestContextMenu();
|
||||
}
|
||||
|
||||
const QPoint TrashWidget::popupMarkPoint()
|
||||
{
|
||||
QPoint p;
|
||||
QWidget *w = this;
|
||||
do {
|
||||
p += w->pos();
|
||||
w = qobject_cast<QWidget *>(w->parent());
|
||||
} while (w);
|
||||
|
||||
const QRect r = rect();
|
||||
switch (qApp->property(PROP_POSITION).value<Dock::Position>())
|
||||
{
|
||||
case Dock::Top: p += QPoint(r.width() / 2, r.height()); break;
|
||||
case Dock::Bottom: p += QPoint(r.width() / 2, 0); break;
|
||||
case Dock::Left: p += QPoint(r.width(), r.height() / 2); break;
|
||||
case Dock::Right: p += QPoint(0, r.height() / 2); break;
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void TrashWidget::updateIcon()
|
||||
{
|
||||
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
|
||||
@ -122,38 +136,6 @@ void TrashWidget::updateIcon()
|
||||
update();
|
||||
}
|
||||
|
||||
void TrashWidget::showMenu()
|
||||
{
|
||||
DMenu *menu = new DMenu(this);
|
||||
menu->setDockMenu(true);
|
||||
|
||||
menu->addAction(&m_openAct);
|
||||
if (!m_popupApplet->empty())
|
||||
menu->addAction(&m_clearAct);
|
||||
|
||||
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
||||
switch (position)
|
||||
{
|
||||
case Dock::Top: menu->setDirection(DMenu::Top); break;
|
||||
case Dock::Left: menu->setDirection(DMenu::Left); break;
|
||||
case Dock::Bottom: menu->setDirection(DMenu::Bottom); break;
|
||||
case Dock::Right: menu->setDirection(DMenu::Right); break;
|
||||
default: Q_UNREACHABLE();
|
||||
}
|
||||
|
||||
const QPoint p = popupMarkPoint();
|
||||
|
||||
connect(menu, &DMenu::triggered, this, &TrashWidget::menuTriggered);
|
||||
|
||||
menu->exec(p);
|
||||
|
||||
m_clearAct.setParent(this);
|
||||
m_openAct.setParent(this);
|
||||
menu->deleteLater();
|
||||
|
||||
emit requestRefershWindowVisible();
|
||||
}
|
||||
|
||||
void TrashWidget::removeApp(const QString &appKey)
|
||||
{
|
||||
const QString cmd("dbus-send --print-reply --dest=com.deepin.dde.Launcher /com/deepin/dde/Launcher com.deepin.dde.Launcher.UninstallApp string:\"" + appKey + "\"");
|
||||
@ -165,14 +147,6 @@ void TrashWidget::removeApp(const QString &appKey)
|
||||
proc->deleteLater();
|
||||
}
|
||||
|
||||
void TrashWidget::menuTriggered(DAction *action)
|
||||
{
|
||||
if (action == &m_clearAct)
|
||||
m_popupApplet->clearTrashFloder();
|
||||
else if (action == &m_openAct)
|
||||
m_popupApplet->openTrashFloder();
|
||||
}
|
||||
|
||||
void TrashWidget::moveToTrash(const QUrl &url)
|
||||
{
|
||||
const QFileInfo info = url.toLocalFile();
|
||||
|
@ -19,10 +19,11 @@ public:
|
||||
QWidget *popupApplet();
|
||||
|
||||
QSize sizeHint() const;
|
||||
const QString contextMenu() const;
|
||||
void invokeMenuItem(const QString &menuId, const bool checked);
|
||||
|
||||
signals:
|
||||
void requestContextMenu() const;
|
||||
void requestRefershWindowVisible() const;
|
||||
|
||||
protected:
|
||||
void dragEnterEvent(QDragEnterEvent *e);
|
||||
@ -31,23 +32,15 @@ protected:
|
||||
void resizeEvent(QResizeEvent *e);
|
||||
void mousePressEvent(QMouseEvent *e);
|
||||
|
||||
private:
|
||||
const QPoint popupMarkPoint();
|
||||
|
||||
private slots:
|
||||
void updateIcon();
|
||||
void showMenu();
|
||||
void removeApp(const QString &appKey);
|
||||
void menuTriggered(Dtk::Widget::DAction *action);
|
||||
void moveToTrash(const QUrl &url);
|
||||
|
||||
private:
|
||||
PopupControlWidget *m_popupApplet;
|
||||
|
||||
QPixmap m_icon;
|
||||
|
||||
Dtk::Widget::DAction m_openAct;
|
||||
Dtk::Widget::DAction m_clearAct;
|
||||
};
|
||||
|
||||
#endif // TRASHWIDGET_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user