-
Notifications
You must be signed in to change notification settings - Fork 3
/
colorhistorymodel.cpp
102 lines (72 loc) · 2.29 KB
/
colorhistorymodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "colorhistorymodel.h"
using namespace Qt::Literals;
ColorHistoryModel::ColorHistoryModel( QObject* parent )
: QAbstractListModel { parent }
, m_historySize { 10 } { }
int ColorHistoryModel::rowCount( const QModelIndex& parent ) const {
if ( !parent.isValid() ) {
return static_cast<int>( m_colors.size() );
}
return 0;
}
QVariant ColorHistoryModel::data( const QModelIndex& index, int role ) const {
if ( !checkIndex( index, CheckIndexOption::IndexIsValid ) ) {
return {};
}
const auto color { m_colors.at( index.row() ) };
if ( role == Color ) {
return color;
}
return {};
}
bool ColorHistoryModel::setData( const QModelIndex& index, const QVariant& value, int role ) {
if ( role != Qt::EditRole ) {
return false;
}
const auto row { index.row() };
const auto color { value.value<QColor>() };
if ( m_colors.at( row ) == color ) {
return false;
}
m_colors[row] = color;
Q_EMIT dataChanged( index, index );
return true;
}
QHash<int, QByteArray> ColorHistoryModel::roleNames() const {
static const QHash<int, QByteArray> roles { { Color, "colorRole"_ba } };
return roles;
}
Qt::ItemFlags ColorHistoryModel::flags( const QModelIndex& index ) const {
if ( !index.isValid() ) {
return Qt::NoItemFlags;
}
return QAbstractListModel::flags( index ) | Qt::ItemIsEditable;
}
bool ColorHistoryModel::removeRows( int row, int count, const QModelIndex& parent ) {
if ( row < 0 && row >= m_colors.size() ) {
return false;
}
beginRemoveRows( parent, row, count );
m_colors.removeAt( row );
endRemoveRows();
return true;
}
void ColorHistoryModel::append( const QColor& newColor ) {
if ( m_colors.contains( newColor ) || !m_colors.isEmpty() && m_colors.first().rgb() == newColor.rgb() ) {
return;
}
const auto index { static_cast<int>( m_colors.size() - 1 ) };
if ( m_colors.size() >= m_historySize ) {
beginRemoveRows( QModelIndex(), index, index );
m_colors.removeFirst();
endRemoveRows();
}
beginInsertRows( QModelIndex(), 0, 0 );
m_colors.prepend( newColor );
endInsertRows();
}
void ColorHistoryModel::clear() {
beginResetModel();
m_colors.clear();
endResetModel();
}