Skip to content

Commit

Permalink
BUG: Fix thumbnail scaling high DPI displays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Punzo committed Jun 6, 2024
1 parent f59aef0 commit 38fbfdb
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions Libs/DICOM/Widgets/ctkDICOMSeriesItemWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,28 +440,25 @@ QImage ctkDICOMSeriesItemWidgetPrivate::drawModalityThumbnail()
font.setBold(true);
font.setPixelSize(textSize);

QPixmap resultPixmap(scaledThumbnailSizePixel, scaledThumbnailSizePixel);
resultPixmap.fill(Qt::transparent);
ctkDICOMThumbnailGenerator thumbnailGenerator;
thumbnailGenerator.setWidth(scaledThumbnailSizePixel);
thumbnailGenerator.setHeight(scaledThumbnailSizePixel);

QImage thumbnailImage;
QPainter painter;

QColor backgroundColor = this->SeriesThumbnail->palette().color(QPalette::Normal, QPalette::Window);
thumbnailGenerator.generateBlankThumbnail(thumbnailImage, backgroundColor);
resultPixmap = QPixmap::fromImage(thumbnailImage);
QPixmap resultPixmap = QPixmap::fromImage(thumbnailImage);
QPainter painter;
if (painter.begin(&resultPixmap))
{
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true);
QRect rect = resultPixmap.rect();
int x = int(rect.width() * 0.5);
int y = int(rect.height() * 0.5);
this->drawTextWithShadow(&painter, font, x, y, Qt::AlignCenter, this->Modality);
painter.end();
}

resultPixmap.setDevicePixelRatio(scalingFactor);
this->SeriesThumbnail->setPixmap(resultPixmap);
return resultPixmap.toImage();
}
Expand All @@ -484,43 +481,42 @@ void ctkDICOMSeriesItemWidgetPrivate::drawThumbnail(const QString& dicomFilePath

if (dicomFilePath.isEmpty())
{
logger.error("drawThumbnail failed, dicomFilePath is empty. \n");
logger.debug("drawThumbnail failed, dicomFilePath is empty. \n");
return;
}

if (studyInstanceUID.isEmpty())
{
logger.error("drawThumbnail failed, studyInstanceUID is empty. \n");
logger.debug("drawThumbnail failed, studyInstanceUID is empty. \n");
return;
}

if (seriesInstanceUID.isEmpty())
{
logger.error("drawThumbnail failed, seriesInstanceUID is empty. \n");
logger.debug("drawThumbnail failed, seriesInstanceUID is empty. \n");
return;
}

if (sopInstanceUID.isEmpty())
{
logger.error("drawThumbnail failed, sopInstanceUID is empty. \n");
logger.debug("drawThumbnail failed, sopInstanceUID is empty. \n");
return;
}

if (modality.isEmpty())
{
logger.error("drawThumbnail failed, modality is empty. \n");
logger.debug("drawThumbnail failed, modality is empty. \n");
return;
}

qreal scalingFactor = q->devicePixelRatioF();
int scaledThumbnailSizePixel = this->ThumbnailSizePixel * scalingFactor;
int margin = floor(scaledThumbnailSizePixel / 50.);
int iconSize = floor(scaledThumbnailSizePixel / 6.);
int textSize = floor(scaledThumbnailSizePixel / 12.);
int margin = floor(this->ThumbnailSizePixel / 50.);
int iconSize = floor(this->ThumbnailSizePixel / 6.);
int textSize = floor(this->ThumbnailSizePixel / 12.);
QFont font = this->SeriesThumbnail->font();
font.setBold(true);
font.setPixelSize(textSize);
QPixmap resultPixmap(scaledThumbnailSizePixel, scaledThumbnailSizePixel);

if (this->ThumbnailImage.isNull())
{
Expand All @@ -545,14 +541,14 @@ void ctkDICOMSeriesItemWidgetPrivate::drawThumbnail(const QString& dicomFilePath
}
}

QPixmap resultPixmap(this->ThumbnailSizePixel, this->ThumbnailSizePixel);
QColor firstPixelColor = this->ThumbnailImage.pixelColor(0, 0);
resultPixmap.fill(firstPixelColor);

QPainter painter;
if (painter.begin(&resultPixmap))
{
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true);
painter.setFont(font);

QRectF rect = resultPixmap.rect();
Expand Down Expand Up @@ -617,12 +613,14 @@ void ctkDICOMSeriesItemWidgetPrivate::drawTextWithShadow(QPainter *painter,
Qt::Alignment alignment,
const QString &text)
{
Q_Q(ctkDICOMSeriesItemWidget);

QColor textColor(60, 164, 255, 225);
QGraphicsDropShadowEffect* dropShadowEffect = new QGraphicsDropShadowEffect;
dropShadowEffect->setXOffset(1);
dropShadowEffect->setYOffset(1);
dropShadowEffect->setBlurRadius(1);
dropShadowEffect->setColor(Qt::gray);
dropShadowEffect->setColor(Qt::darkGray);
QLabel textLabel;
textLabel.setObjectName("ctkDrawTextWithShadowQLabel");
QPalette palette = textLabel.palette();
Expand All @@ -633,7 +631,7 @@ void ctkDICOMSeriesItemWidgetPrivate::drawTextWithShadow(QPainter *painter,
textLabel.setText(text);
QPixmap textPixMap = textLabel.grab();
QRect rect = textPixMap.rect();
qreal scalingFactor = textLabel.devicePixelRatioF();
qreal scalingFactor = q->devicePixelRatioF();
int textWidth = rect.width() / scalingFactor;
int textHeight = rect.height() / scalingFactor;

Expand Down

0 comments on commit 38fbfdb

Please sign in to comment.