usermenudialog.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "usermenudialog.h"
  2. void UserMenuDialog::setPaneContents() {
  3. mContentsWidget = new QWidget();
  4. QVBoxLayout* contentsLayout = new QVBoxLayout(mContentsWidget);
  5. mContentsWidget->setObjectName("innerPane");
  6. // Adding widgets
  7. QLabel* userIconLabel = new QLabel(tr("User Icon"));
  8. userIconLabel->setFont(mTitleFont);
  9. contentsLayout->addWidget(userIconLabel);
  10. QRadioButton* defaultRadioButton = new QRadioButton(tr("Use default icon"));
  11. defaultRadioButton->setFont(mFont);
  12. contentsLayout->addWidget(defaultRadioButton);
  13. QRadioButton* customRadioButton = new QRadioButton(tr("Use custom icon"));
  14. customRadioButton->setFont(mFont);
  15. contentsLayout->addWidget(customRadioButton);
  16. QHBoxLayout* customLayout = new QHBoxLayout();
  17. QPushButton* customButton = new QPushButton();
  18. customButton->setFlat(true);
  19. customButton->setVisible(false);
  20. customLayout->addWidget(customButton);
  21. QLineEdit* customIconLineEdit = new QLineEdit();
  22. customIconLineEdit->setFont(mFont);
  23. customIconLineEdit->setPlaceholderText(tr("Type icon name or path here..."));
  24. customIconLineEdit->setVisible(false);
  25. customLayout->addWidget(customIconLineEdit);
  26. contentsLayout->addLayout(customLayout);
  27. QCheckBox* enableShutdownTimerCheckBox = new QCheckBox("Enable Shutdown Timer");
  28. enableShutdownTimerCheckBox->setFont(mFont);
  29. contentsLayout->addWidget(enableShutdownTimerCheckBox);
  30. QHBoxLayout* shutdownTimerLayout = new QHBoxLayout();
  31. QSpinBox* shutdownTimerSpinBox = new QSpinBox();
  32. shutdownTimerSpinBox->setFont(mFont);
  33. shutdownTimerSpinBox->setMinimum(0);
  34. shutdownTimerSpinBox->setMaximum(INT_MAX);
  35. shutdownTimerSpinBox->setValue(30);
  36. shutdownTimerSpinBox->setVisible(false);
  37. shutdownTimerLayout->addWidget(shutdownTimerSpinBox);
  38. QLabel* secLabel = new QLabel("sec");
  39. secLabel->setFont(mFont);
  40. secLabel->setVisible(false);
  41. shutdownTimerLayout->addWidget(secLabel);
  42. shutdownTimerLayout->addSpacerItem(new QSpacerItem(0, 0,
  43. QSizePolicy::MinimumExpanding,
  44. QSizePolicy::Ignored));
  45. contentsLayout->addLayout(shutdownTimerLayout);
  46. contentsLayout->addSpacerItem(new QSpacerItem(0, 0,
  47. QSizePolicy::Ignored,
  48. QSizePolicy::MinimumExpanding));
  49. QHBoxLayout* buttonsLayout = new QHBoxLayout();
  50. buttonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  51. QSizePolicy::MinimumExpanding,
  52. QSizePolicy::Ignored));
  53. QPushButton* cancelButton = new QPushButton(tr("Cancel"));
  54. cancelButton->setFont(mFont);
  55. buttonsLayout->addWidget(cancelButton);
  56. QPushButton* okButton = new QPushButton(tr("OK"));
  57. okButton->setFont(mFont);
  58. buttonsLayout->addWidget(okButton);
  59. contentsLayout->addLayout(buttonsLayout);
  60. // Making connections
  61. connect(cancelButton, &QPushButton::clicked, this, [this]() {
  62. this->hide();
  63. delete this;
  64. });
  65. connect(okButton, &QPushButton::clicked, this, [this, customRadioButton,
  66. customIconLineEdit,
  67. enableShutdownTimerCheckBox,
  68. shutdownTimerSpinBox]() {
  69. prepareToSave(customRadioButton,
  70. customIconLineEdit,
  71. enableShutdownTimerCheckBox,
  72. shutdownTimerSpinBox);
  73. saveConfig();
  74. this->hide();
  75. delete this;
  76. });
  77. connect(customRadioButton, &QRadioButton::clicked, this, [customIconLineEdit, customButton]() {
  78. customIconLineEdit->setVisible(true);
  79. customButton->setVisible(true);
  80. });
  81. connect(defaultRadioButton, &QRadioButton::clicked, this, [customIconLineEdit, customButton]() {
  82. customIconLineEdit->setVisible(false);
  83. customButton->setVisible(false);
  84. });
  85. connect(customIconLineEdit, &QLineEdit::textChanged, this, [customButton, customIconLineEdit]() {
  86. QString avatar = customIconLineEdit->text();
  87. if (QIcon::hasThemeIcon(avatar)) {
  88. customButton->setIcon(QIcon::fromTheme(avatar));
  89. }
  90. else if (QFile::exists(avatar)) {
  91. customButton->setIcon(QIcon(avatar));
  92. }
  93. else {
  94. customButton->setIcon(QIcon());
  95. }
  96. });
  97. connect(enableShutdownTimerCheckBox, &QCheckBox::stateChanged, this, [enableShutdownTimerCheckBox,
  98. shutdownTimerSpinBox,
  99. secLabel]() {
  100. if (enableShutdownTimerCheckBox->isChecked()) {
  101. shutdownTimerSpinBox->setVisible(true);
  102. secLabel->setVisible(true);
  103. }
  104. else {
  105. shutdownTimerSpinBox->setVisible(false);
  106. secLabel->setVisible(false);
  107. }
  108. });
  109. // Setting current settings
  110. QString avatar = getConfigValue("avatar").toString();
  111. if (avatar.isEmpty()) {
  112. defaultRadioButton->setChecked(true);
  113. }
  114. else {
  115. customRadioButton->setChecked(true);
  116. customIconLineEdit->setText(avatar);
  117. if (QIcon::hasThemeIcon(avatar)) {
  118. customButton->setIcon(QIcon::fromTheme(avatar));
  119. }
  120. else if (QFile::exists(avatar)) {
  121. customButton->setIcon(QIcon(avatar));
  122. }
  123. else {
  124. customButton->setIcon(QIcon());
  125. }
  126. customButton->setVisible(true);
  127. customIconLineEdit->setVisible(true);
  128. }
  129. int seconds = getConfigValue("secondsUntilPowerOff").toInt();
  130. if (seconds > -1) {
  131. enableShutdownTimerCheckBox->setChecked(true);
  132. shutdownTimerSpinBox->setValue(seconds);
  133. shutdownTimerSpinBox->setVisible(true);
  134. }
  135. else {
  136. enableShutdownTimerCheckBox->setChecked(false);
  137. }
  138. finalizeUI();
  139. setWindowGeometry();
  140. setTransparency(this);
  141. }
  142. void UserMenuDialog::prepareToSave(QRadioButton* customRadioButton,
  143. QLineEdit* customLineEdit,
  144. QCheckBox* enableShutdownTimerCheckBox,
  145. QSpinBox* shutdownTimerSpinBox) {
  146. if (customRadioButton->isChecked()) {
  147. setEntry("avatar", customLineEdit->text());
  148. }
  149. else { // Default icon
  150. setEntry("avatar", "");
  151. }
  152. if (enableShutdownTimerCheckBox->isChecked()) {
  153. setEntry("secondsUntilPowerOff", QJsonValue(shutdownTimerSpinBox->value()));
  154. }
  155. else {
  156. setEntry("secondsUntilPowerOff", QJsonValue(-1));
  157. }
  158. }
  159. UserMenuDialog::UserMenuDialog(QJsonObject* cfgObj) : Dialog(cfgObj,
  160. tr("User Menu applet settings"),
  161. "preferences-desktop-user") {
  162. }