Skip to content

Commit

Permalink
fix: improve some ui logics
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed Feb 19, 2023
1 parent fa2d078 commit 4de9027
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
62 changes: 35 additions & 27 deletions src/ui/dialog/GeneralDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

#include "GeneralDialog.h"

#include <exception>

#include "spdlog/spdlog.h"
#include "ui/struct/SettingsObject.h"

GpgFrontend::UI::GeneralDialog::GeneralDialog(std::string name, QWidget *parent)
Expand All @@ -53,7 +56,7 @@ void GpgFrontend::UI::GeneralDialog::slot_restore_settings() noexcept {
int width =
general_windows_state.Check("window_size").Check("width", 400),
height =
general_windows_state.Check("window_size").Check("height", 247);
general_windows_state.Check("window_size").Check("height", 300);

size_ = {width, height};

Expand Down Expand Up @@ -86,21 +89,16 @@ void GpgFrontend::UI::GeneralDialog::slot_restore_settings() noexcept {

SPDLOG_DEBUG("this dialog size width: {} height: {}", size_.width(),
size_.height());
}

if (parent_pos != QPoint{0, 0}) {
QPoint parent_center{parent_pos.x() + parent_size.width() / 2,
parent_pos.y() + parent_size.height() / 2};

pos_ = {parent_center.x() - size_.width() / 2,
parent_center.y() - size_.height() / 2};

// record parent_pos_
this->parent_pos_ = parent_pos;
this->parent_size_ = parent_size;
}
// By default move to center of parent
// Or, move to center of screen
if (parent() != nullptr) {
movePos2CenterOfParent();
} else {
movePos2CenterOfScreen();
}

this->move(pos_);
this->resize(size_);
}

Expand Down Expand Up @@ -129,7 +127,7 @@ void GpgFrontend::UI::GeneralDialog::slot_save_settings() noexcept {
}
}

void GpgFrontend::UI::GeneralDialog::setPosCenterOfScreen() {
void GpgFrontend::UI::GeneralDialog::movePos2CenterOfScreen() {
auto *screen = QGuiApplication::primaryScreen();
QRect geo = screen->availableGeometry();
int screen_width = geo.width();
Expand All @@ -147,22 +145,32 @@ void GpgFrontend::UI::GeneralDialog::setPosCenterOfScreen() {
* @brief
*
*/
void GpgFrontend::UI::GeneralDialog::movePosition2CenterOfParent() {
SPDLOG_DEBUG("parent pos x: {} y: {}", parent_pos_.x(), parent_pos_.y());
void GpgFrontend::UI::GeneralDialog::movePos2CenterOfParent() {
try {
SPDLOG_DEBUG("parent pos x: {} y: {}", parent_pos_.x(), parent_pos_.y());

SPDLOG_DEBUG("parent size width: {}", parent_size_.width(),
"height:", parent_size_.height());
SPDLOG_DEBUG("parent size width: {}, height: {}", parent_size_.width(),
parent_size_.height());

if (parent_pos_ != QPoint{0, 0} && parent_size_ != QSize{0, 0}) {
SPDLOG_DEBUG("update current dialog position now");
QPoint parent_center{parent_pos_.x() + parent_size_.width() / 2,
parent_pos_.y() + parent_size_.height() / 2};
if (parent_pos_ != QPoint{0, 0} && parent_size_ != QSize{0, 0}) {
SPDLOG_DEBUG("update current dialog position now");
QPoint parent_center{parent_pos_.x() + parent_size_.width() / 2,
parent_pos_.y() + parent_size_.height() / 2};

// update size of current dialog
size_ = this->size();
// update size of current dialog
size_ = this->size();

pos_ = {parent_center.x() - size_.width() / 2,
parent_center.y() - size_.height() / 2};
this->move(pos_);
pos_ = {parent_center.x() - size_.width() / 2,
parent_center.y() - size_.height() / 2};
SPDLOG_DEBUG("target dialog position, x: {}, y: {}", pos_.x(), pos_.y());
this->move(pos_);
}
} catch (std::exception &e) {
SPDLOG_WARN(
"error occurred when move to poition of center of parent, exception: "
"{}",
e.what());
} catch (...) {
SPDLOG_WARN("error occurred when move to poition of center of parent.");
}
}
4 changes: 2 additions & 2 deletions src/ui/dialog/GeneralDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ class GeneralDialog : public QDialog {
/**
*
*/
void setPosCenterOfScreen();
void movePos2CenterOfScreen();

/**
* @brief
*
*/
void movePosition2CenterOfParent();
void movePos2CenterOfParent();

private slots:
/**
Expand Down
2 changes: 1 addition & 1 deletion src/ui/dialog/import_export/KeyUploadDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ KeyUploadDialog::KeyUploadDialog(const KeyIdArgsListPtr& keys_ids,
this->setModal(true);
this->setWindowTitle(_("Uploading Public Key"));
this->setFixedSize(240, 42);
this->setPosCenterOfScreen();
this->movePos2CenterOfScreen();
}

void KeyUploadDialog::SlotUpload() {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/dialog/keypair_details/KeyDetailsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ KeyDetailsDialog::KeyDetailsDialog(const GpgKey& key, QWidget* parent)
// this->setMinimumSize({520, 600});

// move to center of the parent
this->movePosition2CenterOfParent();
this->movePos2CenterOfParent();

this->show();
}
Expand Down
1 change: 1 addition & 0 deletions src/ui/dialog/keypair_details/KeyPairUIDTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ KeyPairUIDTab::KeyPairUIDTab(const std::string& key_id, QWidget* parent)
manageUIDButton->setMenu(manage_selected_uid_menu_);
} else {
manageUIDButton->setDisabled(true);
addUIDButton->setDisabled(true);
}

uidButtonsLayout->addWidget(addUIDButton, 0, 1);
Expand Down

0 comments on commit 4de9027

Please sign in to comment.