aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-01-30 09:33:16 +0000
committersaturneric <[email protected]>2024-01-30 09:33:16 +0000
commit2554604b93a8a5949473071bef145f4ec7cf8fce (patch)
treeb498311d793156a22a3466ae6f3c58ab2a959afc
parentdoc: update translations (diff)
downloadGpgFrontend-2554604b93a8a5949473071bef145f4ec7cf8fce.tar.gz
GpgFrontend-2554604b93a8a5949473071bef145f4ec7cf8fce.zip
fix: add general main window rect state fallback logic
-rw-r--r--src/ui/dialog/GeneralDialog.h2
-rw-r--r--src/ui/dialog/import_export/KeyImportDetailDialog.cpp2
-rw-r--r--src/ui/dialog/import_export/KeyImportDetailDialog.h2
-rw-r--r--src/ui/main_window/GeneralMainWindow.cpp88
-rw-r--r--src/ui/main_window/GeneralMainWindow.h20
5 files changed, 108 insertions, 6 deletions
diff --git a/src/ui/dialog/GeneralDialog.h b/src/ui/dialog/GeneralDialog.h
index 604c8475..3576319f 100644
--- a/src/ui/dialog/GeneralDialog.h
+++ b/src/ui/dialog/GeneralDialog.h
@@ -61,7 +61,7 @@ class GeneralDialog : public QDialog {
* @brief
*
*/
- [[nodiscard]] bool isRectRestored();
+ [[nodiscard]] auto isRectRestored() -> bool;
/**
* @brief
diff --git a/src/ui/dialog/import_export/KeyImportDetailDialog.cpp b/src/ui/dialog/import_export/KeyImportDetailDialog.cpp
index 5b71c0f6..dfd6c35a 100644
--- a/src/ui/dialog/import_export/KeyImportDetailDialog.cpp
+++ b/src/ui/dialog/import_export/KeyImportDetailDialog.cpp
@@ -159,7 +159,7 @@ void KeyImportDetailDialog::create_keys_table() {
keys_table_->resizeColumnsToContents();
}
-QString KeyImportDetailDialog::get_status_string(int key_status) {
+auto KeyImportDetailDialog::get_status_string(int key_status) -> QString {
QString status_string;
// keystatus is greater than 15, if key is private
if (key_status > 15) {
diff --git a/src/ui/dialog/import_export/KeyImportDetailDialog.h b/src/ui/dialog/import_export/KeyImportDetailDialog.h
index db355570..2af0397d 100644
--- a/src/ui/dialog/import_export/KeyImportDetailDialog.h
+++ b/src/ui/dialog/import_export/KeyImportDetailDialog.h
@@ -80,7 +80,7 @@ class KeyImportDetailDialog : public GeneralDialog {
* @param keyStatus
* @return QString
*/
- static QString get_status_string(int keyStatus);
+ static auto get_status_string(int) -> QString;
QTableWidget* keys_table_{}; ///<
QGroupBox* general_info_box_{}; ///<
diff --git a/src/ui/main_window/GeneralMainWindow.cpp b/src/ui/main_window/GeneralMainWindow.cpp
index e3577199..f58326f9 100644
--- a/src/ui/main_window/GeneralMainWindow.cpp
+++ b/src/ui/main_window/GeneralMainWindow.cpp
@@ -59,9 +59,11 @@ void GpgFrontend::UI::GeneralMainWindow::slot_restore_settings() noexcept {
GF_UI_LOG_DEBUG("restore main window state: {}",
window_state.window_state_data);
- // state sets pos & size of dock-widgets
- this->restoreState(
- QByteArray::fromBase64(window_state.window_state_data.toUtf8()));
+ if (!window_state.window_state_data.isEmpty()) {
+ // state sets pos & size of dock-widgets
+ this->restoreState(
+ QByteArray::fromBase64(window_state.window_state_data.toUtf8()));
+ }
// restore window size & location
if (window_state.window_save) {
@@ -107,6 +109,8 @@ void GpgFrontend::UI::GeneralMainWindow::slot_restore_settings() noexcept {
this->move(pos_);
this->resize(size_);
+ } else {
+ movePosition2CenterOfParent();
}
// appearance
@@ -148,4 +152,82 @@ void GpgFrontend::UI::GeneralMainWindow::slot_save_settings() noexcept {
}
}
+void GeneralMainWindow::setPosCenterOfScreen() {
+ // update cache
+ update_rect_cache();
+
+ int screen_width = screen_rect_.width();
+ int screen_height = screen_rect_.height();
+ GF_UI_LOG_DEBUG("dialog current screen available geometry", screen_width,
+ screen_height);
+
+ // update rect of current dialog
+ rect_ = this->geometry();
+ this->move(screen_rect_.center() -
+ QPoint(rect_.width() / 2, rect_.height() / 2));
+}
+
+void GeneralMainWindow::movePosition2CenterOfParent() {
+ // update cache
+ update_rect_cache();
+
+ // log for debug
+ GF_UI_LOG_DEBUG("parent pos x: {} y: {}", parent_rect_.x(), parent_rect_.y());
+ GF_UI_LOG_DEBUG("parent size width: {}, height: {}", parent_rect_.width(),
+ parent_rect_.height());
+ GF_UI_LOG_DEBUG("parent center pos x: {}, y: {}", parent_rect_.center().x(),
+ parent_rect_.center().y());
+ GF_UI_LOG_DEBUG("dialog pos x: {} y: {}", rect_.x(), rect_.y());
+ GF_UI_LOG_DEBUG("dialog size width: {} height: {}", rect_.width(),
+ rect_.height());
+
+ if (parent_rect_.topLeft() != QPoint{0, 0} &&
+ parent_rect_.size() != QSize{0, 0}) {
+ if (rect_.width() <= 0) rect_.setWidth(100);
+ if (rect_.height() <= 0) rect_.setHeight(100);
+
+ QPoint target_position =
+ parent_rect_.center() - QPoint(rect_.width() / 2, rect_.height() / 2);
+
+ GF_UI_LOG_DEBUG(
+ "update position to parent's center, target pos, x:{}, y: {}",
+ target_position.x(), target_position.y());
+
+ this->move(target_position);
+ } else {
+ setPosCenterOfScreen();
+ }
+}
+
+void GeneralMainWindow::update_rect_cache() {
+ // update size of current dialog
+ rect_ = this->geometry();
+
+ auto *screen = this->window()->screen();
+ screen_rect_ = screen->availableGeometry();
+
+ // read pos and size from parent
+ if (this->parent() != nullptr) {
+ QRect parent_rect;
+
+ auto *parent_widget = qobject_cast<QWidget *>(this->parent());
+ if (parent_widget != nullptr) {
+ parent_rect = parent_widget->geometry();
+ } else {
+ auto *parent_dialog = qobject_cast<QDialog *>(this->parent());
+ if (parent_dialog != nullptr) {
+ parent_rect = parent_dialog->geometry();
+ } else {
+ auto *parent_window = qobject_cast<QMainWindow *>(this->parent());
+ if (parent_window != nullptr) {
+ parent_rect = parent_window->geometry();
+ }
+ }
+ }
+ parent_rect_ = parent_rect;
+ } else {
+ // reset parent's pos and size
+ this->parent_rect_ = QRect{0, 0, 0, 0};
+ }
+}
} // namespace GpgFrontend::UI \ No newline at end of file
diff --git a/src/ui/main_window/GeneralMainWindow.h b/src/ui/main_window/GeneralMainWindow.h
index e1ff31bb..70dad5f3 100644
--- a/src/ui/main_window/GeneralMainWindow.h
+++ b/src/ui/main_window/GeneralMainWindow.h
@@ -55,6 +55,17 @@ class GeneralMainWindow : public QMainWindow {
*/
void closeEvent(QCloseEvent* event) override;
+ /**
+ *
+ */
+ void setPosCenterOfScreen();
+
+ /**
+ * @brief
+ *
+ */
+ void movePosition2CenterOfParent();
+
QSize icon_size_{}; ///<
int font_size_{}; ///<
Qt::ToolButtonStyle icon_style_; ///<
@@ -70,9 +81,18 @@ class GeneralMainWindow : public QMainWindow {
*/
void slot_save_settings() noexcept;
+ /**
+ * @brief
+ *
+ */
+ void update_rect_cache();
+
private:
QString name_; ///<
QPoint pos_; ///<
QSize size_; ///<
+ QRect rect_;
+ QRect screen_rect_;
+ QRect parent_rect_;
};
} // namespace GpgFrontend::UI