/***************************************************************************
* Copyright (C) 2005 by Teppei Tamra *
* TAM(tam-t@par.odn.ne.jp) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <qfont.h>
#include <qlayout.h>
#include <qtooltip.h>
#include "keybutton.h"
#include <iostream>
KeyGroup::KeyGroup()
{
}
KeyGroup::KeyGroup(const QString &_name,const QString &_label,const QString &_event,const QString &_modifier)
{
setValue(_name,_label,_event,_modifier);
}
KeyGroup::~KeyGroup()
{
}
void KeyGroup::setValue(const QString &_name,const QString &_label,const QString &_event,const QString &_modifier)
{
QStringList l = QStringList::split("+",_name);
l.sort();
name = l.join("+");
label = _label;
event = _event;
modifier = _modifier;
}
int KeyButton::buttonSize = 20;
KeyButton::KeyButton(QWidget *parent,
unsigned int pos_x,unsigned int pos_y,
unsigned int width, unsigned int height,const QColor &keycolor,
bool toggled,const char *name)
: QPushButton(parent, name)
{
x = pos_x;
y = pos_y;
w = width;
h = height;
holded = false;
color = keycolor;
if (toggled) setToggleButton(true);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(hold()));
// QToolTip::add(this,l.replace('\n',' ') + "\n[" + event + "]");
setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
setFixedHeight(h * buttonSize);
setFixedWidth(w * buttonSize);
setPaletteBackgroundColor(color);
if (isToggleButton()) {
connect(this,SIGNAL(toggled(bool)),this,SLOT(keyToggled(bool)));
} else {
connect(this,SIGNAL(pressed()),this,SLOT(keyPressed()));
connect(this,SIGNAL(released()),this,SLOT(keyReleased()));
}
}
KeyButton::~KeyButton()
{
}
/*!
\fn KeyButton::updateLabel()
*/
void KeyButton::updateLabel()
{
QFont f = font();
int s = (w > h ? h : w);
if (current.label.length() > 1) f.setPixelSize(((buttonSize * 6) / 20) * s);
else f.setPixelSize(((buttonSize * 6) / 10) * s);
setFont(f);
setText(current.label);
update();
show();
}
/*!
\fn KeyButton::getX()
*/
int KeyButton::getX()
{
return x;
}
/*!
\fn KeyButton::getY()
*/
int KeyButton::getY()
{
return y;
}
/*!
\fn KeyButton::getH()
*/
int KeyButton::getH()
{
return h;
}
/*!
\fn KeyButton::getW()
*/
int KeyButton::getW()
{
return w;
}
/*!
\fn KeyButton::getElement(QDomDocument &doc)
*/
QDomElement KeyButton::getElement(QDomDocument &doc)
{
QDomElement element = doc.createElement("key");
element.setAttribute("x",x);
element.setAttribute("y",y);
element.setAttribute("width",w);
element.setAttribute("height",h);
element.setAttribute("type","button");
element.setAttribute("color",color.name());
for(unsigned int i = 0;i < list.count();i ++) {
QDomElement e = doc.createElement("group");
e.setAttribute("name",list[i].name);
e.setAttribute("modifier",list[i].modifier);
e.setAttribute("event",list[i].event);
e.appendChild(doc.createTextNode(list[i].label));
element.appendChild(e);
}
return element;
}
/*!
\fn KeyButton::appendGroup(KeyGroup group)
*/
void KeyButton::appendGroup(KeyGroup group)
{
list.append(group);
if (list.count() == 1) {
current = list[0];
updateLabel();
}
}
/*!
\fn KeyButton::changeGroup(const QString &name)
*/
void KeyButton::changeGroup(const QString &name)
{
for(unsigned int i = 0;i < list.count();i ++) {
if ((list[i].name == name) || ((list[i].name == "") && (name == ""))) {
current = list[i];
break;
}
}
updateLabel();
}
/*!
\fn KeyButton::keyPressed()
*/
void KeyButton::keyPressed()
{
if (!holded) {
timer->stop();
timer->start(1000);
emit pressed(current.event,current.modifier);
} else {
setPaletteBackgroundColor(color);
holded = false;
}
}
/*!
\fn KeyButton::keyReleased()
*/
void KeyButton::keyReleased()
{
timer->stop();
if (!holded)
emit released(current.event,current.modifier);
}
/*!
\fn KeyButton::keyToggled(bool s)
*/
void KeyButton::keyToggled(bool s)
{
if (s) keyPressed();
else keyReleased();
}
/*!
\fn KeyButton::hold()
*/
void KeyButton::hold()
{
if (!isToggleButton()) {
holded = true;
setPaletteBackgroundColor(QColor("red"));
}
}