/* * File: mythkeysequence.cpp * * Purpose: See mythkeysequence.h */ #include /* MythKey constructors */ MythKey::MythKey( ) { this->clear( ); } MythKey::MythKey( const int key ) { if (!(this->setKey( key ))) this->clear( ); } MythKey::MythKey( const QString key ) { if (!(this->setKey( key ))) this->clear( ); } /* MythKey member routines */ void MythKey::clear( ) { this->key_assigned = false; this->key_str = ""; this->key_num = 0; } bool MythKey::setKey( const int key ) { bool retval = false; // Passed by key-num are keys, no buttons this->is_button = 0; QKeySequence keyseq( key ); if (keyseq[ 0 ]) { retval = true; this->key_num = keyseq[ 0 ]; this->key_str = keyseq[ 0 ]; } this->key_assigned = retval; return retval; } bool MythKey::isRemote( ) { return this->is_button; } bool MythKey::isAssigned( ) { return this->key_assigned; } bool MythKey::setKey( const QString key ) { this->key_assigned = false; bool retval = false; if (key.left( 6 ).lower( ) == "remote") { retval = true; this->is_button = 1; /* Use this value as-is, no checking at all */ this->key_str = key; this->key_num = 0; } else { this->is_button = 0; /* Use the QKeySequence to check this key's validity and to set * appropriate fields. We pass ',,' to make the QKeySequence * see the ',' -- it'll fail on a *single* ',' */ QString keystr = key.stripWhiteSpace( ); QKeySequence keyseq( (keystr == ","?",,":keystr) ); if (keyseq[ 0 ]) { retval = true; this->key_num = keyseq[ 0 ]; this->key_str = key.stripWhiteSpace( ); } // else printf( "no keyseq\n" ); } this->key_assigned = retval; return retval; } QString MythKey::text( ) { if (this->key_assigned) return this->key_str; else return ""; } int MythKey::keyNum( ) { if (this->key_assigned) return this->key_num; else return 0; } /* MythKeySequence constructors */ MythKeySequence::MythKeySequence( ) { this->clear( ); } MythKeySequence::MythKeySequence( int k1, int k2, int k3, int k4 ) { uint k_index = 0; if (k1 > 0) this->keys[ ++k_index ].setKey( k1 ); if (k2 > 0) this->keys[ ++k_index ].setKey( k2 ); if (k3 > 0) this->keys[ ++k_index ].setKey( k3 ); if (k4 > 0) this->keys[ ++k_index ].setKey( k4 ); } MythKeySequence::MythKeySequence( const QString &keys ) { this->setKeys( keys ); } /* MythKeySequence member routines */ uint MythKeySequence::setKeys( const QString &keys ) { unsigned int token_start = 0, k_index = 0, new_start; unsigned int i, max=keys.length(); bool fc = (keys[ 0 ] == ","), lc = false; int commas = (fc?1:0); int do_comma, do_token; for (i=1; i< max; i++) { do_comma=do_token=0; if (keys[i] == ",") { /* Got a comma -- if this is the first one there * could be a token before it. */ if ((!lc) && (i>1)) do_token=1; else token_start = i + 1; lc=true; commas++; /* If we reached the end here at 2 comma's, we * should register one. */ if ((commas == 2) && (i == max - 1)) { do_comma = 1; new_start = i; } } else { /* No comma -- still in a token. */ if (commas == 0) { if (i==max-1) { i = max; do_token=1; new_start = i + 1; } } else if ((commas == 2) && (fc)) { do_comma = 1; new_start = i; } else if ((commas == 2) && (i == max - 1)) { do_comma = 1; new_start = i; } else if (commas == 3) { if (i < max - 1) { do_comma = 1; new_start = i; } } fc=lc=false; commas = 0; } if (do_token || do_comma) { if (k_index < 4) { if (this->keys[ k_index ].setKey( do_comma?",":keys.mid( token_start, i - token_start ) )) k_index++; } token_start = i + do_token; //new_start; } } this->keys_assigned = k_index; return k_index; } uint MythKeySequence::count( ) { return this->keys_assigned; } void MythKeySequence::clear( ) { this->keys_assigned = 0; for(int i=0; i<4; i++) this->keys[ i ].clear( ); } MythKey* MythKeySequence::operator[]( int index ) { if( (index >= 0) && (index < 4) ) return &(this->keys[ index ]); return NULL; } MythKeySequence::operator QString( ) { QString retval = ""; for (uint i=0; i<4; i++) { if (this->keys[i].key_assigned) { if (retval != "") retval += ", "; retval += this->keys[i].text( ); } } return retval; }