whyclwt
2022-05-18 ab72c6e114d7c4e83564194367ba623ca23bf09d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/***************************************************************************
**
**  <SYSZUXpinyin 1.0 , a chinese input method based on Qt for Embedded linux>
**  Copyright (C) <2010> <Gemfield> <gemfield@civilnet.cn>
**
**  This program is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License version 3 as published
**  by the Free Software Foundation.
**
**  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, see <http://www.gnu.org/licenses/>.
**
**  If you have questions regarding the use of this file, please contact
**  Gemfield at gemfield@civilnet.cn or post your questions at
**  http://civilnet.cn/syszux/bbs
**
****************************************************************************/
 
#include <QtGui>
#include "syszuxpinyin.h"
#include "Driver/buzzerdrv.h"
 
QString numberlist[10]={"1","2","3","4","5","6","7","8","9","0"};
QString syszux_lower_letter[26]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u",
                            "v","w","x","y","z"};
QString syszux_upper_letter[26]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U",
                            "V","W","X","Y","Z"};
 
//SyszuxPinyin::SyszuxPinyin(QWidget *parent, QLineEdit *edit, Work_Thread *thread) :
//    QDialog(parent,Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint),
//    button_group(new QButtonGroup(this)),
//    input_method(0),
//    lower_upper(0),
//    page_count(0)
//{
//    int ret=0;
//    setupUi(this);
//    setGeometry(QRect(0,100,800,380));
//    work_thread = thread;
//    connect(work_thread,SIGNAL(SentInterface(int)),this,SLOT(UpdateInterface(int)));
//    lineEdit = edit;
//    initGb();
//    QFile file;
//    file.setFileName(":new/prefix1/syszuxpinyin");
//    if( !file.open(QFile::ReadOnly))
//        QMessageBox::warning(0,tr("syszuxpinyin"),tr("can't load"));
//    QTextStream pinyin_file(&file);
 
//    regExp.setCaseSensitivity(Qt::CaseSensitive);
//    regExp.setPattern(QString("([a-z]+)"));
 
//    while(!pinyin_file.atEnd())
//    {
//        QString data = pinyin_file.readLine();//.readLine();
//        ret = regExp.indexIn(data,0,QRegExp::CaretAtZero);
//        pinyin_map.insert(regExp.cap(1),data.left(ret));
//    }
//    //connect(this,SIGNAL(sendPinyin(QString)),parent,SLOT(confirmString(QString)));
 
//    lineEdit_ALL->setText(edit->text());
//    input_method = 0;//0为英文 1为中文
//    lower_upper = 0;//0为小写 1为大写
//    pushButton_up->setVisible(false);
//    pushButton_down->setVisible(false);
//    ChooseBtnVisible(false);
//}
 
SyszuxPinyin::SyszuxPinyin(QWidget *parent, QLineEdit *edit) :
    QDialog(parent,Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint),
    button_group(new QButtonGroup(this)),
    input_method(0),
    lower_upper(1),
    page_count(0)
{
    int ret=0;
    setupUi(this);
    setGeometry(QRect(0,100,800,380));
    //work_thread = thread;
    //connect(work_thread,SIGNAL(SentInterface(int)),this,SLOT(UpdateInterface(int)));
    lineEdit = edit;
    initGb();
    QFile file;
    file.setFileName(":new/prefix1/syszuxpinyin");
    if( !file.open(QFile::ReadOnly))
        QMessageBox::warning(0,tr("syszuxpinyin"),tr("can't load"));
    QTextStream pinyin_file(&file);
 
    regExp.setCaseSensitivity(Qt::CaseSensitive);
    regExp.setPattern(QString("([a-z]+)"));
 
    while(!pinyin_file.atEnd())
    {
        QString data = pinyin_file.readLine();//.readLine();
        ret = regExp.indexIn(data,0,QRegExp::CaretAtZero);
        pinyin_map.insert(regExp.cap(1),data.left(ret));
    }
    //connect(this,SIGNAL(sendPinyin(QString)),parent,SLOT(confirmString(QString)));
 
    lineEdit_ALL->setText(edit->text());
 
    if(1 == input_method){//0为英文 1为中文
        QIcon *icon = new QIcon(":new/prefix1/type_cn.png");
        pushButton_ENCN->setIcon(*icon);
    }
    else{
        QIcon *icon = new QIcon(":new/prefix1/type_en.png");
        pushButton_ENCN->setIcon(*icon);
    }
 
    if(0 == lower_upper){//0为小写 1为大写
        QIcon *icon = new QIcon(":new/prefix1/low.png");
        pushButton_capital->setIcon(*icon);
    }
    else{
        QIcon *icon = new QIcon(":new/prefix1/up.png");
        pushButton_capital->setIcon(*icon);
    }
 
    pushButton_up->setVisible(false);
    pushButton_down->setVisible(false);
    ChooseBtnVisible(false);
}
 
SyszuxPinyin::~SyszuxPinyin()
{
}
void SyszuxPinyin::initGb()
{
    QPushButton *pushButton=new QPushButton(this);
    pushButton->hide();
    pushButton=pushButton_up;
    for(int i=1;i<54;i++)
    {
        connect(pushButton,SIGNAL(pressed()),this,SLOT(btn_beep()));
        button_vector.push_back(pushButton);
        button_group->addButton(pushButton,i);
        pushButton=qobject_cast<QPushButton *>(pushButton->nextInFocusChain());
    }
    connect(button_group,SIGNAL(buttonClicked(int)),SLOT(buttonClickResponse(int)));
}
void SyszuxPinyin::buttonClickResponse(int gemfield)
{
    if(gemfield==1)//上页
    {
        selectHanziPre();
        return;
    }
    else if(gemfield==2)//下页
    {
        selectHanziNext();
        return;
    }
    else if(gemfield==3)//大小写
    {
         changeLowerUpper();
         return;
    }
    else if(gemfield==4)//退格
    {
        deleteString();
        return;
    }
    else if(gemfield==5)//清空
    {
        pushButton_up->setVisible(false);
        pushButton_down->setVisible(false);
        ChooseBtnVisible(false);
        lineEdit_ALL->clear();
        lineEdit_py->clear();
        //clearString();
        return;
    }
    else if(gemfield==6)//符号键
    {
        lineEdit_py->clear();
 
        pushButton_up->setVisible(false);
        pushButton_down->setVisible(false);
        ChooseBtnVisible(true);
        pushButton_choose1->setText(",");
        pushButton_choose2->setText(".");
        pushButton_choose3->setText("(");
        pushButton_choose4->setText(")");
        pushButton_choose5->setText("-");
        pushButton_choose6->setText("_");
        pushButton_choose7->setText("/");
        return;
    }
    else if(gemfield==7)//空格键
    {
        if(pushButton_choose1->text()!="" && lineEdit_py->text()!="")
        {
            lineEdit_ALL->insert(pushButton_choose1->text());
        }
        else
        {
            event=new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier," ");
            lineEdit_ALL->setFocus();
            QApplication::sendEvent(focusWidget(),event);
        }
        pushButton_up->setVisible(false);
        pushButton_down->setVisible(false);
        ChooseBtnVisible(false);
        lineEdit_py->clear();
        //clearString();
 
    }
    else if(gemfield==8)//中英切换
    {
        changeInputMethod();
        return;
    }
    else if(gemfield==9)//取消键
    {
        pushButton_up->setVisible(false);
        pushButton_down->setVisible(false);
        ChooseBtnVisible(false);
        lineEdit_py->clear();
        //clearString();
        this->hide();
        return;
    }
    else if(gemfield==10)//确认
    {
        affirmString();
        return;
    }
    else if(gemfield<=17)//选字
    {
        lineEdit_ALL->insert(button_vector.at(gemfield-1)->text());
        lineEdit_py->clear();
        pushButton_up->setVisible(false);
        pushButton_down->setVisible(false);
        ChooseBtnVisible(false);
        //clearString();
        return;
    }
    else if(gemfield>=18 && gemfield<=27)
    {
        event=new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier,numberlist[gemfield-18]);
        lineEdit_ALL->setFocus();
        QApplication::sendEvent(focusWidget(),event);
    }
    else if(gemfield>=28 && gemfield<=53)
    {
        if(lower_upper)
            event=new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier,syszux_upper_letter[gemfield-28]);
        else
            event=new QKeyEvent(QEvent::KeyPress, 0, Qt::NoModifier,syszux_lower_letter[gemfield-28]);
        if(input_method)//中文
        {
            lineEdit_py->setFocus();
            if(!pushButton_choose1->isVisible())
            {
                ChooseBtnVisible(true);
            }
            QApplication::sendEvent(focusWidget(),event);
            matching(lineEdit_py->text());
        }
        else
        {
            lineEdit_ALL->setFocus();
            QApplication::sendEvent(focusWidget(),event);
        }
    }
}
void SyszuxPinyin::matching(QString gemfield)
{
    pinyin_list = pinyin_map.values(gemfield);
    changePage(0);
    page_count=0;
}
 
void SyszuxPinyin::changePage(int index)
{
    int count = pinyin_list.size();
    int i=index*7,j=9;
 
    while(j++ != 16 )
    {
        button_vector.at(j)->setText(tr(pinyin_list.value(count-(++i)).toAscii()));
    }
    if(index==0)
        pushButton_up->setVisible(false);
    else
        pushButton_up->setVisible(true);
    if(pinyin_list.size()>(index*7+7))
        pushButton_down->setVisible(true);
    else
        pushButton_down->setVisible(false);
}
 
void SyszuxPinyin::selectHanziPre()
{
    changePage(--page_count);
}
 
void SyszuxPinyin::selectHanziNext()
{
    changePage(++page_count);
}
void SyszuxPinyin::clearString()
{
    int i=9;
    while(++i!=17)
        button_vector.at(i)->setText("");
}
void SyszuxPinyin::changeInputMethod()
{
    lineEdit_py->clear();
    if(input_method==0)//英文->中文
    {
        if(lower_upper)
        {
            lower_upper=0;//大写->小写
            QIcon *icon = new QIcon(":new/prefix1/low.png");
            pushButton_capital->setIcon(*icon);
            int i=26;
            while(++i!=53)
                button_vector.at(i)->setText(syszux_lower_letter[i-27]);
        }
        input_method=1;
        QIcon *icon = new QIcon(":new/prefix1/type_cn.png");
        pushButton_ENCN->setIcon(*icon);
   }
   else
   {
       pushButton_up->setVisible(false);
       pushButton_down->setVisible(false);
       ChooseBtnVisible(false);
       input_method=0;//中文->英文
       QIcon *icon = new QIcon(":new/prefix1/type_en.png");
       pushButton_ENCN->setIcon(*icon);
   }
}
void SyszuxPinyin::changeLowerUpper()
{
    if(lower_upper==0)//小写->大写
    {
        lower_upper=1;
        QIcon *icon = new QIcon(":new/prefix1/up.png");
        pushButton_capital->setIcon(*icon);
        input_method=0;//设为英文
        lineEdit_py->clear();
        pushButton_up->setVisible(false);
        pushButton_down->setVisible(false);
        ChooseBtnVisible(false);
        icon = new QIcon(":new/prefix1/type_en.png");
        pushButton_ENCN->setIcon(*icon);
        int i=26;
        while(++i!=53)
            button_vector.at(i)->setText(syszux_upper_letter[i-27]);
    }
    else
    {
        lower_upper=0;//大写->小写
        QIcon *icon = new QIcon(":new/prefix1/low.png");
        pushButton_capital->setIcon(*icon);
        int i=26;
        while(++i!=53)
            button_vector.at(i)->setText(syszux_lower_letter[i-27]);
    }
}
void SyszuxPinyin::deleteString()
{
    event=new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
    if(input_method)
    {
        lineEdit_py->text().isEmpty()?lineEdit_ALL->setFocus():lineEdit_py->setFocus();
        QApplication::sendEvent(focusWidget(),event);
        matching(lineEdit_py->text());
    }
    else
    {
        lineEdit_ALL->setFocus();
        QApplication::sendEvent(focusWidget(),event);
    }
}
void SyszuxPinyin::affirmString()
{
    //emit sendPinyin(lineEdit_ALL->text());
    lineEdit->setText(lineEdit_ALL->text());
    this->hide();
}
 
void SyszuxPinyin::ChooseBtnVisible(bool en)
{
    pushButton_choose1->setVisible(en);
    pushButton_choose2->setVisible(en);
    pushButton_choose3->setVisible(en);
    pushButton_choose4->setVisible(en);
    pushButton_choose5->setVisible(en);
    pushButton_choose6->setVisible(en);
    pushButton_choose7->setVisible(en);
}
 
void SyszuxPinyin::btn_beep(void)
{
    BuzzerDrv BD;
    BD.BuzzerBeep(50);
}
 
//void SyszuxPinyin::UpdateInterface(int interface)
//{
//    this->close();
//}