Newer
Older
scim-wnn / nagisa / src / nagisa.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.             *
 ***************************************************************************/

/*
#ifdef Q_WS_X11
# include <X11/Xlib.h>
#endif
*/

#define Uses_SCIM_SOCKET
#define Uses_SCIM_TRANSACTION
#define Uses_SCIM_HELPER
#include <scim.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>

#include "nagisa.h"

#include <nagisakeyboard.h>
#include <qlayout.h>


static scim::HelperAgent agent;

nagisa::nagisa(const QString &xml)
//    : QWidget( 0, "nagisa",Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_StaysOnTop)
    : QWidget( 0, "nagisa", Qt::WStyle_Customize | Qt::WX11BypassWM | Qt::WStyle_StaysOnTop)
{
    QString file = xml;
    if (file == "") file = "jp109.xml";
    id = -1;
    
    mousePress = false;
    
    QVBoxLayout *lay = new QVBoxLayout(this);
    lay->setAutoAdd(true);
    scaleButton = new QToolButton(this);
    scaleButton->setText(tr("Nagisa Keypad"));
    scaleButton->setMouseTracking(true);
    
    menu = new QPopupMenu(scaleButton);
    menu_scale = menu->insertItem(tr("hide"),this,SLOT(scale()));
    menu_connect = menu->insertItem(tr("connect"),this,SLOT(connectToScim()));
    menu_exit = menu->insertItem(tr("exit"),this,SLOT(close()));
    scaleButton->setPopup(menu);
    connect(scaleButton,SIGNAL(clicked()),this,SLOT(scale()));
    NagisaKeyboard *b = new NagisaKeyboard(file,this);
    contents = b;
    b->show();
    
    status = new QLabel(this);
    
    show();
    
    connect(b,SIGNAL(pressed(const QString& )),this,SLOT(sendKeyPressEvent(const QString& )));
    connect(b,SIGNAL(released(const QString& )),this,SLOT(sendKeyReleaseEvent(const QString& )));
    connect(b,SIGNAL(status(const QString& )),this,SLOT(updateStatus(const QString& )));
    
    connectToScim();
}


nagisa::~nagisa()
{
}




/*!
    \fn nagisa::sendKeyPressEvent(const QString &e)
 */
void nagisa::sendKeyPressEvent(const QString &e)
{
    if (id < 0) return;
    scim::KeyEvent key;
    if (!scim::scim_string_to_key(key,e)) return;
    agent.send_key_event(-1,"",key);
}

/*!
    \fn nagisa::sendKeyReleaseEvent(const QString &e)
 */
void nagisa::sendKeyReleaseEvent(const QString &e)
{
    if (id < 0) return;
    scim::KeyEvent key;
    if (!scim::scim_string_to_key(key,e)) return;
    key.mask |= scim::SCIM_KEY_ReleaseMask;
    agent.send_key_event(-1,"",key);
}



/*!
    \fn nagisa::mousePressEvent(QMouseEvent *e)
 */
void nagisa::mousePressEvent(QMouseEvent *e)
{
    mousePress = true;
    mouseX = e->x();
    mouseY = e->y();
}


/*!
    \fn nagisa::mouseReleaseEvent(QMouseEvent *e)
 */
void nagisa::mouseReleaseEvent(QMouseEvent *e)
{
    mousePress = false;
    mouseX = e->x();
    mouseY = e->y();
}


/*!
    \fn nagisa::mouseMoveEvent(QMouseEvent *e)
 */
void nagisa::mouseMoveEvent(QMouseEvent *e)
{
    if (mousePress) {
        move(e->globalX() - mouseX,e->globalY() - mouseY);
    }
}




/*!
    \fn nagisa::scale()
 */
void nagisa::scale()
{
    if (contents->isShown()) {
        contents->hide();
        status->hide();
        menu->changeItem(menu_scale,tr("show"));
    } else {
        contents->show();
        status->show();
        menu->changeItem(menu_scale,tr("hide"));
    }
    adjustSize();
}


/*!
    \fn nagisa::connectToScim()
 */
void nagisa::connectToScim()
{
    if (id < 0) {
        scim::HelperInfo info("e135e0ee-5588-423e-a027-f07d769c12a3","Nagisa","","Nagisa helper.",
            scim::SCIM_HELPER_STAND_ALONE | scim::SCIM_HELPER_NEED_SCREEN_INFO | scim::SCIM_HELPER_NEED_SPOT_LOCATION_INFO);
        // 以下、本来やはりこうするべきかと。
        // #ifdef Q_WS_X11
        // scim::String display = XDisplayString(qt_xdisplay());
        // #else
        // scim::String display = getenv("DISPLAY");
        // #endif
        scim::String display = getenv("DISPLAY");
        id = agent.open_connection(info,display);
    } else {
        agent.close_connection();
        id = -1;
    }

    if (id < 0) menu->changeItem(menu_connect,tr("connect"));
    else menu->changeItem(menu_connect,tr("disconnect"));
}


/*!
    \fn nagisa::updateStatus(const QString &s)
 */
void nagisa::updateStatus(const QString &s)
{
    status->setText(s);
}