appmenudialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "appmenudialog.h"
  2. QIcon AppMenuDialog::resolveIconNameOrPath(QString iconNameOrPath, bool showUnknown) {
  3. if (QIcon::hasThemeIcon(iconNameOrPath)) {
  4. return QIcon::fromTheme(iconNameOrPath);
  5. }
  6. else if (QFile::exists(iconNameOrPath)) {
  7. return QIcon(iconNameOrPath);
  8. }
  9. else if (showUnknown) {
  10. return QIcon::fromTheme("dialog-question");
  11. }
  12. else {
  13. return QIcon();
  14. }
  15. }
  16. void AppMenuDialog::setPaneContents() {
  17. mContentsWidget = new QWidget();
  18. QVBoxLayout* contentsLayout = new QVBoxLayout(mContentsWidget);
  19. mContentsWidget->setObjectName("innerPane");
  20. // Adding widgets
  21. QLabel* menuTitleLabel = new QLabel(tr("Menu Title"));
  22. menuTitleLabel->setFont(mTitleFont);
  23. contentsLayout->addWidget(menuTitleLabel);
  24. QLineEdit* menuTitleLineEdit = new QLineEdit();
  25. menuTitleLineEdit->setPlaceholderText(tr("Type app menu title here..."));
  26. menuTitleLineEdit->setFont(mFont);
  27. contentsLayout->addWidget(menuTitleLineEdit);
  28. QLabel* menuIconLabel = new QLabel(tr("Menu Icon"));
  29. menuIconLabel->setFont(mTitleFont);
  30. contentsLayout->addWidget(menuIconLabel);
  31. QHBoxLayout* menuIconLayout = new QHBoxLayout();
  32. QPushButton* sampleButton = new QPushButton();
  33. sampleButton->setFixedSize(16, 16);
  34. sampleButton->setFlat(true);
  35. menuIconLayout->addWidget(sampleButton);
  36. QLineEdit* menuIconLineEdit = new QLineEdit();
  37. menuIconLineEdit->setPlaceholderText(tr("Type app menu icon name or path here..."));
  38. menuIconLineEdit->setFont(mFont);
  39. menuIconLayout->addWidget(menuIconLineEdit);
  40. contentsLayout->addLayout(menuIconLayout);
  41. QHBoxLayout* menuIconSizeLayout = new QHBoxLayout();
  42. QLabel* menuIconSizeLabel = new QLabel(tr("Menu Icon Size"));
  43. menuIconSizeLabel->setFont(mTitleFont);
  44. menuIconSizeLayout->addWidget(menuIconSizeLabel);
  45. QSpinBox* menuIconSizeSpinBox = new QSpinBox();
  46. menuIconSizeSpinBox->setMinimum(0);
  47. menuIconSizeSpinBox->setMaximum(256);
  48. menuIconSizeSpinBox->setFont(mFont);
  49. menuIconSizeLayout->addWidget(menuIconSizeSpinBox);
  50. contentsLayout->addLayout(menuIconSizeLayout);
  51. QLabel* favAppsLabel = new QLabel(tr("Favorite apps"));
  52. favAppsLabel->setFont(mTitleFont);
  53. contentsLayout->addWidget(favAppsLabel);
  54. QHBoxLayout* favAppsLayout = new QHBoxLayout();
  55. QListWidget* appsListWidget = new QListWidget();
  56. appsListWidget->setFont(mFont);
  57. QString style = QString("QListView::item:selected { "
  58. "background-color: %1; color: #ffffff; "
  59. "};").arg(getConfigValue("accent").toString());
  60. appsListWidget->setStyleSheet(style);
  61. favAppsLayout->addWidget(appsListWidget);
  62. QVBoxLayout* favButtonsLayout = new QVBoxLayout();
  63. QPushButton* addAppButton = new QPushButton("+");
  64. addAppButton->setFont(mFont);
  65. favButtonsLayout->addWidget(addAppButton);
  66. QPushButton* removeAppButton = new QPushButton("-");
  67. removeAppButton->setFont(mFont);
  68. favButtonsLayout->addWidget(removeAppButton);
  69. QPushButton* moveUpButton = new QPushButton("↑"); // U+2191 - Up arrow
  70. moveUpButton->setFont(mFont);
  71. favButtonsLayout->addWidget(moveUpButton);
  72. QPushButton* moveDownButton = new QPushButton("↓"); // U+2193 - Down arrow
  73. moveDownButton->setFont(mFont);
  74. favButtonsLayout->addWidget(moveDownButton);
  75. favAppsLayout->addLayout(favButtonsLayout);
  76. QListWidget* favAppsListWidget = new QListWidget();
  77. favAppsListWidget->setFont(mFont);
  78. favAppsListWidget->setStyleSheet(style);
  79. favAppsLayout->addWidget(favAppsListWidget);
  80. contentsLayout->addLayout(favAppsLayout);
  81. QHBoxLayout* buttonsLayout = new QHBoxLayout();
  82. buttonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  83. QSizePolicy::MinimumExpanding,
  84. QSizePolicy::Ignored));
  85. QPushButton* cancelButton = new QPushButton(tr("Cancel"));
  86. cancelButton->setFont(mFont);
  87. buttonsLayout->addWidget(cancelButton);
  88. QPushButton* okButton = new QPushButton(tr("OK"));
  89. okButton->setFont(mFont);
  90. buttonsLayout->addWidget(okButton);
  91. contentsLayout->addLayout(buttonsLayout);
  92. // Misc
  93. QDir appDir("/usr/share/applications");
  94. QStringList desktopEntriesList = appDir.entryList(QDir::Files | QDir::NoDotAndDotDot);
  95. for (int i = 0; i < desktopEntriesList.length(); ++i) {
  96. QString name, iconNameOrPath;
  97. bool nodisplay;
  98. QString desktopEntryPath = appDir.absoluteFilePath(desktopEntriesList[i]);
  99. if (desktopEntryPath.endsWith(".desktop")) {
  100. QSettings desktopFileReader(desktopEntryPath, QSettings::IniFormat);
  101. desktopFileReader.sync();
  102. desktopFileReader.beginGroup("Desktop Entry");
  103. name = desktopFileReader.value("Name").toString();
  104. iconNameOrPath = desktopFileReader.value("Icon").toString();
  105. nodisplay = desktopFileReader.value("NoDisplay").toBool();
  106. desktopFileReader.endGroup();
  107. if (!nodisplay) {
  108. QListWidgetItem* item = new QListWidgetItem(name);
  109. item->setIcon(resolveIconNameOrPath(iconNameOrPath, true));
  110. appsListWidget->addItem(item);
  111. mEntryByItem[item] = desktopEntryPath;
  112. }
  113. }
  114. }
  115. QString homeDir = getenv("HOME");
  116. if (QDir(homeDir + "/.local/share/applications").exists()) {
  117. appDir.cd(homeDir + "/.local/share/applications");
  118. desktopEntriesList = appDir.entryList(QDir::Files | QDir::NoDotAndDotDot);
  119. for (int i = 0; i < desktopEntriesList.length(); ++i) {
  120. QString name, iconName;
  121. bool nodisplay;
  122. QString desktopEntryPath = appDir.absoluteFilePath(desktopEntriesList[i]);
  123. if (desktopEntryPath.endsWith(".desktop")) {
  124. QSettings desktopFileReader(desktopEntryPath, QSettings::IniFormat);
  125. desktopFileReader.sync();
  126. desktopFileReader.beginGroup("Desktop Entry");
  127. name = desktopFileReader.value("Name").toString();
  128. iconName = desktopFileReader.value("Icon").toString();
  129. nodisplay = desktopFileReader.value("NoDisplay").toBool();
  130. desktopFileReader.endGroup();
  131. if (!nodisplay) {
  132. QListWidgetItem* item = new QListWidgetItem(name);
  133. if (QIcon::hasThemeIcon(iconName)) {
  134. item->setIcon(QIcon::fromTheme(iconName));
  135. }
  136. else if (QFile::exists(iconName)) {
  137. item->setIcon(QIcon(iconName));
  138. }
  139. else {
  140. item->setIcon(QIcon::fromTheme("dialog-question"));
  141. }
  142. appsListWidget->addItem(item);
  143. mEntryByItem[item] = desktopEntryPath;
  144. }
  145. }
  146. }
  147. }
  148. appsListWidget->sortItems();
  149. // Setting current settings
  150. menuTitleLineEdit->setText(getConfigValue("menuText").toString());
  151. menuIconLineEdit->setText(getConfigValue("menuIcon").toString());
  152. sampleButton->setIcon(resolveIconNameOrPath(getConfigValue("menuIcon").toString(), false));
  153. menuIconSizeSpinBox->setValue(getConfigValue("menuIconSize").toInt());
  154. QJsonArray favApps = getConfigValue("favApps").toArray();
  155. foreach (QVariant favApp, favApps) {
  156. QString finalPath = QString("/usr/share/applications/%1").arg(favApp.toString());
  157. if (!QFile(finalPath).exists()) {
  158. QString homePath = QDir::homePath();
  159. finalPath = QString("%1/.local/share/applications/%2").arg(homePath, favApp.toString());
  160. }
  161. QListWidgetItem* item = mEntryByItem.key(finalPath);
  162. QListWidgetItem* favItem = new QListWidgetItem(item->icon(), item->text());
  163. favAppsListWidget->addItem(favItem);
  164. mEntryByItem[favItem] = mEntryByItem[item];
  165. }
  166. // Making connections
  167. connect(menuIconLineEdit, &QLineEdit::textChanged, this, [this,
  168. menuIconLineEdit,
  169. sampleButton]() {
  170. QString iconNameOrPath = menuIconLineEdit->text();
  171. sampleButton->setIcon(resolveIconNameOrPath(iconNameOrPath, false));
  172. });
  173. connect(addAppButton, &QPushButton::clicked, this, [this, appsListWidget, favAppsListWidget]() {
  174. if (!appsListWidget->selectedItems().isEmpty()) {
  175. QListWidgetItem* currentItem = appsListWidget->currentItem();
  176. QListWidgetItem* item = new QListWidgetItem(currentItem->icon(),
  177. currentItem->text());
  178. favAppsListWidget->addItem(item);
  179. mEntryByItem[item] = mEntryByItem[currentItem];
  180. }
  181. });
  182. connect(removeAppButton, &QPushButton::clicked, this, [this, favAppsListWidget, appsListWidget]() {
  183. if (!favAppsListWidget->selectedItems().isEmpty()) {
  184. favAppsListWidget->takeItem(favAppsListWidget->currentRow());
  185. }
  186. });
  187. connect(moveUpButton, &QPushButton::clicked, this, [favAppsListWidget]() {
  188. if (!favAppsListWidget->selectedItems().isEmpty()) {
  189. int currentPos = favAppsListWidget->currentRow();
  190. if (currentPos > 0) {
  191. favAppsListWidget->insertItem(currentPos - 1,
  192. favAppsListWidget->takeItem(currentPos));
  193. favAppsListWidget->setCurrentRow(currentPos - 1);
  194. }
  195. }
  196. });
  197. connect(moveDownButton, &QPushButton::clicked, this, [favAppsListWidget]() {
  198. if (!favAppsListWidget->selectedItems().isEmpty()) {
  199. int currentPos = favAppsListWidget->currentRow();
  200. if (currentPos < favAppsListWidget->count() - 1) {
  201. favAppsListWidget->insertItem(currentPos + 1,
  202. favAppsListWidget->takeItem(currentPos));
  203. favAppsListWidget->setCurrentRow(currentPos + 1);
  204. }
  205. }
  206. });
  207. connect(cancelButton, &QPushButton::clicked, this, [this]() {
  208. this->hide();
  209. delete this;
  210. });
  211. connect(okButton, &QPushButton::clicked, this, [this, menuTitleLineEdit, menuIconLineEdit,
  212. menuIconSizeSpinBox, favAppsListWidget]() {
  213. prepareToSave(menuTitleLineEdit,
  214. menuIconLineEdit,
  215. menuIconSizeSpinBox,
  216. favAppsListWidget);
  217. saveConfig();
  218. this->hide();
  219. delete this;
  220. });
  221. finalizeUI();
  222. setWindowGeometry();
  223. setTransparency(this);
  224. }
  225. void AppMenuDialog::prepareToSave(QLineEdit* menuTitleLineEdit,
  226. QLineEdit* menuIconLineEdit,
  227. QSpinBox* menuIconSizeSpinBox,
  228. QListWidget* favAppsListWidget) {
  229. setEntry("menuText", menuTitleLineEdit->text());
  230. setEntry("menuIcon", menuIconLineEdit->text());
  231. setEntry("menuIconSize", menuIconSizeSpinBox->value());
  232. QJsonArray favApps;
  233. for (int i = 0; i < favAppsListWidget->count(); ++i) {
  234. QString desktopEntryPath = mEntryByItem[favAppsListWidget->item(i)];
  235. QString filename = desktopEntryPath.split('/').last();
  236. favApps.append(filename);
  237. }
  238. setEntry("favApps", favApps);
  239. }
  240. AppMenuDialog::AppMenuDialog(QJsonObject* cfgObj) : Dialog(cfgObj,
  241. tr("App Menu applet settings"),
  242. "app-launcher") {
  243. }