Skip to content

Commit

Permalink
Keep scroll position when reinstantiating science_report
Browse files Browse the repository at this point in the history
The `science_report` loses the scroll position when the view is
closed. This is because the object is destroyed and therefore loses
its state.

This commit adds code to track the scroll position in static fields
and restore the state when the `science_report is` reinstantiated.

Closes longturn#2329.
  • Loading branch information
blabber committed Jul 29, 2024
1 parent f952681 commit c459790
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions client/views/view_research.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <QScrollArea>
#include <QTimer>
#include <QToolTip>
#include <qabstractslider.h>
#include <qscrollbar.h>

// common
#include "game.h"
Expand Down Expand Up @@ -301,6 +303,9 @@ science_report::science_report() : QWidget()
scroll->setPalette(QPalette(QColor(215, 215, 215)));
scroll->setWidget(res_diag);
scroll->setSizePolicy(size_expanding_policy);
scroll->horizontalScrollBar()->setValue(
science_report::horizontalScrollValue);
scroll->verticalScrollBar()->setValue(science_report::verticalScrollValue);
sci_layout->addWidget(scroll, 4, 0, 1, 10);

QObject::connect(researching_combo,
Expand All @@ -314,6 +319,16 @@ science_report::science_report() : QWidget()
QOverload<int>::of(&QComboBox::currentIndexChanged), this,
&science_report::goal_tech_changed);

QScrollBar *horizontalScrollBar = scroll->horizontalScrollBar();
QObject::connect(horizontalScrollBar,
QOverload<int>::of(&QAbstractSlider::valueChanged), this,
&science_report::setHorizontalScrollValue);

QScrollBar *verticalScrollBar = scroll->verticalScrollBar();
QObject::connect(verticalScrollBar,
QOverload<int>::of(&QAbstractSlider::valueChanged), this,
&science_report::setVerticalScrollValue);

setLayout(sci_layout);
}

Expand Down Expand Up @@ -570,6 +585,24 @@ void science_report::push_research()
}
}

/**
Set the backing field for the horizontal scroll value. This is used
to restore the scroll state when the view is reinstantiated.
*/
void science_report::setHorizontalScrollValue(int value)
{
science_report::horizontalScrollValue = value;
}

/**
Set the backing field for the vertical scroll value. This is used
to restore the scroll state when the view is reinstantiated.
*/
void science_report::setVerticalScrollValue(int value)
{
science_report::verticalScrollValue = value;
}

/**
Update the science report.
*/
Expand Down
4 changes: 4 additions & 0 deletions client/views/view_research.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct qlist_item {
class science_report : public QWidget {
Q_OBJECT

static inline int horizontalScrollValue;
static inline int verticalScrollValue;
QComboBox *goal_combo;
QComboBox *researching_combo;
QPushButton *refresh_but;
Expand All @@ -97,6 +99,8 @@ private slots:
void current_tech_changed(int index);
void goal_tech_changed(int index);
void push_research();
void setHorizontalScrollValue(int value);
void setVerticalScrollValue(int value);
};

void popdown_science_report();
Expand Down

0 comments on commit c459790

Please sign in to comment.