00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "multikey.h"
00016
00017
00018 #include <opie2/otaskbarapplet.h>
00019 #include <qpe/qcopenvelope_qws.h>
00020 #include <qpe/qpeapplication.h>
00021 using namespace Opie::Ui;
00022
00023
00024 #include <qdir.h>
00025
00026
00027 Multikey::Multikey(QWidget *parent) : QLabel(parent), popupMenu(this), current("EN")
00028 {
00029 QCopChannel* swChannel = new QCopChannel("MultiKey/Switcher", this);
00030 connect( swChannel, SIGNAL(received(const QCString&,const QByteArray&)),
00031 this, SLOT(message(const QCString&,const QByteArray&)));
00032
00033 QPEApplication::setStylusOperation(this, QPEApplication::RightOnHold);
00034 lang = 0;
00035 QCopEnvelope e("MultiKey/Keyboard", "getmultikey()");
00036 setText("EN");
00037 popupMenu.insertItem("EN", 0);
00038 show();
00039 }
00040
00041 void Multikey::mousePressEvent(QMouseEvent *ev)
00042 {
00043 if (!sw_maps.count())
00044 return;
00045
00046 if (ev->button() == RightButton) {
00047
00048 QPoint p = mapToGlobal(QPoint(0, 0));
00049 QSize s = popupMenu.sizeHint();
00050 int opt = popupMenu.exec(QPoint(p.x() + (width() / 2) - (s.width() / 2),
00051 p.y() - s.height()), 0);
00052
00053 if (opt == -1)
00054 return;
00055 lang = opt;
00056
00057 QCopEnvelope e("MultiKey/Keyboard", "setmultikey(QString)");
00058 e << sw_maps[lang];
00059 setText(labels[lang]);
00060 }
00061 QWidget::mousePressEvent(ev);
00062 }
00063
00064 void Multikey::mouseReleaseEvent(QMouseEvent *ev)
00065 {
00066 if (!sw_maps.count())
00067 return;
00068
00069 lang = lang < sw_maps.count()-1 ? lang+1 : 0;
00070 QCopEnvelope e("MultiKey/Keyboard", "setmultikey(QString)");
00071
00072 e << sw_maps[lang];
00073 setText(labels[lang]);
00074 }
00075
00076 void Multikey::message(const QCString &message, const QByteArray &data)
00077 {
00078 if ( message == "setsw(QString,QString)" ) {
00079
00080 QDataStream stream(data, IO_ReadOnly);
00081 QString maps, current_map;
00082 stream >> maps >> current_map;
00083 QStringList sw = QStringList::split(QChar('|'), maps);
00084 sw.append(current_map);
00085
00086 QDir map_dir(QPEApplication::qpeDir() + "share/multikey/", "*.keymap");
00087 lang = 0;
00088 labels.clear();
00089 sw_maps.clear();
00090 popupMenu.clear();
00091
00092 for (uint i = 0; i < sw.count(); ++i) {
00093 QString keymap_map;
00094 if (sw[i][0] != '/') {
00095
00096 keymap_map = map_dir.absPath() + "/" + sw[i];
00097 } else {
00098
00099 if ((map_dir.exists(QFileInfo(sw[i]).fileName(), false)
00100 && i != sw.count()-1) || !QFile::exists(sw[i])) {
00101 continue;
00102 }
00103 keymap_map = sw[i];
00104 }
00105
00106 QFile map(keymap_map);
00107 if (map.open(IO_ReadOnly)) {
00108 QString line;
00109
00110 map.readLine(line, 1024);
00111 while (!map.atEnd()) {
00112
00113 if (line.find(QRegExp("^sw\\s*=\\s*")) != -1) {
00114
00115 if (i != sw.count()-1) {
00116 if (keymap_map == current_map) {
00117 lang = i;
00118 }
00119 sw_maps.append(keymap_map);
00120 labels.append(line.right(line.length() - line.find(QChar('=')) - 1).stripWhiteSpace());
00121 popupMenu.insertItem(labels[labels.count()-1], labels.count()-1);
00122 } else {
00123 current = line.right(line.length() - line.find(QChar('=')) - 1).stripWhiteSpace();
00124 }
00125 break;
00126 }
00127 map.readLine(line, 1024);
00128 }
00129 map.close();
00130 }
00131 }
00132
00133 setText(current);
00134 }
00135 }
00136
00137 int Multikey::position()
00138 {
00139 return 10;
00140 }
00141
00142 EXPORT_OPIE_APPLET_v1( Multikey )