Examples: Centralize constants in one place

Having all constants in WelcomePageHelpers makes reading the welcome
scren code a bit less interesting.

Change-Id: Idc2e402f33042b49d041c43ecc78a6e8d2d3536a
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Alessandro Portale
2023-05-02 18:28:12 +02:00
parent 2a5f2961a7
commit 765e8a65ea
5 changed files with 17 additions and 18 deletions

View File

@@ -144,10 +144,10 @@ bool SectionGridView::hasHeightForWidth() const
int SectionGridView::heightForWidth(int width) const
{
const int columnCount = width / Core::ListItemDelegate::GridItemWidth;
const int columnCount = width / Core::WelcomePageHelpers::GridItemWidth;
const int rowCount = (model()->rowCount() + columnCount - 1) / columnCount;
const int maxRowCount = m_maxRows ? std::min(*m_maxRows, rowCount) : rowCount;
return maxRowCount * Core::ListItemDelegate::GridItemHeight;
return maxRowCount * Core::WelcomePageHelpers::GridItemHeight;
}
void SectionGridView::wheelEvent(QWheelEvent *e)
@@ -162,8 +162,8 @@ bool SectionGridView::event(QEvent *e)
{
if (e->type() == QEvent::Resize) {
const auto itemsFit = [this](const QSize &size) {
const int maxColumns = std::max(size.width() / ListItemDelegate::GridItemWidth, 1);
const int maxRows = std::max(size.height() / ListItemDelegate::GridItemHeight, 1);
const int maxColumns = std::max(size.width() / WelcomePageHelpers::GridItemWidth, 1);
const int maxRows = std::max(size.height() / WelcomePageHelpers::GridItemHeight, 1);
const int maxItems = maxColumns * maxRows;
const int items = model()->rowCount();
return maxItems >= items;
@@ -178,8 +178,6 @@ bool SectionGridView::event(QEvent *e)
return GridView::event(e);
}
const QSize ListModel::defaultImageSize(214, 160);
ListModel::ListModel(QObject *parent)
: QAbstractListModel(parent)
{
@@ -459,13 +457,14 @@ void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
const QRect rc = option.rect;
const QRect tileRect(0, 0, rc.width() - GridItemGap, rc.height() - GridItemGap);
const QSize thumbnailBgSize = ListModel::defaultImageSize.grownBy(QMargins(1, 1, 1, 1));
const QSize thumbnailBgSize = GridItemImageSize.grownBy(QMargins(1, 1, 1, 1));
const QRect thumbnailBgRect((tileRect.width() - thumbnailBgSize.width()) / 2, GridItemGap,
thumbnailBgSize.width(), thumbnailBgSize.height());
const QRect textArea = tileRect.adjusted(GridItemGap, GridItemGap, -GridItemGap, -GridItemGap);
const bool hovered = option.state & QStyle::State_MouseOver;
constexpr int TagsSeparatorY = GridItemHeight - GridItemGap - 52;
constexpr int tagsBase = TagsSeparatorY + 17;
constexpr int shiftY = TagsSeparatorY - 16;
constexpr int nameY = TagsSeparatorY - 20;

View File

@@ -24,6 +24,12 @@ namespace WelcomePageHelpers {
constexpr int HSpacing = 20;
constexpr int ItemGap = 4;
constexpr int GridItemGap = 3 * ItemGap;
constexpr int GridItemWidth = 240 + GridItemGap;
constexpr int GridItemHeight = GridItemWidth;
constexpr QSize GridItemImageSize(214, 160);
CORE_EXPORT QFont brandFont();
CORE_EXPORT QWidget *panelBar(QWidget *parent = nullptr);
@@ -99,8 +105,6 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void setPixmapFunction(const PixmapFunction &fetchPixmapAndUpdatePixmapCache);
static const QSize defaultImageSize;
void setOwnsItems(bool owns);
private:
@@ -142,11 +146,6 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
static constexpr int GridItemGap = 3 * WelcomePageHelpers::ItemGap;
static constexpr int GridItemWidth = 240 + GridItemGap;
static constexpr int GridItemHeight = GridItemWidth;
static constexpr int TagsSeparatorY = GridItemHeight - GridItemGap - 52;
signals:
void tagClicked(const QString &tag);

View File

@@ -269,7 +269,7 @@ void SectionedProducts::onImageDownloadFinished(QNetworkReply *reply)
if (pixmap.loadFromData(data, imageFormat.toLatin1())) {
const QString url = imageUrl.toString();
const int dpr = qApp->devicePixelRatio();
pixmap = pixmap.scaled(ListModel::defaultImageSize * dpr,
pixmap = pixmap.scaled(WelcomePageHelpers::GridItemImageSize * dpr,
Qt::KeepAspectRatio,
Qt::SmoothTransformation);
pixmap.setDevicePixelRatio(dpr);

View File

@@ -255,7 +255,8 @@ static QPixmap fetchPixmapAndUpdatePixmapCache(const QString &url)
img.convertTo(QImage::Format_RGB32);
const int dpr = qApp->devicePixelRatio();
// boundedTo -> don't scale thumbnails up
const QSize scaledSize = Core::ListModel::defaultImageSize.boundedTo(img.size()) * dpr;
const QSize scaledSize =
WelcomePageHelpers::GridItemImageSize.boundedTo(img.size()) * dpr;
pixmap = QPixmap::fromImage(
img.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
pixmap.setDevicePixelRatio(dpr);

View File

@@ -280,8 +280,8 @@ public:
// for macOS dark mode
pal.setColor(QPalette::Text, Utils::creatorTheme()->color(Theme::Welcome_TextColor));
exampleSetSelector->setPalette(pal);
exampleSetSelector->setMinimumWidth(Core::ListItemDelegate::GridItemWidth);
exampleSetSelector->setMaximumWidth(Core::ListItemDelegate::GridItemWidth);
exampleSetSelector->setMinimumWidth(Core::WelcomePageHelpers::GridItemWidth);
exampleSetSelector->setMaximumWidth(Core::WelcomePageHelpers::GridItemWidth);
exampleSetSelector->setModel(s_exampleSetModel);
exampleSetSelector->setCurrentIndex(s_exampleSetModel->selectedExampleSet());
connect(exampleSetSelector,