1 Star 0 Fork 0

开源应用/phototonic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
thumbview.cpp 24.67 KB
一键复制 编辑 原始数据 按行查看 历史
弯弯 提交于 2021-06-28 18:01 . first commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
/*
* Copyright (C) 2013-2014 Ofer Kashayov <[email protected]>
* This file is part of Phototonic Image Viewer.
*
* Phototonic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Phototonic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Phototonic. If not, see <http://www.gnu.org/licenses/>.
*/
#include "thumbview.h"
ThumbView::ThumbView(QWidget *parent, MetadataCache *mdCache) : QListView(parent)
{
this->mdCache = mdCache;
GData::thumbsBackgroundColor = GData::appSettings->value("backgroundThumbColor").value<QColor>();
GData::thumbsTextColor = GData::appSettings->value("textThumbColor").value<QColor>();
setThumbColors();
GData::thumbSpacing = GData::appSettings->value("thumbSpacing").toInt();
GData::thumbPagesReadahead = GData::appSettings->value("thumbPagesReadahead").toInt();
GData::thumbsLayout = GData::appSettings->value("thumbLayout").toInt();
thumbSize = GData::appSettings->value("thumbsZoomVal").toInt();
currentRow = 0;
setViewMode(QListView::IconMode);
setSelectionMode(QAbstractItemView::ExtendedSelection);
setResizeMode(QListView::Adjust);
setWordWrap(true);
setDragEnabled(true);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setItemDelegate(new QItemDelegate);
setUniformItemSizes(false);
thumbViewModel = new QStandardItemModel(this);
thumbViewModel->setSortRole(SortRole);
setModel(thumbViewModel);
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(loadVisibleThumbs(int)));
connect(this->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this, SLOT(handleSelectionChanged(QItemSelection)));
connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
parent, SLOT(loadImagefromThumb(const QModelIndex &)));
connect(this->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
parent, SLOT(updateViewerImageBySelection(QItemSelection)));
thumbsDir = new QDir();
fileFilters = new QStringList;
emptyImg.load(":/images/no_image.png");
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
mainWindow = parent;
infoView = new InfoView(this);
}
void ThumbView::setThumbColors()
{
QString bgColor = "background: rgb(%1, %2, %3); ";
bgColor = bgColor.arg(GData::thumbsBackgroundColor.red())
.arg(GData::thumbsBackgroundColor.green())
.arg(GData::thumbsBackgroundColor.blue());
QString ss = "QListView { " + bgColor + "background-image: url("
+ GData::thumbsBackImage
+ "); background-attachment: fixed; }";
setStyleSheet(ss);
QPalette scrollBarOrigPal = verticalScrollBar()->palette();
QPalette thumbViewOrigPal = palette();
thumbViewOrigPal.setColor(QPalette::Text, GData::thumbsTextColor);
setPalette(thumbViewOrigPal);
verticalScrollBar()->setPalette(scrollBarOrigPal);
}
void ThumbView::selectCurrentIndex()
{
if (currentIndex.isValid() && thumbViewModel->rowCount() > 0)
{
scrollTo(currentIndex);
setCurrentIndex(currentIndex);
}
}
QString ThumbView::getSingleSelectionFilename()
{
if (selectionModel()->selectedIndexes().size() == 1)
return thumbViewModel->item(selectionModel()->selectedIndexes().first().row())->data(FileNameRole).toString();
return("");
}
int ThumbView::getNextRow()
{
if (currentRow == thumbViewModel->rowCount() - 1)
return -1;
return currentRow + 1;
}
int ThumbView::getPrevRow()
{
if (currentRow == 0)
return -1;
return currentRow - 1;
}
int ThumbView::getLastRow()
{
return thumbViewModel->rowCount() - 1;
}
int ThumbView::getRandomRow()
{
return qrand() % (thumbViewModel->rowCount());
}
int ThumbView::getCurrentRow()
{
return currentRow;
}
void ThumbView::setCurrentRow(int row)
{
if (row >= 0)
currentRow = row;
else
currentRow = 0;
}
void ThumbView::setImageviewWindowTitle()
{
QString title = thumbViewModel->item(currentRow)->data(FileNameRole).toString()
+ " - ["
+ QString::number(currentRow + 1)
+ "/"
+ QString::number(thumbViewModel->rowCount())
+ "] - Phototonic";
mainWindow->setWindowTitle(title);
}
bool ThumbView::setCurrentIndexByName(QString &FileName)
{
QModelIndexList indexList = thumbViewModel->match(thumbViewModel->index(0, 0), FileNameRole, FileName);
if (indexList.size())
{
currentIndex = indexList[0];
setCurrentRow(currentIndex.row());
setRowHidden(currentIndex.row(), false);
return true;
}
return false;
}
bool ThumbView::setCurrentIndexByRow(int row)
{
QModelIndex idx = thumbViewModel->indexFromItem(thumbViewModel->item(row));
if (idx.isValid())
{
currentIndex = idx;
setCurrentRow(idx.row());
return true;
}
return false;
}
void ThumbView::updateExifInfo(QString imageFullPath)
{
Exiv2::Image::AutoPtr exifImage;
QString key;
QString val;
try {
exifImage = Exiv2::ImageFactory::open(imageFullPath.toStdString());
exifImage->readMetadata();
}
catch (Exiv2::Error &error) {
return;
}
Exiv2::ExifData &exifData = exifImage->exifData();
if (!exifData.empty()) {
Exiv2::ExifData::const_iterator end = exifData.end();
infoView->addTitleEntry("Exif");
for (Exiv2::ExifData::const_iterator md = exifData.begin(); md != end; ++md) {
key = QString::fromUtf8(md->tagName().c_str());
val = QString::fromUtf8(md->print().c_str());
infoView->addEntry(key, val);
}
}
Exiv2::IptcData &iptcData = exifImage->iptcData();
if (!iptcData.empty()) {
Exiv2::IptcData::iterator end = iptcData.end();
infoView->addTitleEntry("IPTC");
for (Exiv2::IptcData::iterator md = iptcData.begin(); md != end; ++md) {
key = QString::fromUtf8(md->tagName().c_str());
val = QString::fromUtf8(md->print().c_str());
infoView->addEntry(key, val);
}
}
Exiv2::XmpData &xmpData = exifImage->xmpData();
if (!xmpData.empty()) {
Exiv2::XmpData::iterator end = xmpData.end();
infoView->addTitleEntry("XMP");
for (Exiv2::XmpData::iterator md = xmpData.begin(); md != end; ++md) {
key = QString::fromUtf8(md->tagName().c_str());
val = QString::fromUtf8(md->print().c_str());
infoView->addEntry(key, val);
}
}
}
void ThumbView::handleSelectionChanged(const QItemSelection&)
{
QString info;
QModelIndexList indexesList = selectionModel()->selectedIndexes();
int nSelected = indexesList.size();
QString imageFullPath;
QString statusStr;
infoView->clear();
if (nSelected == 1) {
QString imageFullPath = thumbViewModel->item(indexesList.first().row())->data(FileNameRole).toString();
imageInfoReader.setFileName(imageFullPath);
QString key;
QString val;
QFileInfo imageInfo = QFileInfo(imageFullPath);
infoView->addTitleEntry(tr("General"));
key = tr("File name");
val = imageInfo.fileName();
infoView->addEntry(key, val);
key = tr("Location");
val = imageInfo.path();
infoView->addEntry(key, val);
key = tr("Size");
val = QString::number(imageInfo.size() / 1024.0, 'f', 2) + "K";
infoView->addEntry(key, val);
key = tr("Modified");
val = imageInfo.lastModified().toString(Qt::SystemLocaleShortDate);
infoView->addEntry(key, val);
if (imageInfoReader.size().isValid()) {
key = tr("Format");
val = imageInfoReader.format().toUpper();
infoView->addEntry(key, val);
key = tr("Resolution");
val = QString::number(imageInfoReader.size().width())
+ "x"
+ QString::number(imageInfoReader.size().height());
infoView->addEntry(key, val);
key = tr("Megapixel");
val = QString::number((imageInfoReader.size().width() * imageInfoReader.size().height()) / 1000000.0, 'f', 2);
infoView->addEntry(key, val);
updateExifInfo(imageFullPath);
recentThumb = imageFullPath;
} else {
imageInfoReader.read();
key = tr("Error");
val = imageInfoReader.errorString();
infoView->addEntry(key, val);
}
}
if (imageTags->currentDisplayMode == SelectionTagsDisplay) {
imageTags->showSelectedImagesTags();
}
/* update status bar */
if (!nSelected) {
updateThumbsCount();
return;
} else if (nSelected >= 1) {
statusStr = tr("Selected %1 of%2")
.arg(QString::number(nSelected))
.arg(tr(" %n image(s)", "", thumbViewModel->rowCount()));
}
emit setStatus(statusStr);
}
QStringList ThumbView::getSelectedThumbsList()
{
QModelIndexList indexesList = selectionModel()->selectedIndexes();
QStringList SelectedThumbsPaths;
for (int tn = indexesList.size() - 1; tn >= 0 ; --tn) {
SelectedThumbsPaths <<
thumbViewModel->item(indexesList[tn].row())->data(FileNameRole).toString();
}
return SelectedThumbsPaths;
}
void ThumbView::startDrag(Qt::DropActions)
{
QModelIndexList indexesList = selectionModel()->selectedIndexes();
if (indexesList.isEmpty()) {
return;
}
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QList<QUrl> urls;
for (QModelIndexList::const_iterator it = indexesList.constBegin(),
end = indexesList.constEnd(); it != end; ++it)
{
urls << QUrl(thumbViewModel->item(it->row())->data(FileNameRole).toString());
}
mimeData->setUrls(urls);
drag->setMimeData(mimeData);
QPixmap pix;
if (indexesList.count() > 1) {
pix = QPixmap(128, 112);
pix.fill(Qt::transparent);
QPainter painter(&pix);
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(Qt::white, 2));
int x = 0, y = 0, xMax = 0, yMax = 0;
for (int i = 0; i < qMin(5, indexesList.count()); ++i) {
QPixmap pix = thumbViewModel->item(indexesList.at(i).row())->icon().pixmap(72);
if (i == 4) {
x = (xMax - pix.width()) / 2;
y = (yMax - pix.height()) / 2;
}
painter.drawPixmap(x, y, pix);
xMax = qMax(xMax, qMin(128, x + pix.width()));
yMax = qMax(yMax, qMin(112, y + pix.height()));
painter.drawRect(x + 1, y + 1, qMin(126, pix.width() - 2), qMin(110, pix.height() - 2));
x = !(x == y) * 56;
y = !y * 40;
}
painter.end();
pix = pix.copy(0, 0, xMax, yMax);
drag->setPixmap(pix);
} else {
pix = thumbViewModel->item(indexesList.at(0).row())->icon().pixmap(128);
drag->setPixmap(pix);
}
drag->setHotSpot(QPoint(pix.width() / 2, pix.height() / 2));
drag->exec(Qt::CopyAction | Qt::MoveAction | Qt::LinkAction, Qt::IgnoreAction);
}
void ThumbView::abort()
{
abortOp = true;
}
void ThumbView::loadVisibleThumbs(int scrollBarValue)
{
static int lastScrollBarValue = 0;
if (GData::thumbsLayout == Compact) {
scrolledForward = true;
} else {
scrolledForward = (scrollBarValue >= lastScrollBarValue);
}
lastScrollBarValue = scrollBarValue;
Start:
int firstVisible = getFirstVisibleThumb();
int lastVisible = getLastVisibleThumb();
if (abortOp || firstVisible < 0 || lastVisible < 0) {
return;
}
if (scrolledForward) {
lastVisible += ((lastVisible - firstVisible) * (GData::thumbPagesReadahead + 1));
if (lastVisible >= thumbViewModel->rowCount()) {
lastVisible = thumbViewModel->rowCount() - 1;
}
} else {
firstVisible -= (lastVisible - firstVisible) * (GData::thumbPagesReadahead + 1);
if (firstVisible < 0) {
firstVisible = 0;
}
lastVisible += 10;
if (lastVisible >= thumbViewModel->rowCount()) {
lastVisible = thumbViewModel->rowCount() - 1;
}
}
if (thumbsRangeFirst == firstVisible && thumbsRangeLast == lastVisible) {
return;
}
thumbsRangeFirst = firstVisible;
thumbsRangeLast = lastVisible;
loadThumbsRange();
if (!abortOp) {
goto Start;
}
}
int ThumbView::getFirstVisibleThumb()
{
QModelIndex idx;
for (int currThumb = 0; currThumb < thumbViewModel->rowCount(); ++currThumb) {
idx = thumbViewModel->indexFromItem(thumbViewModel->item(currThumb));
if (viewport()->rect().contains(QPoint(0, visualRect(idx).y() + visualRect(idx).height() + 1))) {
return idx.row();
}
}
return -1;
}
int ThumbView::getLastVisibleThumb()
{
QModelIndex idx;
for (int currThumb = thumbViewModel->rowCount() -1; currThumb >= 0 ; --currThumb) {
idx = thumbViewModel->indexFromItem(thumbViewModel->item(currThumb));
if (viewport()->rect().contains(QPoint(0, visualRect(idx).y() + visualRect(idx).height() + 1))) {
return idx.row();
}
}
return -1;
}
bool ThumbView::isThumbVisible(QModelIndex idx)
{
if (viewport()->rect().contains(QPoint(0, visualRect(idx).y() + visualRect(idx).height() + 1))) {
return true;
}
return false;
}
void ThumbView::updateThumbsCount()
{
QString state ;
if (thumbViewModel->rowCount() > 0) {
state = tr("%n image(s)", "", thumbViewModel->rowCount());
} else {
state = tr("No images");
}
thumbsDir->setPath(GData::currentViewDir);
emit setStatus(state);
}
void ThumbView::loadPrepare()
{
float thumbAspect = 1.33;
if (GData::thumbsLayout == Compact) {
thumbAspect = 1.77;
} else if (GData::thumbsLayout == Squares) {
thumbAspect = 2;
}
thumbHeight = (GData::thumbsLayout == Squares)? thumbSize * thumbAspect : thumbSize;
thumbWidth = (GData::thumbsLayout == Squares)? thumbSize * thumbAspect : thumbHeight * thumbAspect;
setIconSize(QSize(thumbWidth, thumbHeight));
fileFilters->clear();
QString textFilter("*");
textFilter+= filterStr;
*fileFilters << textFilter + "*.bmp"
<< textFilter + "*.cur"
<< textFilter + "*.dds"
<< textFilter + "*.gif"
<< textFilter + "*.icns"
<< textFilter + "*.ico"
<< textFilter + "*.jpeg"
<< textFilter + "*.jpg"
<< textFilter + "*.jp2"
<< textFilter + "*.jpe"
<< textFilter + "*.mng"
<< textFilter + "*.pbm"
<< textFilter + "*.pgm"
<< textFilter + "*.png"
<< textFilter + "*.ppm"
<< textFilter + "*.svg"
<< textFilter + "*.svgz"
<< textFilter + "*.tga"
<< textFilter + "*.tif"
<< textFilter + "*.tiff"
<< textFilter + "*.wbmp"
<< textFilter + "*.webp"
<< textFilter + "*.xbm"
<< textFilter + "*.xpm";
thumbsDir->setNameFilters(*fileFilters);
thumbsDir->setFilter(QDir::Files);
if (GData::showHiddenFiles) {
thumbsDir->setFilter(thumbsDir->filter() | QDir::Hidden);
}
thumbsDir->setPath(GData::currentViewDir);
QDir::SortFlags tempThumbsSortFlags = thumbsSortFlags;
if (tempThumbsSortFlags & QDir::Size || tempThumbsSortFlags & QDir::Time) {
tempThumbsSortFlags ^= QDir::Reversed;
}
thumbsDir->setSorting(tempThumbsSortFlags);
thumbViewModel->clear();
setSpacing(GData::thumbSpacing);
if (isNeedScroll) {
scrollToTop();
}
abortOp = false;
newIndex = 0;
thumbsRangeFirst = -1;
thumbsRangeLast = -1;
imageTags->resetTagsState();
}
void ThumbView::load()
{
emit showBusy(true);
loadPrepare();
initThumbs();
updateThumbsCount();
loadVisibleThumbs();
if (GData::includeSubFolders) {
QDirIterator iterator(GData::currentViewDir, QDirIterator::Subdirectories);
while (iterator.hasNext()) {
iterator.next();
if (iterator.fileInfo().isDir() && iterator.fileName() != "." && iterator.fileName() != "..") {
thumbsDir->setPath(iterator.filePath());
initThumbs();
updateThumbsCount();
loadVisibleThumbs();
if (abortOp) {
goto finish;
}
}
QApplication::processEvents();
}
QItemSelection dummy;
handleSelectionChanged(dummy);
}
finish:
emit showBusy(false);
busy = false;
return;
}
void ThumbView::loadDuplicates()
{
loadPrepare();
emit showBusy(true);
emit setStatus(tr("Searching duplicate images..."));
dupImageHashes.clear();
findDupes(true);
if (GData::includeSubFolders) {
QDirIterator iterator(GData::currentViewDir, QDirIterator::Subdirectories);
while (iterator.hasNext()) {
iterator.next();
if (iterator.fileInfo().isDir() && iterator.fileName() != "." && iterator.fileName() != "..") {
thumbsDir->setPath(iterator.filePath());
findDupes(false);
if (abortOp) {
goto finish;
}
}
QApplication::processEvents();
}
}
finish:
busy = false;
emit showBusy(false);
return;
}
void ThumbView::initThumbs()
{
thumbFileInfoList = thumbsDir->entryInfoList();
static QStandardItem *thumbIitem;
static int fileIndex;
static QPixmap emptyPixMap;
static QSize hintSize;
int processEventsCounter = 0;
emptyPixMap = QPixmap::fromImage(emptyImg).scaled(thumbWidth, thumbHeight);
if (GData::thumbsLayout == Squares) {
hintSize = QSize(thumbWidth / 2, thumbWidth / 2);
} else if (GData::thumbsLayout == Classic) {
hintSize = QSize(thumbWidth, thumbHeight +
(GData::showLabels? QFontMetrics(font()).height() + 5 : 0));
}
for (fileIndex = 0; fileIndex < thumbFileInfoList.size(); ++fileIndex) {
thumbFileInfo = thumbFileInfoList.at(fileIndex);
mdCache->loadImageMetadata(thumbFileInfo.filePath());
if (imageTags->folderFilteringActive && imageTags->isImageFilteredOut(thumbFileInfo.filePath())) {
continue;
}
thumbIitem = new QStandardItem();
thumbIitem->setData(false, LoadedRole);
thumbIitem->setData(fileIndex, SortRole);
thumbIitem->setData(thumbFileInfo.filePath(), FileNameRole);
if (GData::thumbsLayout != Squares && GData::showLabels) {
thumbIitem->setData(thumbFileInfo.fileName(), Qt::DisplayRole);
}
if (GData::thumbsLayout == Compact) {
thumbIitem->setIcon(emptyPixMap);
}
thumbIitem->setTextAlignment(Qt::AlignTop | Qt::AlignHCenter);
if (GData::thumbsLayout != Compact) {
thumbIitem->setSizeHint(hintSize);
}
thumbViewModel->appendRow(thumbIitem);
++processEventsCounter;
if (processEventsCounter > 100) {
processEventsCounter = 0;
QApplication::processEvents();
}
}
imageTags->populateTagsTree();
if (imageTags->currentDisplayMode == SelectionTagsDisplay) {
imageTags->showSelectedImagesTags();
} else {
imageTags->showTagsFilter();
}
if (thumbFileInfoList.size() && selectionModel()->selectedIndexes().size() == 0) {
selectThumbByRow(0);
}
}
void ThumbView::selectThumbByRow(int row)
{
setCurrentIndexByRow(row);
selectCurrentIndex();
}
void ThumbView::updateFoundDupesState(int duplicates, int filesScanned, int originalImages)
{
QString state;
state = tr("Scanned %1, displaying %2 (%3 and %4)")
.arg(tr("%n image(s)", "", filesScanned))
.arg(tr("%n image(s)", "", originalImages + duplicates))
.arg(tr("%n original(s)", "", originalImages))
.arg(tr("%n duplicate(s)", "", duplicates));
emit setStatus(state);
}
void ThumbView::findDupes(bool resetCounters)
{
thumbFileInfoList = thumbsDir->entryInfoList();
int processEventsCounter = 0;
static int originalImages;
static int foundDups;
static int totalFiles;
if (resetCounters) {
originalImages = totalFiles = foundDups = 0;
}
for (int currThumb = 0; currThumb < thumbFileInfoList.size(); ++currThumb) {
thumbFileInfo = thumbFileInfoList.at(currThumb);
QCryptographicHash md5gen(QCryptographicHash::Md5);
QString currentFilePath = thumbFileInfo.filePath();
QFile file(currentFilePath);
if (!file.open(QIODevice::ReadOnly)) {
continue;
}
totalFiles++;
md5gen.addData(file.readAll());
file.close();
QString md5 = md5gen.result().toHex();
if (dupImageHashes.contains(md5)) {
if (dupImageHashes[md5].duplicates < 1) {
addThumb(dupImageHashes[md5].filePath);
originalImages++;
}
foundDups++;
dupImageHashes[md5].duplicates++;
addThumb(currentFilePath);
} else {
DuplicateImage dupImage;
dupImage.filePath = currentFilePath;
dupImage.duplicates = 0;
dupImageHashes.insert(md5, dupImage);
}
++processEventsCounter;
if (processEventsCounter > 9) {
processEventsCounter = 0;
QApplication::processEvents();
}
updateFoundDupesState(foundDups, totalFiles, originalImages);
if (abortOp) {
break;
}
}
updateFoundDupesState(foundDups, totalFiles, originalImages);
}
void ThumbView::loadThumbsRange()
{
static bool inProgress = false;
static QImageReader thumbReader;
static QSize currThumbSize;
static int currRowCount;
static QString imageFileName;
QImage thumb;
int currThumb;
bool imageReadOk;
if (inProgress) {
abortOp = true;
QTimer::singleShot(0, this, SLOT(loadThumbsRange()));
return;
}
inProgress = true;
currRowCount = thumbViewModel->rowCount();
for ( scrolledForward? currThumb = thumbsRangeFirst : currThumb = thumbsRangeLast;
(scrolledForward? currThumb <= thumbsRangeLast : currThumb >= thumbsRangeFirst);
scrolledForward? ++currThumb : --currThumb) {
if (abortOp || thumbViewModel->rowCount() != currRowCount || currThumb < 0) {
break;
}
if (thumbViewModel->item(currThumb)->data(LoadedRole).toBool()) {
continue;
}
imageFileName = thumbViewModel->item(currThumb)->data(FileNameRole).toString();
thumbReader.setFileName(imageFileName);
currThumbSize = thumbReader.size();
imageReadOk = false;
if (currThumbSize.isValid()) {
if (!GData::noEnlargeSmallThumb
|| (currThumbSize.width() > thumbWidth || currThumbSize.height() > thumbHeight))
{
currThumbSize.scale(QSize(thumbWidth, thumbHeight), Qt::KeepAspectRatio);
}
thumbReader.setScaledSize(currThumbSize);
imageReadOk = thumbReader.read(&thumb);
}
if (imageReadOk) {
if (GData::exifThumbRotationEnabled) {
imageView->rotateByExifRotation(thumb, imageFileName);
currThumbSize = thumb.size();
currThumbSize.scale(QSize(thumbWidth, thumbHeight), Qt::KeepAspectRatio);
}
thumbViewModel->item(currThumb)->setIcon(QPixmap::fromImage(thumb));
} else {
thumbViewModel->item(currThumb)->setIcon(QIcon::fromTheme("image-missing",
QIcon(":/images/error_image.png")).pixmap(BAD_IMG_SZ, BAD_IMG_SZ));
currThumbSize.setHeight(BAD_IMG_SZ);
currThumbSize.setWidth(BAD_IMG_SZ);
}
if (GData::thumbsLayout == Compact) {
if (GData::showLabels)
currThumbSize.setHeight(currThumbSize.height() + QFontMetrics(font()).height() + 5);
thumbViewModel->item(currThumb)->setSizeHint(currThumbSize);
if (isThumbVisible(thumbViewModel->item(currThumb)->index()))
setRowHidden(currThumb, false);
}
thumbViewModel->item(currThumb)->setData(true, LoadedRole);
QApplication::processEvents();
}
if (GData::thumbsLayout == Compact && thumbViewModel->rowCount() > 0) {
setRowHidden(0 , false);
}
inProgress = false;
abortOp = false;
}
void ThumbView::addThumb(QString &imageFullPath)
{
QStandardItem *thumbIitem = new QStandardItem();
QImageReader thumbReader;
QSize hintSize;
QSize currThumbSize;
static QImage thumb;
if (GData::thumbsLayout == Squares)
hintSize = QSize(thumbWidth / 2, thumbWidth / 2);
else if (GData::thumbsLayout == Classic)
hintSize = QSize(thumbWidth, thumbHeight +
(GData::showLabels? QFontMetrics(font()).height() + 5 : 0));
thumbFileInfo = QFileInfo(imageFullPath);
thumbIitem->setData(true, LoadedRole);
thumbIitem->setData(0, SortRole);
thumbIitem->setData(thumbFileInfo.filePath(), FileNameRole);
if (GData::thumbsLayout != Squares && GData::showLabels)
thumbIitem->setData(thumbFileInfo.fileName(), Qt::DisplayRole);
thumbReader.setFileName(imageFullPath);
currThumbSize = thumbReader.size();
if (currThumbSize.isValid())
{
if (!GData::noEnlargeSmallThumb
|| (currThumbSize.width() > thumbWidth || currThumbSize.height() > thumbHeight))
{
currThumbSize.scale(QSize(thumbWidth, thumbHeight), Qt::KeepAspectRatio);
}
thumbReader.setScaledSize(currThumbSize);
thumb = thumbReader.read();
if (GData::exifThumbRotationEnabled)
{
imageView->rotateByExifRotation(thumb, imageFullPath);
currThumbSize = thumb.size();
currThumbSize.scale(QSize(thumbWidth, thumbHeight), Qt::KeepAspectRatio);
}
thumbIitem->setIcon(QPixmap::fromImage(thumb));
}
else
{
thumbIitem->setIcon(QIcon::fromTheme("image-missing",
QIcon(":/images/error_image.png")).pixmap(BAD_IMG_SZ, BAD_IMG_SZ));
currThumbSize.setHeight(BAD_IMG_SZ);
currThumbSize.setWidth(BAD_IMG_SZ);
}
if (GData::thumbsLayout == Compact)
{
if (GData::showLabels)
currThumbSize.setHeight(currThumbSize.height() + QFontMetrics(font()).height() + 5);
thumbIitem->setSizeHint(currThumbSize);
}
else
thumbIitem->setSizeHint(hintSize);
thumbViewModel->appendRow(thumbIitem);
}
void ThumbView::wheelEvent(QWheelEvent *event)
{
if (event->delta() < 0)
verticalScrollBar()->setValue(verticalScrollBar()->value() + thumbHeight);
else
verticalScrollBar()->setValue(verticalScrollBar()->value() - thumbHeight);
}
void ThumbView::mousePressEvent(QMouseEvent *event)
{
QListView::mousePressEvent(event);
if (GData::reverseMouseBehavior && event->button() == Qt::MiddleButton)
{
if (selectionModel()->selectedIndexes().size() == 1)
emit(doubleClicked(selectionModel()->selectedIndexes().first()));
}
}
void ThumbView::invertSelection()
{
QItemSelection toggleSelection;
QModelIndex firstIndex = thumbViewModel->index(0, 0);
QModelIndex lastIndex = thumbViewModel->index(thumbViewModel->rowCount() - 1, 0);
toggleSelection.select(firstIndex, lastIndex);
selectionModel()->select(toggleSelection, QItemSelectionModel::Toggle);
}
void ThumbView::setNeedScroll(bool needScroll)
{
isNeedScroll = needScroll;
}
void ThumbView::setImageView(ImageView *imageView)
{
this->imageView = imageView;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/deepin-opensource/phototonic.git
[email protected]:deepin-opensource/phototonic.git
deepin-opensource
phototonic
phototonic
master

搜索帮助