1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Alexander Sokoloff <sokoloff.a@gmail.com>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "xdgmenuwidget.h"
#include "xdgicon.h"
#include "xmlhelper.h"
#include "xdgaction.h"
#include "xdgmenu.h"
#include <QEvent>
#include <QDebug>
#include <QUrl>
#include <QMimeData>
#include <QtGui/QDrag>
#include <QtGui/QMouseEvent>
#include <QApplication>
class XdgMenuWidgetPrivate
{
private:
XdgMenuWidget* const q_ptr;
Q_DECLARE_PUBLIC(XdgMenuWidget)
public:
explicit XdgMenuWidgetPrivate(XdgMenuWidget* parent):
q_ptr(parent)
{}
void init(const QDomElement& xml);
void buildMenu();
QDomElement mXml;
void mouseMoveEvent(QMouseEvent *event);
QPoint mDragStartPosition;
private:
XdgAction* createAction(const QDomElement& xml);
static QString escape(QString string);
};
XdgMenuWidget::XdgMenuWidget(const XdgMenu& xdgMenu, const QString& title, QWidget* parent):
QMenu(parent),
d_ptr(new XdgMenuWidgetPrivate(this))
{
d_ptr->init(xdgMenu.xml().documentElement());
setTitle(XdgMenuWidgetPrivate::escape(title));
}
XdgMenuWidget::XdgMenuWidget(const QDomElement& menuElement, QWidget* parent):
QMenu(parent),
d_ptr(new XdgMenuWidgetPrivate(this))
{
d_ptr->init(menuElement);
}
XdgMenuWidget::XdgMenuWidget(const XdgMenuWidget& other, QWidget* parent):
QMenu(parent),
d_ptr(new XdgMenuWidgetPrivate(this))
{
d_ptr->init(other.d_ptr->mXml);
}
void XdgMenuWidgetPrivate::init(const QDomElement& xml)
{
Q_Q(XdgMenuWidget);
mXml = xml;
q->clear();
QString title;
if (! xml.attribute(QLatin1String("title")).isEmpty())
title = xml.attribute(QLatin1String("title"));
else
title = xml.attribute(QLatin1String("name"));
q->setTitle(escape(title));
//q->setToolTip(xml.attribute(QLatin1String("comment")));
q->setToolTipsVisible(true);
QIcon parentIcon;
QMenu* parentMenu = qobject_cast<QMenu*>(q->parent());
if (parentMenu)
parentIcon = parentMenu->icon();
q->setIcon(XdgIcon::fromTheme(mXml.attribute(QLatin1String("icon")), parentIcon));
buildMenu();
}
XdgMenuWidget::~XdgMenuWidget()
{
delete d_ptr;
}
XdgMenuWidget& XdgMenuWidget::operator=(const XdgMenuWidget& other)
{
Q_D(XdgMenuWidget);
d->init(other.d_ptr->mXml);
return *this;
}
bool XdgMenuWidget::event(QEvent* event)
{
Q_D(XdgMenuWidget);
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *e = static_cast<QMouseEvent*>(event);
if (e->button() == Qt::LeftButton)
d->mDragStartPosition = e->pos();
}
else if (event->type() == QEvent::MouseMove)
{
QMouseEvent *e = static_cast<QMouseEvent*>(event);
d->mouseMoveEvent(e);
}
return QMenu::event(event);
}
void XdgMenuWidgetPrivate::mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
if ((event->pos() - mDragStartPosition).manhattanLength() < QApplication::startDragDistance())
return;
Q_Q(XdgMenuWidget);
XdgAction *a = qobject_cast<XdgAction*>(q->actionAt(mDragStartPosition));
if (!a)
return;
QList<QUrl> urls;
urls << QUrl::fromLocalFile(a->desktopFile().fileName());
QMimeData *mimeData = new QMimeData();
mimeData->setUrls(urls);
QDrag *drag = new QDrag(q);
drag->setMimeData(mimeData);
drag->exec(Qt::CopyAction | Qt::LinkAction);
}
void XdgMenuWidgetPrivate::buildMenu()
{
Q_Q(XdgMenuWidget);
QAction* first = nullptr;
if (!q->actions().isEmpty())
first = q->actions().constLast();
DomElementIterator it(mXml, QString());
while(it.hasNext())
{
QDomElement xml = it.next();
// Build submenu ........................
if (xml.tagName() == QLatin1String("Menu"))
q->insertMenu(first, new XdgMenuWidget(xml, q));
//Build application link ................
else if (xml.tagName() == QLatin1String("AppLink"))
q->insertAction(first, createAction(xml));
//Build separator .......................
else if (xml.tagName() == QLatin1String("Separator"))
q->insertSeparator(first);
}
}
XdgAction* XdgMenuWidgetPrivate::createAction(const QDomElement& xml)
{
Q_Q(XdgMenuWidget);
XdgAction* action = new XdgAction(xml.attribute(QLatin1String("desktopFile")), q);
QString title;
if (!xml.attribute(QLatin1String("title")).isEmpty())
title = xml.attribute(QLatin1String("title"));
else
title = xml.attribute(QLatin1String("name"));
action->setText(escape(title));
if (!xml.attribute(QLatin1String("genericName")).isEmpty() &&
xml.attribute(QLatin1String("genericName")) != title)
action->setToolTip(xml.attribute(QLatin1String("genericName")));
return action;
}
/************************************************
This should be used when a menu item text is set
otherwise Qt uses the &'s for creating mnemonics
************************************************/
QString XdgMenuWidgetPrivate::escape(QString string)
{
return string.replace(QLatin1Char('&'), QLatin1String("&&"));
}
|