| 1 | /*
|
|---|
| 2 | * File: mythkeysequence.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Purpose: See mythkeysequence.h
|
|---|
| 5 | */
|
|---|
| 6 | #include <mythkeysequence.h>
|
|---|
| 7 | #include <QString>
|
|---|
| 8 | #include <QStringList>
|
|---|
| 9 |
|
|---|
| 10 | /*** MythKey ***
|
|---|
| 11 | ***************/
|
|---|
| 12 | MythKey::MythKey( )
|
|---|
| 13 | {
|
|---|
| 14 | this->clear( );
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | MythKey::MythKey( const int key )
|
|---|
| 18 | {
|
|---|
| 19 | if (!(this->setKey( key )))
|
|---|
| 20 | this->clear( );
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | MythKey::MythKey( const QString key )
|
|---|
| 24 | {
|
|---|
| 25 | if (!(this->setKey( key )))
|
|---|
| 26 | this->clear( );
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /* MythKey member routines */
|
|---|
| 30 | void MythKey::clear( )
|
|---|
| 31 | {
|
|---|
| 32 | this->key_assigned = false;
|
|---|
| 33 | this->key_str = "";
|
|---|
| 34 | this->key_num = 0;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | bool MythKey::setKey( const int key )
|
|---|
| 38 | {
|
|---|
| 39 | bool retval = false;
|
|---|
| 40 |
|
|---|
| 41 | // Passed by key-num are keys, no buttons
|
|---|
| 42 | this->is_button = 0;
|
|---|
| 43 | QKeySequence keyseq( key );
|
|---|
| 44 | if (keyseq.count( ) > 0)
|
|---|
| 45 | {
|
|---|
| 46 | retval = true;
|
|---|
| 47 | this->key_num = keyseq[ 0 ];
|
|---|
| 48 | this->key_str = QString(keyseq);
|
|---|
| 49 | }
|
|---|
| 50 | this->key_assigned = retval;
|
|---|
| 51 | return retval;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | bool MythKey::isRemote( )
|
|---|
| 55 | {
|
|---|
| 56 | return this->is_button;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | bool MythKey::isAssigned( )
|
|---|
| 60 | {
|
|---|
| 61 | return this->key_assigned;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | bool MythKey::setKey( const QString key )
|
|---|
| 65 | {
|
|---|
| 66 | this->key_assigned = false;
|
|---|
| 67 | bool retval = false;
|
|---|
| 68 | if (key.left( 6 ).toLower( ) == "remote")
|
|---|
| 69 | {
|
|---|
| 70 | retval = true;
|
|---|
| 71 | this->is_button = 1;
|
|---|
| 72 |
|
|---|
| 73 | /* Use this value as-is, no checking at all */
|
|---|
| 74 | this->key_str = key;
|
|---|
| 75 | this->key_num = 0;
|
|---|
| 76 | }
|
|---|
| 77 | else
|
|---|
| 78 | {
|
|---|
| 79 | this->is_button = 0;
|
|---|
| 80 |
|
|---|
| 81 | /* Use the QKeySequence to check this key's validity and to set
|
|---|
| 82 | * appropriate fields. We pass ',,' to make the QKeySequence
|
|---|
| 83 | * see the ',' because it'll fail on a *single* ',' */
|
|---|
| 84 | QString keystr = key.trimmed( );
|
|---|
| 85 | QKeySequence keyseq( (keystr == ","?",,":keystr) );
|
|---|
| 86 | if (keyseq[ 0 ])
|
|---|
| 87 | {
|
|---|
| 88 | retval = true;
|
|---|
| 89 | this->key_num = keyseq[ 0 ];
|
|---|
| 90 | this->key_str = keystr.trimmed( );
|
|---|
| 91 | }
|
|---|
| 92 | // else printf( "no keyseq\n" );
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | this->key_assigned = retval;
|
|---|
| 96 | return retval;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | QString MythKey::text( )
|
|---|
| 101 | {
|
|---|
| 102 | if (this->key_assigned)
|
|---|
| 103 | {
|
|---|
| 104 | if (this->key_str.length() == 0)
|
|---|
| 105 | {
|
|---|
| 106 | QString key;
|
|---|
| 107 | key.setNum( this->key_num );
|
|---|
| 108 | return key;
|
|---|
| 109 | }
|
|---|
| 110 | else
|
|---|
| 111 | {
|
|---|
| 112 | return this->key_str;
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return "";
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | int MythKey::value( )
|
|---|
| 120 | {
|
|---|
| 121 | if (this->key_assigned)
|
|---|
| 122 | return this->key_num;
|
|---|
| 123 | else
|
|---|
| 124 | return 0;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | MythKey::operator int( )
|
|---|
| 128 | {
|
|---|
| 129 | return this->value( );
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | /*** MythKeySequence ***
|
|---|
| 134 | ***********************/
|
|---|
| 135 | MythKeySequence::MythKeySequence( )
|
|---|
| 136 | {
|
|---|
| 137 | this->clear( );
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | MythKeySequence::MythKeySequence( int k1, int k2, int k3, int k4 )
|
|---|
| 141 | {
|
|---|
| 142 | uint k_index = 0;
|
|---|
| 143 |
|
|---|
| 144 | if (k1 > 0) this->keys[ ++k_index ].setKey( k1 );
|
|---|
| 145 | if (k2 > 0) this->keys[ ++k_index ].setKey( k2 );
|
|---|
| 146 | if (k3 > 0) this->keys[ ++k_index ].setKey( k3 );
|
|---|
| 147 | if (k4 > 0) this->keys[ ++k_index ].setKey( k4 );
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | MythKeySequence::MythKeySequence( const QString &keys )
|
|---|
| 151 | {
|
|---|
| 152 | this->setKeys( keys );
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | /* MythKeySequence member routines */
|
|---|
| 157 | uint MythKeySequence::setKeys( const QString &keys )
|
|---|
| 158 | {
|
|---|
| 159 | QStringList allKeys = keys.split( ",", QString::KeepEmptyParts );
|
|---|
| 160 | int max = allKeys.count( );
|
|---|
| 161 | int k_index = 0, e_cnt = 0, e_at_start = 0;
|
|---|
| 162 |
|
|---|
| 163 | //printf( "Found %d keys in the list [%s].\n", max, keys.data( ) );
|
|---|
| 164 | for(int i = 0; i < max; i++)
|
|---|
| 165 | {
|
|---|
| 166 | //printf( "Processing key [%s].\n", allKeys[i].data( ) );
|
|---|
| 167 |
|
|---|
| 168 | if (allKeys[ i ].isEmpty( )) // 2 empties is 1 ','
|
|---|
| 169 | {
|
|---|
| 170 | e_cnt++;
|
|---|
| 171 | if (e_cnt == 2)
|
|---|
| 172 | {
|
|---|
| 173 | this->keys[ k_index ].setKey( QString(",") );
|
|---|
| 174 | k_index++;
|
|---|
| 175 | e_cnt = 0;
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | else
|
|---|
| 179 | {
|
|---|
| 180 | //printf( "Adding [%s] as a key...\n", allKeys[i].data( ) );
|
|---|
| 181 | this->keys[ k_index ].setKey( allKeys[ i ] );
|
|---|
| 182 | k_index++;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (k_index >= MYTHKEY_SEQLEN)
|
|---|
| 186 | {
|
|---|
| 187 | // positions exhausted -- illegal sequence string...
|
|---|
| 188 | break;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | } // for
|
|---|
| 192 |
|
|---|
| 193 | /// printf( "%d keys assigned.\n", k_index );
|
|---|
| 194 | this->keys_assigned = k_index;
|
|---|
| 195 | return k_index;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | uint MythKeySequence::count( )
|
|---|
| 199 | {
|
|---|
| 200 | return this->keys_assigned;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | void MythKeySequence::clear( )
|
|---|
| 204 | {
|
|---|
| 205 | this->keys_assigned = 0;
|
|---|
| 206 |
|
|---|
| 207 | for(int i=0; i<MYTHKEY_SEQLEN; i++)
|
|---|
| 208 | this->keys[ i ].clear( );
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | MythKey* MythKeySequence::operator[]( int index )
|
|---|
| 213 | {
|
|---|
| 214 | if( (index >= 0) && (index < MYTHKEY_SEQLEN) )
|
|---|
| 215 | return &(this->keys[ index ]);
|
|---|
| 216 | return 0;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | MythKeySequence::operator QString( )
|
|---|
| 220 | {
|
|---|
| 221 | QString retval = "";
|
|---|
| 222 | for (uint i=0; i<MYTHKEY_SEQLEN; i++)
|
|---|
| 223 | {
|
|---|
| 224 | if (this->keys[i].key_assigned)
|
|---|
| 225 | {
|
|---|
| 226 | if (retval != "") retval += ", ";
|
|---|
| 227 | retval += this->keys[i].text( );
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 | return retval;
|
|---|
| 231 | }
|
|---|