Skip to content

Commit

Permalink
现在可以打开多个窗口,保留参考宽度值,参考宽度改变时更新窗口
Browse files Browse the repository at this point in the history
  • Loading branch information
cathaysia committed Jun 10, 2021
1 parent eb9d9d5 commit a3f4dd0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)

project(digikamflowplugin
VERSION 0.0.6
VERSION 0.0.7
DESCRIPTION "a plugin add a Flow View to digikam"
HOMEPAGE_URL https://github.com/cathaysia/digikamflowplugin)

Expand Down
88 changes: 50 additions & 38 deletions src/PicFlowView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,9 @@

namespace Cathaysia {

PicFlowView::PicFlowView(QObject* const parent)
: DPluginGeneric { parent }, main_dialog_ { new QDialog }, content_ { new Z::FlowLayout } {
// 图片的容器的宽度与主窗口保持一致
main_dialog_->installEventFilter(this);
// 添加 QScrollArea 及其环境
auto mainLayout = new QHBoxLayout;
auto scrollWidget = new QScrollArea;
auto box = new QWidget;

content_->setParent(box);

main_dialog_->setLayout(mainLayout);
mainLayout->addWidget(scrollWidget);
box->setLayout(content_);
PicFlowView::PicFlowView(QObject* const parent) : DPluginGeneric { parent } { }

scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollWidget->setWidget(box);
}

PicFlowView::~PicFlowView() noexcept {
if(main_dialog_) delete main_dialog_;
if(content_) delete content_;
}
PicFlowView::~PicFlowView() noexcept { }

QString PicFlowView::name() const {
return tr("PicFlowPlugin");
Expand Down Expand Up @@ -88,8 +68,11 @@ QList<Digikam::DPluginAuthor> PicFlowView::authors() const {
// clang-format on

bool PicFlowView::eventFilter(QObject* watched, QEvent* event) {
auto dialog = qobject_cast<QDialog*>(watched);
if(!dialog) return false;
if(event->type() == QEvent::Resize) {
content_->parentWidget()->resize(main_dialog_->width(), content_->innerHeight());
auto content = dialog->findChild<Z::FlowLayout*>("FlowLayout");
content->parentWidget()->resize(dialog->width(), content->innerHeight());
return true;
} else if(event->type() == QEvent::Close) {
stop_ = true;
Expand All @@ -114,29 +97,58 @@ void PicFlowView::setup(QObject* const parent) {
// 添加设置
connect(widthAction, &QAction::triggered, [this]() {
bool ok = false;
auto result = QInputDialog::getDouble(nullptr, tr("输入参考宽度"), tr("参考宽度"), 300, 10, 9999, 1, &ok);
if(ok) this->width_ = result;
auto result = QInputDialog::getDouble(nullptr, tr("输入参考宽度"), tr("参考宽度"), width_, 10, 9999,
1, &ok);
if(ok) {
this->width_ = result;
emit widthChanged(result);
};
});
ac->setMenu(setting);
connect(ac, &DPluginAction::triggered, this, &PicFlowView::flowView);
addAction(ac);
}

Cathaysia::PicFlowView::ShareData PicFlowView::getShareData() {
auto mainDialog = new QDialog;
auto mainLayout = new Z::FlowLayout;
mainLayout->setObjectName("FlowLayout");

// 图片的容器的宽度与主窗口保持一致
mainDialog->installEventFilter(this);
// 添加 QScrollArea 及其环境
auto dialogLayout = new QHBoxLayout;
auto scrollWidget = new QScrollArea;
auto box = new QWidget;

mainLayout->setParent(box);

mainDialog->setLayout(dialogLayout);
dialogLayout->addWidget(scrollWidget);
box->setLayout(mainLayout);

scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scrollWidget->setWidget(box);
//设置图片的参考宽度
mainLayout->setWidgetWidth(width_);
//
connect(this, &PicFlowView::widthChanged, mainLayout, &Z::FlowLayout::setWidgetWidth);
return ShareData(mainDialog, mainLayout);
}

void PicFlowView::flowView() {
auto* const iface = infoIface(sender());
// 设置图片的参考宽度
content_->setWidgetWidth(width_);
// 首先清空容器内的元素
while(content_->list().length()) {
auto item = content_->takeAt(0);
// 防止第二次添加时出现重叠问题
item->widget()->setParent(nullptr);
delete item;
}
/**
* @todo 1. 是否让多个窗口共用一个锁(现在不是)
*
*/
auto shareData = getShareData();
auto mainDialog = shareData.first;
auto mainLayout = shareData.second;
// 先显示
content_->parentWidget()->resize(800, content_->innerHeight());
main_dialog_->resize(800, 600);
main_dialog_->show();
mainLayout->parentWidget()->resize(800, mainLayout->innerHeight());
mainDialog->resize(800, 600);
mainDialog->show();

std::list<QPixmap> imgBuf;
std::atomic_bool over = false;
Expand Down Expand Up @@ -199,7 +211,7 @@ void PicFlowView::flowView() {
empty.release();
// 离开临界区
img->setScaledContents(true);
content_->addWidget(img);
mainLayout->addWidget(img);
// 防止界面卡顿
qApp->processEvents();
}
Expand Down
7 changes: 5 additions & 2 deletions src/PicFlowView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <flowlayout.h>
#include <semaphore>

#include <utility>

#define DPLUGIN_IID "org.kde.digikam.plugin.generic.FlowView"

using namespace Digikam;
Expand All @@ -19,6 +21,7 @@ class PicFlowView : public DPluginGeneric {
Q_INTERFACES(Digikam::DPluginGeneric)

public:
typedef std::pair<QDialog*, Z::FlowLayout*> ShareData;
explicit PicFlowView(QObject* const parent = nullptr);
~PicFlowView() noexcept;
QString name() const override;
Expand All @@ -33,19 +36,19 @@ class PicFlowView : public DPluginGeneric {

signals:
void imagePathResponse(const QString path);
void widthChanged(qreal width);

protected:
void flowView();
bool eventFilter(QObject* watched, QEvent* event) override;
ShareData getShareData();

private:
/**
* @brief 图片的参考宽度
*
*/
qreal width_ = 300;
QDialog* main_dialog_;
Z::FlowLayout* content_;
std::atomic_bool stop_ = false;
};

Expand Down

0 comments on commit a3f4dd0

Please sign in to comment.