diff --git a/include/mainwindow.h b/include/mainwindow.h index 248982e..f6c6e63 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -86,7 +86,7 @@ private: void onNextButtonClicked(); void onPrevButtonClicked(); void updateNavigationButtons(qsizetype currentIndex); - void updateUI(); + void updateUI(const QString ¤tDeviceName = QString()); void populateWindow(const QJsonObject &tempObj, const QString &health, const QVector>& nvmeLogOrdered = QVector>()); void addSCSIErrorCounterLogTable(const QJsonObject &scsiErrorCounterLog); void addNvmeLogTable(const QVector>& nvmeLogOrdered); diff --git a/include/utils.h b/include/utils.h index cc53e11..06fa8ec 100644 --- a/include/utils.h +++ b/include/utils.h @@ -11,7 +11,7 @@ class utils public: utils() = default; - void clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalLayout, QSpacerItem* buttonStretch, QMenu* menuDisk); + QString clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalLayout, QSpacerItem* buttonStretch, QMenu* menuDisk); QString getSmartctlPath(); QString getSmartctlOutput(const QStringList &arguments, bool root, bool initializing); QPair scanDevices(bool initializing); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0ec7217..ec3a182 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -182,13 +182,21 @@ void MainWindow::updateNavigationButtons(qsizetype currentIndex) ); } -void MainWindow::updateUI() +void MainWindow::updateUI(const QString ¤tDeviceName) { QVector diskItems; bool firstTime = true; + bool isFahrenheit = ui->actionUse_Fahrenheit->isChecked(); globalIsNvme = false; + QString degreeSymbol; + if (isFahrenheit) { // We don't do Kelvin + degreeSymbol = "°F"; + } else { + degreeSymbol = "°C"; + } + QList oldActions = disksGroup->actions(); for (QAction *action : std::as_const(oldActions)) { disksGroup->removeAction(action); @@ -196,6 +204,8 @@ void MainWindow::updateUI() delete action; } + int deviceToSelect = -1; + for (int i = 0; i < devices.size(); ++i) { QJsonObject device = devices[i].toObject(); QString deviceName = device["name"].toString(); @@ -220,7 +230,7 @@ void MainWindow::updateUI() } QJsonArray attributes = localObj["ata_smart_attributes"].toObject()["table"].toArray(); - QString temperature = "-- °C"; + QString temperature = "-- " + degreeSymbol; QJsonValue smartStatusValue = localObj.value("smart_status"); bool healthPassed = localObj["smart_status"].toObject()["passed"].toBool(); bool caution = false; @@ -243,12 +253,13 @@ void MainWindow::updateUI() QJsonObject temperatureObj = localObj["temperature"].toObject(); int temperatureInt = temperatureObj["current"].toInt(); if (temperatureInt > 0) { - if (ui->actionUse_Fahrenheit->isChecked()) { + if (isFahrenheit) { int fahrenheit = static_cast((temperatureInt * 9.0 / 5.0) + 32.0); - temperature = QString::number(fahrenheit) + " °F"; + temperature = QString::number(fahrenheit); } else { - temperature = QString::number(temperatureInt) + " °C"; + temperature = QString::number(temperatureInt); } + temperature = temperature + " " + degreeSymbol; } QVector> nvmeSmartOrdered; @@ -324,6 +335,7 @@ void MainWindow::updateUI() CustomButton *button = new CustomButton(health, temperature, deviceName, healthColor, this); button->setToolTip(tr("Disk") + " " + QString::number(i) + " : " + modelName + " : " + diskCapacityString); + button->setProperty("deviceName", deviceName); buttonGroup->addButton(button); horizontalLayout->addWidget(button); @@ -376,6 +388,19 @@ void MainWindow::updateUI() } } + if (!currentDeviceName.isEmpty() && deviceName == currentDeviceName) { + deviceToSelect = i; + globalObj = localObj; + globalHealth = health; + globalIsNvme = isNvme; + if (isNvme) { + globalNvmeSmartOrdered = nvmeSmartOrdered; + } + button->setChecked(true); + diskAction->setChecked(true); + firstTime = false; + } + connect(gridView, &GridView::diskSelected, this, [=](int selectedIndex) { if (selectedIndex >= 0 && selectedIndex < buttonGroup->buttons().size()) { QAbstractButton *gridButton = buttonGroup->buttons().at(selectedIndex); @@ -395,8 +420,10 @@ void MainWindow::updateUI() populateWindow(globalObj, globalHealth); } - updateNavigationButtons(buttonGroup->buttons().indexOf(buttonGroup->checkedButton())); gridView->setDisks(diskItems); + int activeIndex = deviceToSelect >= 0 ? deviceToSelect : 0; + gridView->setActiveIndex(activeIndex); + updateNavigationButtons(activeIndex); } void MainWindow::populateWindow(const QJsonObject &localObj, const QString &health, const QVector>& nvmeLogOrdered) @@ -1315,8 +1342,7 @@ void MainWindow::on_actionRescan_Refresh_triggered() deviceOutputs = values.first; devices = values.second; if (!deviceOutputs.isEmpty()) { - Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk); - updateUI(); + updateUI(Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk)); } } @@ -1324,8 +1350,7 @@ void MainWindow::on_actionIgnore_C4_Reallocation_Event_Count_toggled(bool enable { settings.setValue("IgnoreC4", enabled); if (!initializing) { - Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk); - updateUI(); + updateUI(Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk)); } } @@ -1333,8 +1358,7 @@ void MainWindow::on_actionHEX_toggled(bool enabled) { settings.setValue("HEX", enabled); if (!initializing) { - Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk); - updateUI(); + updateUI(Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk)); } } @@ -1342,8 +1366,7 @@ void MainWindow::on_actionUse_Fahrenheit_toggled(bool enabled) { settings.setValue("Fahrenheit", enabled); if (!initializing) { - Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk); - updateUI(); + updateUI(Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk)); } } @@ -1358,8 +1381,7 @@ void MainWindow::on_actionUse_GB_instead_of_TB_toggled(bool gigabytes) { settings.setValue("UseGB", gigabytes); if (!initializing) { - Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk); - updateUI(); + updateUI(Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk)); } } @@ -1390,8 +1412,7 @@ void MainWindow::on_actionClear_Settings_triggered() ui->actionUse_GB_instead_of_TB->setChecked(false); if (!initializing) { - Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk); - updateUI(); + updateUI(Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk)); } } } diff --git a/src/utils.cpp b/src/utils.cpp index f1f9e05..1b52780 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,8 +11,10 @@ #include #include -void utils::clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalLayout, QSpacerItem* buttonStretch, QMenu* menuDisk) +QString utils::clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalLayout, QSpacerItem* buttonStretch, QMenu* menuDisk) { + QString currentDeviceName = buttonGroup->checkedButton()->property("deviceName").toString(); + QList buttons = buttonGroup->buttons(); for (QAbstractButton* button : std::as_const(buttons)) { buttonGroup->removeButton(button); @@ -33,6 +35,8 @@ void utils::clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalL foundSeparator = true; } } + + return currentDeviceName; } QString utils::getSmartctlPath() {