diff --git a/mainwindow.cpp b/mainwindow.cpp index 517f68e..2c297e3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include MainWindow::MainWindow(QWidget *parent) @@ -82,20 +83,10 @@ void MainWindow::setupConnections() }); connect(m_listView, &QListView::clicked, this, - [this](const QModelIndex &index) - { - QString path = m_imageModel->filePath(index); - - QImageReader reader(path); - reader.setAutoTransform(true); - QImage image = reader.read(); + [this](const QModelIndex &index) { showPreview(index); }); - m_previewLabel->setPixmap( - QPixmap::fromImage(image).scaled( - m_previewLabel->size(), - Qt::KeepAspectRatio, - Qt::SmoothTransformation)); - }); + connect(m_listView->selectionModel(), &QItemSelectionModel::currentChanged, + this, [this](const QModelIndex ¤t) { showPreview(current); }); connect(m_listView->verticalScrollBar(), &QScrollBar::valueChanged, this, [this]() @@ -130,3 +121,21 @@ void MainWindow::loadVisibleThumbnails() std::cout << "loadVisibleThumbnails: " << first << " - " << last << "\n"; m_imageModel->requestThumbnails(first, last); } + +void MainWindow::showPreview(const QModelIndex &index) +{ + if (!index.isValid()) + return; + + QString path = m_imageModel->filePath(index); + + QImageReader reader(path); + reader.setAutoTransform(true); + QImage image = reader.read(); + + m_previewLabel->setPixmap( + QPixmap::fromImage(image).scaled( + m_previewLabel->size(), + Qt::KeepAspectRatio, + Qt::SmoothTransformation)); +} diff --git a/mainwindow.h b/mainwindow.h index f801f66..7b01b44 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -19,6 +19,7 @@ private: void setupUi(); void setupConnections(); void loadVisibleThumbnails(); + void showPreview(const QModelIndex &index); bool eventFilter(QObject *obj, QEvent *event) override; diff --git a/thumbnailworker.cpp b/thumbnailworker.cpp index 1ac22d2..9b094ba 100644 --- a/thumbnailworker.cpp +++ b/thumbnailworker.cpp @@ -1,5 +1,6 @@ #include "thumbnailworker.h" #include +#include ThumbnailWorker::ThumbnailWorker(const QString &path, int row, @@ -26,13 +27,18 @@ void ThumbnailWorker::run() if (!image.isNull()) { - // Scale to cover the 128x128 area, then center-crop + // Scale to fit within 128x128 keeping aspect ratio QImage scaled = image.scaled(128, 128, - Qt::KeepAspectRatioByExpanding, + Qt::KeepAspectRatio, Qt::SmoothTransformation); - int x = (scaled.width() - 128) / 2; - int y = (scaled.height() - 128) / 2; - QImage thumb = scaled.copy(x, y, 128, 128); + // Center on a 128x128 canvas with transparent/white fill + QImage thumb(128, 128, QImage::Format_ARGB32_Premultiplied); + thumb.fill(Qt::transparent); + int x = (128 - scaled.width()) / 2; + int y = (128 - scaled.height()) / 2; + QPainter painter(&thumb); + painter.drawImage(x, y, scaled); + painter.end(); emit finished(m_row, thumb); } }