1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
4  *   SPDX-FileCopyrightText: 2015-2016 Teo Mrnjavac <teo@kde.org>
5  *   SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
6  *   SPDX-License-Identifier: GPL-3.0-or-later
7  *
8  *   Calamares is Free Software: see the License-Identifier above.
9  *
10  */
11 
12 #ifndef PARTITIONLABELSVIEW_H
13 #define PARTITIONLABELSVIEW_H
14 
15 #include "PartitionViewSelectionFilter.h"
16 
17 #include <QAbstractItemView>
18 
19 /**
20  * A Qt model view which displays colored labels for partitions.
21  *
22  * It has been created to be used with a PartitionModel instance, but does not
23  * call any PartitionModel-specific methods: it should be usable with other
24  * models as long as they provide the same roles PartitionModel provides.
25  */
26 class PartitionLabelsView : public QAbstractItemView
27 {
28     Q_OBJECT
29 public:
30     explicit PartitionLabelsView( QWidget* parent = nullptr );
31     ~PartitionLabelsView() override;
32 
33     QSize minimumSizeHint() const override;
34 
35     QSize sizeHint() const override;
36 
37     void paintEvent( QPaintEvent* event ) override;
38 
39     // QAbstractItemView API
40     QModelIndex indexAt( const QPoint& point ) const override;
41     QRect visualRect( const QModelIndex& idx ) const override;
42     void scrollTo( const QModelIndex& index, ScrollHint hint = EnsureVisible ) override;
43 
44     void setCustomNewRootLabel( const QString& text );
45 
46     void setSelectionModel( QItemSelectionModel* selectionModel ) override;
47 
48     void setSelectionFilter( SelectionFilter canBeSelected );
49 
50     void setExtendedPartitionHidden( bool hidden );
51 
52 protected:
53     // QAbstractItemView API
54     QRegion visualRegionForSelection( const QItemSelection& selection ) const override;
55     int horizontalOffset() const override;
56     int verticalOffset() const override;
57     bool isIndexHidden( const QModelIndex& index ) const override;
58     QModelIndex moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) override;
59     void setSelection( const QRect& rect, QItemSelectionModel::SelectionFlags flags ) override;
60 
61     void mouseMoveEvent( QMouseEvent* event ) override;
62     void leaveEvent( QEvent* event ) override;
63     void mousePressEvent( QMouseEvent* event ) override;
64 
65 protected slots:
66     void updateGeometries() override;
67 
68 private:
69     QRect labelsRect() const;
70     void drawLabels( QPainter* painter, const QRect& rect, const QModelIndex& parent );
71     QSize sizeForAllLabels( int maxLineWidth ) const;
72     QSize sizeForLabel( const QStringList& text ) const;
73     void drawLabel( QPainter* painter, const QStringList& text, const QColor& color, const QPoint& pos, bool selected );
74     QModelIndexList getIndexesToDraw( const QModelIndex& parent ) const;
75     QStringList buildTexts( const QModelIndex& index ) const;
76 
77     SelectionFilter m_canBeSelected;
78     bool m_extendedPartitionHidden;
79 
80     QString m_customNewRootLabel;
81     QPersistentModelIndex m_hoveredIndex;
82 };
83 
84 #endif  // PARTITIONLABELSVIEW_H
85