Adjust horizontal gap between icons in grid view to equal distribute

icons over space.
Issue #1169
This commit is contained in:
Craig Drummond
2018-01-19 21:49:48 +00:00
parent 81f64b86d8
commit 43e59bf01d
5 changed files with 52 additions and 3 deletions

View File

@@ -43,6 +43,8 @@
30. Add link in server settings page to wiki page on github explaining how
files are accessed.
31. Make more actions shortcut assignable.
32. Adjust horizontal gap between icons in grid view to equal distribute icons
over space.
2.2.0
-----

View File

@@ -154,6 +154,7 @@ public:
ListDelegate(ListView *v, QAbstractItemView *p)
: ActionItemDelegate(p)
, view(v)
, iconWidth(0)
{
}
@@ -161,11 +162,35 @@ public:
{
}
// Calculate width for each item in IconMode. The idea is to have the icons evenly spaced out.
void calcItemWidth() const
{
static const int constViewGap = Utils::scaleForDpi(8);
int viewWidth = view->viewport()->width();
// KVantum returns a -ve number for spacing if using overlay scrollbars. I /think/ this
// is what messes the layout code. The subtracion below seems to work-around this - but
// give a larger right-hand margin!
if (view->style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing)<0) {
viewWidth-=3*constViewGap;
}
iconWidth = zoomedSize(view, gridCoverSize)+constViewGap;
int numItems = viewWidth/iconWidth;
if (numItems>1) {
iconWidth=qMax(iconWidth-1, (int)(viewWidth/numItems)-constViewGap);
}
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
Q_UNUSED(option)
if (view && QListView::IconMode==view->viewMode()) {
return QSize(zoomedSize(view, gridCoverSize)+8, zoomedSize(view, gridCoverSize)+(QApplication::fontMetrics().height()*2.5));
if (0==iconWidth) {
calcItemWidth();
}
// return QSize(zoomedSize(view, gridCoverSize)+8, zoomedSize(view, gridCoverSize)+(QApplication::fontMetrics().height()*2.5));
return QSize(iconWidth, zoomedSize(view, gridCoverSize)+(QApplication::fontMetrics().height()*2.5));
} else {
int imageSize = index.data(Cantata::Role_ListImage).toBool() ? listCoverSize : 0;
// TODO: Any point to checking one-line here? All models return sub-text...
@@ -399,6 +424,7 @@ public:
protected:
ListView *view;
mutable int iconWidth;
};
class TreeDelegate : public ListDelegate
@@ -651,6 +677,7 @@ ItemView::ItemView(QWidget *p)
connect(listView, SIGNAL(activated(const QModelIndex &)), this, SLOT(activateItem(const QModelIndex &)));
connect(listView, SIGNAL(itemDoubleClicked(const QModelIndex &)), this, SIGNAL(doubleClicked(const QModelIndex &)));
connect(listView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(itemClicked(const QModelIndex &)));
connect(listView, SIGNAL(widthChanged()), this, SLOT(calcIconViewWidth()));
connect(backAction, SIGNAL(triggered()), this, SLOT(backActivated()));
connect(listViewEventHandler, SIGNAL(backspacePressed()), this, SLOT(backActivated()));
connect(title, SIGNAL(addToPlayQueue()), this, SLOT(addTitleButtonClicked()));
@@ -1487,7 +1514,7 @@ void ItemView::zoomIn()
if (listView->zoom()+constZoomStep<=constMaxZoom) {
listView->setZoom(listView->zoom()+constZoomStep);
listView->setGridSize(zoomedSize(listView, iconGridSize));
listView->viewport()->update();
calcIconViewWidth();
}
}
}
@@ -1498,11 +1525,17 @@ void ItemView::zoomOut()
if (listView->zoom()-constZoomStep>=constMinZoom) {
listView->setZoom(listView->zoom()-constZoomStep);
listView->setGridSize(zoomedSize(listView, iconGridSize));
listView->viewport()->update();
calcIconViewWidth();
}
}
}
void ItemView::calcIconViewWidth()
{
static_cast<ListDelegate *>(listView->itemDelegate())->calcItemWidth();
listView->viewport()->update();
}
void ItemView::delaySearchItems()
{
if (searchWidget->text().isEmpty()) {

View File

@@ -189,6 +189,7 @@ private Q_SLOTS:
void coverLoaded(const Song &song, int size);
void zoomIn();
void zoomOut();
void calcIconViewWidth();
private:
QAction * getAction(const QModelIndex &index);

View File

@@ -40,6 +40,7 @@ ListView::ListView(QWidget *parent)
, eventFilter(nullptr)
, menu(nullptr)
, zoomLevel(1.0)
, prevWidth(0)
{
setDragEnabled(true);
setContextMenuPolicy(Qt::NoContextMenu);
@@ -140,6 +141,15 @@ void ListView::paintEvent(QPaintEvent *e)
QListView::paintEvent(e);
}
void ListView::resizeEvent(QResizeEvent *e)
{
QListView::resizeEvent(e);
if (IconMode==viewMode() && width()!=prevWidth) {
emit widthChanged();
prevWidth=width();
}
}
// Workaround for https://bugreports.qt-project.org/browse/QTBUG-18009
void ListView::correctSelection()
{

View File

@@ -50,6 +50,7 @@ public:
void addDefaultAction(QAction *act);
void setBackgroundImage(const QIcon &icon);
void paintEvent(QPaintEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
void installFilter(QObject *f) { eventFilter=f; installEventFilter(f); }
QObject * filter() const { return eventFilter; }
double zoom() const { return zoomLevel; }
@@ -63,12 +64,14 @@ private Q_SLOTS:
Q_SIGNALS:
bool itemsSelected(bool);
void itemDoubleClicked(const QModelIndex &idx);
void widthChanged();
private:
QObject *eventFilter;
QMenu *menu;
QPixmap bgnd;
double zoomLevel;
int prevWidth;
};
#endif