pane.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "pane.h"
  2. QJsonValue Pane::getConfigValue(QString entry) {
  3. return mCfgObj->operator[](entry);
  4. }
  5. QJsonValue Pane::getConfigValue(QString entry, QString subentry) {
  6. return mCfgObj->operator[](entry).toObject()[subentry];
  7. }
  8. void Pane::setAppearance(QWidget* widget) {
  9. // Stylesheet
  10. QString themeName = getConfigValue("theme").toString();
  11. QString stylesheetPath = QString("/usr/share/plainDE/styles/%1").arg(themeName);
  12. QFile stylesheetReader(stylesheetPath);
  13. stylesheetReader.open(QIODevice::ReadOnly | QIODevice::Text);
  14. QTextStream styleSheet(&stylesheetReader);
  15. widget->setStyleSheet(styleSheet.readAll());
  16. stylesheetReader.close();
  17. // Font
  18. mFont.setFamily(getConfigValue("fontFamily").toString());
  19. mFont.setPointSize(getConfigValue("fontSize").toInt());
  20. mTitleFont = mFont;
  21. mTitleFont.setBold(true);
  22. widget->setFont(mFont);
  23. }
  24. void Pane::setBasicUI(QString paneName, QString iconName) {
  25. QHBoxLayout* paneTitleLayout = new QHBoxLayout();
  26. paneTitleLayout->setContentsMargins(1, 1, 1, 1);
  27. QLabel* iconLabel = new QLabel();
  28. iconLabel->setPixmap(QIcon::fromTheme(iconName).pixmap(32, 32));
  29. paneTitleLayout->addWidget(iconLabel);
  30. QLabel* titleLabel = new QLabel(paneName);
  31. titleLabel->setFont(mTitleFont);
  32. paneTitleLayout->addWidget(titleLabel);
  33. paneTitleLayout->addSpacerItem(new QSpacerItem(0, 0,
  34. QSizePolicy::MinimumExpanding,
  35. QSizePolicy::Ignored));
  36. mLayout->addLayout(paneTitleLayout);
  37. mContentsWidget = new QWidget();
  38. QVBoxLayout* contentsLayout = new QVBoxLayout(mContentsWidget);
  39. contentsLayout->setContentsMargins(10, 2, 2, 2);
  40. setAppearance(mContentsWidget);
  41. }
  42. void Pane::finalizeUI() {
  43. QScrollArea* scrollArea = new QScrollArea();
  44. scrollArea->setWidget(mContentsWidget);
  45. scrollArea->setWidgetResizable(true);
  46. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  47. mLayout->addWidget(scrollArea);
  48. }
  49. void Pane::setEntry(QString key, QJsonValue value) {
  50. mCfgObj->operator[](key) = value;
  51. }
  52. void Pane::saveConfig() {
  53. QString configPath = QDir::homePath() + "/.config/plainDE/config.json";
  54. QJsonDocument doc(*mCfgObj);
  55. QFile file(configPath);
  56. file.open(QFile::WriteOnly);
  57. file.write(doc.toJson(QJsonDocument::Indented));
  58. file.close();
  59. QDBusConnection bus = QDBusConnection::sessionBus();
  60. QDBusMessage req = QDBusMessage::createMethodCall("org.plainDE.plainPanel",
  61. "/Actions",
  62. "org.plainDE.actions",
  63. "reconfigurePanel");
  64. QDBusMessage resp = bus.call(req);
  65. // plainPanel crashed or is not running at the moment
  66. if (resp.type() == QDBusMessage::ErrorMessage) {
  67. QMessageBox msg;
  68. msg.setWindowTitle(tr("Settings saved"));
  69. msg.setText(tr("Settings were saved. Unable to reconfigure plainPanel. "
  70. "Restart it manually."));
  71. msg.setStandardButtons(QMessageBox::Ok);
  72. msg.setIcon(QMessageBox::Warning);
  73. msg.exec();
  74. }
  75. }
  76. void Pane::setPaneContents() {
  77. }
  78. Pane::Pane(QWidget* parent,
  79. QJsonObject* cfgObj,
  80. QString paneName,
  81. QString iconName) : QWidget(parent) {
  82. mCfgObj = cfgObj;
  83. QIcon::setThemeName(getConfigValue("iconTheme").toString());
  84. mLayout = new QVBoxLayout(this);
  85. mLayout->setContentsMargins(4, 4, 4, 4);
  86. setAppearance(this);
  87. setBasicUI(paneName, iconName);
  88. }