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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2012 Razor team
* Authors:
* Łukasz Twarduś <ltwardus@gmail.com>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "lxqtsensorsconfiguration.h"
#include "ui_lxqtsensorsconfiguration.h"
#include <QCheckBox>
#include <QColorDialog>
#include <QDebug>
#include <QPushButton>
#include <QStringList>
LXQtSensorsConfiguration::LXQtSensorsConfiguration(PluginSettings *settings, QWidget *parent) :
LXQtPanelPluginConfigDialog(settings, parent),
ui(new Ui::LXQtSensorsConfiguration),
mLockSettingChanges(false)
{
setAttribute(Qt::WA_DeleteOnClose);
setObjectName(QStringLiteral("SensorsConfigurationWindow"));
ui->setupUi(this);
// We load settings here cause we have to set up dynamic widgets
loadSettings();
connect(ui->buttons, &QDialogButtonBox::clicked, this, &LXQtSensorsConfiguration::dialogButtonsAction);
connect(ui->updateIntervalSB, QOverload<int>::of(&QSpinBox::valueChanged), this, &LXQtSensorsConfiguration::saveSettings);
connect(ui->tempBarWidthSB, QOverload<int>::of(&QSpinBox::valueChanged), this, &LXQtSensorsConfiguration::saveSettings);
connect(ui->detectedChipsCB, QOverload<int>::of(&QComboBox::activated), this, &LXQtSensorsConfiguration::detectedChipSelected);
connect(ui->celsiusTempScaleRB, &QRadioButton::toggled, this, &LXQtSensorsConfiguration::saveSettings);
// We don't need signal from the other radio box as celsiusTempScaleRB will send one
//connect(ui->fahrenheitTempScaleRB, SIGNAL(toggled(bool)), this, SLOT(saveSettings()));
connect(ui->warningAboutHighTemperatureChB, &QCheckBox::toggled, this, &LXQtSensorsConfiguration::saveSettings);
/**
* Signals for enable/disable and bar color change are set in the loadSettings method because
* we are creating them dynamically.
*/
}
LXQtSensorsConfiguration::~LXQtSensorsConfiguration()
{
delete ui;
}
void LXQtSensorsConfiguration::loadSettings()
{
mLockSettingChanges = true;
ui->updateIntervalSB->setValue(settings().value(QStringLiteral("updateInterval")).toInt());
ui->tempBarWidthSB->setValue(settings().value(QStringLiteral("tempBarWidth")).toInt());
if (settings().value(QStringLiteral("useFahrenheitScale")).toBool())
{
ui->fahrenheitTempScaleRB->setChecked(true);
}
// In case of reloading settings we have to clear GUI elements
ui->detectedChipsCB->clear();
settings().beginGroup(QStringLiteral("chips"));
QStringList chipNames = settings().childGroups();
for (int i = 0; i < chipNames.size(); ++i)
{
ui->detectedChipsCB->addItem(chipNames[i]);
}
settings().endGroup();
// Load feature for the first chip if exist
if (chipNames.size() > 0)
{
detectedChipSelected(0);
}
ui->warningAboutHighTemperatureChB->setChecked(
settings().value(QStringLiteral("warningAboutHighTemperature")).toBool());
mLockSettingChanges = false;
}
void LXQtSensorsConfiguration::saveSettings()
{
if (mLockSettingChanges)
return;
settings().setValue(QStringLiteral("updateInterval"), ui->updateIntervalSB->value());
settings().setValue(QStringLiteral("tempBarWidth"), ui->tempBarWidthSB->value());
if (ui->fahrenheitTempScaleRB->isChecked())
{
settings().setValue(QStringLiteral("useFahrenheitScale"), true);
}
else
{
settings().setValue(QStringLiteral("useFahrenheitScale"), false);
}
settings().beginGroup(QStringLiteral("chips"));
QStringList chipNames = settings().childGroups();
if (chipNames.size())
{
QStringList chipFeatureLabels;
QPushButton* colorButton = nullptr;
QCheckBox* enabledCheckbox = nullptr;
settings().beginGroup(chipNames[ui->detectedChipsCB->currentIndex()]);
chipFeatureLabels = settings().childGroups();
for (int j = 0; j < chipFeatureLabels.size(); ++j)
{
settings().beginGroup(chipFeatureLabels[j]);
enabledCheckbox = qobject_cast<QCheckBox*>(ui->chipFeaturesT->cellWidget(j, 0));
// We know what we are doing so we don't have to check if enabledCheckbox == 0
settings().setValue(QStringLiteral("enabled"), enabledCheckbox->isChecked());
colorButton = qobject_cast<QPushButton*>(ui->chipFeaturesT->cellWidget(j, 2));
// We know what we are doing so we don't have to check if colorButton == 0
settings().setValue(
QStringLiteral("color"),
colorButton->palette().color(QPalette::Normal, QPalette::Button).name());
settings().endGroup();
}
settings().endGroup();
}
settings().endGroup();
settings().setValue(QStringLiteral("warningAboutHighTemperature"),
ui->warningAboutHighTemperatureChB->isChecked());
}
void LXQtSensorsConfiguration::changeProgressBarColor()
{
QAbstractButton* btn = qobject_cast<QAbstractButton*>(sender());
if (btn)
{
QPalette pal = btn->palette();
QColor color = QColorDialog::getColor(pal.color(QPalette::Normal, QPalette::Button), this);
if (color.isValid())
{
pal.setColor(QPalette::Normal, QPalette::Button, color);
btn->setPalette(pal);
saveSettings();
}
}
else
{
qDebug() << "LXQtSensorsConfiguration::changeProgressBarColor():" << "invalid button cast";
}
}
void LXQtSensorsConfiguration::detectedChipSelected(int index)
{
settings().beginGroup(QStringLiteral("chips"));
QStringList chipNames = settings().childGroups();
QStringList chipFeatureLabels;
QPushButton* colorButton = nullptr;
QCheckBox* enabledCheckbox = nullptr;
QTableWidgetItem *chipFeatureLabel = nullptr;
if (index < chipNames.size())
{
qDebug() << "Selected chip: " << ui->detectedChipsCB->currentText();
// In case of reloading settings we have to clear GUI elements
ui->chipFeaturesT->setRowCount(0);
// Add detected chips and features
QStringList chipFeaturesLabels;
chipFeaturesLabels << tr("Enabled") << tr("Label") << tr("Color");
ui->chipFeaturesT->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->chipFeaturesT->setHorizontalHeaderLabels(chipFeaturesLabels);
settings().beginGroup(chipNames[index]);
chipFeatureLabels = settings().childGroups();
for (int j = 0; j < chipFeatureLabels.size(); ++j)
{
settings().beginGroup(chipFeatureLabels[j]);
ui->chipFeaturesT->insertRow(j);
enabledCheckbox = new QCheckBox(ui->chipFeaturesT);
enabledCheckbox->setChecked(settings().value(QStringLiteral("enabled")).toBool());
// Connect here after the setChecked call because we don't want to send signal
#if (QT_VERSION >= QT_VERSION_CHECK(6,7,0))
connect(enabledCheckbox, &QCheckBox::checkStateChanged, this, &LXQtSensorsConfiguration::saveSettings);
#else
connect(enabledCheckbox, &QCheckBox::stateChanged, this, &LXQtSensorsConfiguration::saveSettings);
#endif
ui->chipFeaturesT->setCellWidget(j, 0, enabledCheckbox);
chipFeatureLabel = new QTableWidgetItem(chipFeatureLabels[j]);
chipFeatureLabel->setFlags(Qt::ItemIsEnabled);
ui->chipFeaturesT->setItem(j, 1, chipFeatureLabel);
colorButton = new QPushButton(ui->chipFeaturesT);
connect(colorButton, &QPushButton::clicked, this, &LXQtSensorsConfiguration::changeProgressBarColor);
QPalette pal = colorButton->palette();
pal.setColor(QPalette::Normal, QPalette::Button,
QColor(settings().value(QStringLiteral("color")).toString()));
colorButton->setPalette(pal);
ui->chipFeaturesT->setCellWidget(j, 2, colorButton);
settings().endGroup();
}
settings().endGroup();
}
else
{
qDebug() << "Invalid chip index: " << index;
}
settings().endGroup();
}
|