Skip to content
This repository has been archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
refactor: move general widgets to widget-kit
Browse files Browse the repository at this point in the history
I rebuild my local qt library and the issues methioned in 48580c9
disappears, so QObject::connect has reverted to the style after qt5.
  • Loading branch information
zymelaii committed May 26, 2024
1 parent 3bc51d3 commit db76f9b
Show file tree
Hide file tree
Showing 46 changed files with 1,166 additions and 809 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ if(WIN32)
add_custom_target(
deploy
COMMAND windeployqt ${CMAKE_INSTALL_PREFIX}/$<TARGET_FILE_NAME:${CMAKE_PROJECT_NAME}>
$<$<STREQUAL:${CMAKE_BUILD_TYPE},Debug>:--debug>
$<$<STREQUAL:${CMAKE_BUILD_TYPE},Release>:--release>
COMMENT "Deploy the application..."
DEPENDS install-runtime-deps
)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.29.18.3022
0.29.19.3066
1 change: 1 addition & 0 deletions assets/res.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file alias="app.ico">icon/jwrite-icon-1.ico</file>
<file>res/default-cover.png</file>
<file>res/icons/warning.svg</file>
<file>res/icons/close-large.svg</file>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(minizip)
add_subdirectory(qt-material)
add_subdirectory(widget-kit)
add_subdirectory(jwrite)
1 change: 1 addition & 0 deletions src/jwrite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ target_link_libraries(
${CMAKE_PROJECT_NAME}
PRIVATE qt-material
PRIVATE minizip
PRIVATE widget-kit
PRIVATE jwrite-core
PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
PRIVATE QWindowKit::Widgets
Expand Down
2 changes: 1 addition & 1 deletion src/jwrite/ProfileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void Profiler::setup(int interval_sec) {
timer_->setInterval(interval_sec_ * 1000);
timer_->setSingleShot(false);
timer_->start();
connect(timer_, SIGNAL(timeout()), this, SLOT(summary_collected_data()));
connect(timer_, &QTimer::timeout, this, &Profiler::summary_collected_data);
}

void Profiler::start(ProfileTarget target) {
Expand Down
5 changes: 0 additions & 5 deletions src/jwrite/TwoLevelDataModel.cpp

This file was deleted.

59 changes: 49 additions & 10 deletions src/jwrite/ui/BookInfoEdit.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <jwrite/ui/BookInfoEdit.h>
#include <QVBoxLayout>
#include <QPainter>
#include <QFileDialog>

namespace jwrite::ui {

BookInfoEdit::BookInfoEdit(QWidget *parent)
: QWidget(parent) {
BookInfoEdit::BookInfoEdit()
: widgetkit::OverlayDialog() {
setupUi();
setupConnections();
}
Expand Down Expand Up @@ -39,13 +40,48 @@ void BookInfoEdit::setBookInfo(const BookInfo &info) {
book_info_.uuid = info.uuid;
}

QString BookInfoEdit::getCoverPath(QWidget *parent, bool validate, QImage *out_image) {
const auto filter = "图片 (*.bmp *.jpg *.jpeg *.png)";
auto path = QFileDialog::getOpenFileName(parent, "选择封面", "", filter);

if (path.isEmpty()) { return ""; }

if (!validate) { return path; }

QImage image(path);
if (image.isNull()) { return ""; }

if (out_image) { *out_image = std::move(image); }

return path;
}

std::optional<BookInfo>
BookInfoEdit::getBookInfo(widgetkit::OverlaySurface *surface, const BookInfo &initial) {
auto edit = std::make_unique<BookInfoEdit>();
edit->setBookInfo(initial);
const int request = edit->exec(surface);
if (request == BookInfoEdit::Submit) {
return {edit->book_info_};
} else {
return std::nullopt;
}
}

void BookInfoEdit::selectCoverImage() {
QImage image{};
const auto path = getCoverPath(this, true, &image);
if (path.isEmpty()) { return; }
setCover(path);
}

void BookInfoEdit::setupUi() {
ui_title_edit_ = new QtMaterialTextField;
ui_author_edit_ = new QtMaterialTextField;
ui_cover_ = new PlainImageView;
ui_cover_select_ = new FlatButton;
ui_submit_ = new FlatButton;
ui_cancel_ = new FlatButton;
ui_cover_ = new widgetkit::ImageLabel;
ui_cover_select_ = new widgetkit::FlatButton;
ui_submit_ = new widgetkit::FlatButton;
ui_cancel_ = new widgetkit::FlatButton;

auto row_container = new QWidget;
auto btn_container = new QWidget;
Expand Down Expand Up @@ -131,13 +167,16 @@ void BookInfoEdit::setupUi() {
}

void BookInfoEdit::setupConnections() {
connect(ui_submit_, &FlatButton::pressed, this, [this] {
connect(ui_submit_, &widgetkit::FlatButton::pressed, this, [this] {
book_info_.title = ui_title_edit_->text();
book_info_.author = ui_author_edit_->text();
emit submitRequested(book_info_);
exit(Request::Submit);
});
connect(ui_cancel_, &widgetkit::FlatButton::pressed, this, [this] {
exit(Request::Cancel);
});
connect(ui_cancel_, &FlatButton::pressed, this, &BookInfoEdit::cancelRequested);
connect(ui_cover_select_, &FlatButton::pressed, this, &BookInfoEdit::changeCoverRequested);
connect(
ui_cover_select_, &widgetkit::FlatButton::pressed, this, &BookInfoEdit::selectCoverImage);
}

void BookInfoEdit::paintEvent(QPaintEvent *event) {
Expand Down
62 changes: 48 additions & 14 deletions src/jwrite/ui/BookInfoEdit.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#pragma once

#include <jwrite/BookManager.h>
#include <jwrite/ui/PlainImageView.h>
#include <jwrite/ui/FlatButton.h>
#include <widget-kit/FlatButton.h>
#include <widget-kit/ImageLabel.h>
#include <widget-kit/OverlayDialog.h>
#include <qt-material/qtmaterialtextfield.h>
#include <qt-material/qtmaterialraisedbutton.h>
#include <QWidget>
#include <QLabel>

namespace jwrite::ui {

class BookInfoEdit : public QWidget {
class BookInfoEdit : public widgetkit::OverlayDialog {
Q_OBJECT

public:
explicit BookInfoEdit(QWidget *parent = nullptr);
enum Request {
Submit,
Cancel,
};

public:
BookInfoEdit();
~BookInfoEdit() override;

public:
Expand All @@ -23,10 +30,37 @@ class BookInfoEdit : public QWidget {
void setCover(const QString &cover_url);
void setBookInfo(const BookInfo &info);

signals:
void submitRequested(BookInfo info);
void cancelRequested();
void changeCoverRequested();
BookInfoEdit &withTitle(const QString &title) {
setTitle(title);
return *this;
}

BookInfoEdit &withAuthor(const QString &author) {
setAuthor(author);
return *this;
}

BookInfoEdit &withCover(const QString &cover_url) {
setCover(cover_url);
return *this;
}

BookInfoEdit &withBookInfo(const BookInfo &info) {
setBookInfo(info);
return *this;
}

const BookInfo &bookInfo() const {
return book_info_;
}

static QString getCoverPath(QWidget *parent, bool validate, QImage *out_image);

static std::optional<BookInfo>
getBookInfo(widgetkit::OverlaySurface *surface, const BookInfo &initial);

protected slots:
void selectCoverImage();

protected:
void setupUi();
Expand All @@ -37,12 +71,12 @@ class BookInfoEdit : public QWidget {
private:
BookInfo book_info_;

QtMaterialTextField *ui_title_edit_;
QtMaterialTextField *ui_author_edit_;
PlainImageView *ui_cover_;
FlatButton *ui_cover_select_;
FlatButton *ui_submit_;
FlatButton *ui_cancel_;
QtMaterialTextField *ui_title_edit_;
QtMaterialTextField *ui_author_edit_;
widgetkit::ImageLabel *ui_cover_;
widgetkit::FlatButton *ui_cover_select_;
widgetkit::FlatButton *ui_submit_;
widgetkit::FlatButton *ui_cancel_;
};

} // namespace jwrite::ui
40 changes: 0 additions & 40 deletions src/jwrite/ui/ColorSelectorButton.cpp

This file was deleted.

31 changes: 0 additions & 31 deletions src/jwrite/ui/ColorSelectorButton.h

This file was deleted.

24 changes: 15 additions & 9 deletions src/jwrite/ui/ColorThemeDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <jwrite/ui/ColorThemeDialog.h>
#include <jwrite/ui/ColorSelectorButton.h>
#include <widget-kit/ColorPreviewItem.h>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
Expand Down Expand Up @@ -53,17 +53,20 @@ void ColorThemeDialog::setupUi() {
auto container = new QWidget;
auto layout_container = new QHBoxLayout(container);
auto label = new QLabel(desc);
auto selector = new ColorSelectorButton;
auto selector = new widgetkit::ColorPreviewItem;
layout_container->addWidget(selector);
layout_container->addWidget(label);
layout_color_picker->addWidget(container, index / 2, index % 2);

selector->setFixedWidth(40);
selector->setColor(*color_ref);

connect(selector, &ColorSelectorButton::colorChanged, [&ref = *color_ref](QColor color) {
ref = color;
});
connect(
selector,
&widgetkit::ColorPreviewItem::colorChanged,
[&ref = *color_ref](QColor color) {
ref = color;
});
connect(
this, &ColorThemeDialog::themeChanged, selector, [this, selector, &ref = *color_ref] {
blockSignals(true);
Expand All @@ -88,11 +91,14 @@ void ColorThemeDialog::setupUi() {
layout->addWidget(color_picker_conatiner);
layout->addWidget(button_container);

connect(ok_button, SIGNAL(clicked()), this, SLOT(accept()));
connect(apply_button, SIGNAL(clicked()), this, SLOT(notifyThemeApplied()));
connect(cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
connect(ok_button, &QPushButton::clicked, this, &ColorThemeDialog::accept);
connect(apply_button, &QPushButton::clicked, this, &ColorThemeDialog::notifyThemeApplied);
connect(cancel_button, &QPushButton::clicked, this, &ColorThemeDialog::reject);
connect(
ui_schema_select_, SIGNAL(currentIndexChanged(int)), this, SLOT(notifyThemeChanged(int)));
ui_schema_select_,
&QComboBox::currentIndexChanged,
this,
&ColorThemeDialog::notifyThemeChanged);

ui_schema_select_->addItem("亮色", QVariant::fromValue(ColorSchema::Light));
ui_schema_select_->addItem("暗色", QVariant::fromValue(ColorSchema::Dark));
Expand Down
Loading

0 comments on commit db76f9b

Please sign in to comment.