Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit a3f4dd0

Browse files
committed
现在可以打开多个窗口,保留参考宽度值,参考宽度改变时更新窗口
1 parent eb9d9d5 commit a3f4dd0

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.0)
22

33
project(digikamflowplugin
4-
VERSION 0.0.6
4+
VERSION 0.0.7
55
DESCRIPTION "a plugin add a Flow View to digikam"
66
HOMEPAGE_URL https://github.com/cathaysia/digikamflowplugin)
77

src/PicFlowView.cpp

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,9 @@
3333

3434
namespace Cathaysia {
3535

36-
PicFlowView::PicFlowView(QObject* const parent)
37-
: DPluginGeneric { parent }, main_dialog_ { new QDialog }, content_ { new Z::FlowLayout } {
38-
// 图片的容器的宽度与主窗口保持一致
39-
main_dialog_->installEventFilter(this);
40-
// 添加 QScrollArea 及其环境
41-
auto mainLayout = new QHBoxLayout;
42-
auto scrollWidget = new QScrollArea;
43-
auto box = new QWidget;
44-
45-
content_->setParent(box);
46-
47-
main_dialog_->setLayout(mainLayout);
48-
mainLayout->addWidget(scrollWidget);
49-
box->setLayout(content_);
36+
PicFlowView::PicFlowView(QObject* const parent) : DPluginGeneric { parent } { }
5037

51-
scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52-
scrollWidget->setWidget(box);
53-
}
54-
55-
PicFlowView::~PicFlowView() noexcept {
56-
if(main_dialog_) delete main_dialog_;
57-
if(content_) delete content_;
58-
}
38+
PicFlowView::~PicFlowView() noexcept { }
5939

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

9070
bool PicFlowView::eventFilter(QObject* watched, QEvent* event) {
71+
auto dialog = qobject_cast<QDialog*>(watched);
72+
if(!dialog) return false;
9173
if(event->type() == QEvent::Resize) {
92-
content_->parentWidget()->resize(main_dialog_->width(), content_->innerHeight());
74+
auto content = dialog->findChild<Z::FlowLayout*>("FlowLayout");
75+
content->parentWidget()->resize(dialog->width(), content->innerHeight());
9376
return true;
9477
} else if(event->type() == QEvent::Close) {
9578
stop_ = true;
@@ -114,29 +97,58 @@ void PicFlowView::setup(QObject* const parent) {
11497
// 添加设置
11598
connect(widthAction, &QAction::triggered, [this]() {
11699
bool ok = false;
117-
auto result = QInputDialog::getDouble(nullptr, tr("输入参考宽度"), tr("参考宽度"), 300, 10, 9999, 1, &ok);
118-
if(ok) this->width_ = result;
100+
auto result = QInputDialog::getDouble(nullptr, tr("输入参考宽度"), tr("参考宽度"), width_, 10, 9999,
101+
1, &ok);
102+
if(ok) {
103+
this->width_ = result;
104+
emit widthChanged(result);
105+
};
119106
});
120107
ac->setMenu(setting);
121108
connect(ac, &DPluginAction::triggered, this, &PicFlowView::flowView);
122109
addAction(ac);
123110
}
124111

112+
Cathaysia::PicFlowView::ShareData PicFlowView::getShareData() {
113+
auto mainDialog = new QDialog;
114+
auto mainLayout = new Z::FlowLayout;
115+
mainLayout->setObjectName("FlowLayout");
116+
117+
// 图片的容器的宽度与主窗口保持一致
118+
mainDialog->installEventFilter(this);
119+
// 添加 QScrollArea 及其环境
120+
auto dialogLayout = new QHBoxLayout;
121+
auto scrollWidget = new QScrollArea;
122+
auto box = new QWidget;
123+
124+
mainLayout->setParent(box);
125+
126+
mainDialog->setLayout(dialogLayout);
127+
dialogLayout->addWidget(scrollWidget);
128+
box->setLayout(mainLayout);
129+
130+
scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
131+
scrollWidget->setWidget(box);
132+
//设置图片的参考宽度
133+
mainLayout->setWidgetWidth(width_);
134+
//
135+
connect(this, &PicFlowView::widthChanged, mainLayout, &Z::FlowLayout::setWidgetWidth);
136+
return ShareData(mainDialog, mainLayout);
137+
}
138+
125139
void PicFlowView::flowView() {
126140
auto* const iface = infoIface(sender());
127-
// 设置图片的参考宽度
128-
content_->setWidgetWidth(width_);
129-
// 首先清空容器内的元素
130-
while(content_->list().length()) {
131-
auto item = content_->takeAt(0);
132-
// 防止第二次添加时出现重叠问题
133-
item->widget()->setParent(nullptr);
134-
delete item;
135-
}
141+
/**
142+
* @todo 1. 是否让多个窗口共用一个锁(现在不是)
143+
*
144+
*/
145+
auto shareData = getShareData();
146+
auto mainDialog = shareData.first;
147+
auto mainLayout = shareData.second;
136148
// 先显示
137-
content_->parentWidget()->resize(800, content_->innerHeight());
138-
main_dialog_->resize(800, 600);
139-
main_dialog_->show();
149+
mainLayout->parentWidget()->resize(800, mainLayout->innerHeight());
150+
mainDialog->resize(800, 600);
151+
mainDialog->show();
140152

141153
std::list<QPixmap> imgBuf;
142154
std::atomic_bool over = false;
@@ -199,7 +211,7 @@ void PicFlowView::flowView() {
199211
empty.release();
200212
// 离开临界区
201213
img->setScaledContents(true);
202-
content_->addWidget(img);
214+
mainLayout->addWidget(img);
203215
// 防止界面卡顿
204216
qApp->processEvents();
205217
}

src/PicFlowView.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <flowlayout.h>
88
#include <semaphore>
99

10+
#include <utility>
11+
1012
#define DPLUGIN_IID "org.kde.digikam.plugin.generic.FlowView"
1113

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

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

3437
signals:
3538
void imagePathResponse(const QString path);
39+
void widthChanged(qreal width);
3640

3741
protected:
3842
void flowView();
3943
bool eventFilter(QObject* watched, QEvent* event) override;
44+
ShareData getShareData();
4045

4146
private:
4247
/**
4348
* @brief 图片的参考宽度
4449
*
4550
*/
4651
qreal width_ = 300;
47-
QDialog* main_dialog_;
48-
Z::FlowLayout* content_;
4952
std::atomic_bool stop_ = false;
5053
};
5154

0 commit comments

Comments
 (0)