This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
PixelDelegate.cpp
102 lines (85 loc) · 3.66 KB
/
PixelDelegate.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
/*
Copyright (C) 2010 Casey Link <[email protected]>
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 3 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 "PixelDelegate.h"
#include "ImageModel.h"
#include "ViewMonitor.h"
#include "UndoHandler.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QMenu>
#include <QAction>
#include <QApplication>
PixelDelegate::PixelDelegate( ViewMonitor* monitor, UndoHandler* handler, QMenu* menu, QObject* parent ) : QStyledItemDelegate( parent ), mMonitor( monitor ), mUndoHandler(handler), mContextMenu( menu )
{
}
void PixelDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
if ( option.state & QStyle::State_Selected )
painter->fillRect( option.rect, option.palette.highlight() );
QColor c = index.model()->data( index, Qt::DisplayRole ).value<QColor>();
painter->save();
painter->setBrush( QBrush( c ) );
// shorten the rectangle a little to provide some spacing
QRect shortRect = option.rect.adjusted( 1, 1, -1, -1 );
QPen pen = painter->pen();
if( index.data( ImageModel::IsCurrentDebugRole ).toBool() ) {
pen.setColor( Qt::black );
pen.setWidth( 2 );
painter->setPen( pen );
}
painter->drawRect( shortRect );
painter->restore();
// This seems to break using QT 4.8.7, it draws over everything this method has
// just done
/*QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
opt.text = "";
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter);*/
}
QSize PixelDelegate::sizeHint( const QStyleOptionViewItem & /* option */,
const QModelIndex & /* index */ ) const
{
return QSize( mMonitor->pixelSize(), mMonitor->pixelSize() );
}
bool PixelDelegate::editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
{
if ( !index.isValid() )
return false;
switch ( event->type() ) {
case QEvent::MouseButtonPress:
case QEvent::MouseMove: {
QMouseEvent *mev = static_cast<QMouseEvent*>( event );
if ( mev->buttons() & Qt::LeftButton ) {
mUndoHandler->createEditPixel(index.column(), index.row(), mMonitor->currentColor(), mev->type() == QEvent::MouseMove );
emit imageEdited();
// return true;
} else if ( (mev->modifiers() == Qt::NoModifier ) && (mev->button() == Qt::RightButton ) ) {
mContextMenu->popup(mev->globalPos());
// return false;
} else if ( (mev->modifiers() == Qt::ControlModifier ) && ( mev->button() == Qt::RightButton) ) {
mMonitor->setCurrentColor( index.model()->data( index, Qt::DisplayRole ).value<QColor>() );
// return false;
}
break;
}
default:
break;
// return false;
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
#include "PixelDelegate.moc"