Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

function_keyboard.cpp

Go to the documentation of this file.
00001 #include "function_keyboard.h"
00002 
00003 /* QT */
00004 #include <qlayout.h>
00005 #include <qlistbox.h>
00006 #include <qlabel.h>
00007 #include <qdir.h>
00008 
00009 #define DEFAULT_ROWS 2
00010 #define DEFAULT_COLS 12
00011 
00012 /* FunctionKeyboard {{{1 */
00013 
00014 FunctionKeyboard::FunctionKeyboard(QWidget *parent) :
00015     QFrame(parent), numRows(DEFAULT_ROWS), numCols(DEFAULT_COLS),
00016     pressedRow(0), pressedCol(0) {
00017 
00018     setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
00019 
00020     /*
00021      * all the saving/loading is now done in a profile. downside is that you cant modify
00022      * the keyboard for all profiles, but must do it on a profile-basis
00023      *
00024 
00025     Config conf("opie-console-keys");
00026     conf.setGroup("keys");
00027     for (uint r = 0; r < numRows; r++)
00028         for (uint c = 0; c < numCols; c++) {
00029 
00030             QString handle = "r" + QString::number(r) + "c" + QString::number(c);
00031             QStringList value_list = conf.readListEntry( handle, '|');
00032 
00033             if (value_list.isEmpty()) continue;
00034 
00035             keys.insert(
00036 
00037                  handle,
00038                  FKey (value_list[0], value_list[1], value_list[2].toUShort(), value_list[3].toUShort())
00039             );
00040         }
00041     */
00042     if (keys.isEmpty()) loadDefaults();
00043 
00044 
00045 
00046 }
00047 
00048 FunctionKeyboard::~FunctionKeyboard() {}
00049 
00050 void FunctionKeyboard::changeRows(int r) {
00051 
00052     numRows = r;
00053 
00054     // have to do this so the whole thing gets redrawn
00055     hide(); show();
00056 }
00057 void FunctionKeyboard::changeCols(int c) {
00058 
00059     numCols = c;
00060     keyWidth = (double)width()/numCols; // have to reset this thing too
00061     repaint(false);
00062 }
00063 void FunctionKeyboard::load (const Profile& prof) {
00064 
00065     keys.clear();
00066 
00067     numRows = prof.readNumEntry("keb_rows", 2);
00068     numCols = prof.readNumEntry("keb_cols", 10);
00069     keyWidth = (double)width()/numCols; // have to reset this thing too
00070 
00071     /* load all the keys to the keyboard */
00072     for (ushort i = 0; i <= numRows - 1; i++)
00073         for (ushort j = 0; j <= numCols - 1; j++) {
00074 
00075             QString h = "r" + QString::number(i) + "c" + QString::number(j);
00076             QString values = prof.readEntry("keb_" + h);
00077 
00078             if (!values.isEmpty()) {
00079 
00080                 QStringList l = QStringList::split(QChar('|'), values, TRUE);
00081                 keys[h] = FKey(l[0], l[1], l[2].toInt(), l[3].toInt());
00082 
00083                 // load pixmap if used
00084                 if (!l[1].isEmpty()) {
00085 
00086                     keys[h].pix = new QPixmap( Opie::Core::OResource::loadPixmap( "console/keys/" + l[1] ) );
00087                 }
00088             }
00089         }
00090 
00091     if (keys.isEmpty()) loadDefaults();
00092 
00093     hide();
00094     show();
00095 
00096 }
00097 
00098 void FunctionKeyboard::paintEvent(QPaintEvent *e) {
00099 
00100     QPainter p(this);
00101     p.setClipRect(e->rect());
00102     p.fillRect(0, 0, width(), height(), QColor(255,255,255));
00103 
00104     p.setPen(QColor(0,0,0));
00105 
00106     /* those decimals do count! becomes short if use plain int */
00107     for (double i = 0; i <= width(); i += keyWidth) {
00108 
00109         p.drawLine((int)i, 0, (int)i, height());
00110     }
00111 
00112     // sometimes the last line doesnt get drawn
00113     p.drawLine(width() -1, 0, width() -1, height());
00114 
00115     for (int i = 0; i <= height(); i += keyHeight) {
00116 
00117         p.drawLine(0, i, width(), i);
00118     }
00119 
00120     for (uint r = 0; r < numRows; r++) {
00121         for (uint c = 0; c < numCols; c++) {
00122 
00123             QString handle = "r" + QString::number(r) + "c" + QString::number(c);
00124             if (keys.contains(handle)) {
00125 
00126                 if (keys[handle].pixFile.isEmpty())
00127                     p.drawText( c * keyWidth + 1, r * keyHeight + 1,
00128                                 keyWidth, keyHeight,
00129                                 Qt::AlignHCenter | Qt::AlignVCenter,
00130                                 keys[handle].label
00131                     );
00132                 else  {
00133 
00134                     ushort centerX = (ushort)(c *keyWidth) + (ushort)(keyWidth - keys[handle].pix->width()) / 2;
00135                     ushort centerY = r * keyHeight + (keyHeight - keys[handle].pix->height()) / 2;
00136                     p.drawPixmap(centerX, centerY, *keys[handle].pix);
00137                 }
00138             }
00139         }
00140     }
00141 }
00142 
00143 void FunctionKeyboard::paintKey(uint row, uint col) {
00144 
00145     QPainter p(this);
00146 
00147     p.fillRect(QRect(QPoint(col * keyWidth + 1, row * keyHeight + 1),
00148                      QPoint((col + 1) * keyWidth - 1, row * keyHeight + keyHeight- 1)),
00149                (pressedRow != -1 && pressedCol != -1 ) ? QColor(97,119,155) : QColor(255,255,255));
00150 
00151     QString handle ("r" + QString::number(row) + "c" + QString::number(col));
00152     if (keys[handle].pixFile.isEmpty())
00153         p.drawText(
00154                     col * keyWidth + 1, row * keyHeight + 1,
00155                     keyWidth, keyHeight,
00156                     Qt::AlignHCenter | Qt::AlignVCenter,
00157                     keys[handle].label
00158         );
00159     else {
00160 
00161         ushort centerX = (ushort)(col *keyWidth) + (ushort)(keyWidth - keys[handle].pix->width()) / 2;
00162         ushort centerY = row * keyHeight + (keyHeight - keys[handle].pix->height()) / 2;
00163         p.drawPixmap(centerX, centerY, *keys[handle].pix);
00164     }
00165 
00166     if (col == numCols - 1) {
00167 
00168         // sometimes it doesnt draw the last line
00169 
00170         p.drawLine((col+1) * keyWidth -1, row * keyHeight,
00171                    (col+1) * keyWidth -1, (row + 1) * keyHeight
00172         );
00173     }
00174 
00175 }
00176 
00177 void FunctionKeyboard::mousePressEvent(QMouseEvent *e) {
00178 
00179     pressedRow = e->y() / keyHeight;
00180     pressedCol = (int) (e->x() / keyWidth);
00181 
00182     paintKey(pressedRow, pressedCol);
00183 
00184     // emit that sucker!
00185     FKey k = keys["r" + QString::number(pressedRow) + "c" + QString::number(pressedCol)];
00186     emit keyPressed(k, pressedRow, pressedCol, 1);
00187 
00188 }
00189 
00190 void FunctionKeyboard::mouseReleaseEvent(QMouseEvent *) {
00191 
00192     if (pressedRow != -1 && pressedRow != -1) {
00193 
00194         int row = pressedRow; pressedRow = -1;
00195         int col = pressedCol; pressedCol = -1;
00196         paintKey(row, col);
00197 
00198         FKey k = keys["r" + QString::number(row) + "c" + QString::number(col)];
00199         emit keyPressed(k, row, col, 0);
00200     }
00201 
00202 }
00203 
00204 
00205 void FunctionKeyboard::resizeEvent(QResizeEvent*) {
00206 
00207     /* set he default font height/width */
00208     QFontMetrics fm=fontMetrics();
00209     keyHeight = fm.lineSpacing() + 2;
00210     keyWidth = (double)width()/numCols;
00211 
00212 }
00213 
00214 QSize FunctionKeyboard::sizeHint() const {
00215 
00216     return QSize(width(), keyHeight * numRows + 1);
00217 }
00218 
00219 void FunctionKeyboard::loadDefaults() {
00220 
00221     numRows = DEFAULT_ROWS;
00222     numCols = DEFAULT_COLS;
00223     keyWidth = (double)width()/numCols; // have to reset this thing too
00224 
00225     keys.insert( "r0c0", FKey ("Enter", "enter", Qt::Key_Enter, 0));
00226     keys.insert( "r0c1", FKey ("Space", "space", Qt::Key_Space, Qt::Key_Space));
00227     keys.insert( "r0c2", FKey ("Tab", "tab", Qt::Key_Tab, 0));
00228     keys.insert( "r0c3", FKey ("Up", "up", Qt::Key_Up, 0));
00229     keys.insert( "r0c4", FKey ("Down", "down", Qt::Key_Down, 0));
00230 
00231     keys.insert( "r0c7", FKey ("Ho", 0, 4112, 0));
00232     keys.insert( "r0c8", FKey ("End", 0, 4113, 0));
00233     keys.insert( "r0c9", FKey ("Pu", 0, 4118, 0));
00234     keys.insert( "r0c10", FKey ("Pd", 0, 4119, 0));
00235     keys.insert( "r0c11", FKey ("Esc", 0, Qt::Key_Escape, 0xfff));
00236 
00237     keys.insert( "r1c0", FKey ("F1", 0, 4144, 0));
00238     keys.insert( "r1c1", FKey ("F2", 0, 4145, 0));
00239     keys.insert( "r1c2", FKey ("F3", 0, 4146, 0));
00240     keys.insert( "r1c3", FKey ("F4", 0, 4147, 0));
00241     keys.insert( "r1c4", FKey ("F5", 0, 4148, 0));
00242     keys.insert( "r1c5", FKey ("F6", 0, 4149, 0));
00243     keys.insert( "r1c6", FKey ("F7", 0, 4150, 0));
00244     keys.insert( "r1c7", FKey ("F8", 0, 4151, 0));
00245     keys.insert( "r1c8", FKey ("F9", 0, 4152, 0));
00246     keys.insert( "r1c9", FKey ("F10", 0, 4153, 0));
00247     keys.insert( "r1c10", FKey ("F11", 0, 4154, 0));
00248     keys.insert( "r1c11", FKey ("F12", 0, 4155, 0));
00249 
00250 
00251 }
00252 
00253 /* FunctionKeyboardConfig {{{1 */
00254 
00255 FunctionKeyboardConfig::FunctionKeyboardConfig(const QString& name, QWidget* parent, const char* na )
00256     : ProfileDialogKeyWidget(name, parent, na),
00257       selectedRow(0), selectedCol(0)
00258 {
00259     kb = new FunctionKeyboard(this);
00260     connect (kb, SIGNAL(keyPressed(FKey,ushort,ushort,bool)),
00261              this, SLOT(slotKeyPressed(FKey,ushort,ushort,bool)));
00262 
00263     QGroupBox *dimentions = new QGroupBox(2, Qt::Horizontal, tr("Dimensions"), this);
00264     QLabel *l = new QLabel(tr("Rows"), dimentions);
00265     m_rowBox = new QSpinBox(1, 15, 1, dimentions);
00266     connect (m_rowBox, SIGNAL(valueChanged(int)), this, SLOT(slotChangeRows(int)));
00267     l = new QLabel(tr("Columns"), dimentions);
00268     m_colBox = new QSpinBox(1, 15, 1, dimentions);
00269     connect (m_colBox, SIGNAL(valueChanged(int)), this, SLOT(slotChangeCols(int)));
00270 
00271     QGroupBox *editKey = new QGroupBox(2, Qt::Horizontal, tr("Edit Key"), this);
00272     l = new QLabel(tr("Label"), editKey);
00273     m_labels = new QComboBox(true, editKey);
00274     m_labels->setInsertionPolicy(QComboBox::AtCurrent);
00275     m_labels->insertItem("");
00276 
00277     QStringList files = QDir( QPEApplication::qpeDir() + "pics/console/keys/", "*.png").entryList();
00278 
00279     for (uint i = 0; i < files.count(); i++) {
00280         m_labels->insertItem( Opie::Core::OResource::loadPixmap("console/keys/" + files[i]), files[i]);
00281     }
00282     connect (m_labels, SIGNAL(activated(int)), this, SLOT(slotChangeIcon(int)));
00283     connect (m_labels, SIGNAL(textChanged(const QString&)), this, SLOT(slotChangeLabelText(const QString&)));
00284 
00285     l = new QLabel(tr("Q Keycode", "Qt Key Code for the OnScreen Keyboard"), editKey);
00286     m_qvalues = new QComboBox(true, editKey);
00287     m_qvalues->setInsertionPolicy(QComboBox::AtTop);
00288     m_qvalues->setDuplicatesEnabled(false);
00289     m_qvalues->insertItem("");
00290     connect (m_qvalues, SIGNAL(textChanged(const QString&)), this, SLOT(slotChangeQCode(const QString&)));
00291 
00292     l = new QLabel(tr("Unicode Value", "The Unicode value of the key"), editKey);
00293     m_uniValues = new QComboBox(true, editKey);
00294     m_uniValues->setInsertionPolicy(QComboBox::AtTop);
00295     m_uniValues->setDuplicatesEnabled(false);
00296     m_uniValues->insertItem("");
00297     connect (m_uniValues, SIGNAL(textChanged(const QString&)), this, SLOT(slotChangeUnicode(const QString&)));
00298 
00299     QVBoxLayout *root = new QVBoxLayout(this, 2);
00300     root->addWidget(kb);
00301     root->addWidget(dimentions);
00302     root->addWidget(editKey);
00303 }
00304 FunctionKeyboardConfig::~FunctionKeyboardConfig() {
00305 
00306 }
00307 void FunctionKeyboardConfig::load (const Profile& prof) {
00308 
00309     kb->keys.clear();
00310     kb->loadDefaults();
00311 
00312     m_rowBox->setValue(prof.readNumEntry("keb_rows", 2));
00313     m_colBox->setValue(prof.readNumEntry("keb_cols", 10));
00314 
00315     /* load all the keys to the keyboard */
00316     for (int i = 0; i <= m_rowBox->value() -1; i++)
00317         for (int j = 0; j <= m_colBox->value() -1; j++) {
00318 
00319             QString h = "r" + QString::number(i) + "c" + QString::number(j);
00320             QString values = prof.readEntry("keb_" + h);
00321 
00322             if (!values.isEmpty()) {
00323 
00324                 QStringList l = QStringList::split(QChar('|'), values, TRUE);
00325                 kb->keys[h] = FKey(l[0], l[1], l[2].toInt(), l[3].toInt());
00326 
00327                 // load pixmap if used
00328                 if (!l[1].isEmpty()) {
00329 
00330                     kb->keys[h].pix = new QPixmap( Opie::Core::OResource::loadPixmap( "console/keys/" + l[1] ) );
00331                 }
00332             }
00333         }
00334 
00335 }
00336 void FunctionKeyboardConfig::save (Profile& prof) {
00337 
00338     prof.writeEntry("keb_rows", m_rowBox->value());
00339     prof.writeEntry("keb_cols", m_colBox->value());
00340 
00341     QMap<QString, FKey>::Iterator it;
00342     for ( it = kb->keys.begin(); it != kb->keys.end(); it++) {
00343 
00344         FKey k = it.data();
00345         QString entry = k.label + "|"
00346                         + k.pixFile + "|"
00347                         + QString::number(k.qcode) + "|"
00348                         + QString::number(k.unicode);
00349 
00350         prof.writeEntry("keb_" + it.key(), entry);
00351 
00352     }
00353 
00354 }
00355 void FunctionKeyboardConfig::slotChangeRows(int r) {
00356 
00357     kb->changeRows(r);
00358 
00359 }
00360 void FunctionKeyboardConfig::slotChangeCols(int c) {
00361 
00362     kb->changeCols(c);
00363 }
00364 void FunctionKeyboardConfig::slotKeyPressed(FKey k, ushort r, ushort c, bool pressed) {
00365 
00366     if (!pressed) return;
00367 
00368     selectedHandle = "r" + QString::number(r) +
00369                      "c" + QString::number(c);
00370     selectedRow = r;
00371     selectedCol = c;
00372 
00373     if (k.pixFile.isEmpty()) {
00374 
00375         m_labels->setEditable(true);
00376         m_labels->setCurrentItem(0);
00377         m_labels->changeItem(k.label, 0);
00378 
00379     } else {
00380 
00381         // any better way to select the pixmap?
00382         m_labels->setCurrentItem((m_labels->listBox())->index((m_labels->listBox())->findItem(kb->keys[selectedHandle].pixFile)));
00383         m_labels->setEditable(false);
00384     }
00385     m_qvalues->changeItem(QString::number(k.qcode), 0);
00386     m_uniValues->changeItem(QString::number(k.unicode), 0);
00387 }
00388 void FunctionKeyboardConfig::slotChangeIcon(int index) {
00389 
00390     if (index == 0) {
00391 
00392         // is text
00393         m_labels->setEditable(true);
00394         // why tf does the text get erased unless i do this?
00395         m_labels->changeItem(m_labels->text(0), 0);
00396 
00397         kb->keys[selectedHandle].pixFile = "";
00398         delete kb->keys[selectedHandle].pix;
00399 
00400     } else {
00401 
00402         // is a pixmap
00403         m_labels->setEditable(false);
00404         kb->keys[selectedHandle].pixFile = m_labels->currentText();
00405         kb->keys[selectedHandle].pix = new QPixmap( Opie::Core::OResource::loadPixmap( "console/keys/" + m_labels->currentText() ) );
00406     }
00407     kb->paintKey(selectedRow, selectedCol);
00408 }
00409 void FunctionKeyboardConfig::slotChangeLabelText(const QString &label) {
00410 
00411     kb->keys[selectedHandle].label = label;
00412 
00413     kb->paintKey(selectedRow, selectedCol);
00414 }
00415 void FunctionKeyboardConfig::slotChangeQCode(const QString& qcode) {
00416 
00417     kb->keys[selectedHandle].qcode = qcode.toUInt();
00418 }
00419 void FunctionKeyboardConfig::slotChangeUnicode(const QString& uni) {
00420 
00421     kb->keys[selectedHandle].unicode = uni.toUInt();
00422 }

Generated on Sat Nov 5 16:16:49 2005 for OPIE by  doxygen 1.4.2