Examples: Hide "Show All" link if items fit

It doesn't make sense to indicate to the user that there are more items,
if there aren't.

Change-Id: I80a3a113dc5f7aea693c054bb1755923cb02944c
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2023-04-25 15:36:40 +02:00
parent ea5e78e822
commit d42e48f424
2 changed files with 34 additions and 1 deletions

View File

@@ -158,6 +158,26 @@ void SectionGridView::wheelEvent(QWheelEvent *e)
GridView::wheelEvent(e);
}
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 maxItems = maxColumns * maxRows;
const int items = model()->rowCount();
return maxItems >= items;
};
auto resizeEvent = static_cast<QResizeEvent *>(e);
const bool itemsCurrentyFit = itemsFit(size());
if (!resizeEvent->oldSize().isValid()
|| itemsFit(resizeEvent->oldSize()) != itemsCurrentyFit) {
emit itemsFitChanged(itemsCurrentyFit);
}
}
return GridView::event(e);
}
const QSize ListModel::defaultImageSize(214, 160);
ListModel::ListModel(QObject *parent)
@@ -754,7 +774,14 @@ ListModel *SectionedGridView::addSection(const Section &section, const QList<Lis
using namespace Layouting;
auto seeAllLink = new QLabel("<a href=\"link\">" + Tr::tr("Show All") + " &gt;</a>", this);
seeAllLink->setVisible(gridView->maxRows().has_value());
if (gridView->maxRows().has_value()) {
seeAllLink->setVisible(true);
connect(gridView, &SectionGridView::itemsFitChanged, seeAllLink, [seeAllLink](bool fits) {
seeAllLink->setVisible(!fits);
});
} else {
seeAllLink->setVisible(false);
}
connect(seeAllLink, &QLabel::linkActivated, this, [this, section] { zoomInSection(section); });
QWidget *sectionLabel = Row{section.name, createSeparator(this), seeAllLink, Space(HSpacing)}
.emerge(Layouting::WithoutMargins);

View File

@@ -48,6 +48,8 @@ protected:
class CORE_EXPORT SectionGridView : public GridView
{
Q_OBJECT
public:
explicit SectionGridView(QWidget *parent);
@@ -58,6 +60,10 @@ public:
int heightForWidth(int width) const override;
void wheelEvent(QWheelEvent *e) override;
bool event(QEvent *e) override;
signals:
void itemsFitChanged(bool fit);
private:
std::optional<int> m_maxRows;