Files
o3de/Code/Framework/AzToolsFramework/AzToolsFramework/Thumbnails/ThumbnailWidget.cpp
T
Steve Pham 38261d0800 Shorten copyright headers by splitting into 2 lines (#2213)
* Updated all copyright headers to split the longer original copyright line into 2 shorter lines

Signed-off-by: Steve Pham <spham@amazon.com>
2021-07-16 15:25:48 -07:00

89 lines
2.9 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/PlatformDef.h>
#include <AzToolsFramework/Thumbnails/ThumbnailWidget.h>
#include <AzToolsFramework/Thumbnails/ThumbnailerBus.h>
// 4127: conditional expression is constant
// 4800 'uint': forcing value to bool 'true' or 'false' (performance warning)
// 4251: 'QPainter::d_ptr': class 'QScopedPointer<QPainterPrivate,QScopedPointerDeleter<T>>' needs to have dll-interface to be used by clients of class 'QPainter'
AZ_PUSH_DISABLE_WARNING(4127 4800 4251, "-Wunknown-warning-option")
#include <QPainter>
#include <QPixmap>
#include <QtGlobal>
AZ_POP_DISABLE_WARNING
namespace AzToolsFramework
{
namespace Thumbnailer
{
ThumbnailWidget::ThumbnailWidget(QWidget* parent)
: QWidget(parent)
{
}
void ThumbnailWidget::SetThumbnailKey(SharedThumbnailKey key, const char* contextName)
{
if (m_key)
{
disconnect(m_key.data(), &ThumbnailKey::ThumbnailUpdatedSignal, this, &ThumbnailWidget::KeyUpdatedSlot);
}
m_key = key;
m_contextName = contextName;
connect(m_key.data(), &ThumbnailKey::ThumbnailUpdatedSignal, this, &ThumbnailWidget::KeyUpdatedSlot);
repaint();
}
void ThumbnailWidget::ClearThumbnail()
{
m_key.clear();
repaint();
}
int ThumbnailWidget::heightForWidth(int w) const
{
return w;
}
QSize ThumbnailWidget::sizeHint() const
{
int w = this->width();
return QSize(w, heightForWidth(w));
}
void ThumbnailWidget::paintEvent(QPaintEvent* event)
{
if (m_key)
{
// thumbnail instance is not stored locally, but retrieved each paintEvent since thumbnail mapped to a specific key may change
SharedThumbnail thumbnail;
ThumbnailerRequestsBus::BroadcastResult(thumbnail, &ThumbnailerRequests::GetThumbnail, m_key, m_contextName.c_str());
QPainter painter(this);
// Scaling and centering pixmap within bounds to preserve aspect ratio
const QPixmap pixmap = thumbnail->GetPixmap().scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
const QSize sizeDelta = size() - pixmap.size();
const QPoint pointDelta = QPoint(sizeDelta.width() / 2, sizeDelta.height() / 2);
painter.drawPixmap(pointDelta, pixmap);
}
QWidget::paintEvent(event);
}
void ThumbnailWidget::KeyUpdatedSlot()
{
repaint();
}
} // namespace Thumbnailer
} // namespace AzToolsFramework
#include "Thumbnails/moc_ThumbnailWidget.cpp"