|
| 1 | +#include "usermenu.h" |
| 2 | + |
| 3 | +userMenuUI UserMenuApplet::__createUI__(PanelLocation location, short panelHeight, QFont font, |
| 4 | + short buttonX, short buttonXRight) { |
| 5 | + QWidget* userMenuWidget = new QWidget; |
| 6 | + QFontMetrics fm(font); |
| 7 | + |
| 8 | + // Window flags |
| 9 | + userMenuWidget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | |
| 10 | + Qt::X11BypassWindowManagerHint); |
| 11 | + |
| 12 | + // Geometry |
| 13 | + QScreen* primaryScreen = QGuiApplication::primaryScreen(); |
| 14 | + |
| 15 | + short userMenuWidth = fm.horizontalAdvance("About plainDE") + 20; |
| 16 | + short userMenuHeight = 150; |
| 17 | + short ax = 0, ay = 0; |
| 18 | + if (location == top) { |
| 19 | + ay = panelHeight + 5; |
| 20 | + } |
| 21 | + else { |
| 22 | + ay = primaryScreen->geometry().height() - panelHeight - userMenuHeight - 5; |
| 23 | + } |
| 24 | + if (primaryScreen->geometry().width() - buttonX >= userMenuWidth) { |
| 25 | + ax = buttonX; |
| 26 | + } |
| 27 | + else { |
| 28 | + ax = buttonXRight - userMenuWidth; |
| 29 | + } |
| 30 | + userMenuWidget->setFixedSize(userMenuWidth, userMenuHeight); |
| 31 | + userMenuWidget->move(ax, ay); |
| 32 | + |
| 33 | + // Set font |
| 34 | + userMenuWidget->setFont(font); |
| 35 | + |
| 36 | + // UI |
| 37 | + QVBoxLayout* userMenuLayout = new QVBoxLayout; |
| 38 | + userMenuLayout->setContentsMargins(1, 1, 1, 1); |
| 39 | + userMenuWidget->setLayout(userMenuLayout); |
| 40 | + |
| 41 | + QFile stylesheetReader(":/styles/styles/userMenu.qss"); |
| 42 | + stylesheetReader.open(QIODevice::ReadOnly | QIODevice::Text); |
| 43 | + QTextStream styleSheet(&stylesheetReader); |
| 44 | + userMenuWidget->setStyleSheet(styleSheet.readAll()); |
| 45 | + |
| 46 | + QPushButton* settingsEntry = new QPushButton; |
| 47 | + settingsEntry->setFlat(true); |
| 48 | + settingsEntry->setText("Settings"); |
| 49 | + settingsEntry->setIcon(QIcon::fromTheme("preferences-system")); |
| 50 | + userMenuWidget->layout()->addWidget(settingsEntry); |
| 51 | + |
| 52 | + QPushButton* aboutEntry = new QPushButton; |
| 53 | + aboutEntry->setFlat(true); |
| 54 | + aboutEntry->setText("About plainDE"); |
| 55 | + aboutEntry->setIcon(QIcon::fromTheme("help-about")); |
| 56 | + userMenuWidget->layout()->addWidget(aboutEntry); |
| 57 | + |
| 58 | + QPushButton* logOutEntry = new QPushButton; |
| 59 | + logOutEntry->setFlat(true); |
| 60 | + logOutEntry->setText("Log Out"); |
| 61 | + logOutEntry->setIcon(QIcon::fromTheme("system-log-out")); |
| 62 | + userMenuWidget->layout()->addWidget(logOutEntry); |
| 63 | + |
| 64 | + QPushButton* powerOffEntry = new QPushButton; |
| 65 | + powerOffEntry->setFlat(true); |
| 66 | + powerOffEntry->setText("Power Off"); |
| 67 | + powerOffEntry->setIcon(QIcon::fromTheme("system-shutdown")); |
| 68 | + userMenuWidget->layout()->addWidget(powerOffEntry); |
| 69 | + |
| 70 | + QPushButton* rebootEntry = new QPushButton; |
| 71 | + rebootEntry->setFlat(true); |
| 72 | + rebootEntry->setText("Reboot"); |
| 73 | + rebootEntry->setIcon(QIcon::fromTheme("system-reboot")); |
| 74 | + userMenuWidget->layout()->addWidget(rebootEntry); |
| 75 | + |
| 76 | + // Make connections |
| 77 | + userMenuWidget->connect(powerOffEntry, &QPushButton::clicked, userMenuWidget, |
| 78 | + [userMenuWidget]() { |
| 79 | + userMenuWidget->hide(); |
| 80 | + |
| 81 | + QMessageBox powerOffMsg; |
| 82 | + powerOffMsg.setWindowTitle("Power Off"); |
| 83 | + powerOffMsg.setText("Are you sure you want to shut down your system?"); |
| 84 | + powerOffMsg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
| 85 | + powerOffMsg.setIcon(QMessageBox::Question); |
| 86 | + |
| 87 | + if (powerOffMsg.exec() == QMessageBox::Yes) { |
| 88 | + system("systemctl poweroff"); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + userMenuWidget->connect(rebootEntry, &QPushButton::clicked, userMenuWidget, |
| 93 | + [userMenuWidget]() { |
| 94 | + userMenuWidget->hide(); |
| 95 | + |
| 96 | + QMessageBox powerOffMsg; |
| 97 | + powerOffMsg.setWindowTitle("Reboot"); |
| 98 | + powerOffMsg.setText("Are you sure you want to reboot your system?"); |
| 99 | + powerOffMsg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
| 100 | + powerOffMsg.setIcon(QMessageBox::Question); |
| 101 | + |
| 102 | + if (powerOffMsg.exec() == QMessageBox::Yes) { |
| 103 | + system("systemctl reboot"); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + userMenuWidget->connect(logOutEntry, &QPushButton::clicked, userMenuWidget, |
| 108 | + [userMenuWidget]() { |
| 109 | + userMenuWidget->hide(); |
| 110 | + |
| 111 | + QMessageBox powerOffMsg; |
| 112 | + powerOffMsg.setWindowTitle("Log Out"); |
| 113 | + powerOffMsg.setText("Are you sure you want to log out?"); |
| 114 | + powerOffMsg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
| 115 | + powerOffMsg.setIcon(QMessageBox::Question); |
| 116 | + |
| 117 | + if (powerOffMsg.exec() == QMessageBox::Yes) { |
| 118 | + system("loginctl kill-user $USER"); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + userMenuWidget->connect(aboutEntry, &QPushButton::clicked, userMenuWidget, |
| 123 | + [userMenuWidget]() { |
| 124 | + userMenuWidget->hide(); |
| 125 | + |
| 126 | + QProcess* process = new QProcess(userMenuWidget); |
| 127 | + process->start("plainAbout"); |
| 128 | + }); |
| 129 | + |
| 130 | + |
| 131 | + return {userMenuWidget, settingsEntry, aboutEntry, logOutEntry, powerOffEntry, rebootEntry}; |
| 132 | +} |
| 133 | + |
0 commit comments