Skip to content

Commit 04509a8

Browse files
committed
changing Config value will change position of main window
1 parent 6d2f732 commit 04509a8

File tree

5 files changed

+70
-32
lines changed

5 files changed

+70
-32
lines changed

SerialPrograms/Source/CommonFramework/Logging/FileWindowLogger.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ void FileWindowLoggerWindow::resizeEvent(QResizeEvent* event){
267267
}
268268

269269
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());
270+
GlobalSettings::instance().LOG_WINDOW_SIZE->X_POS.set(x());
271+
GlobalSettings::instance().LOG_WINDOW_SIZE->Y_POS.set(y());
272272
}
273273

274274

SerialPrograms/Source/CommonFramework/Options/ResolutionOption.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ ResolutionOption::ResolutionOption(
2020
, DESCRIPTION(std::move(description))
2121
, WIDTH("<b>Width:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_width(default_width))
2222
, HEIGHT("<b>Height:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_height(default_height))
23-
, INITIAL_X_POS("<b>Initial X position:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_width(initial_x_pos))
24-
, INITIAL_Y_POS("<b>Initial Y position:</b>", LockMode::LOCK_WHILE_RUNNING, scale_dpi_height(initial_y_pos))
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))
2525
{
2626
PA_ADD_STATIC(DESCRIPTION);
2727
PA_ADD_OPTION(WIDTH);
2828
PA_ADD_OPTION(HEIGHT);
2929
if (PreloadSettings::instance().DEVELOPER_MODE){
30-
PA_ADD_OPTION(INITIAL_X_POS);
31-
PA_ADD_OPTION(INITIAL_Y_POS);
30+
PA_ADD_OPTION(X_POS);
31+
PA_ADD_OPTION(Y_POS);
3232
}
3333
}
3434

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<int32_t> INITIAL_X_POS;
29-
SimpleIntegerOption<int32_t> INITIAL_Y_POS;
28+
SimpleIntegerOption<int32_t> X_POS;
29+
SimpleIntegerOption<int32_t> Y_POS;
3030
};
3131

3232

SerialPrograms/Source/CommonFramework/Windows/MainWindow.cpp

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ MainWindow::MainWindow(QWidget* parent)
4848
GlobalSettings::instance().WINDOW_SIZE->HEIGHT
4949
);
5050
// 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 initial_x_pos_main = GlobalSettings::instance().WINDOW_SIZE->INITIAL_X_POS;
55-
int32_t initial_y_pos_main = GlobalSettings::instance().WINDOW_SIZE->INITIAL_Y_POS;
56-
int32_t move_x_main = std::max(0, std::min(initial_x_pos_main, (int32_t)(screen_width*0.97)));
57-
int32_t move_y_main = std::max(0, std::min(initial_y_pos_main, (int32_t)(screen_height*0.97)));
58-
move(scale_dpi_width(move_x_main), scale_dpi_height(move_y_main));
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);
5959

6060
centralwidget = new QWidget(this);
6161
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
@@ -222,8 +222,8 @@ MainWindow::MainWindow(QWidget* parent)
222222
);
223223
// get snapshot of the saved initial x/y position, since activating the window seems to change the window coordinates,
224224
// which then causes initial x/y position to change, due to triggering moveEvent().
225-
int32_t initial_x_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_X_POS;
226-
int32_t initial_y_pos_log = GlobalSettings::instance().LOG_WINDOW_SIZE->INITIAL_Y_POS;
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;
227227

228228
if (GlobalSettings::instance().LOG_WINDOW_STARTUP){ // show the Output Window on startup
229229
m_output_window->show();
@@ -232,9 +232,9 @@ MainWindow::MainWindow(QWidget* parent)
232232
}
233233

234234
// move the output window to desired position on startup
235-
int32_t move_x_log = std::max(0, std::min(initial_x_pos_log, (int32_t)(screen_width*0.97))); // ensure move_x is greater than 0, but less than screen_width
236-
int32_t move_y_log = std::max(0, std::min(initial_y_pos_log, (int32_t)(screen_height*0.97)));
237-
m_output_window->move(scale_dpi_width(move_x_log), scale_dpi_height(move_y_log));
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);
238238
}
239239
{
240240
QPushButton* settings = new QPushButton("Settings", support_box);
@@ -260,6 +260,8 @@ MainWindow::MainWindow(QWidget* parent)
260260

261261
GlobalSettings::instance().WINDOW_SIZE->WIDTH.add_listener(*this);
262262
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);
263265
SystemSleepController::instance().add_listener(*this);
264266
// cout << "Done constructing" << endl;
265267
}
@@ -268,6 +270,24 @@ MainWindow::~MainWindow(){
268270
SystemSleepController::instance().remove_listener(*this);
269271
GlobalSettings::instance().WINDOW_SIZE->WIDTH.remove_listener(*this);
270272
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+
271291
}
272292

273293

@@ -283,8 +303,10 @@ void MainWindow::resizeEvent(QResizeEvent* event){
283303
}
284304

285305
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());
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;
288310
}
289311

290312
void MainWindow::close_panel() noexcept{
@@ -373,14 +395,25 @@ void MainWindow::on_idle(){
373395

374396

375397
void MainWindow::on_config_value_changed(void* object){
376-
QMetaObject::invokeMethod(this, [this]{
377-
if (!m_pending_resize){
378-
resize(
379-
GlobalSettings::instance().WINDOW_SIZE->WIDTH,
380-
GlobalSettings::instance().WINDOW_SIZE->HEIGHT
381-
);
382-
}
383-
});
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+
}
384417
}
385418
void MainWindow::sleep_suppress_state_changed(SleepSuppress new_state){
386419
QMetaObject::invokeMethod(this, [=, this]{

SerialPrograms/Source/CommonFramework/Windows/MainWindow.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ 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;
@@ -86,10 +87,14 @@ class MainWindow :
8687
std::unique_ptr<SleepSuppressScope> m_sleep_scope;
8788

8889
bool m_pending_resize = false;
90+
bool m_pending_move = false;
8991
bool m_panel_transition = false;
9092
};
9193

92-
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);
9398

9499

95100
}

0 commit comments

Comments
 (0)