20
20
#include "BoardKey.h"
21
21
#include "Helpers.h"
22
22
#include <QPainter>
24
23
#include <plasma/theme.h>
25
#include <kpushbutton.h>
27
BoardKey::BoardKey(PlasmaboardWidget *parent):
28
Plasma::PushButton(parent),fontSize(60) {
31
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored, QSizePolicy::DefaultType);
33
connect(this, SIGNAL( clicked() ), this, SLOT( released() ) );
34
connect(static_cast<const KPushButton*>(this->nativeWidget()), SIGNAL( pressed() ), this, SLOT( pressed() ) );
36
m_pushUp = new QTimer();
37
connect(m_pushUp, SIGNAL( timeout() ), this, SLOT( reset() ) );
40
BoardKey::~BoardKey() {
44
unsigned int BoardKey::getKeycode() {
48
void BoardKey::pressed(){
52
void BoardKey::released(){
54
nativeWidget()->setDown(true);
59
void BoardKey::reset(){
60
nativeWidget()->setDown(false);
64
void BoardKey::sendKeycodePress() {
65
Helpers::fakeKeyPress(getKeycode());
68
void BoardKey::sendKeycodeRelease() {
69
Helpers::fakeKeyRelease(getKeycode());
73
void BoardKey::sendKeycodeToggled() {}
75
void BoardKey::setUpPainter(QPainter *painter){
76
painter->setRenderHints(QPainter::Antialiasing);
77
painter->setPen(QPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor)));
79
painter->translate(contentsRect().center());
80
double mul = qMin(contentsRect().width(), contentsRect().height()) / 10;
81
painter->scale(mul, mul);
84
void BoardKey::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
85
Plasma::PushButton::paint(painter, option, widget);
87
setUpPainter(painter); // scales the matrix
88
painter->scale(0.1, 0.1);
90
painter->setFont(QFont ( Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont).toString(), fontSize));
91
painter->drawText(QRect(-50,-50,100,100), Qt::AlignCenter , labelText); // don't know why rect must be like that
94
void BoardKey::setText(QString text) {
96
fontSize = (text.size() > 1) ? ((text.size() > 3) ? 20 : 40) : 60;
97
//Plasma::PushButton::setText(text);
100
QString BoardKey::text() {
25
BoardKey::BoardKey(QPoint relativePosition, QSize relativeSize, unsigned int keycode) :
26
m_relativePosition(relativePosition), m_relativeSize(relativeSize){
35
bool BoardKey::contains (const QPoint &point) const
37
return m_rect.contains(point);
40
bool BoardKey::intersects (const QRectF &rect) const
42
return m_rect.intersects(rect);
46
unsigned int BoardKey::getKeycode() const
51
unsigned int BoardKey::getKeysymbol(int level) const
53
return Helpers::keycodeToKeysym(getKeycode(), level);
56
QString BoardKey::label() const
61
void BoardKey::paint(QPainter *painter)
63
//painter->eraseRect(m_rect);
64
//painter->fillRect(m_rect, QColor(Qt::transparent));
65
painter->drawPixmap(m_rect.topLeft(), *m_pixmap);
66
//painter->drawPixmap(m_rect.toRect(), *m_pixmap);
67
//painter->drawRect(QRect(m_position, QPoint( frames[m_size].width() + m_position.x(), frames[m_size].height() + m_position.y() )));
70
QPoint BoardKey::position() const
72
return m_rect.topLeft().toPoint();
75
void BoardKey::pressed()
80
void BoardKey::pressRepeated()
82
Helpers::fakeKeyPress(getKeycode());
85
void BoardKey::released()
90
QRectF BoardKey::rect() const
95
QSize BoardKey::relativeSize() const
97
return m_relativeSize;
100
void BoardKey::reset()
104
void BoardKey::sendKey()
110
void BoardKey::sendKeyPress()
112
Helpers::fakeKeyPress(getKeycode());
115
void BoardKey::sendKeyRelease()
117
Helpers::fakeKeyRelease(getKeycode());
120
void BoardKey::setKeycode(unsigned int keycode){
124
void BoardKey::setPixmap(QPixmap *pixmap)
129
void BoardKey::setUpPainter(QPainter *painter) const
131
painter->translate(position() + QPoint(size().width()/2, size().height()/2) );
134
QSize BoardKey::size() const
136
return m_rect.size().toSize();
139
void BoardKey::unpressed()
144
void BoardKey::updateDimensions(double factor_x, double factor_y)
146
QPoint position = QPoint(m_relativePosition.x() * factor_x, m_relativePosition.y() * factor_y);
147
QSize size = QSize(m_relativeSize.width() * factor_x, m_relativeSize.height() * factor_y);
148
m_rect = QRect(position, size);