UI: save and restore ui settings in primary and secondary uis
This commit is contained in:
parent
afb2d23173
commit
51193a5d0b
7 changed files with 132 additions and 4 deletions
22
src/main.cpp
22
src/main.cpp
|
|
@ -58,6 +58,8 @@ int main(int argc, char *argv[])
|
|||
|
||||
int retVal;
|
||||
|
||||
QString uiSettingsPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/smartvos_ui.json";
|
||||
|
||||
if(programMode == PROGRAM_MODE_PRIMARY || programMode == PROGRAM_MODE_HEADLESS_PRIMARY)
|
||||
{
|
||||
QString settingsPath = parser.value(settingsPathOption);
|
||||
|
|
@ -111,11 +113,13 @@ int main(int argc, char *argv[])
|
|||
microDevice = microPort;
|
||||
}
|
||||
PrimaryMainObject mainObject(microDevice, settingsPath, parser.value(hostOption), parser.value(portOption).toInt());
|
||||
QObject::connect(mainObject.tcpServer, &TcpServer::sigRequestSave, &mainObject, [&mainObject, settingsPath](){mainObject.storeToDisk(settingsPath);});
|
||||
MainWindow* w = nullptr;
|
||||
if(programMode != PROGRAM_MODE_HEADLESS_PRIMARY)
|
||||
{
|
||||
w = new MainWindow(&mainObject);
|
||||
QJsonObject uiSettings = MainObject::getJsonObjectFromDisk(uiSettingsPath);
|
||||
w->load(uiSettings);
|
||||
|
||||
QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), w, SLOT(changeHeaderLableText(QString)));
|
||||
QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), w, SLOT(changeHeaderLableText(QString)));
|
||||
QObject::connect(w, &MainWindow::sigSetRgb, &mainObject.micro, &Microcontroller::changeRgbColor);
|
||||
|
|
@ -123,8 +127,16 @@ int main(int argc, char *argv[])
|
|||
QObject::connect(w, &MainWindow::createdItem, &globalItems, &ItemStore::addItem);
|
||||
w->show();
|
||||
}
|
||||
|
||||
retVal = a->exec();
|
||||
|
||||
if(programMode != PROGRAM_MODE_HEADLESS_PRIMARY)
|
||||
{
|
||||
QJsonObject uiSettingsJson;
|
||||
w->store(uiSettingsJson);
|
||||
MainObject::storeJsonObjectToDisk(uiSettingsPath, uiSettingsJson);
|
||||
}
|
||||
|
||||
delete w;
|
||||
delete microDevice;
|
||||
}
|
||||
|
|
@ -132,11 +144,19 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
SecondaryMainObject mainObject(parser.value(hostOption), parser.value(portOption).toInt());
|
||||
MainWindow w(&mainObject);
|
||||
QJsonObject uiSettings = MainObject::getJsonObjectFromDisk(uiSettingsPath);
|
||||
w.load(uiSettings);
|
||||
|
||||
QObject::connect(&w, &MainWindow::createdItem, &globalItems, &ItemStore::addItem);
|
||||
QObject::connect(&w, &MainWindow::sigSave, mainObject.tcpClient, &TcpClient::sendItems);
|
||||
|
||||
w.show();
|
||||
|
||||
retVal = a->exec();
|
||||
|
||||
QJsonObject uiSettingsJson;
|
||||
w.store(uiSettingsJson);
|
||||
MainObject::storeJsonObjectToDisk(uiSettingsPath, uiSettingsJson);
|
||||
}
|
||||
|
||||
delete a;
|
||||
|
|
|
|||
|
|
@ -163,6 +163,12 @@ void ItemScrollBox::ensureTabExists(const QString& groupName)
|
|||
|
||||
ui->tabWidget->addTab(tab.scroller, groupName);
|
||||
tabs_[groupName] = tab;
|
||||
|
||||
if(groupName == pendingSelectedGroup_)
|
||||
{
|
||||
ui->tabWidget->setCurrentWidget(tab.scroller);
|
||||
pendingSelectedGroup_.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -189,3 +195,21 @@ void ItemScrollBox::cleanupEmptyTabs()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ItemScrollBox::store(QJsonObject& json) const
|
||||
{
|
||||
QJsonObject itemScrollBoxJson;
|
||||
int currentIndex = ui->tabWidget->currentIndex();
|
||||
if(currentIndex >= 0)
|
||||
{
|
||||
QString selectedGroup = ui->tabWidget->tabText(currentIndex);
|
||||
itemScrollBoxJson["SelectedGroup"] = selectedGroup;
|
||||
}
|
||||
json["ItemScrollBox"] = itemScrollBoxJson;
|
||||
}
|
||||
|
||||
void ItemScrollBox::load(const QJsonObject& json)
|
||||
{
|
||||
QJsonObject itemScrollBoxJson = json["ItemScrollBox"].toObject();
|
||||
pendingSelectedGroup_ = itemScrollBoxJson["SelectedGroup"].toString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <memory>
|
||||
#include <QScrollArea>
|
||||
#include <QSpacerItem>
|
||||
#include <QJsonObject>
|
||||
#include "itemwidget.h"
|
||||
#include "../items/item.h"
|
||||
#include "../items/itemstore.h"
|
||||
|
|
@ -30,6 +31,7 @@ private:
|
|||
QMap<QString, Tab> tabs_;
|
||||
QMap<QString, std::vector<ItemWidget*>> widgets_;
|
||||
Ui::RelayScrollBox *ui;
|
||||
QString pendingSelectedGroup_;
|
||||
|
||||
signals:
|
||||
void deleteRequest(const ItemData& item);
|
||||
|
|
@ -41,6 +43,9 @@ public:
|
|||
|
||||
void setItemStore(ItemStore* itemStore);
|
||||
|
||||
void store(QJsonObject& json) const;
|
||||
void load(const QJsonObject& json);
|
||||
|
||||
public slots:
|
||||
|
||||
void addItem(std::weak_ptr<Item> item);
|
||||
|
|
|
|||
|
|
@ -132,3 +132,36 @@ void MainWindow::changeHeaderLableText(QString string)
|
|||
}
|
||||
ui->label_serialRecive->setText(string);
|
||||
}
|
||||
|
||||
void MainWindow::store(QJsonObject& json) const
|
||||
{
|
||||
QJsonObject mainWindowJson;
|
||||
|
||||
QList<int> splitterSizes = ui->splitter->sizes();
|
||||
QJsonArray splitterSizeArray;
|
||||
for(int size : splitterSizes)
|
||||
splitterSizeArray.append(size);
|
||||
mainWindowJson["SplitterSizes"] = splitterSizeArray;
|
||||
|
||||
ui->relayList->store(mainWindowJson);
|
||||
ui->sensorListView->store(mainWindowJson);
|
||||
|
||||
json["MainWindow"] = mainWindowJson;
|
||||
}
|
||||
|
||||
void MainWindow::load(const QJsonObject& json)
|
||||
{
|
||||
QJsonObject mainWindowJson = json["MainWindow"].toObject();
|
||||
|
||||
QJsonArray splitterSizes = mainWindowJson["SplitterSizes"].toArray();
|
||||
if(!splitterSizes.isEmpty())
|
||||
{
|
||||
QList<int> sizes;
|
||||
for(const QJsonValue& size : splitterSizes)
|
||||
sizes.append(size.toInt());
|
||||
ui->splitter->setSizes(sizes);
|
||||
}
|
||||
|
||||
ui->relayList->load(mainWindowJson);
|
||||
ui->sensorListView->load(mainWindowJson);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#include <QListWidgetItem>
|
||||
#include <QTime>
|
||||
#include <memory>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include<items/item.h>
|
||||
|
||||
|
|
@ -24,6 +25,9 @@ public:
|
|||
explicit MainWindow(MainObject * const mainObject, QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
void store(QJsonObject& json) const;
|
||||
void load(const QJsonObject& json);
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
MainObject* const mainObject;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
#include <QHeaderView>
|
||||
#include <QScroller>
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include "sensorsettingsdialog.h"
|
||||
|
||||
|
|
@ -13,8 +15,8 @@ SensorListWidget::SensorListWidget(const bool showHidden, QWidget *parent): QTre
|
|||
setColumnCount(3);
|
||||
setHeaderLabels({"Sensor", "Value", "Time"});
|
||||
setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
header()->setSectionResizeMode(0, QHeaderView::Interactive);
|
||||
header()->setSectionResizeMode(1, QHeaderView::Interactive);
|
||||
header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
|
||||
header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
|
||||
header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
|
||||
QScroller::grabGesture(this, QScroller::LeftMouseButtonGesture);
|
||||
setAutoScroll(true);
|
||||
|
|
@ -120,7 +122,12 @@ void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
|
|||
{
|
||||
groupItem = new QTreeWidgetItem(this);
|
||||
groupItem->setText(0, sensor.groupName);
|
||||
|
||||
bool wasExpanded = expandedStates.value(sensor.groupName, false);
|
||||
if(!wasExpanded && pendingGroupExpandedStates_.contains(sensor.groupName))
|
||||
{
|
||||
wasExpanded = pendingGroupExpandedStates_[sensor.groupName];
|
||||
}
|
||||
groupItem->setExpanded(wasExpanded);
|
||||
groupItems[sensor.groupName] = groupItem;
|
||||
}
|
||||
|
|
@ -162,6 +169,36 @@ void SensorListWidget::setShowHidden(const bool showHidden)
|
|||
sensorsChanged(*globalSensors.getSensors());
|
||||
}
|
||||
|
||||
void SensorListWidget::store(QJsonObject& json) const
|
||||
{
|
||||
QJsonObject sensorListJson;
|
||||
|
||||
QJsonObject groupStates;
|
||||
for(int i = 0; i < topLevelItemCount(); ++i)
|
||||
{
|
||||
QTreeWidgetItem* item = topLevelItem(i);
|
||||
if(item->type() != 1001)
|
||||
{
|
||||
groupStates[item->text(0)] = item->isExpanded();
|
||||
}
|
||||
}
|
||||
sensorListJson["GroupStates"] = groupStates;
|
||||
|
||||
json["SensorList"] = sensorListJson;
|
||||
}
|
||||
|
||||
void SensorListWidget::load(const QJsonObject& json)
|
||||
{
|
||||
QJsonObject sensorListJson = json["SensorList"].toObject();
|
||||
|
||||
QJsonObject groupStates = sensorListJson["GroupStates"].toObject();
|
||||
pendingGroupExpandedStates_.clear();
|
||||
for(auto it = groupStates.begin(); it != groupStates.end(); ++it)
|
||||
{
|
||||
pendingGroupExpandedStates_[it.key()] = it.value().toBool();
|
||||
}
|
||||
}
|
||||
|
||||
const Sensor& SensorListItem::getSensor()
|
||||
{
|
||||
return sensor;
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ class SensorListWidget : public QTreeWidget
|
|||
Q_OBJECT
|
||||
|
||||
bool showHidden_;
|
||||
QString savedSelectedGroup_;
|
||||
QMap<QString, bool> pendingGroupExpandedStates_;
|
||||
|
||||
public:
|
||||
|
||||
|
|
@ -26,6 +28,9 @@ public:
|
|||
|
||||
const Sensor& getSensorForIndex(const QModelIndex &index);
|
||||
|
||||
void store(QJsonObject& json) const;
|
||||
void load(const QJsonObject& json);
|
||||
|
||||
public slots:
|
||||
|
||||
void setShowHidden(const bool showHidden);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue