initial version
[kumushedit] / vkb.cpp
1 #include <QGridLayout>
2 #include <QVBoxLayout>
3 #include <QPushButton>
4 #include <QString>
5 #include <QDebug>
6 #include <QStyle>
7 #include <QFont>
8 #include <QIcon>
9 #include "vkb.h"
10
11 static unsigned short keys[3][10]={
12     { 0x0686, 0x06cb, 0x06d0, 0x0631, 0x062a, 0x064a, 0x06c7, 0x06ad, 0x0648, 0x067e },  //ch, w, e, r, t, y, u, ng, o, p
13     { 0x06be, 0x0633, 0x062f, 0x0627, 0x06d5, 0x0649, 0x0642, 0x0643, 0x0644, 0x061B },  //h,  s, d, a, e, i, q, k,  l, ؛
14     { 0x0632, 0x0634, 0x063a, 0x06c8, 0x0628, 0x0646, 0x0645, 0x060C, 0x002E, 0x0626 }   //z, sh, gh,ü, b, n, m, ،,  ., hemze
15 };
16
17 static unsigned short skeys[3][10]={
18     { 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039 },  //0, 1, 2, 3, 4, 5, 6, 7, 8, 9
19     { 0x00BB, 0x00AB, 0x0698, 0x0641, 0x06af, 0x062e, 0x062c, 0x06c6, 0x002B, 0x003A },  //», «, dr, f, g, x, j, ö, +, :
20     { 0x0021, 0x066A, 0x066D, 0x002f, 0x002d, 0x2014, 0x003D, 0x203a, 0x2039, 0x061F }   //!, %, *, /, -, —, =, ›, ‹, ؟
21 };
22
23 static int BUTTON_MAXIMUM_HEIGHT = 50;
24
25 VKB::VKB(QWidget *parent) :
26     QWidget(parent),
27     mShift(false)
28 {
29     mUGFont = new QFont("UyghurEdit Riwaj");
30     QVBoxLayout *layout = new QVBoxLayout;
31     mvkb = createVKB(keys);
32     layout->addWidget(mvkb);
33     msvkb = createVKB(skeys);
34     layout->addWidget(msvkb);
35     msvkb->hide();
36     createToolBar();
37     layout->addWidget(mtbar);
38     layout->setSpacing(0);
39     setLayout(layout);
40 }
41
42 VKB::~VKB()
43 {
44     delete mUGFont;
45 }
46
47 void VKB::keyClicked()
48 {
49     QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
50     emit VKBButtonClicked(clickedButton->text());
51 }
52
53 void VKB::shiftButtonClicked()
54 {
55     if (mShiftToggled) {
56         msvkb->hide();
57         mvkb->show();
58         mShift->setText("");
59         mShiftToggled=false;
60     } else {
61         mvkb->hide();
62         msvkb->show();
63         mShift->setText("*");
64         mShiftToggled=true;
65     }
66 }
67
68 void VKB::spaceButtonClicked()
69 {
70     emit VKBButtonClicked(QString(" "));
71 }
72
73 void VKB::enterButtonClicked()
74 {
75     emit VKBButtonClicked(QString("\n"));
76 }
77
78 QWidget* VKB::createVKB(const unsigned short  keys[3][10])
79 {
80     QWidget *vkb = new QWidget();
81     QGridLayout *layout = new QGridLayout;
82     for (int i=0; i<3; i++) {
83         for (int j=0; j<10; j++) {
84             QPushButton *key = new QPushButton(QString(keys[i][j]));
85             qDebug()<<key->fontInfo().family();
86             key->setFont(*mUGFont);
87             key->setMaximumHeight(BUTTON_MAXIMUM_HEIGHT);
88             qDebug()<<key->fontInfo().family();
89             connect(key, SIGNAL(clicked()), this, SLOT(keyClicked()));
90             layout->addWidget(key, i, j);
91         }
92     }
93     layout->setSpacing(0);
94     vkb->setLayout(layout);
95     return vkb;
96 }
97
98 /**
99  * toolbar always visible and it contains following buttons
100  * Open: open a file
101  * Save: save Text to a file
102  * Copy: copy the content to clipboard
103  * Shift: controls to switch between two VKB
104  * forward: move the cursor to the forward
105  * Space: input blank space
106  * backward: move the cursor to the backward
107  * Enter: input line break
108  * Backspace: delete the character before the cursor
109  */
110 void VKB::createToolBar()
111 {
112     mtbar = new QWidget();
113     QHBoxLayout *layout = new QHBoxLayout;
114     mOpen = new QPushButton(QIcon(":/images/open.png"), QString());
115     connect(mOpen, SIGNAL(clicked()), this, SIGNAL(openButtonClicked()));
116     layout->addWidget(mOpen);
117
118     mSave = new QPushButton(QIcon(":/images/save.png"), QString());
119     connect(mSave, SIGNAL(clicked()), this, SIGNAL(saveButtonClicked()));
120     layout->addWidget(mSave);
121
122     mCopy = new QPushButton(QIcon(":/images/copy.png"), QString());
123     connect(mCopy, SIGNAL(clicked()), this, SIGNAL(copyButtonClicked()));
124     layout->addWidget(mCopy);
125
126     mPaste = new QPushButton(QIcon(":/images/paste.png"), QString());
127     connect(mPaste, SIGNAL(clicked()), this, SIGNAL(pasteButtonClicked()));
128     layout->addWidget(mPaste);
129
130     mShift = new QPushButton(QIcon(":/images/shift.png"), QString());
131     connect(mShift, SIGNAL(clicked()), this, SLOT(shiftButtonClicked()));
132     layout->addWidget(mShift);
133
134     mFW = new QPushButton(style()->standardIcon(QStyle::SP_ArrowLeft), QString());
135     connect(mFW, SIGNAL(clicked()), this, SIGNAL(fwButtonClicked()));
136     layout->addWidget(mFW);
137
138     mSpace = new QPushButton(QIcon(":/images/space.png"), QString());
139     connect(mSpace, SIGNAL(clicked()), this, SLOT(spaceButtonClicked()));
140     layout->addWidget(mSpace);
141
142     mBW = new QPushButton(style()->standardIcon(QStyle::SP_ArrowRight), QString());
143     connect(mBW, SIGNAL(clicked()), this, SIGNAL(bwButtonClicked()));
144     layout->addWidget(mBW);
145
146     mEnter = new QPushButton(QIcon(":/images/enter.png"), QString());
147     connect(mEnter, SIGNAL(clicked()), this, SLOT(enterButtonClicked()));
148     layout->addWidget(mEnter);
149
150     mBSpace = new QPushButton(QIcon(":/images/bkspc.png"), QString());
151     connect(mBSpace, SIGNAL(clicked()), this, SIGNAL(bspaceButtonClicked()));
152     layout->addWidget(mBSpace);
153     layout->setSpacing(0);
154     mtbar->setLayout(layout);
155 }