-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTColorPicker.cpp
More file actions
79 lines (60 loc) · 1.71 KB
/
TColorPicker.cpp
File metadata and controls
79 lines (60 loc) · 1.71 KB
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
#include <QCoreApplication>
#include <QFrame>
#include <QStyleOption>
#include "TColorPicker.h"
TColorPicker::TColorPicker(QWidget* parent) :
QPushButton( parent )
{
Init();
}
TColorPicker::TColorPicker(const QString &text, QWidget *parent /*= Q_NULLPTR*/)
: QPushButton(text, parent)
{
Init();
}
void TColorPicker::Init()
{
mPopup = new TColorPickerPopup(0);
mPopup->setObjectName("TColorPickerPopup");
mPopup->setFrameShape(QFrame::StyledPanel);
mPopup->setFrameShadow(QFrame::Plain);
connect(mPopup, SIGNAL(sigSelected(QColor)),
this, SLOT(OnPopupSelected(QColor)));
setMaximumSize(25, 20);
mSelectedColor = Qt::black;
QMetaObject::connectSlotsByName(this);
}
void TColorPicker::paintEvent ( QPaintEvent* )
{
QStyleOptionButton option;
option.initFrom( this );
option.features = QStyleOptionButton::HasMenu;
QPixmap icon( 3, 3 );
QPainter iconPainter( &icon );
iconPainter.fillRect( 0, 0, 3, 3, mSelectedColor );
option.text = "A";
option.icon = icon;
QString strStyle = QString("TColorPicker {text-align:left; font-weight:bold; font-size:15px; border-width:6px; border-style:none none solid none; border-color:%1;}")
.arg(mSelectedColor.name());
setStyleSheet(strStyle);
QPainter painter ( this );
style()->drawControl( QStyle::CE_PushButton, &option, &painter, this );
}
void TColorPicker::OnPopupSelected( QColor color )
{
mSelectedColor = color;
repaint();
}
QColor TColorPicker::GetColor(const QColor &initial /*= Qt::white*/,
const QPoint &pos )
{
mSelectedColor = initial;
mPopup->move(pos);
mPopup->setWindowFlags( Qt::Popup );
mPopup->show();
while (mPopup->isVisible() == true)
{
QCoreApplication::processEvents();
}
return mSelectedColor;
}