Debian Codegraph @

src/album/widgets/imgviewwidget.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "imgviewwidget.h"
22 #include "application.h"
23 #include "utils/baseutils.h"
24 #include "utils/imageutils.h"
25 #include "utils/unionimage.h"
26
27 #include "dbmanager/dbmanager.h"
28 #include "controller/configsetter.h"
29 #include "widgets/elidedlabel.h"
30 #include "imageengine/imageengineapi.h"
31 #include "ac-desktop-define.h"
32
33 #include <QTimer>
34 #include <QScroller>
35 #include <QScrollBar>
36 #include <QFileInfo>
37 #include <QHBoxLayout>
38 #include <QDebug>
39 #include <QPainterPath>
40 #include <DLabel>
41 #include <QAbstractItemModel>
42 #include <DImageButton>
43 #include <DThumbnailProvider>
44 #include <DApplicationHelper>
45 #include <DSpinner>
46 #include <QtMath>
47
48 #include "imgviewlistview.h"
49 #include "imagedataservice.h"
50
51 DWIDGET_USE_NAMESPACE
52
53 MyImageListWidget::MyImageListWidget(QWidget *parent)
54 : QWidget(parent)
55 {
56 QHBoxLayout *hb = new QHBoxLayout(this);
57 hb->setContentsMargins(0, 0, 0, 0);
58 hb->setSpacing(0);
59 this->setLayout(hb);
60 m_listview = new ImgViewListView(this);
61 m_listview->setObjectName("m_imgListWidget");
62 hb->addWidget(m_listview);
63
64 connect(m_listview, &ImgViewListView::clicked, this, &MyImageListWidget::onClicked);
65 connect(m_listview, &ImgViewListView::openImg, this, &MyImageListWidget::openImg);
66 connect(m_listview->horizontalScrollBar(), &QScrollBar::valueChanged, this, &MyImageListWidget::onScrollBarValueChanged);
67 }
68
69 MyImageListWidget::~MyImageListWidget()
70 {
71 }
72
73 void MyImageListWidget::setAllFile(const SignalManager::ViewInfo &info, QString path)
74 {
75 m_listview->setAllFile(info, path);
76 bool visual = info.dBImgInfos.size() > 1;
77 if (!visual) {
78 //尝试paths的大小
79 visual = info.paths.size() > 1;
80 }
81 this->setVisible(visual);
82 setSelectCenter();
83 emit openImg(m_listview->getSelectIndexByPath(path), path);
84 }
85
86 DBImgInfo MyImageListWidget::getImgInfo(const QString &path)
87 {
88 DBImgInfo info;
89 for (int i = 0; i < m_listview->m_model->rowCount(); i++) {
90 QModelIndex indexImg = m_listview->m_model->index(i, 0);
91 DBImgInfo infoImg = indexImg.data(Qt::DisplayRole).value<DBImgInfo>();
92 if (infoImg.filePath == path) {
93 info = infoImg;
94 break;
95 }
96 }
97 return info;
98 }
99 //将选中项移到最前面,后期可能有修改,此时获取的列表宽度不正确
100 void MyImageListWidget::setSelectCenter()
101 {
102 m_listview->setSelectCenter();
103 }
104
105 int MyImageListWidget::getImgCount()
106 {
107 return m_listview->m_model->rowCount();
108 }
109
110 void MyImageListWidget::removeCurrent()
111 {
112 m_listview->removeCurrent();
113 this->setVisible(getImgCount() > 1);
114 }
115
116 void MyImageListWidget::reloadImage(int value)
117 {
118 //加载上下两百张
119 if (m_loadTimer == nullptr) {
120 m_loadTimer = new QTimer(this);
121 m_loadTimer->setInterval(50);
122 m_loadTimer->setSingleShot(true);
123 connect(m_loadTimer, &QTimer::timeout, this, [ = ] {
124 qDebug() << __FUNCTION__ << "---m_value = " << m_value;
125 QModelIndex load = m_listview->indexAt(QPoint(m_value, 30));
126 if (!load.isValid())
127 {
128 load = m_listview->indexAt(QPoint(m_value + 10, 30));
129 }
130 if (!load.isValid())
131 {
132 load = m_listview->indexAt(QPoint(m_value + 20, 30));
133 }
134 if (!load.isValid())
135 {
136 load = m_listview->m_model->index(0, 0);
137 }
138 if (!load.isValid())
139 {
140 return;
141 }
142 QStringList pathlist;
143 int count = 0;
144 DBImgInfo dataload = load.data(Qt::DisplayRole).value<DBImgInfo>();
145 qDebug() << __FUNCTION__ << "---" << dataload.filePath;
146 for (int i = load.row(); i >= 0; i--)
147 {
148 QModelIndex index = m_listview->m_model->index(i, 0);
149 DBImgInfo data = index.data(Qt::DisplayRole).value<DBImgInfo>();
150 pathlist << data.filePath;
151 count++;
152 if (count >= 200) {
153 break;
154 }
155 }
156 count = 0;
157 for (int i = load.row(); i < m_listview->m_model->rowCount(); i++)
158 {
159 QModelIndex index = m_listview->m_model->index(i, 0);
160 DBImgInfo data = index.data(Qt::DisplayRole).value<DBImgInfo>();
161 pathlist << data.filePath;
162 count++;
163 if (count >= 200) {
164 break;
165 }
166 }
167 qDebug() << __FUNCTION__ << "---pathlist = " << pathlist.size();
168 ImageDataService::instance()->readThumbnailByPaths(pathlist, false);
169 m_listview->update();
170 });
171 }
172 if (m_loadTimer->isActive()) {
173 m_loadTimer->stop();
174 }
175 m_loadTimer->start();
176 }
177
178 void MyImageListWidget::onScrollBarValueChanged(int value)
179 {
180 m_value = value;
181 reloadImage(value);
182 Q_UNUSED(value)
183 QModelIndex index = m_listview->indexAt(QPoint((m_listview->width() - 15), 10));
184 if (!index.isValid()) {
185 index = m_listview->indexAt(QPoint((m_listview->width() - 20), 10));
186 }
187 }
188
189 void MyImageListWidget::openNext()
190 {
191 m_listview->openNext();
192 }
193
194 void MyImageListWidget::openPre()
195 {
196 m_listview->openPre();
197 }
198
199 bool MyImageListWidget::isLast()
200 {
201 return m_listview->isLast();
202 }
203
204 bool MyImageListWidget::isFirst()
205 {
206 return m_listview->isFirst();
207 }
208
209 void MyImageListWidget::onClicked(const QModelIndex &index)
210 {
211 m_listview->onClicked(index);
212 }