Skip to content

Commit 8eca0e2

Browse files
committed
Make StatsBar editable
1 parent d9e8b34 commit 8eca0e2

File tree

11 files changed

+328
-158
lines changed

11 files changed

+328
-158
lines changed

SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.cpp

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
*
55
*/
66

7-
#include <QMessageBox>
8-
#include <QVBoxLayout>
9-
#include <QLabel>
10-
#include <QPushButton>
7+
#include <QMessageBox>
8+
#include <QHBoxLayout>
9+
#include <QVBoxLayout>
10+
#include <QLabel>
11+
#include <QPushButton>
12+
#include <QLineEdit>
1113
#include "CommonFramework/Globals.h"
1214
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
1315
#include "PanelElements.h"
@@ -97,44 +99,84 @@ CollapsibleGroupBox* make_panel_header(
9799

98100

99101

100-
StatsBar::StatsBar(QWidget& parent)
101-
: QLabel(&parent)
102-
{
103-
this->setWordWrap(true);
104-
this->setVisible(false);
105-
this->setAlignment(Qt::AlignCenter);
106-
// this->setText("<b>Encounters: 1,267 - Corrections: 0 - Star Shinies: 1 - Square Shinies: 0</b>");
107-
QFont font = this->font();
108-
font.setPointSize(10);
109-
this->setFont(font);
110-
}
111-
void StatsBar::set_stats(std::string current_stats, std::string historical_stats){
112-
if (current_stats.empty() && historical_stats.empty()){
113-
this->setText("");
114-
this->setVisible(false);
115-
return;
116-
}
117-
118-
if (!current_stats.empty() && historical_stats.empty()){
119-
this->setText(QString::fromStdString(current_stats));
120-
this->setVisible(true);
121-
return;
122-
}
123-
124-
if (current_stats.empty() && !historical_stats.empty()){
125-
this->setText(QString::fromStdString("<b>Past Runs</b> - " + historical_stats));
126-
this->setVisible(true);
127-
return;
128-
}
129-
130-
std::string str;
131-
str += "<b>Current Run</b> - " + current_stats;
132-
str += "<br>";
133-
str += "<b>Past Totals</b> - " + historical_stats;
134-
135-
this->setText(QString::fromStdString(str));
136-
this->setVisible(true);
137-
}
102+
StatsBar::StatsBar(QWidget& parent)
103+
: QWidget(&parent)
104+
{
105+
QVBoxLayout* layout = new QVBoxLayout(this);
106+
layout->setContentsMargins(0, 0, 0, 0);
107+
layout->setSpacing(4);
108+
109+
QFont font = this->font();
110+
font.setPointSize(10);
111+
112+
{
113+
QHBoxLayout* row = new QHBoxLayout();
114+
row->setContentsMargins(0, 0, 0, 0);
115+
row->setSpacing(6);
116+
117+
m_current_label = new QLabel("Current Run:", this);
118+
m_current_label->setFont(font);
119+
row->addWidget(m_current_label);
120+
121+
m_current_stats = new QLineEdit(this);
122+
m_current_stats->setFont(font);
123+
row->addWidget(m_current_stats, 1);
124+
125+
layout->addLayout(row);
126+
}
127+
128+
{
129+
QHBoxLayout* row = new QHBoxLayout();
130+
row->setContentsMargins(0, 0, 0, 0);
131+
row->setSpacing(6);
132+
133+
m_historical_label = new QLabel("Past Totals:", this);
134+
m_historical_label->setFont(font);
135+
row->addWidget(m_historical_label);
136+
137+
m_historical_stats = new QLineEdit(this);
138+
m_historical_stats->setFont(font);
139+
row->addWidget(m_historical_stats, 1);
140+
141+
layout->addLayout(row);
142+
}
143+
144+
m_apply_button = new QPushButton("Apply Stats", this);
145+
m_apply_button->setFont(font);
146+
layout->addWidget(m_apply_button, 0, Qt::AlignRight);
147+
148+
connect(
149+
m_current_stats, &QLineEdit::textEdited,
150+
this, [this](const QString&){
151+
m_user_editing = true;
152+
}
153+
);
154+
connect(
155+
m_historical_stats, &QLineEdit::textEdited,
156+
this, [this](const QString&){
157+
m_user_editing = true;
158+
}
159+
);
160+
connect(
161+
m_apply_button, &QPushButton::clicked,
162+
this, [this](bool){
163+
m_user_editing = false;
164+
emit stats_edited(
165+
m_current_stats->text().toStdString(),
166+
m_historical_stats->text().toStdString()
167+
);
168+
}
169+
);
170+
171+
this->setVisible(false);
172+
}
173+
void StatsBar::set_stats(std::string current_stats, std::string historical_stats){
174+
if (!m_user_editing){
175+
m_current_stats->setText(QString::fromStdString(current_stats));
176+
m_historical_stats->setText(QString::fromStdString(historical_stats));
177+
}
178+
this->setVisible(!current_stats.empty() || !historical_stats.empty() || m_user_editing);
179+
}
138180

139181

140182

SerialPrograms/Source/CommonFramework/Panels/UI/PanelElements.h

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
*/
66

77
#ifndef PokemonAutomation_PanelElements_H
8-
#define PokemonAutomation_PanelElements_H
9-
10-
#include <string>
11-
#include <QLabel>
12-
#include "Common/Qt/CollapsibleGroupBox.h"
13-
#include "CommonFramework/Globals.h"
14-
#include "CommonFramework/Panels/ProgramDescriptor.h"
15-
//#include "Controllers/ControllerCapability.h"
16-
17-
class QPushButton;
18-
19-
namespace PokemonAutomation{
8+
#define PokemonAutomation_PanelElements_H
9+
10+
#include <string>
11+
#include <QWidget>
12+
#include <QLabel>
13+
#include "Common/Qt/CollapsibleGroupBox.h"
14+
#include "CommonFramework/Globals.h"
15+
#include "CommonFramework/Panels/ProgramDescriptor.h"
16+
//#include "Controllers/ControllerCapability.h"
17+
18+
class QPushButton;
19+
class QLineEdit;
20+
21+
namespace PokemonAutomation{
2022

2123

2224

@@ -37,14 +39,25 @@ CollapsibleGroupBox* make_panel_header(
3739

3840

3941

40-
class StatsBar : public QLabel{
41-
Q_OBJECT
42-
public:
43-
StatsBar(QWidget& parent);
44-
45-
public slots:
46-
void set_stats(std::string current_stats, std::string historical_stats);
47-
};
42+
class StatsBar : public QWidget{
43+
Q_OBJECT
44+
public:
45+
StatsBar(QWidget& parent);
46+
47+
public slots:
48+
void set_stats(std::string current_stats, std::string historical_stats);
49+
50+
signals:
51+
void stats_edited(std::string current_stats, std::string historical_stats);
52+
53+
private:
54+
bool m_user_editing = false;
55+
QLabel* m_current_label = nullptr;
56+
QLineEdit* m_current_stats = nullptr;
57+
QLabel* m_historical_label = nullptr;
58+
QLineEdit* m_historical_stats = nullptr;
59+
QPushButton* m_apply_button = nullptr;
60+
};
4861

4962

5063

SerialPrograms/Source/CommonFramework/ProgramSession.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,34 @@ void ProgramSession::report_stats_changed(){
7575
std::lock_guard<Mutex> lg(m_lock);
7676
push_stats();
7777
}
78-
void ProgramSession::report_error(const std::string& message){
79-
std::lock_guard<Mutex> lg(m_lock);
80-
push_error(message);
81-
}
78+
void ProgramSession::report_error(const std::string& message){
79+
std::lock_guard<Mutex> lg(m_lock);
80+
push_error(message);
81+
}
82+
void ProgramSession::overwrite_stats(const std::string& current_stats, const std::string& historical_stats){
83+
std::lock_guard<Mutex> lg(m_lock);
84+
85+
if (m_current_stats){
86+
m_current_stats->replace_from_line(current_stats);
87+
}
88+
if (m_historical_stats){
89+
m_historical_stats->replace_from_line(historical_stats);
90+
m_logger.log("Saving edited historical stats...");
91+
bool ok = StatSet::replace_file(
92+
GlobalSettings::instance().STATS_FILE,
93+
m_descriptor.identifier(),
94+
*m_historical_stats
95+
);
96+
if (ok){
97+
m_logger.log("Edited stats successfully saved!", COLOR_BLUE);
98+
}else{
99+
m_logger.log("Unable to save edited stats.", COLOR_RED);
100+
push_error("Unable to save edited stats.");
101+
}
102+
}
103+
104+
push_stats();
105+
}
82106

83107

84108
void ProgramSession::set_state(ProgramState state){

SerialPrograms/Source/CommonFramework/ProgramSession.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ class ProgramSession : public TrackableProgram{
9797

9898
// virtual void restore_defaults(){ return; }
9999

100-
public:
101-
void report_stats_changed();
102-
void report_error(const std::string& message);
100+
public:
101+
void report_stats_changed();
102+
void report_error(const std::string& message);
103+
void overwrite_stats(const std::string& current_stats, const std::string& historical_stats);
103104

104105

105106
protected:

SerialPrograms/Source/CommonFramework/ProgramStats/StatsDatabase.cpp

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const std::map<std::string, std::string> STATS_DATABASE_ALIASES{
4141

4242

4343

44-
StatLine::StatLine(StatsTracker& tracker)
44+
StatLine::StatLine(const StatsTracker& tracker)
4545
: m_time(current_time_to_str())
4646
, m_stats(tracker.to_str(StatsTracker::SAVE_TO_STATS_FILE))
4747
{}
@@ -80,12 +80,19 @@ std::string StatLine::to_str() const{
8080

8181

8282

83-
void StatList::operator+=(StatsTracker& tracker){
84-
m_list.emplace_back(tracker);
85-
}
86-
void StatList::operator+=(const std::string& line){
87-
m_list.emplace_back(line);
88-
}
83+
void StatList::operator+=(const StatsTracker& tracker){
84+
m_list.emplace_back(tracker);
85+
}
86+
void StatList::operator+=(const std::string& line){
87+
m_list.emplace_back(line);
88+
}
89+
void StatList::replace_with(const StatsTracker& tracker){
90+
m_list.clear();
91+
std::string stats = tracker.to_str(StatsTracker::SAVE_TO_STATS_FILE);
92+
if (!stats.empty()){
93+
m_list.emplace_back(tracker);
94+
}
95+
}
8996
std::string StatList::to_str() const{
9097
std::string str;
9198
for (const StatLine& line : m_list){
@@ -150,11 +157,11 @@ void StatSet::open_from_file(const std::string& filepath){
150157
load_from_string(str.c_str());
151158
}
152159

153-
bool StatSet::update_file(
154-
const std::string& filepath,
155-
const std::string& identifier,
156-
StatsTracker& tracker
157-
){
160+
bool StatSet::update_file(
161+
const std::string& filepath,
162+
const std::string& identifier,
163+
const StatsTracker& tracker
164+
){
158165
QFile file(QString::fromStdString(filepath));
159166
while (!file.open(QIODevice::ReadWrite)){
160167
return false;
@@ -169,10 +176,33 @@ bool StatSet::update_file(
169176
data = set.to_str();
170177
file.seek(0);
171178
file.write(data.c_str(), data.size());
172-
file.resize(data.size());
173-
174-
return true;
175-
}
179+
file.resize(data.size());
180+
181+
return true;
182+
}
183+
bool StatSet::replace_file(
184+
const std::string& filepath,
185+
const std::string& identifier,
186+
const StatsTracker& tracker
187+
){
188+
QFile file(QString::fromStdString(filepath));
189+
while (!file.open(QIODevice::ReadWrite)){
190+
return false;
191+
}
192+
std::string data = file.readAll().data();
193+
194+
StatSet set;
195+
set.load_from_string(data.c_str());
196+
197+
set[identifier].replace_with(tracker);
198+
199+
data = set.to_str();
200+
file.seek(0);
201+
file.write(data.c_str(), data.size());
202+
file.resize(data.size());
203+
204+
return true;
205+
}
176206

177207

178208
bool StatSet::get_line(std::string& line, const char*& ptr){

0 commit comments

Comments
 (0)