-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXdgDesktopEntryWidget.cpp
114 lines (104 loc) · 3.45 KB
/
XdgDesktopEntryWidget.cpp
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
#include <QAction>
#include <QLabel>
#include <QToolButton>
#include <QVBoxLayout>
#include "XdgApplicationEntry.hpp"
#include "XdgDesktopEntryWidget.hpp"
#include "XdgDesktopMenu.hpp"
#include "XdgInfo.hpp"
XdgDesktopEntryWidget::XdgDesktopEntryWidget(XdgAbstractEntry *entry_, QWidget* parent)
: QToolButton(parent), m_entry(entry_)
{
Q_ASSERT(m_entry != nullptr);
recalculateGeometry();
this->setEnabled(true);
switch (m_entry->type) {
case XdgAbstractEntry::Application:
// just for kicks until we are able to:
// 1. parse the Exec, TryExec and Terminal desktop entries properly
// 2. manage child processes
// 3.
connect(this,&QToolButton::clicked,[this](){
XdgApplicationEntry *appEntry = dynamic_cast<XdgApplicationEntry *>(m_entry);
qDebug() << QString("Starting Process : %1").arg(appEntry->exec);
});
break;
case XdgAbstractEntry::Menu:
// just for kicks until we are able to:
// 1. display submenus
connect(this,&QToolButton::clicked,[this](){
XdgDesktopMenu *menuEntry = dynamic_cast<XdgDesktopMenu *>(m_entry);
qDebug() << QString("Navigating to Submenu : %1").arg(menuEntry->name);
});
break;
default:
// This shouldn't happen:
connect(this,&QToolButton::clicked,[this](){
qWarning() << QString("Unsupported action for entry : %1").arg(m_entry->name);
});
break;
}
}
void XdgDesktopEntryWidget::recalculateGeometry()
{
QString newIconName = m_entry->icon;
QString backgroundImage = "";
if (m_entry->type == XdgAbstractEntry::Menu) {
XdgDesktopMenu *menuEntry = dynamic_cast<XdgDesktopMenu *>(m_entry);
if (menuEntry->directoryEntry) {
newIconName = menuEntry->directoryEntry->icon;
backgroundImage = QString("background-image : url(%1); ")
.arg(XdgInfo::getInstance().find(menuEntry->directoryEntry->backgroundImage, "pixmaps", ".png"));
}
}
QString iconFileName = XdgInfo::getInstance().find(newIconName, "icons", ".png");
if (iconFileName.isEmpty()) {
qDebug() << "No Icon found. Using default";
iconFileName = XdgInfo::getInstance().find("penguin", "pixmaps/faces", ".jpg");
Q_ASSERT(!iconFileName.isEmpty());
setIcon(QIcon(iconFileName));
} else {
setIcon(QIcon(iconFileName));
}
setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
setIconSize(m_iconSize);
setText(m_entry->name);
setObjectName(m_entry->name);
setFixedSize(m_widgetSize);
QString styleSheet = QString("QToolButton%1 { border : none; font-size : %2pt; margin: 0px; padding: 0px; %3}")
.arg(m_entry->name.isEmpty() ? "" : QString("#" + m_entry->name))
.arg(m_fontSize)
.arg(backgroundImage);
setStyleSheet(styleSheet);
}
void XdgDesktopEntryWidget::entry(XdgAbstractEntry *e) {
m_entry = e;
recalculateGeometry();
}
void XdgDesktopEntryWidget::fontSize(int size) {
m_fontSize = size;
recalculateGeometry();
emit fontSizeChanged(m_fontSize);
}
void XdgDesktopEntryWidget::iconSize(QSize size) {
m_iconSize = size;
recalculateGeometry();
emit iconSizeChanged(m_iconSize);
}
void XdgDesktopEntryWidget::widgetSize(QSize size) {
m_widgetSize = size;
recalculateGeometry();
emit widgetSizeChanged(m_widgetSize);
}
XdgAbstractEntry *XdgDesktopEntryWidget::entry() const {
return m_entry;
}
int XdgDesktopEntryWidget::fontSize() const {
return m_fontSize;
}
QSize XdgDesktopEntryWidget::iconSize() const {
return m_iconSize;
}
QSize XdgDesktopEntryWidget::widgetSize() const {
return m_widgetSize;
}