Skip to content

Commit

Permalink
remove fixed array from setting and change structure to qvector
Browse files Browse the repository at this point in the history
  • Loading branch information
softengineer authored Feb 12, 2022
1 parent 08ba7b8 commit 1ac91a3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
46 changes: 35 additions & 11 deletions setting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void Setting::closeEvent(QCloseEvent *event)
this->setTime("rest", restTimeSpinBox->value());
this->setTime("seatedwork", seatedWorkTimeSpinBox->value());
this->setTime("standwork", standWorkTimeSpinBox->value());
qDebug() << "save setting :" << getTime("standwork") << "|" << standWorkTimeSpinBox->value() ;
saveJsonSetting();
hide();
event->ignore();
Expand Down Expand Up @@ -210,14 +211,35 @@ void Setting::createIconGroupBox()
iconGroupBox->setLayout(iconLayout);
}

ConfigItem & ConfigItem::operator = ( ConfigItem & item) {
ConfigItem & ConfigItem::operator = ( const ConfigItem & item) {
this->key = item.key;
this->value = item.value;
this->category = item.category;

return *this;
}



ConfigItem::ConfigItem ( const ConfigItem & item) {
this->key = item.key;
this->value = item.value;
this->category = item.category;

}

ConfigItem::ConfigItem ( ConfigItem & item) {
this->key = item.key;
this->value = item.value;
this->category = item.category;

}

ConfigItem::ConfigItem ( ) {


}

void Setting::loadJsonSetting() {
qDebug() << CONFIG_PATH;
QFile inFile(CONFIG_PATH);
Expand All @@ -244,10 +266,10 @@ void Setting::loadJsonSetting() {
item.key = obj.value("key").toString();
item.value = obj.value("value").toInt();
item.category = obj.value("category").toString();
all[i] = item;
this->configItems.push_back(item);
}

this->ConfigItems = all;

}

bool Setting::saveJsonSetting() {
Expand All @@ -260,6 +282,7 @@ bool Setting::saveJsonSetting() {
QJsonObject rootObject = JsonDocument.object();
QJsonArray array;
QJsonObject qrest;

qrest.insert("key", "rest");
qrest.insert("value", getTime("rest"));
qrest.insert("category", "rest");
Expand Down Expand Up @@ -288,26 +311,27 @@ bool Setting::saveJsonSetting() {
}

int Setting::getTime( QString name) {
int length = 3;//sizeof( this->ConfigItems ) / sizeof( ConfigItem );
int length = this->configItems.size();//sizeof( this->ConfigItems ) / sizeof( ConfigItem );

for (int j = 0; j< length; j++) {


if (this->ConfigItems [ j].key == name) {
return this->ConfigItems [ j].value;
if (this->configItems [ j].key == name) {
return this->configItems [ j].value;
}
}

return -1;
}

void Setting::setTime( QString name, int value) {
int length = sizeof( ConfigItem ) / sizeof( ConfigItem );
int length = this->configItems.size();

for (int j = 0; j< length; j++) {
ConfigItem * p = this->ConfigItems + j;
if (p->key == name) {
p->value = value;
ConfigItem & p = this->configItems[ j];
if (p.key == name) {

p.value = value;
}
}

Expand Down Expand Up @@ -403,7 +427,7 @@ void Setting::createTimeSettingGroupBox() {
standWorkTimeSpinBox = new QSpinBox;
standWorkTimeSpinBox->setRange(5, 60);
standWorkTimeSpinBox->setSuffix(" m");
standWorkTimeSpinBox->setValue(1);
standWorkTimeSpinBox->setValue(this->getTime("standwork"));

QGridLayout *messageLayout = new QGridLayout;

Expand Down
8 changes: 6 additions & 2 deletions setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#include <QDialog>
#include <QDir>
#include <QVector>
#include <timesetting.h>

QT_BEGIN_NAMESPACE
Expand Down Expand Up @@ -84,11 +85,14 @@ const QString CONFIG_PATH = QDir::currentPath() + "/setting.json";
class ConfigItem : public QObject{
Q_OBJECT
public:
ConfigItem(const ConfigItem&);
ConfigItem(ConfigItem &);
ConfigItem();
QString key;
int value;
QString category;

ConfigItem & operator = ( ConfigItem &);
ConfigItem & operator = ( const ConfigItem &);
};


Expand Down Expand Up @@ -127,7 +131,7 @@ private slots:
void createTrayIcon();


ConfigItem * ConfigItems;
QVector <ConfigItem> configItems;
QGroupBox *iconGroupBox;
QLabel *iconLabel;
QComboBox *iconComboBox;
Expand Down

0 comments on commit 1ac91a3

Please sign in to comment.