forked from Ramblurr/PietCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KColorCells.cpp
330 lines (270 loc) · 10.1 KB
/
KColorCells.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* This file is part of the KDE libraries
Copyright (C) 1997 Martin Jones (mjones@kde.org)
Copyright (C) 2007 Roberto Raggi (roberto@kdevelop.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "KColorCells.h"
#include "KColorMimeData.h"
#include "ColorUtils.h"
#include <QtWidgets>
#include <QtGui>
#include <math.h>
class KColorCells::KColorCellsPrivate
{
public:
KColorCellsPrivate( KColorCells *q ): q( q ) {
inMouse = false;
selected = -1;
shade = false;
}
KColorCells *q;
QPoint mousePos;
int selected;
bool shade;
bool inMouse;
};
class KColorCellsItemDelegate: public QStyledItemDelegate
{
public:
KColorCellsItemDelegate( KColorCells *parent ): QStyledItemDelegate( parent ) {}
virtual void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
QStyleOptionViewItemV4 opt( option );
initStyleOption( &opt, index );
//Get the current cell color
QColor backgroundColor = index.data( Qt::BackgroundRole ).value<QColor>();
if ( backgroundColor.isValid() ) {
//Paint the general background
painter->fillRect( opt.rect, backgroundColor );
//Paint the selection mark (circle)
if ( opt.state & QStyle::State_Selected ) {
//Use black or white, depending on the contrast
QColor color = QColor( 0, 0, 0, 220 );
if ( ColorUtils::contrastRatio( color, backgroundColor ) < 5 ) {
color = QColor( 255, 255, 255, 220 );
}
//Draw the selection (radiobutton-like) circle
painter->save();
painter->setRenderHint( QPainter::Antialiasing, true );
painter->setRenderHint( QPainter::HighQualityAntialiasing, true );
painter->setPen( QPen( color, 1.2, Qt::SolidLine ) );
painter->setBrush( QBrush() );
painter->drawEllipse( opt.rect.adjusted( 2, 2, -2, -2 ) );
painter->restore();
}
} else {
//Paint the "X" (missing) cross on empty background color
backgroundColor = opt.palette.color( QPalette::Window );
painter->fillRect( opt.rect, backgroundColor );
painter->save();
QColor crossColor = qGray( backgroundColor.rgb() ) > 192 ? backgroundColor.darker( 106 ) :
backgroundColor.lighter( 106 );
painter->setPen( QPen( crossColor, 1.5 ) );
painter->drawLine( opt.rect.topLeft(), opt.rect.bottomRight() );
painter->drawLine( opt.rect.topRight(), opt.rect.bottomLeft() );
painter->restore();
}
}
};
KColorCells::KColorCells( QWidget *parent, int rows, int cols )
: QTableWidget( parent ), d( new KColorCellsPrivate( this ) )
{
setItemDelegate( new KColorCellsItemDelegate( this ) );
setFrameShape( QFrame::NoFrame );
d->shade = true;
setRowCount( rows );
setColumnCount( cols );
verticalHeader()->hide();
horizontalHeader()->hide();
d->selected = -1;
d->inMouse = false;
// Drag'n'Drop
setAcceptDrops( true );
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
viewport()->setBackgroundRole( QPalette::Background );
setBackgroundRole( QPalette::Background );
setSelectionMode( QAbstractItemView::SingleSelection );
setDragEnabled( false );
}
KColorCells::~KColorCells()
{
delete d;
}
QColor KColorCells::color( int index ) const
{
QTableWidgetItem * tmpItem = 0;
if (index >= 0 && columnCount() > 0)
tmpItem = item( index / columnCount(), index % columnCount() );
if ( tmpItem != 0 )
return tmpItem->data( Qt::BackgroundRole ).value<QColor>();
return QColor();
}
int KColorCells::count() const
{
return rowCount() * columnCount();
}
void KColorCells::setShading( bool _shade )
{
d->shade = _shade;
}
void KColorCells::setAcceptDrags( bool _acceptDrags )
{
this->setDragEnabled( _acceptDrags );
}
void KColorCells::setSelected( int index )
{
Q_ASSERT( index >= 0 && index < count() );
d->selected = index;
}
int KColorCells::selectedIndex() const
{
return d->selected;
}
void KColorCells::setColor( int column, const QColor &color )
{
const int tableRow = column / columnCount();
const int tableColumn = column % columnCount();
Q_ASSERT( tableRow >= 0 && tableRow < rowCount() );
Q_ASSERT( tableColumn >= 0 && tableColumn < columnCount() );
QTableWidgetItem * tableItem = item( tableRow, tableColumn );
if ( tableItem == 0 ) {
tableItem = new QTableWidgetItem();
setItem( tableRow, tableColumn, tableItem );
}
tableItem->setData( Qt::BackgroundRole , color );
}
void KColorCells::resizeEvent( QResizeEvent* )
{
// According to the Qt doc:
// If you need to set the width of a given column to a fixed value, call
// QHeaderView::resizeSection() on the table's {horizontal,vertical}
// header.
// Therefore we iterate over each row and column and set the header section
// size, as the sizeHint does indeed appear to be ignored in favor of a
// minimum size that is larger than what we want.
for ( int index = 0 ; index < columnCount() ; index++ )
horizontalHeader()->resizeSection( index, sizeHintForColumn( index ) );
for ( int index = 0 ; index < rowCount() ; index++ )
verticalHeader()->resizeSection( index, sizeHintForRow( index ) );
// QHeaderView::resizeSection()
}
int KColorCells::sizeHintForColumn( int /*column*/ ) const
{
return width() / columnCount() ;
}
int KColorCells::sizeHintForRow( int /*row*/ ) const
{
return height() / rowCount() ;
}
void KColorCells::mousePressEvent( QMouseEvent *e )
{
d->inMouse = true;
d->mousePos = e->pos();
QTableWidget::mousePressEvent( e );
}
int KColorCells::positionToCell( const QPoint &pos, bool ignoreBorders ) const
{
//TODO ignoreBorders not yet handled
Q_UNUSED( ignoreBorders )
QTableWidgetItem* tableItem = itemAt( pos );
if ( !tableItem )
return -1;
const int itemRow = row( tableItem );
const int itemColumn = column( tableItem );
int cell = itemRow * columnCount() + itemColumn;
/*if (!ignoreBorders)
{
int border = 2;
int x = pos.x() - col * cellWidth();
int y = pos.y() - row * cellHeight();
if ( (x < border) || (x > cellWidth()-border) ||
(y < border) || (y > cellHeight()-border))
return -1;
}*/
return cell;
}
void KColorCells::mouseMoveEvent( QMouseEvent *e )
{
if ( this->dragEnabled() || this->acceptDrops() ) {
if ( !( e->buttons() & Qt::LeftButton ) ) return;
if ( d->inMouse ) {
if ( e->x() > d->mousePos.x() || e->x() < d->mousePos.x() ||
e->y() > d->mousePos.y() || e->y() < d->mousePos.y() ) {
// Drag color object
QTableWidgetItem * tableItem = itemAt( d->mousePos );
if ( tableItem ) {
QVariant var = tableItem->data( Qt::BackgroundRole );
QColor tmpCol = var.value<QColor>();
if ( tmpCol.isValid() )
KColorMimeData::createDrag( tmpCol, this )->start();
}
}
}
} else
QTableWidget::mouseMoveEvent( e );
}
void KColorCells::dragEnterEvent( QDragEnterEvent *event )
{
qDebug() << "KColorCells::dragEnterEvent() acceptDrags="
<< this->dragEnabled()
<< " canDecode=" << KColorMimeData::canDecode( event->mimeData() )
<< endl;
event->setAccepted( this->dragEnabled() && KColorMimeData::canDecode( event->mimeData() ) );
}
// Reimplemented to override QTableWidget's override. Else dropping doesn't work.
void KColorCells::dragMoveEvent( QDragMoveEvent *event )
{
qDebug() << "KColorCells::dragMoveEvent() acceptDrags="
<< this->dragEnabled()
<< " canDecode=" << KColorMimeData::canDecode( event->mimeData() )
<< endl;
event->setAccepted( this->dragEnabled() && KColorMimeData::canDecode( event->mimeData() ) );
}
void KColorCells::dropEvent( QDropEvent *event )
{
QColor c = KColorMimeData::fromMimeData( event->mimeData() );
qDebug() << "KColorCells::dropEvent() color.isValid=" << c.isValid();
if ( c.isValid() ) {
QTableWidgetItem * tableItem = itemAt( event->pos() );
if ( tableItem )
tableItem->setData( Qt::BackgroundRole , c );
}
}
void KColorCells::selectionChanged( const QItemSelection & selected, const QItemSelection & deselected )
{
QModelIndexList indexList = selected.indexes();
d->selected = -1;
foreach (QModelIndex index, indexList)
{
int row = index.row();
int column = index.column();
int cell = row * columnCount() + column;
d->selected = cell;
emit colorSelected( cell , color( cell ) );
break;
}
QTableView::selectionChanged( selected, deselected );
}
void KColorCells::mouseReleaseEvent( QMouseEvent *e )
{
d->inMouse = false;
QTableWidget::mouseReleaseEvent( e );
}
void KColorCells::mouseDoubleClickEvent( QMouseEvent * /*e*/ )
{
int cell = positionToCell( d->mousePos );
if ( cell != -1 )
emit colorDoubleClicked( cell , color( cell ) );
}
#include "KColorCells.moc"