| 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 "viewpanel.h" |
| 22 |
#include "application.h" |
| 23 |
#include "controller/configsetter.h" |
| 24 |
#include "controller/wallpapersetter.h" |
| 25 |
#include "navigationwidget.h" |
| 26 |
#include "scen/imageview.h" |
| 27 |
#include "utils/baseutils.h" |
| 28 |
#include "utils/imageutils.h" |
| 29 |
#include "utils/unionimage.h" |
| 30 |
#include "widgets/printhelper.h" |
| 31 |
#include "mainwindow.h" |
| 32 |
#include "allpicview.h" |
| 33 |
#include <DMenu> |
| 34 |
#include <QKeySequence> |
| 35 |
#include <QJsonArray> |
| 36 |
#include <QJsonDocument> |
| 37 |
#include <QShortcut> |
| 38 |
#include <QStyleFactory> |
| 39 |
#include <QFileInfo> |
| 40 |
#include "widgets/dialogs/imgdeletedialog.h" |
| 41 |
|
| 42 |
namespace { |
| 43 |
//LMH 500改200 |
| 44 |
//const int SWITCH_IMAGE_DELAY = 200; //上一张下一张时间间隔 |
| 45 |
const int DELETE_IMAGE_DELAY = 400; //删除时间间隔 |
| 46 |
const QString SHORTCUTVIEW_GROUP = "SHORTCUTVIEW"; |
| 47 |
const int VIEW_MAINWINDOW_POPVIEW = 4; |
| 48 |
|
| 49 |
QString ss(const QString &text, const QString &defaultValue) |
| 50 |
{ |
| 51 |
QString str = dApp->setter->value(SHORTCUTVIEW_GROUP, text, defaultValue).toString(); |
| 52 |
str.replace(" ", ""); |
| 53 |
return str; |
| 54 |
} |
| 55 |
|
| 56 |
enum MenuItemId { |
| 57 |
IdFullScreen, |
| 58 |
IdExitFullScreen, |
| 59 |
IdStartSlideShow, |
| 60 |
IdPrint, |
| 61 |
IdAddToAlbum, |
| 62 |
IdExport, |
| 63 |
IdCopy, |
| 64 |
IdMoveToTrash, |
| 65 |
IdRemoveFromTimeline, |
| 66 |
IdRemoveFromAlbum, |
| 67 |
IdAddToFavorites, |
| 68 |
IdRemoveFromFavorites, |
| 69 |
IdShowNavigationWindow, |
| 70 |
IdHideNavigationWindow, |
| 71 |
IdRotateClockwise, |
| 72 |
IdRotateCounterclockwise, |
| 73 |
IdSetAsWallpaper, |
| 74 |
IdDisplayInFileManager, |
| 75 |
IdImageInfo, |
| 76 |
IdSubMenu |
| 77 |
// IdDrawingBoard//lmh0407 |
| 78 |
}; |
| 79 |
|
| 80 |
} // namespace |
| 81 |
|
| 82 |
void ViewPanel::initPopupMenu() |
| 83 |
{ |
| 84 |
m_menu = new QMenu; |
| 85 |
connect(this, &ViewPanel::customContextMenuRequested, this, [ = ] { |
| 86 |
if (! m_filepathlist.isEmpty() |
| 87 |
#ifdef LITE_DIV |
| 88 |
&& !m_currentpath.isEmpty() |
| 89 |
#endif |
| 90 |
) |
| 91 |
{ |
| 92 |
updateMenuContent(); |
| 93 |
this->setCursor(Qt::ArrowCursor); |
| 94 |
m_menu->popup(QCursor::pos()); |
| 95 |
} |
| 96 |
}); |
| 97 |
connect(m_menu, &DMenu::aboutToHide, this, [ = ] { |
| 98 |
dApp->getDAppNew()->restoreOverrideCursor(); |
| 99 |
}); |
| 100 |
connect(m_menu, &DMenu::triggered, this, &ViewPanel::onMenuItemClicked); |
| 101 |
connect(dApp->setter, &ConfigSetter::valueChanged, this, [ = ] { |
| 102 |
if (/*this && */this->isVisible()) |
| 103 |
{ |
| 104 |
updateMenuContent(); |
| 105 |
} |
| 106 |
}); |
| 107 |
} |
| 108 |
|
| 109 |
void ViewPanel::appendAction(int id, const QString &text, const QString &shortcut) |
| 110 |
{ |
| 111 |
QAction *ac = new QAction(m_menu); |
| 112 |
addAction(ac); |
| 113 |
ac->setText(text); |
| 114 |
ac->setProperty("MenuID", id); |
| 115 |
ac->setShortcut(QKeySequence(shortcut)); |
| 116 |
m_menu->addAction(ac); |
| 117 |
} |
| 118 |
void ViewPanel::appendAction_darkmenu(int id, const QString &text, const QString &shortcut) |
| 119 |
{ |
| 120 |
QAction *ac = new QAction(m_menu); |
| 121 |
addAction(ac); |
| 122 |
ac->setDisabled(true); |
| 123 |
ac->setText(text); |
| 124 |
ac->setProperty("MenuID", id); |
| 125 |
ac->setShortcut(QKeySequence(shortcut)); |
| 126 |
m_menu->addAction(ac); |
| 127 |
} |
| 128 |
|
| 129 |
void ViewPanel::onMenuItemClicked(QAction *action) |
| 130 |
{ |
| 131 |
using namespace utils::base; |
| 132 |
using namespace utils::image; |
| 133 |
|
| 134 |
const QString path1 = m_currentpath; |
| 135 |
const int id = action->property("MenuID").toInt(); |
| 136 |
|
| 137 |
switch (MenuItemId(id)) { |
| 138 |
case IdFullScreen: |
| 139 |
case IdExitFullScreen: |
| 140 |
if (m_bFirstFullScreen) |
| 141 |
emit dApp->signalM->hideImageView(); |
| 142 |
else { |
| 143 |
toggleFullScreen(); |
| 144 |
} |
| 145 |
break; |
| 146 |
case IdStartSlideShow: { |
| 147 |
auto vinfo = m_vinfo; |
| 148 |
vinfo.fullScreen = window()->isFullScreen(); |
| 149 |
// vinfo.lastPanel = this; |
| 150 |
vinfo.lastPanel = nullptr; |
| 151 |
vinfo.path = path1; |
| 152 |
// vinfo.paths = paths(); |
| 153 |
vinfo.paths = m_filepathlist; |
| 154 |
vinfo.viewMainWindowID = VIEW_MAINWINDOW_POPVIEW; |
| 155 |
|
| 156 |
QStringList pathlist; |
| 157 |
pathlist.clear(); |
| 158 |
for (auto path : m_vinfo.paths) { |
| 159 |
if (QFileInfo(path).exists()) { |
| 160 |
pathlist << path; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
m_vinfo.paths = pathlist; |
| 165 |
emit dApp->signalM->startSlideShow(vinfo, m_vinfo.inDatabase); |
| 166 |
emit dApp->signalM->showSlidePanel(VIEW_MAINWINDOW_POPVIEW); |
| 167 |
break; |
| 168 |
} |
| 169 |
case IdPrint: { |
| 170 |
PrintHelper::getIntance()->showPrintDialog(QStringList(path1), this); |
| 171 |
break; |
| 172 |
} |
| 173 |
// case IdDrawingBoard: { |
| 174 |
// emit dApp->signalM->sigDrawingBoard(QStringList(path1)); |
| 175 |
// break; |
| 176 |
// } |
| 177 |
|
| 178 |
#if 1 |
| 179 |
//添加到相册 |
| 180 |
case IdAddToAlbum: { |
| 181 |
const QString album = action->data().toString(); |
| 182 |
if (album != "Add to new album") { |
| 183 |
if (! DBManager::instance()->isImgExistInAlbum(album, path1)) { |
| 184 |
emit dApp->signalM->sigAddToAlbToast(album); |
| 185 |
QStringList paths; |
| 186 |
paths << path1; |
| 187 |
// 相册照片更新时的.更新路径相册名缓存,用于listview的setdata userrole + 2 |
| 188 |
ImageEngineApi::instance()->setImgPathAndAlbumNames(DBManager::instance()->getAllPathAlbumNames()); |
| 189 |
emit SignalManager::instance()->sigSyncListviewModelData(paths, album, IdAddToAlbum); |
| 190 |
} |
| 191 |
DBManager::instance()->insertIntoAlbum(album, QStringList(path1)); |
| 192 |
} else { |
| 193 |
emit dApp->signalM->viewCreateAlbum(path1, false); |
| 194 |
} |
| 195 |
} |
| 196 |
break; |
| 197 |
//收藏 |
| 198 |
case IdAddToFavorites: { |
| 199 |
DBManager::instance()->insertIntoAlbum(COMMON_STR_FAVORITES, QStringList(path1), AlbumDBType::Favourite); |
| 200 |
emit dApp->signalM->insertedIntoAlbum(COMMON_STR_FAVORITES, QStringList(path1)); |
| 201 |
} |
| 202 |
break; |
| 203 |
//取消收藏 |
| 204 |
case IdRemoveFromFavorites: { |
| 205 |
DBManager::instance()->removeFromAlbum(COMMON_STR_FAVORITES, QStringList(path1), AlbumDBType::Favourite); |
| 206 |
} |
| 207 |
break; |
| 208 |
//导出 |
| 209 |
case IdExport: |
| 210 |
emit dApp->signalM->exportImage(QStringList(path1)); |
| 211 |
break; |
| 212 |
#endif |
| 213 |
case IdCopy: |
| 214 |
copyImageToClipboard(QStringList(path1)); |
| 215 |
break; |
| 216 |
case IdMoveToTrash: { |
| 217 |
emit SignalManager::instance()->deleteByMenu(); |
| 218 |
if (m_vinfo.fullScreen == true) { |
| 219 |
emit SignalManager::instance()->hideBottomToolbar(); |
| 220 |
} |
| 221 |
} |
| 222 |
break; |
| 223 |
case IdRemoveFromAlbum: |
| 224 |
DBManager::instance()->removeFromAlbum(m_vinfo.viewType, QStringList(m_currentpath)); |
| 225 |
removeCurrentImage(); |
| 226 |
break; |
| 227 |
case IdShowNavigationWindow: |
| 228 |
m_nav->setAlwaysHidden(false); |
| 229 |
break; |
| 230 |
case IdHideNavigationWindow: |
| 231 |
m_nav->setAlwaysHidden(true); |
| 232 |
break; |
| 233 |
case IdRotateClockwise: |
| 234 |
rotateImage(true); |
| 235 |
break; |
| 236 |
case IdRotateCounterclockwise: |
| 237 |
rotateImage(false); |
| 238 |
break; |
| 239 |
case IdSetAsWallpaper: |
| 240 |
dApp->wpSetter->setBackground(path1); |
| 241 |
break; |
| 242 |
case IdDisplayInFileManager: |
| 243 |
emit dApp->signalM->showInFileManager(path1); |
| 244 |
break; |
| 245 |
case IdImageInfo: |
| 246 |
dApp->signalM->showInfoDlg(path1); |
| 247 |
break; |
| 248 |
default: |
| 249 |
break; |
| 250 |
} |
| 251 |
|
| 252 |
updateMenuContent(); |
| 253 |
} |
| 254 |
|
| 255 |
void ViewPanel::updateMenuContent() |
| 256 |
{ |
| 257 |
m_menu->clear(); |
| 258 |
qDeleteAll(this->actions()); |
| 259 |
|
| 260 |
if (m_vinfo.viewType.compare(COMMON_STR_TRASH) == 0) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if (m_filepathlist.isEmpty()) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
// 如果该图片原文件不存在,则不显示右键菜单 |
| 270 |
if (!QFileInfo(m_currentpath).exists()) { |
| 271 |
return; |
| 272 |
} |
| 273 |
#ifndef tablet_PC |
| 274 |
if (window()->isFullScreen()) { |
| 275 |
appendAction(IdExitFullScreen, tr("Exit fullscreen"), ss("Fullscreen", "F11")); |
| 276 |
} else { |
| 277 |
appendAction(IdFullScreen, tr("Fullscreen"), ss("Fullscreen", "F11")); |
| 278 |
} |
| 279 |
appendAction(IdPrint, tr("Print"), ss("Print", "Ctrl+P")); |
| 280 |
appendAction(IdStartSlideShow, tr("Slide show"), ss("Slide show", "F5")); |
| 281 |
#ifndef LITE_DIV |
| 282 |
appendAction(IdStartSlideShow, tr("Slide show"), ss("Slide show")); |
| 283 |
#endif |
| 284 |
// appendAction(IdPrint, tr("Print"), ss("Print", "Ctrl+P")); |
| 285 |
#ifndef LITE_DIV |
| 286 |
if (m_vinfo.inDatabase) { |
| 287 |
DMenu *am = createAlbumMenu(); |
| 288 |
if (am) { |
| 289 |
m_menu->addMenu(am); |
| 290 |
} |
| 291 |
} |
| 292 |
#endif |
| 293 |
m_menu->addSeparator(); |
| 294 |
/**************************************************************************/ |
| 295 |
#endif |
| 296 |
m_menu->addMenu(createAblumMenu()); //添加到相册 |
| 297 |
#ifndef tablet_PC |
| 298 |
appendAction(IdExport, tr("Export"), ss("Export", "Ctrl+E")); //导出 |
| 299 |
#endif |
| 300 |
appendAction(IdCopy, tr("Copy"), ss("Copy", "Ctrl+C")); |
| 301 |
if (COMMON_STR_TRASH == m_vinfo.viewType) { |
| 302 |
// appendAction(IdMoveToTrash, tr("Delete"), ss("Throw to trash", "Delete")); |
| 303 |
} else { |
| 304 |
appendAction(IdMoveToTrash, tr("Delete"), ss("Throw to trash", "Delete")); |
| 305 |
} |
| 306 |
// 移除多余判断,采用相册名是否有效来判断 |
| 307 |
if (!m_vinfo.album.isEmpty()) { |
| 308 |
appendAction(IdRemoveFromAlbum, tr("Remove from album"), ss("Remove from album", "")); |
| 309 |
} |
| 310 |
m_menu->addSeparator(); |
| 311 |
/**************************************************************************/ |
| 312 |
if (DBManager::instance()->isImgExistInAlbum(COMMON_STR_FAVORITES, m_currentpath, AlbumDBType::Favourite)) { |
| 313 |
appendAction(IdRemoveFromFavorites, tr("Unfavorite"), ""); //取消收藏 |
| 314 |
} else { |
| 315 |
appendAction(IdAddToFavorites, tr("Favorite"), ""); //收藏 |
| 316 |
} |
| 317 |
#ifndef tablet_PC |
| 318 |
m_menu->addSeparator(); |
| 319 |
if (! m_viewB->isWholeImageVisible() && m_nav->isAlwaysHidden()) { |
| 320 |
appendAction(IdShowNavigationWindow, |
| 321 |
tr("Show navigation window"), ss("Show navigation window", "")); |
| 322 |
} else if (! m_viewB->isWholeImageVisible() && !m_nav->isAlwaysHidden()) { |
| 323 |
appendAction(IdHideNavigationWindow, |
| 324 |
tr("Hide navigation window"), ss("Hide navigation window", "")); |
| 325 |
} |
| 326 |
/**************************************************************************/ |
| 327 |
if (UnionImage_NameSpace::isImageSupportRotate(m_currentpath)) { |
| 328 |
m_menu->addSeparator(); |
| 329 |
if (QFileInfo(m_currentpath).isReadable() && |
| 330 |
!QFileInfo(m_currentpath).isWritable()) { |
| 331 |
appendAction_darkmenu(IdRotateClockwise, tr("Rotate clockwise"), ss("Rotate clockwise", "Ctrl+R")); |
| 332 |
appendAction_darkmenu(IdRotateCounterclockwise, tr("Rotate counterclockwise"), ss("Rotate counterclockwise", "Ctrl+Shift+R")); |
| 333 |
} else { |
| 334 |
appendAction(IdRotateClockwise, tr("Rotate clockwise"), ss("Rotate clockwise", "Ctrl+R")); |
| 335 |
appendAction(IdRotateCounterclockwise, tr("Rotate counterclockwise"), ss("Rotate counterclockwise", "Ctrl+Shift+R")); |
| 336 |
} |
| 337 |
} |
| 338 |
/**************************************************************************/ |
| 339 |
appendAction(IdSetAsWallpaper, tr("Set as wallpaper"), ss("Set as wallpaper", "Ctrl+F9")); |
| 340 |
appendAction(IdDisplayInFileManager, tr("Display in file manager"), ss("Display in file manager", "Alt+D")); |
| 341 |
appendAction(IdImageInfo, tr("Photo info"), ss("Photo info", "Ctrl+I")); |
| 342 |
#endif |
| 343 |
} |
| 344 |
|
| 345 |
DMenu *ViewPanel::createAblumMenu() |
| 346 |
{ |
| 347 |
DMenu *am = new DMenu(tr("Add to album")); |
| 348 |
|
| 349 |
QStringList albums = DBManager::instance()->getAllAlbumNames(); |
| 350 |
albums.removeAll(COMMON_STR_FAVORITES); |
| 351 |
albums.removeAll(COMMON_STR_TRASH); |
| 352 |
albums.removeAll(COMMON_STR_RECENT_IMPORTED); |
| 353 |
|
| 354 |
QAction *ac1 = new QAction(am); |
| 355 |
ac1->setProperty("MenuID", IdAddToAlbum); |
| 356 |
ac1->setText(tr("New album")); |
| 357 |
ac1->setData("Add to new album"); |
| 358 |
ac1->setShortcut(QKeySequence("Ctrl+Shift+N")); |
| 359 |
am->addAction(ac1); |
| 360 |
am->addSeparator(); |
| 361 |
|
| 362 |
QStringList albumNames; |
| 363 |
// 增加外部打开图片时主界面未创建时判断逻辑 |
| 364 |
if (albums.count() > 0 && dApp->getMainWindow() != nullptr) { |
| 365 |
QStandardItemModel *pTempModel = dApp->getMainWindow()->m_pAllPicView->getAllPicThumbnailListViewModel()->m_model; |
| 366 |
for (int i = 0; i < pTempModel->rowCount(); i++) { |
| 367 |
QModelIndex idx = pTempModel->index(i, 0); |
| 368 |
DBImgInfo info = idx.data(Qt::DisplayRole).value<DBImgInfo>(); |
| 369 |
if (info.filePath == m_currentpath) { |
| 370 |
albumNames = idx.model()->data(idx, Qt::UserRole + 2).toStringList(); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
for (QString album : albums) { |
| 375 |
QAction *ac = new QAction(am); |
| 376 |
ac->setProperty("MenuID", IdAddToAlbum); |
| 377 |
ac->setText(fontMetrics().elidedText(QString(album).replace("&", "&&"), Qt::ElideMiddle, 200)); |
| 378 |
ac->setData(album); |
| 379 |
am->addAction(ac); |
| 380 |
if (albumNames.contains(album)) { |
| 381 |
ac->setEnabled(false); |
| 382 |
} |
| 383 |
} |
| 384 |
return am; |
| 385 |
} |
| 386 |
|
| 387 |
void ViewPanel::initShortcut() |
| 388 |
{ |
| 389 |
// Delay image toggle delete |
| 390 |
m_deletetimer = new QTimer(this); |
| 391 |
m_deletetimer->setSingleShot(true); |
| 392 |
m_deletetimer->setInterval(DELETE_IMAGE_DELAY); |
| 393 |
|
| 394 |
QShortcut *sc = nullptr; |
| 395 |
// Previous |
| 396 |
sc = new QShortcut(QKeySequence(Qt::Key_Left), this); |
| 397 |
sc->setContext(Qt::WindowShortcut); |
| 398 |
connect(sc, &QShortcut::activated, this, [ = ] { |
| 399 |
// if (! m_dt->isActive()) |
| 400 |
// { |
| 401 |
// dt->start(); |
| 402 |
m_ttbc->onPreButton(); |
| 403 |
// } |
| 404 |
}); |
| 405 |
// Next |
| 406 |
sc = new QShortcut(QKeySequence(Qt::Key_Right), this); |
| 407 |
sc->setContext(Qt::WindowShortcut); |
| 408 |
connect(sc, &QShortcut::activated, this, [ = ] { |
| 409 |
m_ttbc->onNextButton(); |
| 410 |
}); |
| 411 |
|
| 412 |
// Zoom out (Ctrl++ Not working, This is a confirmed bug in Qt 5.5.0) |
| 413 |
connect(dApp->signalM, &SignalManager::sigCtrlADDKeyActivated, this, [ = ] { |
| 414 |
m_viewB->setScaleValue(1.1); |
| 415 |
}); |
| 416 |
|
| 417 |
connect(dApp->signalM, &SignalManager::sigCtrlSubtractKeyActivated, this, [ = ] { |
| 418 |
m_viewB->setScaleValue(0.9); |
| 419 |
}); |
| 420 |
|
| 421 |
//1:1 size |
| 422 |
QShortcut *adaptImage = new QShortcut(QKeySequence("Ctrl+0"), this); |
| 423 |
adaptImage->setContext(Qt::WindowShortcut); |
| 424 |
connect(adaptImage, &QShortcut::activated, this, [ = ] { |
| 425 |
m_viewB->fitImage(); |
| 426 |
}); |
| 427 |
|
| 428 |
} |
| 429 |
|
| 430 |
void ViewPanel::keyPressEvent(QKeyEvent *e) |
| 431 |
{ |
| 432 |
if (e->key() == Qt::Key_Period) { |
| 433 |
if (!DBManager::instance()->isImgExistInAlbum(COMMON_STR_FAVORITES, m_currentpath, AlbumDBType::Favourite)) { |
| 434 |
DBManager::instance()->insertIntoAlbum(COMMON_STR_FAVORITES, QStringList(m_currentpath), AlbumDBType::Favourite); |
| 435 |
emit dApp->signalM->insertedIntoAlbum(COMMON_STR_FAVORITES, QStringList(m_currentpath)); |
| 436 |
} else { |
| 437 |
DBManager::instance()->removeFromAlbum(COMMON_STR_FAVORITES, QStringList(m_currentpath), AlbumDBType::Favourite); |
| 438 |
} |
| 439 |
} |
| 440 |
} |