Skip to content

Commit 7e30ae6

Browse files
fix critical segmentation fault, and implemented hiding main_window when about_window is opened to improve UX
1 parent 3ba2351 commit 7e30ae6

7 files changed

Lines changed: 81 additions & 19 deletions

File tree

src/front_end/about_widget.cxx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <qdebug.h>
44
#include <qgridlayout.h>
55
#include <qlogging.h>
6+
#include <qmainwindow.h>
67
#include <qobject.h>
78
#include <qpushbutton.h>
89

@@ -17,19 +18,23 @@ using Self = AboutWidget;
1718
AboutWidget::AboutWidget() { show_about = make_unique<QPushButton>(); }
1819

1920
void Self::setup_event() {
20-
const auto function = [this]() {
21-
if (!about_window) {
22-
about_window = make_unique<AboutWindow>();
23-
}
21+
about_window = make_unique<AboutWindow>();
2422

23+
// clang-format off
24+
const auto function = [this]() {
2525
about_window->show_window();
26-
};
26+
}; // clang-format on
2727

2828
QObject::connect(show_about.get(), &QPushButton::clicked, function);
2929
}
3030

3131
void Self::setup_widget(QGridLayout *grid_layout) {
3232
show_about->setText("About");
33-
this->setup_event();
3433
grid_layout->addWidget(show_about.get(), 1, 1);
34+
35+
this->setup_event();
36+
}
37+
38+
void Self::setup_about_window_event(QMainWindow *main_window) {
39+
about_window->on_about_window_event(main_window);
3540
}

src/front_end/about_window.cxx

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <qdialog.h>
44
#include <qgridlayout.h>
55
#include <qlogging.h>
6+
#include <qmainwindow.h>
67
#include <qmessagebox.h>
78
#include <qobject.h>
89
#include <qpushbutton.h>
@@ -15,8 +16,16 @@ using Lazyboard::front_end::AboutWindow;
1516
using std::make_unique;
1617
using Self = AboutWindow;
1718

19+
#if defined(LAZY_DEBUG)
20+
#include <string>
21+
22+
#include "../front_end_utils/include/utils.hxx"
23+
using Lazyboard::front_end_utils::debug_info;
24+
using Lazyboard::front_end_utils::dump_ptr_address;
25+
using std::string;
26+
#endif
27+
1828
AboutWindow::AboutWindow() {
19-
about_window = make_unique<QDialog>();
2029
grid_layout = make_unique<QGridLayout>();
2130
github_button = make_unique<QPushButton>();
2231
github_issue_button = make_unique<QPushButton>();
@@ -31,8 +40,7 @@ void Self::is_open_browser_ok(ResultContext &status) {
3140
break;
3241

3342
case R::FAILED:
34-
QMessageBox::critical(about_window.get(), "Error",
35-
"Could not open your browser");
43+
QMessageBox::critical(this, "Error", "Could not open your browser");
3644
}
3745
}
3846

@@ -66,14 +74,48 @@ void Self::setup_buttons() {
6674
}
6775

6876
void Self::setup_front_end() {
69-
about_window->setLayout(this->grid_layout.get());
77+
this->setLayout(this->grid_layout.get());
7078
this->setup_buttons();
7179
}
7280

7381
void Self::show_window() {
74-
about_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
75-
about_window->setWindowTitle("Lazyboard About");
82+
this->setMinimumSize(MIN_WIDTH, MIN_HEIGHT);
83+
this->setWindowTitle("Lazyboard About");
7684

7785
this->setup_front_end();
78-
about_window->exec();
86+
this->exec();
87+
}
88+
89+
void Self::on_closed(QMainWindow *main_window) {
90+
using O = QObject;
91+
const auto fn = [this, main_window] {
92+
main_window->show();
93+
94+
// clang-format off
95+
#if defined (LAZY_DEBUG)
96+
debug_info("About window is closed");
97+
#endif // clang-format on
98+
};
99+
100+
O::connect(this, &QDialog::rejected, fn);
101+
}
102+
103+
void Self::showEvent(QShowEvent *event) {
104+
QDialog::showEvent(event);
105+
this->_main_window->setHidden(true);
106+
107+
// clang-format off
108+
#if defined (LAZY_DEBUG)
109+
debug_info("About Dialog is open");
110+
#endif // clang-format on
111+
}
112+
113+
void Self::on_about_window_event(QMainWindow *main_window) {
114+
this->_main_window = main_window;
115+
this->on_closed(this->_main_window);
116+
117+
// clang-format off
118+
#if defined (LAZY_DEBUG)
119+
dump_ptr_address(main_window);
120+
#endif // clang-format on
79121
}

src/front_end/include/about_widget.hxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define ABOUT_WIDGET_HXX
33

44
#include <qgridlayout.h>
5+
#include <qmainwindow.h>
56
#include <qpushbutton.h>
67

78
#include <memory>
@@ -24,6 +25,7 @@ class AboutWidget {
2425

2526
public:
2627
void setup_widget(QGridLayout *layout);
28+
void setup_about_window_event(QMainWindow *main_window);
2729
};
2830

2931
} // namespace Lazyboard::front_end

src/front_end/include/about_window.hxx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <qdialog.h>
55
#include <qgridlayout.h>
6+
#include <qmainwindow.h>
67
#include <qpushbutton.h>
78

89
#include <memory>
@@ -11,9 +12,8 @@
1112
using std::unique_ptr;
1213

1314
namespace Lazyboard::front_end {
14-
class AboutWindow {
15+
class AboutWindow : public QDialog {
1516
private:
16-
unique_ptr<QDialog> about_window;
1717
unique_ptr<QGridLayout> grid_layout;
1818
unique_ptr<QPushButton> github_button;
1919
unique_ptr<QPushButton> github_issue_button;
@@ -28,17 +28,25 @@ class AboutWindow {
2828
const char *GITHUB_PULL_URL =
2929
"https://github.com/reim-developer/zClipboard/pulls";
3030

31+
private:
32+
QMainWindow *_main_window = nullptr;
33+
34+
protected:
35+
void showEvent(QShowEvent *event) override;
36+
3137
private:
3238
void is_open_browser_ok(ResultContext &status);
3339
void open_browser_when_clicked(QPushButton *button, const char *url);
3440

3541
private:
3642
void setup_front_end();
3743
void setup_buttons();
44+
void on_closed(QMainWindow *main_window);
3845

3946
public:
4047
AboutWindow();
4148
void show_window();
49+
void on_about_window_event(QMainWindow *main_window);
4250
};
4351
} // namespace Lazyboard::front_end
4452

src/front_end/main_window.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ void Self::front_end_show() {
5555

5656
table_widget->setup_widget(this->grid_layout);
5757
setting_widget->setup_widget(this->grid_layout);
58+
5859
about_widget->setup_widget(this->grid_layout);
60+
about_widget->setup_about_window_event(main_window.get());
5961
}

src/front_end/table_widget.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <qabstractitemview.h>
44
#include <qgridlayout.h>
55
#include <qheaderview.h>
6+
#include <qmainwindow.h>
67
#include <qtableview.h>
78
#include <qtablewidget.h>
89

src/front_end_utils/include/utils.hxx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <algorithm>
1313
#include <initializer_list>
14+
#include <string>
1415
#include <type_traits>
1516
#include <utility>
1617
#include <vector>
@@ -22,6 +23,7 @@ using std::for_each;
2223
using std::forward;
2324
using std::initializer_list;
2425
using std::is_same_v;
26+
using std::string;
2527
using std::stringstream;
2628
using std::vector;
2729

@@ -34,9 +36,9 @@ using std::source_location;
3436

3537
namespace Lazyboard::front_end_utils {
3638
template <typename T>
37-
concept is_qcolor = requires(const T& t) { QColor(t); };
39+
concept qcolor_bound = requires(const T& t) { QColor(t); };
3840

39-
template <is_qcolor... Args>
41+
template <qcolor_bound... Args>
4042
inline constexpr bool is_valid_hex_color(Args&&... args) noexcept {
4143
return (... && [&]() {
4244
QColor color(std::forward<Args>(args));
@@ -91,11 +93,11 @@ inline void dump_ptr_address(T* t,
9193
}
9294

9395
template <typename T>
94-
concept string_bound = requires(const T& t) { string(t); };
96+
concept string_bound = requires(const T& t) { std::string(t); };
9597

9698
template <typename string_bound>
9799
inline void debug_info(string_bound&& information) {
98-
auto info = forward<string_bound>(information);
100+
auto info = std::forward<string_bound>(information);
99101

100102
println("{}[INFO_DEBUG]{} {}", green, white, info);
101103
}

0 commit comments

Comments
 (0)