Skip to content

Commit 08d1dc5

Browse files
committed
Save X/Y position of window, so that it will re-open at that position. Fix bug with re-opening the Output window.
1 parent 1fabc4c commit 08d1dc5

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,16 @@ void FileWindowLoggerWindow::log(QString msg){
260260
}
261261

262262
void FileWindowLoggerWindow::resizeEvent(QResizeEvent* event){
263-
m_pending_resize = true;
263+
// m_pending_resize = true;
264264
GlobalSettings::instance().LOG_WINDOW_SIZE->WIDTH.set(width());
265265
GlobalSettings::instance().LOG_WINDOW_SIZE->HEIGHT.set(height());
266-
m_pending_resize = false;
266+
// m_pending_resize = false;
267267
}
268268

269+
void FileWindowLoggerWindow::moveEvent(QMoveEvent* event){
270+
GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_X_POS.set(x());
271+
GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_Y_POS.set(y());
272+
}
269273

270274

271275

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class FileWindowLoggerWindow : public QMainWindow{
8181

8282
void log(QString msg);
8383
virtual void resizeEvent(QResizeEvent* event) override;
84+
virtual void moveEvent(QMoveEvent* event) override;
8485

8586
signals:
8687
void signal_log(QString msg);
@@ -89,7 +90,7 @@ class FileWindowLoggerWindow : public QMainWindow{
8990
FileWindowLogger& m_logger;
9091
QMenuBar* m_menubar;
9192
QTextEdit* m_text;
92-
bool m_pending_resize = false;
93+
// bool m_pending_resize = false;
9394
};
9495

9596

SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ResolutionOption::ResolutionOption(
2626
PA_ADD_STATIC(DESCRIPTION);
2727
PA_ADD_OPTION(WIDTH);
2828
PA_ADD_OPTION(HEIGHT);
29-
if (PreloadSettings::instance().DEVELOPER_MODE){
29+
if (PreloadSettings::instance().DEVELOPER_MODE){
3030
PA_ADD_OPTION(INITIAL_X_POS);
3131
PA_ADD_OPTION(INITIAL_Y_POS);
3232
}

SerialPrograms/Source/CommonFramework/Options/ResolutionOption.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class ResolutionOption : public GroupOption{
2525
StaticTextOption DESCRIPTION;
2626
SimpleIntegerOption<uint32_t> WIDTH;
2727
SimpleIntegerOption<uint32_t> HEIGHT;
28-
SimpleIntegerOption<uint32_t> INITIAL_X_POS;
29-
SimpleIntegerOption<uint32_t> INITIAL_Y_POS;
28+
SimpleIntegerOption<int32_t> INITIAL_X_POS;
29+
SimpleIntegerOption<int32_t> INITIAL_Y_POS;
3030
};
3131

3232

SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include "MainWindow.h"
2828

2929

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

3434
namespace PokemonAutomation{
3535

@@ -212,25 +212,26 @@ MainWindow::MainWindow(QWidget* parent)
212212
m_output_window.reset(new FileWindowLoggerWindow((FileWindowLogger&)global_logger_raw()));
213213
QPushButton* output = new QPushButton("Output Window", support_box);
214214
buttons->addWidget(output);
215-
auto show_output_window = [&](){
216-
m_output_window->show();
217-
m_output_window->raise(); // bring the window to front on macOS
218-
m_output_window->activateWindow(); // bring the window to front on Windows
219-
};
220215
connect(
221216
output, &QPushButton::clicked,
222217
this, [&](bool){
223-
show_output_window();
218+
m_output_window->show();
219+
m_output_window->raise(); // bring the window to front on macOS
220+
m_output_window->activateWindow(); // bring the window to front on Windows
224221
}
225222
);
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+
uint32_t initial_x_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_X_POS;
226+
uint32_t initial_y_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_Y_POS;
226227

227228
if (GlobalSettings::instance().LOG_WINDOW_STARTUP){ // show the Output Window on startup
228-
show_output_window();
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
229232
}
230233

231234
// move the output window to desired position on startup
232-
uint32_t initial_x_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_X_POS;
233-
uint32_t initial_y_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_Y_POS;
234235
uint32_t move_x_log = std::min(initial_x_pos_log, screen_width-100);
235236
uint32_t move_y_log = std::min(initial_y_pos_log, screen_height-100);
236237
m_output_window->move(scale_dpi_width(move_x_log), scale_dpi_height(move_y_log));
@@ -281,6 +282,11 @@ void MainWindow::resizeEvent(QResizeEvent* event){
281282
m_pending_resize = false;
282283
}
283284

285+
void MainWindow::moveEvent(QMoveEvent* event){
286+
GlobalSettings::instance().WINDOW_SIZE->INITIAL_X_POS.set(x());
287+
GlobalSettings::instance().WINDOW_SIZE->INITIAL_Y_POS.set(y());
288+
}
289+
284290
void MainWindow::close_panel() noexcept{
285291
// cout << "close_panel(): enter: " << m_current_panel_widget << endl;
286292
// Must destroy the widget first since it references the instance.

SerialPrograms/Source/CommonFramework/Windows/MainWindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class MainWindow :
3737
private:
3838
virtual void closeEvent(QCloseEvent* event) override;
3939
virtual void resizeEvent(QResizeEvent* event) override;
40+
virtual void moveEvent(QMoveEvent* event) override;
4041

4142
void close_panel() noexcept;
4243

0 commit comments

Comments
 (0)