refactor: improve image loading and handling in ImageData and ImagesCarousel
This commit is contained in:
+34
-11
@@ -1,34 +1,56 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @Date: 2025-11-30 20:32:27
|
||||
* @LastEditTime: 2025-11-30 23:07:52
|
||||
* @LastEditTime: 2026-01-14 23:31:41
|
||||
* @Description: Image item widget for displaying an image.
|
||||
*/
|
||||
#include "image_item.h"
|
||||
|
||||
#include <QImageReader>
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
using namespace GeneralLogger;
|
||||
|
||||
ImageData* ImageData::create(const QString& p, const int initWidth, const int initHeight) {
|
||||
ImageData* data = new ImageData(p);
|
||||
data->image = new QImage();
|
||||
|
||||
if (!data->image.load(p)) {
|
||||
QImageReader reader(p);
|
||||
if (!reader.canRead()) {
|
||||
error(QString("Failed to load image from path: %1").arg(p));
|
||||
delete data;
|
||||
return nullptr;
|
||||
}
|
||||
const QSize targetSize(initWidth, initHeight);
|
||||
data->image = data->image.scaled(targetSize, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
|
||||
|
||||
// Crop to center
|
||||
int x = (data->image.width() - targetSize.width()) / 2;
|
||||
int y = (data->image.height() - targetSize.height()) / 2;
|
||||
data->image = data->image.copy(x, y, targetSize.width(), targetSize.height());
|
||||
const QSize targetSize(initWidth, initHeight);
|
||||
const QSize originalSize = reader.size();
|
||||
|
||||
if (originalSize.isValid()) {
|
||||
double widthRatio = (double)targetSize.width() / originalSize.width();
|
||||
double heightRatio = (double)targetSize.height() / originalSize.height();
|
||||
double scaleFactor = std::max(widthRatio, heightRatio);
|
||||
|
||||
QSize scaledSize = originalSize * scaleFactor;
|
||||
reader.setScaledSize(scaledSize);
|
||||
}
|
||||
|
||||
if (!reader.read(data->image)) {
|
||||
error(QString("Failed to load image from path: %1").arg(p));
|
||||
delete data;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (data->image->size() != targetSize) {
|
||||
int x = (data->image->width() - targetSize.width()) / 2;
|
||||
int y = (data->image->height() - targetSize.height()) / 2;
|
||||
*data->image = data->image->copy(x, y, targetSize.width(), targetSize.height());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
ImageItem::ImageItem(const ImageData* data,
|
||||
ImageItem::ImageItem(ImageData* data,
|
||||
const int itemWidth,
|
||||
const int itemHeight,
|
||||
const int itemFocusWidth,
|
||||
@@ -40,11 +62,12 @@ ImageItem::ImageItem(const ImageData* data,
|
||||
m_itemFocusSize(itemFocusWidth, itemFocusHeight) {
|
||||
assert(data != nullptr);
|
||||
setScaledContents(true);
|
||||
if (data->image.isNull()) {
|
||||
if (!data->isValid()) {
|
||||
setText(":(");
|
||||
setAlignment(Qt::AlignCenter);
|
||||
} else {
|
||||
setPixmap(QPixmap::fromImage(data->image));
|
||||
setPixmap(QPixmap::fromImage(data->getImage()));
|
||||
data->releaseImage();
|
||||
}
|
||||
setFixedSize(itemWidth, itemHeight);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user