Debian Codegraph @

src/album/thumbnail/imgviewlistview.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 "imgviewlistview.h"
22 #include "application.h"
23 #include <math.h>
24 #include <QDebug>
25 #include <QDrag>
26 #include <QFileInfo>
27 #include <QImageReader>
28 #include <QMimeData>
29 #include <QScrollBar>
30 #include <QMutex>
31 #include <QScroller>
32
33 #include "controller/signalmanager.h"
34 #include "controller/wallpapersetter.h"
35 #include "widgets/dialogs/imgdeletedialog.h"
36 #include "utils/baseutils.h"
37 #include "utils/imageutils.h"
38 #include "utils/unionimage.h"
39 #include "imageengine/imageengineapi.h"
40 #include "imageengine/imageenginethread.h"
41 #include "ac-desktop-define.h"
42 #include "imagedataservice.h"
43
44 const int ITEM_SPACING = 2;//间隔
45
46 ImgViewListView::ImgViewListView(QWidget *parent)
47 : DListView(parent)
48 {
49 m_model = new QStandardItemModel(this);
50 m_delegate = new ImgViewDelegate(this);
51 setResizeMode(QListView::Adjust);
52 setViewMode(QListView::IconMode);
53 setEditTriggers(QAbstractItemView::NoEditTriggers);
54 setSpacing(ITEM_SPACING);
55 setDragEnabled(false);
56 setSelectionMode(QAbstractItemView::SingleSelection);
57 QListView::setFlow(QListView::LeftToRight);
58 QListView::setWrapping(false);
59 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
61 QScroller::grabGesture(viewport(), QScroller::LeftMouseButtonGesture);
62
63 // setUniformItemSizes(true);
64
65 setItemDelegate(m_delegate);
66 setModel(m_model);
67 // installEventFilter(viewport());
68
69 setMouseTracking(true);
70 this->viewport()->setMouseTracking(true);
71
72 connect(ImageEngineApi::instance(), &ImageEngineApi::sigOneImgReady, this, &ImgViewListView::slotOneImgReady);
73 }
74
75 ImgViewListView::~ImgViewListView()
76 {
77 }
78
79 void ImgViewListView::setAllFile(const SignalManager::ViewInfo &inf, const QString &path)
80 {
81 qDebug() << "---" << __FUNCTION__ << "---path = " << path;
82 m_model->clear();
83 m_currentPath = path;
84 int count = inf.dBImgInfos.size();
85 if (count == 0) { //外部打开,没有数据库内容
86 count = inf.paths.size();
87 }
88 for (int i = 0; i < count; i++) {
89 DBImgInfo info;
90 if (!inf.dBImgInfos.isEmpty()) {
91 info = inf.dBImgInfos.at(i);
92 } else {
93 info.filePath = inf.paths.at(i);
94 }
95
96 if (info.filePath == path) {
97 info.imgWidth = ITEM_CURRENT_WH;
98 info.imgHeight = ITEM_CURRENT_WH;
99 m_currentRow = i;
100 } else {
101 info.imgWidth = ITEM_NORMAL_WIDTH;
102 info.imgHeight = ITEM_NORMAL_HEIGHT;
103 }
104 QStandardItem *item = new QStandardItem;
105 QVariant infoVariant;
106 infoVariant.setValue(info);
107 item->setData(infoVariant, Qt::DisplayRole);
108
109 item->setData(QVariant(QSize(info.imgWidth, info.imgHeight)), Qt::SizeHintRole);
110 m_model->appendRow(item);
111 }
112
113 doItemsLayout();
114
115 // this->setFixedSize((2 * (count + 1) + 40 * count + 10), 60);
116 }
117
118 int ImgViewListView::getSelectIndexByPath(const QString &path)
119 {
120 int index = -1;
121 for (int i = 0; i < m_model->rowCount(); i++) {
122 QModelIndex itemIndex = m_model->index(i, 0);
123 DBImgInfo info = itemIndex.data(Qt::DisplayRole).value<DBImgInfo>();
124 if (info.filePath == path) {
125 return i;
126 }
127 }
128 return index;
129 }
130
131 void ImgViewListView::setSelectCenter()
132 {
133 QModelIndex itemIndex = m_model->index(m_currentRow, 0);
134 QRect rect = this->visualRect(itemIndex);
135 this->horizontalScrollBar()->setValue(rect.x());
136 }
137
138 void ImgViewListView::openNext()
139 {
140 if (m_currentRow == (m_model->rowCount() - 1)) {
141 return;
142 }
143
144 QModelIndex currentIndex = m_model->index(m_currentRow, 0);
145 QModelIndex nextIndex = m_model->index((m_currentRow + 1), 0);
146 if (!nextIndex.isValid()) {
147 return;
148 }
149
150 DBImgInfo info = nextIndex.data(Qt::DisplayRole).value<DBImgInfo>();
151 if (info.filePath.isEmpty()) {
152 return;
153 }
154
155 if (currentIndex.isValid()) {
156 //重置前一个选中项的宽高
157 m_model->setData(currentIndex,
158 QVariant(QSize(ImgViewListView::ITEM_NORMAL_WIDTH, ImgViewListView::ITEM_NORMAL_HEIGHT)), Qt::SizeHintRole);
159 }
160
161 if (nextIndex.isValid()) {
162 //重置新选中项的宽高
163 m_model->setData(nextIndex,
164 QVariant(QSize(ImgViewListView::ITEM_CURRENT_WH, ImgViewListView::ITEM_CURRENT_WH)), Qt::SizeHintRole);
165
166 }
167 doItemsLayout();
168
169 m_currentRow = m_currentRow + 1;
170 m_currentPath = info.filePath;
171
172 loadFiftyRight();
173
174 startMoveToLeftAnimation();
175
176 emit openImg(m_currentRow, m_currentPath);
177 }
178
179 bool ImgViewListView::isLast()
180 {
181 return m_currentRow == (m_model->rowCount() - 1);
182 }
183
184 void ImgViewListView::openPre()
185 {
186 if (m_currentRow <= 0) {
187 return;
188 }
189
190 QModelIndex currentIndex = m_model->index(m_currentRow, 0);
191 QModelIndex preIndex = m_model->index((m_currentRow - 1), 0);
192 if (!preIndex.isValid()) {
193 return;
194 }
195
196 DBImgInfo info = preIndex.data(Qt::DisplayRole).value<DBImgInfo>();
197 if (info.filePath.isEmpty()) {
198 return;
199 }
200
201 if (currentIndex.isValid()) {
202 //重置前一个选中项的宽高
203 m_model->setData(currentIndex,
204 QVariant(QSize(ImgViewListView::ITEM_NORMAL_WIDTH, ImgViewListView::ITEM_NORMAL_HEIGHT)), Qt::SizeHintRole);
205 }
206
207 if (preIndex.isValid()) {
208 //重置新选中项的宽高
209 m_model->setData(preIndex,
210 QVariant(QSize(ImgViewListView::ITEM_CURRENT_WH, ImgViewListView::ITEM_CURRENT_WH)), Qt::SizeHintRole);
211
212 }
213 doItemsLayout();
214
215 m_currentRow = m_currentRow - 1;
216 m_currentPath = info.filePath;
217
218 emit openImg(m_currentRow, m_currentPath);
219 }
220
221 bool ImgViewListView::isFirst()
222 {
223 return m_currentRow == 0;
224 }
225
226 void ImgViewListView::removeCurrent()
227 {
228 //当前显示数量大于3
229 if (m_model->rowCount() > 1) {
230 //删除最后一个,继续显示第一张
231 qDebug() << "---" << __FUNCTION__ << "---m_currentRow = " << m_currentRow;
232 qDebug() << "---" << __FUNCTION__ << "---m_model->rowCount() = " << m_model->rowCount();
233 if (m_currentRow == (m_model->rowCount() - 1)) {
234 QModelIndex index = m_model->index(0, 0);
235 onClicked(index);
236 m_model->removeRow(m_model->rowCount() - 1);
237 if (m_model->rowCount() > 0) {
238 this->horizontalScrollBar()->setValue(0);
239 }
240 } else {
241 //显示下一张
242 QModelIndex index = m_model->index((m_currentRow + 1), 0);
243 onClicked(index);
244 m_currentRow = m_currentRow - 1;
245 //m_currentRow在onClicked中已经被修改了
246 m_model->removeRow((m_currentRow));
247 }
248 } else if (m_model->rowCount() == 1) {
249 //数量只有一张
250 m_model->clear();
251 m_currentRow = -1;
252 m_currentPath = "";
253 }
254 }
255
256 void ImgViewListView::slotOneImgReady(const QString &path, QPixmap pix)
257 {
258 for (int i = 0; i < m_model->rowCount(); i++) {
259 QModelIndex index = m_model->index(i, 0);
260 DBImgInfo data = index.data(Qt::DisplayRole).value<DBImgInfo>();
261 if (data.filePath == path) {
262 data.image = pix;
263 cutPixmap(data);
264 QVariant meta;
265 meta.setValue(data);
266 // ImageEngineApi::instance()->m_AllImageData[data.filePath].imgpixmap = pix;
267 ImageDataService::instance()->addImage(data.filePath, data.image.toImage());
268 m_model->setData(index, meta, Qt::DisplayRole);
269 break;
270 }
271 }
272 }
273
274 void ImgViewListView::onClicked(const QModelIndex &index)
275 {
276 if (index.row() == m_currentRow) {
277 return;
278 }
279 DBImgInfo info = index.data(Qt::DisplayRole).value<DBImgInfo>();
280 if (info.filePath.isEmpty()) {
281 return;
282 }
283
284 QModelIndex currentIndex = m_model->index(m_currentRow, 0);
285 if (currentIndex.isValid()) {
286 //重置前一个选中项的宽高
287 m_model->setData(currentIndex,
288 QVariant(QSize(ImgViewListView::ITEM_NORMAL_WIDTH, ImgViewListView::ITEM_NORMAL_HEIGHT)), Qt::SizeHintRole);
289 }
290 //重置新选中项的宽高
291 m_model->setData(index,
292 QVariant(QSize(ImgViewListView::ITEM_CURRENT_WH, ImgViewListView::ITEM_CURRENT_WH)), Qt::SizeHintRole);
293
294 m_currentRow = index.row();
295 m_currentPath = info.filePath;
296 qDebug() << "---" << __FUNCTION__ << "---m_currentRow = " << m_currentRow;
297 //刷新界面
298 doItemsLayout();
299 //如果点击的是最后一个则向前移动
300 startMoveToLeftAnimation();
301 //提前加载后面图片缩略图
302 loadFiftyRight();
303
304 emit openImg(m_currentRow, m_currentPath);
305 }
306
307 void ImgViewListView::cutPixmap(DBImgInfo &DBImgInfo)
308 {
309 int width = DBImgInfo.image.width();
310 if (width == 0)
311 width = 180;
312 int height = DBImgInfo.image.height();
313 if (abs((width - height) * 10 / width) >= 1) {
314 QRect rect = DBImgInfo.image.rect();
315 int x = rect.x() + width / 2;
316 int y = rect.y() + height / 2;
317 if (width > height) {
318 x = x - height / 2;
319 y = 0;
320 DBImgInfo.image = DBImgInfo.image.copy(x, y, height, height);
321 } else {
322 y = y - width / 2;
323 x = 0;
324 DBImgInfo.image = DBImgInfo.image.copy(x, y, width, width);
325 }
326 }
327 }
328 //加载后50张
329 void ImgViewListView::loadFiftyRight()
330 {
331 int count = 0;
332 for (int i = m_currentRow; i < m_model->rowCount(); i++) {
333 count++;
334 QModelIndex indexImg = m_model->index(i, 0);
335 DBImgInfo infoImg = indexImg.data(Qt::DisplayRole).value<DBImgInfo>();
336 if (infoImg.image.isNull()) {
337 if (!ImageEngineApi::instance()->m_imgLoaded.contains(infoImg.filePath)) {
338 emit ImageEngineApi::instance()->sigLoadThumbnailIMG(infoImg.filePath);
339 ImageEngineApi::instance()->m_imgLoaded.append(infoImg.filePath);
340 }
341 }
342 if (count == 50) {
343 break;
344 }
345 }
346 }
347
348 void ImgViewListView::startMoveToLeftAnimation()
349 {
350 if (m_moveAnimation == nullptr) {
351 m_moveAnimation = new QPropertyAnimation(this->horizontalScrollBar(), "value", this);
352 }
353
354 m_moveAnimation->setDuration(100);
355 m_moveAnimation->setEasingCurve(QEasingCurve::OutQuad);
356 m_moveAnimation->setStartValue(this->horizontalScrollBar()->value());
357 m_moveAnimation->setEndValue((this->horizontalScrollBar()->value() + 32));
358 //如果点击的是最后一个则向左滑动
359 QRect rect = this->visualRect(m_model->index(m_currentRow, 0));
360 if ((rect.x() + 52) >= (this->width() - 32)) {
361 if (m_moveAnimation->state() == QPropertyAnimation::State::Running) {
362 m_moveAnimation->stop();
363 }
364 m_moveAnimation->start();
365 }
366 }
367
368 const QString ImgViewListView::getPathByRow(int row)
369 {
370 QString result;
371 if (row <= (m_model->rowCount() - 1)) {
372 QModelIndex index = m_model->index(row, 0);
373 if (index.isValid()) {
374 DBImgInfo info = index.data(Qt::DisplayRole).value<DBImgInfo>();
375 result = info.filePath;
376 }
377 }
378 return result;
379 }