moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / ktouch / src / ktouchkeys.cpp
blobc2b0db77d9ced83f05b02ba5a7ed407b4393eb93
1 /***************************************************************************
2 * ktouchkeys.cpp *
3 * -------------- *
4 * Copyright (C) 2000 by Håvard Frøiland, 2004 by Andreas Nicolai *
5 * ghorwin@users.sourceforge.net *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 ***************************************************************************/
13 #include "ktouchkeys.h"
14 #include <kdebug.h>
15 #include <algorithm> // for std::min
17 #include "ktouch.h"
18 #include "prefs.h"
20 // Initialisation of static variables
21 int KTouchFingerKey::m_fingerKeyCount = 0;
22 // -----------------------------------------------------------------------------
25 // ***** Implementation of class KTouchKey ****
27 void KTouchKey::paint(QPainter& p) const {
28 // We simply paint the key using the current brush and pen, so the derived classes
29 // will have to care about that
30 p.fillRect(m_xS, m_yS, m_wS, m_hS, p.brush());
31 p.drawRect(m_xS, m_yS, m_wS, m_hS);
34 void KTouchKey::resize(double scale) {
35 m_xS = static_cast<int>(scale*m_x);
36 m_yS = static_cast<int>(scale*m_y);
37 m_wS = static_cast<int>(scale*m_w);
38 m_hS = static_cast<int>(scale*m_h);
39 // we set the font from the keyboard widget
40 m_font=Prefs::keyboardFont();
41 m_font.setPointSize( static_cast<int>(std::min(m_wS,m_hS)/m_font_scale) );
43 // -----------------------------------------------------------------------------
47 // **** Implementation of class KTouchNormalKey ****
49 KTouchNormalKey::KTouchNormalKey(const QChar& keyChar, const QString& keyText, int x, int y, int w, int h)
50 : KTouchKey(keyChar, keyText, x, y, w, h), m_colorIndex(0)
52 m_type = NORMAL_KEY;
53 m_font_scale =2;
56 void KTouchNormalKey::paint(QPainter& p) const {
57 const KTouchColorScheme& colorScheme = KTouchPtr->colorSchemes()[Prefs::colorScheme()];
58 QColor textColor;
59 if (m_isNextKey) {
60 // mark the key as "next"
61 p.setBrush( colorScheme.m_backgroundH );
62 p.setPen( colorScheme.m_frame );
63 textColor = colorScheme.m_textH;
65 else {
66 p.setBrush( colorScheme.m_background[m_colorIndex] );
67 p.setPen( colorScheme.m_frame );
68 textColor = colorScheme.m_text;
70 KTouchKey::paint(p); // call the parents paint() function
71 p.setPen(textColor);
73 p.setFont( m_font );
74 p.drawText(m_xS, m_yS, m_wS, m_hS, QPainter::AlignCenter, m_keyText);
76 // --------------------------------------------------------------
80 // **** Implementation of class KTouchFingerKey ****
82 KTouchFingerKey::KTouchFingerKey(const QChar& keyChar, const QString& keyText, int x, int y, int w, int h)
83 : KTouchNormalKey(keyChar, keyText, x, y, w, h)
85 m_colorIndex = m_fingerKeyCount++;
86 if (m_colorIndex>=8) {
87 kdDebug() << "[KTouchFingerKey::KTouchFingerKey] Number of finger keys = "
88 << m_colorIndex << "! Setting colour index to 0" << endl;
89 m_colorIndex=0;
91 m_type = FINGER_KEY;
92 m_font_scale =2;
95 void KTouchFingerKey::paint(QPainter& p) const {
96 const KTouchColorScheme& colorScheme = KTouchPtr->colorSchemes()[Prefs::colorScheme()];
97 p.setFont( m_font );
98 if (m_isActive) {
99 p.setBrush( colorScheme.m_background[m_colorIndex] );
100 p.setPen( colorScheme.m_frame );
101 KTouchKey::paint(p); // draw background and frame
102 p.setPen( QPen(colorScheme.m_frame,3) );
103 p.drawRect(m_xS+2, m_yS+2, m_wS-4, m_hS-4); // mark it as "active"
104 p.setPen( colorScheme.m_text );
105 p.drawText(m_xS, m_yS, m_wS, m_hS, QPainter::AlignCenter, m_keyText);
107 else if (m_isNextKey) {
108 p.setBrush( colorScheme.m_backgroundH );
109 p.setPen( colorScheme.m_frame );
110 KTouchKey::paint(p);
111 p.setPen( colorScheme.m_textH );
112 p.drawText(m_xS, m_yS, m_wS, m_hS, QPainter::AlignCenter, m_keyText);
114 else {
115 p.setBrush( colorScheme.m_background[m_colorIndex] );
116 p.setPen( colorScheme.m_frame );
117 KTouchKey::paint(p);
118 p.drawRoundRect(m_xS+2, m_yS+2, m_wS-4, m_hS-4);
119 p.setPen( colorScheme.m_text );
120 p.drawText(m_xS, m_yS, m_wS, m_hS, QPainter::AlignCenter, m_keyText);
123 // --------------------------------------------------------------
127 // **** Implementation of class KTouchControlKey ****
129 KTouchControlKey::KTouchControlKey(const QChar& keyChar, const QString& keyText, int x, int y, int w, int h)
130 : KTouchKey(keyChar, keyText, x, y, w, h)
132 m_type = CONTROL_KEY;
133 m_font_scale = 4;
136 void KTouchControlKey::paint(QPainter& p) const {
137 const KTouchColorScheme& colorScheme = KTouchPtr->colorSchemes()[Prefs::colorScheme()];
138 p.setFont( m_font );
139 QColor textColor;
140 if (m_isActive) {
141 p.setBrush( colorScheme.m_cBackgroundH );
142 p.setPen( colorScheme.m_frame );
143 textColor = colorScheme.m_cTextH;
145 else if (m_isNextKey) {
146 p.setBrush( colorScheme.m_backgroundH );
147 p.setPen( colorScheme.m_frame );
148 textColor = colorScheme.m_textH;
150 else {
151 p.setBrush( colorScheme.m_cBackground );
152 p.setPen( colorScheme.m_frame );
153 textColor = colorScheme.m_cText;
155 KTouchKey::paint(p);
156 p.setPen( textColor );
157 int h = std::min(m_wS, m_hS);
158 int ch = static_cast<int>(h*0.5); // the height for the special chars
159 if (m_keyText=="Shift") {
160 int x = m_xS+h/2;
161 int y = m_yS+m_hS/2;
162 p.moveTo(x-ch/2, y);
163 p.lineTo(x-ch/4, y);
164 p.lineTo(x-ch/4, y+ch/2);
165 p.lineTo(x+ch/4, y+ch/2);
166 p.lineTo(x+ch/4, y);
167 p.lineTo(x+ch/2, y);
168 p.lineTo(x, y-ch/2);
169 p.lineTo(x-ch/2, y);
171 else if (m_keyText=="CapsLock") {
172 int x = m_xS+h/2;
173 int y = m_yS+m_hS/2;
174 p.moveTo(x-ch/2, y);
175 p.lineTo(x-ch/4, y);
176 p.lineTo(x-ch/4, y-ch/2);
177 p.lineTo(x+ch/4, y-ch/2);
178 p.lineTo(x+ch/4, y);
179 p.lineTo(x+ch/2, y);
180 p.lineTo(x, y+ch/2);
181 p.lineTo(x-ch/2, y);
183 else if (m_keyText=="Tab") {
184 int xleft = m_xS+h/2-ch/2;
185 int xright = m_xS + std::min(m_wS-h/2+ch/2,h);
186 int y = m_yS+m_hS/2;
187 p.drawLine(xleft, y,xleft, y-ch/2);
188 p.drawLine(xleft, y-ch/4, xright, y-ch/4);
189 p.drawLine(xleft, y-ch/4, xleft+ch/2, y-static_cast<int>(ch*0.10));
190 p.drawLine(xleft, y-ch/4, xleft+ch/2, y-static_cast<int>(ch*0.40));
191 p.drawLine(xright, y, xright, y+ch/2);
192 p.drawLine(xleft, y+ch/4, xright, y+ch/4);
193 p.drawLine(xright, y+ch/4, xright-ch/2, y+static_cast<int>(ch*0.10));
194 p.drawLine(xright, y+ch/4, xright-ch/2, y+static_cast<int>(ch*0.40));
197 else if (m_keyText=="BackSpace") {
198 int xleft = m_xS+h/2-ch/2;
199 int xright = m_xS + std::min(m_wS-h/2+ch/2,h);
200 int y = m_yS+m_hS/2;
201 p.drawLine(xleft, y,xright, y);
202 p.drawLine(xleft, y, xleft+ch/2, y-static_cast<int>(ch*0.15));
203 p.drawLine(xleft, y, xleft+ch/2, y+static_cast<int>(ch*0.15));
205 else if (m_keyText=="Enter") {
206 int xleft = m_xS+h/2-ch/2;
207 int xright = m_xS + std::min(m_wS-h/2+ch/2,h);
208 int y = m_yS+m_hS/2;
209 p.drawLine(xright, y-ch/2,xright, y);
210 p.drawLine(xleft, y,xright, y);
211 p.drawLine(xleft, y, xleft+ch/3, y-static_cast<int>(ch*0.15));
212 p.drawLine(xleft, y, xleft+ch/3, y+static_cast<int>(ch*0.15));
214 else if (m_keyText=="AltGr") {
215 p.drawText(m_xS, m_yS, m_wS, m_hS, QPainter::AlignCenter | QPainter::AlignVCenter, "Alt Gr");
217 else
218 p.drawText(m_xS, m_yS, m_wS, m_hS, QPainter::AlignCenter | QPainter::AlignVCenter, m_keyText);