strawberry-audio-player-win.../src/widgets/groupediconview.h

132 lines
4.2 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef GROUPEDICONVIEW_H
#define GROUPEDICONVIEW_H
#include "config.h"
#include <QObject>
2018-02-27 18:06:05 +01:00
#include <QListView>
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include <QItemSelectionModel>
#include <QString>
#include <QFont>
#include <QPalette>
#include <QPoint>
#include <QRect>
#include <QRegion>
#include <QVector>
2018-02-27 18:06:05 +01:00
2020-02-09 02:29:35 +01:00
class QWidget;
class QPainter;
class QModelIndex;
class QPaintEvent;
class QResizeEvent;
2018-02-27 18:06:05 +01:00
class MultiSortFilterProxy;
class GroupedIconView : public QListView {
Q_OBJECT
// Vertical space separating a header from the items above and below it.
Q_PROPERTY(int header_spacing READ header_spacing WRITE set_header_spacing)
// Horizontal space separating a header from the left and right edges of the widget.
Q_PROPERTY(int header_indent READ header_indent WRITE set_header_indent)
// Horizontal space separating an item from the left and right edges of the widget.
Q_PROPERTY(int item_indent READ item_indent WRITE set_item_indent)
// The text of each group's header. Must contain "%1".
2019-09-15 20:27:32 +02:00
Q_PROPERTY(QString header_text READ header_text WRITE set_header_text)
2018-02-27 18:06:05 +01:00
public:
2020-04-07 16:49:15 +02:00
explicit GroupedIconView(QWidget *parent = nullptr);
2018-02-27 18:06:05 +01:00
enum Role {
Role_Group = 1158300,
};
2021-01-26 16:48:04 +01:00
void AddSortSpec(const int role, const Qt::SortOrder order = Qt::AscendingOrder);
2018-02-27 18:06:05 +01:00
int header_spacing() const { return header_spacing_; }
int header_indent() const { return header_indent_; }
int item_indent() const { return item_indent_; }
const QString &header_text() const { return header_text_;}
2021-01-26 16:48:04 +01:00
void set_header_spacing(const int value) { header_spacing_ = value; }
void set_header_indent(const int value) { header_indent_ = value; }
void set_item_indent(const int value) { item_indent_ = value; }
2018-02-27 18:06:05 +01:00
void set_header_text(const QString &value) { header_text_ = value; }
// QAbstractItemView
2020-06-15 21:55:05 +02:00
QModelIndex moveCursor(CursorAction action, Qt::KeyboardModifiers modifiers) override;
void setModel(QAbstractItemModel *model) override;
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
static void DrawHeader(QPainter *painter, const QRect rect, const QFont &font, const QPalette &palette, const QString &text);
2018-02-27 18:06:05 +01:00
2020-04-07 16:49:15 +02:00
protected:
2018-02-27 18:06:05 +01:00
virtual int header_height() const;
// QWidget
2020-06-15 21:55:05 +02:00
void paintEvent(QPaintEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
2018-02-27 18:06:05 +01:00
// QAbstractItemView
2020-06-15 21:55:05 +02:00
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int>& = QVector<int>()) override;
QModelIndex indexAt(const QPoint &p) const override;
void rowsInserted(const QModelIndex &parent, int start, int end) override;
void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override;
2021-01-26 16:48:04 +01:00
QRect visualRect(const QModelIndex &idx) const override;
2020-06-15 21:55:05 +02:00
QRegion visualRegionForSelection(const QItemSelection &selection) const override;
2018-02-27 18:06:05 +01:00
2020-04-07 16:49:15 +02:00
private slots:
2018-02-27 18:06:05 +01:00
void LayoutItems();
2020-04-07 16:49:15 +02:00
private:
2018-02-27 18:06:05 +01:00
static const int kBarThickness;
static const int kBarMarginTop;
struct Header {
int y;
int first_row;
QString text;
};
// Returns the items that are wholly or partially inside the rect.
2021-06-20 19:04:08 +02:00
QVector<QModelIndex> IntersectingItems(const QRect rect) const;
2018-02-27 18:06:05 +01:00
// Returns the index of the item above (d=-1) or below (d=+1) the given item.
2021-01-26 16:48:04 +01:00
int IndexAboveOrBelow(int index, const int d) const;
2018-02-27 18:06:05 +01:00
MultiSortFilterProxy *proxy_model_;
QVector<QRect> visual_rects_;
QVector<Header> headers_;
const int default_header_height_;
int header_spacing_;
int header_indent_;
int item_indent_;
QString header_text_;
};
#endif // GROUPEDICONVIEW_H