Newer
Older
scim-wnn / nagisa / src / nagisakeyboard.cpp
@tamra tamra on 29 Oct 2005 5 KB なぎさたん投入。
/***************************************************************************
 *   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 "nagisakeyboard.h"
#include <qfile.h>

NagisaKeyboard::NagisaKeyboard(QWidget *parent, const char *name)
 : QWidget(parent, name)
{
    init();
}

NagisaKeyboard::NagisaKeyboard(const QString &xml, QWidget *parent, const char *name)
 : QWidget(parent, name)
{
    init();
    setTableFile(xml);
}

void NagisaKeyboard::init()
{
    grid = new QGridLayout(this,1,1,0,20);
}


NagisaKeyboard::~NagisaKeyboard()
{
}




/*!
    \fn NagisaKeyboard::layout()
 */
void NagisaKeyboard::layout()
{
    for(unsigned int i = 0;i < keys.count();i ++) {
        grid->addMultiCellWidget(keys[i],
            keys[i]->getY(),keys[i]->getY() + keys[i]->getH(),
            keys[i]->getX(),keys[i]->getX() + keys[i]->getW());
    }
    adjustSize();
    show();
}


/*!
    \fn NagisaKeyboard::pressed(const QString &name,const QString &modifier)
 */
void NagisaKeyboard::pressed(const QString &name,const QString &modifier)
{
    QString keyEvent;
    if (uniq(modifiers).join("+") != "") keyEvent = uniq(modifiers).join("+") + "+" + name;
    else keyEvent = name;
    emit status(keyEvent);
    show();
    emit pressed(keyEvent);
    if (modifier != "") {
        modifiers.append(modifier);
        modifiers.sort();
        for(unsigned int i = 0;i < keys.count();i ++) {
            keys[i]->changeGroup(uniq(modifiers).join("+"));
        }
    }
}


/*!
    \fn NagisaKeyboard::released(const QString &name,const QString &modifier)
 */
void NagisaKeyboard::released(const QString &name,const QString &modifier)
{
    QString keyEvent;
    if (uniq(modifiers).join("+") != "") keyEvent = uniq(modifiers).join("+") + "+" + name;
    else keyEvent = name;
    emit status("");
    show();
    emit released(keyEvent);
    if (modifier != "") {
        // iteratorでremoveしなきゃダメ。
        modifiers.remove(modifiers.find(modifier));
        for(unsigned int i = 0;i < keys.count();i ++) {
            keys[i]->changeGroup(uniq(modifiers).join("+"));
        }
    }
}




/*!
    \fn NagisaKeyboard::parser(const QString &xml)
 */
void NagisaKeyboard::parser(const QString &xml)
{
    QFile file(xml);
    if (!file.open(IO_ReadOnly)) {
        file.setName(QString(NAGISADATADIR) + "/" + xml);
        if (!file.open(IO_ReadOnly)) {
            return;
        }
    }
    if (!doc.setContent(&file)) return;
    file.close();
    QDomElement root = doc.documentElement();
    QDomNode node = root.firstChild();
    while(!node.isNull()) {
        QDomElement element = node.toElement();
        if(!element.isNull()) {
            if (element.tagName() == "key") {
                KeyButton *key = new KeyButton(this,
                    element.attribute("x","0").toInt(),
                    element.attribute("y","0").toInt(),
                    element.attribute("width","2").toInt(),
                    element.attribute("height","2").toInt(),
                    element.attribute("color","white"),
                    element.attribute("type","button") == "toggle"
                );
                connect(key,SIGNAL(pressed(const QString&, const QString& )),this,SLOT(pressed(const QString&, const QString& )));
                connect(key,SIGNAL(released(const QString&, const QString& )),this,SLOT(released(const QString&, const QString& )));

                QDomNode group = element.firstChild();
                while(!group.isNull()) {
                    QDomElement groupElement = group.toElement();
                    if (!groupElement.isNull()) {
                        if (groupElement.tagName() == "group") {
                            key->appendGroup(KeyGroup(
                                groupElement.attribute("name",""),
                                groupElement.text(),
                                groupElement.attribute("event",""),
                                groupElement.attribute("modifier","")
                            ));
                        }
                    }
                    group = group.nextSibling();
                }
                keys.append(key);
            }
        }
        node = node.nextSibling();
    }
}


/*!
    \fn NagisaKeyboard::setTableFile(const QString &xml)
 */
void NagisaKeyboard::setTableFile(const QString &xml)
{
    parser(xml);
    layout();
    show();
}


/*!
    \fn NagisaKeyboard::uniq(const QStringList &list)
 */
const QStringList NagisaKeyboard::uniq(const QStringList &list)
{
    QStringList res;
    for(unsigned int i = 0;i < list.count();i ++) {
        if (!res.contains(list[i])) res.append(list[i]);
    }
    return res;
}