Skip to content

Commit b8dd7c7

Browse files
authored
Merge pull request #630 from jw098/log-window
Save size, position for Main Window, Output Window on program start
2 parents dc2deb0 + 10477d2 commit b8dd7c7

File tree

8 files changed

+172
-20
lines changed

8 files changed

+172
-20
lines changed

SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,23 @@ GlobalSettings::GlobalSettings()
130130
, THEME(CONSTRUCT_TOKEN)
131131
, WINDOW_SIZE(
132132
CONSTRUCT_TOKEN,
133-
"Window Size:",
134-
"Set the size of the window. Takes effect immediately.<br>"
133+
"Window Size/Position:",
134+
"Set the size/position of the window. Takes effect immediately.<br>"
135135
"Use this to easily set the window to a specific resolution for streaming alignment.",
136-
1280, 1000
136+
1280, 1000,
137+
0, 0
138+
)
139+
, LOG_WINDOW_SIZE(
140+
CONSTRUCT_TOKEN,
141+
"Output Window Size/Position:",
142+
"Set the size/position of the output window. Takes effect immediately.<br>",
143+
600, 1200,
144+
0, 0
145+
)
146+
, LOG_WINDOW_STARTUP(
147+
"<b>Open Output Window at startup:</b>",
148+
LockMode::UNLOCK_WHILE_RUNNING,
149+
false
137150
)
138151
, STREAM_HISTORY(CONSTRUCT_TOKEN)
139152
, SLEEP_SUPPRESS(CONSTRUCT_TOKEN)
@@ -196,6 +209,8 @@ GlobalSettings::GlobalSettings()
196209
PA_ADD_OPTION(TEMP_FOLDER);
197210
PA_ADD_OPTION(THEME);
198211
PA_ADD_OPTION(WINDOW_SIZE);
212+
PA_ADD_OPTION(LOG_WINDOW_SIZE);
213+
PA_ADD_OPTION(LOG_WINDOW_STARTUP);
199214
#if (QT_VERSION_MAJOR == 6) && (QT_VERSION_MINOR >= 8)
200215
if (IS_BETA_VERSION || PreloadSettings::instance().DEVELOPER_MODE){
201216
PA_ADD_OPTION(STREAM_HISTORY);

SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class GlobalSettings : public BatchOption, private ConfigOption::Listener{
9696

9797
Pimpl<ThemeSelectorOption> THEME;
9898
Pimpl<ResolutionOption> WINDOW_SIZE;
99+
Pimpl<ResolutionOption> LOG_WINDOW_SIZE;
100+
BooleanCheckBoxOption LOG_WINDOW_STARTUP;
99101

100102
Pimpl<StreamHistoryOption> STREAM_HISTORY;
101103
Pimpl<SleepSuppressOptions> SLEEP_SUPPRESS;

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
#include <QMenuBar>
99
#include <QDir>
1010
#include "CommonFramework/Globals.h"
11+
#include "CommonFramework/GlobalSettingsPanel.h"
1112
#include "CommonFramework/Windows/DpiScaler.h"
1213
#include "CommonFramework/Windows/WindowTracker.h"
14+
#include "CommonFramework/Windows/MainWindow.h"
15+
#include "CommonFramework/Options/ResolutionOption.h"
1316
#include "FileWindowLogger.h"
1417

1518
//#include <iostream>
@@ -212,7 +215,9 @@ FileWindowLoggerWindow::FileWindowLoggerWindow(FileWindowLogger& logger, QWidget
212215
if (objectName().isEmpty()){
213216
setObjectName(QString::fromUtf8("TextWindow"));
214217
}
215-
resize(scale_dpi_width(1200), scale_dpi_height(600));
218+
uint32_t width = GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH;
219+
uint32_t height = GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT;
220+
resize(scale_dpi_width(width), scale_dpi_height(height));
216221
m_text = new QTextEdit(this);
217222
m_text->setObjectName(QString::fromUtf8("centralwidget"));
218223
setCentralWidget(m_text);
@@ -236,6 +241,11 @@ FileWindowLoggerWindow::FileWindowLoggerWindow(FileWindowLogger& logger, QWidget
236241
}
237242
);
238243

244+
GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH.add_listener(*this);
245+
GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT.add_listener(*this);
246+
GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS.add_listener(*this);
247+
GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS.add_listener(*this);
248+
239249
m_logger += *this;
240250
log("================================================================================");
241251
log("<b>Window Startup...</b>");
@@ -248,13 +258,53 @@ FileWindowLoggerWindow::FileWindowLoggerWindow(FileWindowLogger& logger, QWidget
248258
FileWindowLoggerWindow::~FileWindowLoggerWindow(){
249259
remove_window(*this);
250260
m_logger -= *this;
261+
GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH.remove_listener(*this);
262+
GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT.remove_listener(*this);
263+
GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS.remove_listener(*this);
264+
GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS.remove_listener(*this);
251265
}
252266

253267
void FileWindowLoggerWindow::log(QString msg){
254268
// cout << "FileWindowLoggerWindow::log(): " << msg.toStdString() << endl;
255269
emit signal_log(msg);
256270
}
257271

272+
void FileWindowLoggerWindow::resizeEvent(QResizeEvent* event){
273+
m_pending_resize = true;
274+
GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH.set(width());
275+
GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT.set(height());
276+
m_pending_resize = false;
277+
}
278+
279+
void FileWindowLoggerWindow::moveEvent(QMoveEvent* event){
280+
m_pending_move = true;
281+
GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS.set(x());
282+
GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS.set(y());
283+
m_pending_move = false;
284+
}
285+
286+
void FileWindowLoggerWindow::on_config_value_changed(void* object){
287+
if (object == &GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH || object == &GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT){
288+
QMetaObject::invokeMethod(this, [this]{
289+
if (!m_pending_resize){
290+
resize(
291+
GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH,
292+
GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT
293+
);
294+
}
295+
});
296+
}else if (object == &GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS || object == &GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS){
297+
QMetaObject::invokeMethod(this, [this]{
298+
if (!m_pending_move){
299+
move(
300+
move_x_within_screen_bounds(GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS),
301+
move_y_within_screen_bounds(GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS)
302+
);
303+
}
304+
});
305+
}
306+
}
307+
258308

259309

260310

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <QTextEdit>
1717
#include <QMainWindow>
1818
#include "Common/Cpp/AbstractLogger.h"
19+
#include "Common/Cpp/Options/ConfigOption.h"
1920
//#include "Common/Cpp/LifetimeSanitizer.h"
2021

2122
namespace PokemonAutomation{
@@ -72,22 +73,27 @@ class FileWindowLogger : public Logger{
7273
};
7374

7475

75-
class FileWindowLoggerWindow : public QMainWindow{
76+
class FileWindowLoggerWindow : public QMainWindow, public ConfigOption::Listener{
7677
Q_OBJECT
7778

7879
public:
7980
FileWindowLoggerWindow(FileWindowLogger& logger, QWidget* parent = nullptr);
8081
virtual ~FileWindowLoggerWindow();
8182

8283
void log(QString msg);
84+
virtual void resizeEvent(QResizeEvent* event) override;
85+
virtual void moveEvent(QMoveEvent* event) override;
8386

8487
signals:
8588
void signal_log(QString msg);
8689

8790
private:
91+
virtual void on_config_value_changed(void* object) override;
8892
FileWindowLogger& m_logger;
8993
QMenuBar* m_menubar;
9094
QTextEdit* m_text;
95+
bool m_pending_resize = false;
96+
bool m_pending_move = false;
9197
};
9298

9399

SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include "CommonFramework/GlobalSettingsPanel.h"
78
#include "CommonFramework/Windows/DpiScaler.h"
89
#include "ResolutionOption.h"
910

@@ -12,16 +13,21 @@ namespace PokemonAutomation{
1213

1314
ResolutionOption::ResolutionOption(
1415
std::string label, std::string description,
15-
int default_width, int default_height
16+
int default_width, int default_height,
17+
int initial_x_pos, int initial_y_pos
1618
)
1719
: GroupOption(std::move(label), LockMode::LOCK_WHILE_RUNNING)
1820
, DESCRIPTION(std::move(description))
1921
, WIDTH("<b>Width:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_width(default_width))
2022
, HEIGHT("<b>Height:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_height(default_height))
23+
, X_POS("<b>X-position:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_width(initial_x_pos))
24+
, Y_POS("<b>Y-position:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_height(initial_y_pos))
2125
{
2226
PA_ADD_STATIC(DESCRIPTION);
2327
PA_ADD_OPTION(WIDTH);
2428
PA_ADD_OPTION(HEIGHT);
29+
PA_ADD_OPTION(X_POS);
30+
PA_ADD_OPTION(Y_POS);
2531
}
2632

2733

SerialPrograms/Source/CommonFramework/Options/ResolutionOption.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ class ResolutionOption : public GroupOption{
1818
public:
1919
ResolutionOption(
2020
std::string label, std::string description,
21-
int default_width, int default_height
21+
int default_width, int default_height,
22+
int initial_x_pos, int initial_y_pos
2223
);
2324

2425
StaticTextOption DESCRIPTION;
2526
SimpleIntegerOption<uint32_t> WIDTH;
2627
SimpleIntegerOption<uint32_t> HEIGHT;
28+
SimpleIntegerOption<int32_t> X_POS;
29+
SimpleIntegerOption<int32_t> Y_POS;
2730
};
2831

2932

SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
#include "CommonFramework/Startup/NewVersionCheck.h"
2121
#include "CommonFramework/Options/ResolutionOption.h"
2222
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
23+
#include "CommonFramework/Windows/DpiScaler.h"
2324
#include "PanelLists.h"
2425
#include "WindowTracker.h"
2526
#include "ButtonDiagram.h"
2627
#include "MainWindow.h"
2728

2829

29-
//#include <iostream>
30-
//using std::cout;
31-
//using std::endl;
30+
#include <iostream>
31+
using std::cout;
32+
using std::endl;
3233

3334
namespace PokemonAutomation{
3435

@@ -46,6 +47,16 @@ MainWindow::MainWindow(QWidget* parent)
4647
GlobalSettings::instance().WINDOW_SIZE->WIDTH,
4748
GlobalSettings::instance().WINDOW_SIZE->HEIGHT
4849
);
50+
// move main window to desired position on startup
51+
// auto const screen_geometry = QGuiApplication::primaryScreen()->availableGeometry();
52+
// uint32_t const screen_width = (uint32_t)screen_geometry.width();
53+
// uint32_t const screen_height = (uint32_t)screen_geometry.height();
54+
int32_t x_pos_main = GlobalSettings::instance().WINDOW_SIZE->X_POS;
55+
int32_t y_pos_main = GlobalSettings::instance().WINDOW_SIZE->Y_POS;
56+
int32_t move_x_main = move_x_within_screen_bounds(x_pos_main);
57+
int32_t move_y_main = move_y_within_screen_bounds(y_pos_main);
58+
move(move_x_main, move_y_main);
59+
4960
centralwidget = new QWidget(this);
5061
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
5162
setCentralWidget(centralwidget);
@@ -203,12 +214,27 @@ MainWindow::MainWindow(QWidget* parent)
203214
buttons->addWidget(output);
204215
connect(
205216
output, &QPushButton::clicked,
206-
this, [this](bool){
217+
this, [&](bool){
207218
m_output_window->show();
208219
m_output_window->raise(); // bring the window to front on macOS
209220
m_output_window->activateWindow(); // bring the window to front on Windows
210221
}
211222
);
223+
// get snapshot of the saved initial x/y position, since activating the window seems to change the window coordinates,
224+
// which then causes initial x/y position to change, due to triggering moveEvent().
225+
int32_t x_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS;
226+
int32_t y_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS;
227+
228+
if (GlobalSettings::instance().LOG_WINDOW_STARTUP){ // show the Output Window on startup
229+
m_output_window->show();
230+
m_output_window->raise(); // bring the window to front on macOS
231+
m_output_window->activateWindow(); // bring the window to front on Windows
232+
}
233+
234+
// move the output window to desired position on startup
235+
int32_t move_x_log = move_x_within_screen_bounds(x_pos_log);
236+
int32_t move_y_log = move_y_within_screen_bounds(y_pos_log);
237+
m_output_window->move(move_x_log, move_y_log);
212238
}
213239
{
214240
QPushButton* settings = new QPushButton("Settings", support_box);
@@ -234,6 +260,8 @@ MainWindow::MainWindow(QWidget* parent)
234260

235261
GlobalSettings::instance().WINDOW_SIZE->WIDTH.add_listener(*this);
236262
GlobalSettings::instance().WINDOW_SIZE->HEIGHT.add_listener(*this);
263+
GlobalSettings::instance().WINDOW_SIZE->X_POS.add_listener(*this);
264+
GlobalSettings::instance().WINDOW_SIZE->Y_POS.add_listener(*this);
237265
SystemSleepController::instance().add_listener(*this);
238266
// cout << "Done constructing" << endl;
239267
}
@@ -242,6 +270,24 @@ MainWindow::~MainWindow(){
242270
SystemSleepController::instance().remove_listener(*this);
243271
GlobalSettings::instance().WINDOW_SIZE->WIDTH.remove_listener(*this);
244272
GlobalSettings::instance().WINDOW_SIZE->HEIGHT.remove_listener(*this);
273+
GlobalSettings::instance().WINDOW_SIZE->X_POS.remove_listener(*this);
274+
GlobalSettings::instance().WINDOW_SIZE->Y_POS.remove_listener(*this);
275+
}
276+
277+
int32_t move_x_within_screen_bounds(int32_t x_pos){
278+
auto static const screen_geometry = QGuiApplication::primaryScreen()->availableGeometry();
279+
uint32_t static const screen_width = (uint32_t)screen_geometry.width();
280+
// ensure move_x is greater than 0, but less than screen_width
281+
return scale_dpi_width(std::max(0, std::min(x_pos, (int32_t)(screen_width*0.97))));
282+
283+
}
284+
285+
int32_t move_y_within_screen_bounds(int32_t y_pos){
286+
auto static const screen_geometry = QGuiApplication::primaryScreen()->availableGeometry();
287+
uint32_t const screen_height = (uint32_t)screen_geometry.height();
288+
// ensure move_y is greater than 0, but less than screen_height
289+
return scale_dpi_width(std::max(0, std::min(y_pos, (int32_t)(screen_height*0.97))));
290+
245291
}
246292

247293

@@ -256,6 +302,13 @@ void MainWindow::resizeEvent(QResizeEvent* event){
256302
m_pending_resize = false;
257303
}
258304

305+
void MainWindow::moveEvent(QMoveEvent* event){
306+
m_pending_move = true;
307+
GlobalSettings::instance().WINDOW_SIZE->X_POS.set(x());
308+
GlobalSettings::instance().WINDOW_SIZE->Y_POS.set(y());
309+
m_pending_move = false;
310+
}
311+
259312
void MainWindow::close_panel() noexcept{
260313
// cout << "close_panel(): enter: " << m_current_panel_widget << endl;
261314
// Must destroy the widget first since it references the instance.
@@ -342,14 +395,25 @@ void MainWindow::on_idle(){
342395

343396

344397
void MainWindow::on_config_value_changed(void* object){
345-
QMetaObject::invokeMethod(this, [this]{
346-
if (!m_pending_resize){
347-
resize(
348-
GlobalSettings::instance().WINDOW_SIZE->WIDTH,
349-
GlobalSettings::instance().WINDOW_SIZE->HEIGHT
350-
);
351-
}
352-
});
398+
if (object == &GlobalSettings::instance().WINDOW_SIZE->WIDTH || object == &GlobalSettings::instance().WINDOW_SIZE->HEIGHT){
399+
QMetaObject::invokeMethod(this, [this]{
400+
if (!m_pending_resize){
401+
resize(
402+
GlobalSettings::instance().WINDOW_SIZE->WIDTH,
403+
GlobalSettings::instance().WINDOW_SIZE->HEIGHT
404+
);
405+
}
406+
});
407+
}else if (object == &GlobalSettings::instance().WINDOW_SIZE->X_POS || object == &GlobalSettings::instance().WINDOW_SIZE->Y_POS){
408+
QMetaObject::invokeMethod(this, [this]{
409+
if (!m_pending_move){
410+
move(
411+
move_x_within_screen_bounds(GlobalSettings::instance().WINDOW_SIZE->X_POS),
412+
move_y_within_screen_bounds(GlobalSettings::instance().WINDOW_SIZE->Y_POS)
413+
);
414+
}
415+
});
416+
}
353417
}
354418
void MainWindow::sleep_suppress_state_changed(SleepSuppress new_state){
355419
QMetaObject::invokeMethod(this, [=, this]{

SerialPrograms/Source/CommonFramework/Windows/MainWindow.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ class MainWindow :
3434
MainWindow(QWidget* parent = nullptr);
3535
~MainWindow();
3636

37+
3738
private:
3839
virtual void closeEvent(QCloseEvent* event) override;
3940
virtual void resizeEvent(QResizeEvent* event) override;
41+
virtual void moveEvent(QMoveEvent* event) override;
4042

4143
void close_panel() noexcept;
4244

@@ -85,10 +87,14 @@ class MainWindow :
8587
std::unique_ptr<SleepSuppressScope> m_sleep_scope;
8688

8789
bool m_pending_resize = false;
90+
bool m_pending_move = false;
8891
bool m_panel_transition = false;
8992
};
9093

91-
94+
// returns given X value, but bounded by 0 and screen_width
95+
int32_t move_x_within_screen_bounds(int32_t x_pos);
96+
// returns given y value, but bounded by 0 and screen_height
97+
int32_t move_y_within_screen_bounds(int32_t y_pos);
9298

9399

94100
}

0 commit comments

Comments
 (0)