2010-08-24 21:57:43 +02:00
/* This file is part of Clementine.
Clementine is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Clementine is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Clementine . If not , see < http : //www.gnu.org/licenses/>.
*/
2010-08-25 18:19:54 +02:00
# include "ui/wiimotedevshortcutsconfig.h"
2010-08-24 21:57:43 +02:00
# include "ui_wiimotedevshortcutsconfig.h"
2010-08-29 17:21:50 +02:00
# include "ui/wiimotedevshortcutgrabber.h"
2010-08-24 21:57:43 +02:00
2010-08-25 18:19:54 +02:00
# include "wiimotedev/shortcuts.h"
2010-08-25 20:10:44 +02:00
# include "wiimotedev/consts.h"
2010-08-24 21:57:43 +02:00
2010-09-01 17:46:22 +02:00
# include <QMessageBox>
2010-08-24 21:57:43 +02:00
WiimotedevShortcutsConfig : : WiimotedevShortcutsConfig ( QWidget * parent )
: QWidget ( parent ) ,
ui_ ( new Ui_WiimotedevShortcutsConfig )
{
ui_ - > setupUi ( this ) ;
ui_ - > list - > header ( ) - > setResizeMode ( QHeaderView : : ResizeToContents ) ;
2010-08-25 18:19:54 +02:00
2010-08-29 17:21:50 +02:00
text_buttons_ . insert ( WIIMOTE_BTN_1 , " Wiiremote 1 " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_2 , " Wiiremote 2 " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_A , " Wiiremote A " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_B , " Wiiremote B " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_PLUS , " Wiiremote Plus " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_MINUS , " Wiiremote Minus " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_HOME , " Wiiremote Home " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_UP , " Wiiremote Up " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_DOWN , " Wiiremote Down " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_LEFT , " Wiiremote Left " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_RIGHT , " Wiiremote Right " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_UP , " Wiiremote Shift Up " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_DOWN , " Wiiremote Shift Down " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_LEFT , " Wiiremote Shift Left " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_RIGHT , " Wiiremote Shift Right " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_FORWARD , " Wiiremote Shift Forward " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_BACKWARD , " Wiiremote Shift Backward " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_TILT_FRONT , " Wiiremote Tilt Front " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_TILT_BACK , " Wiiremote Tilt Back " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_TILT_LEFT , " Wiiremote Tilt Left " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_TILT_RIGHT , " Wiiremote Tilt Right " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_Z , " Nunchuk Z " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_C , " Nunchuk B " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_STICK_UP , " Nunchuk Stick Up " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_STICK_DOWN , " Nunchuk Stick Down " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_STICK_LEFT , " Nunchuk Stick Left " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_STICK_RIGHT , " Nunchuk Stick Right " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_UP , " Nunchuk Shift Up " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_DOWN , " Nunchuk Shift Down " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_LEFT , " Nunchuk Shift Left " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_RIGHT , " Nunchuk Shift Right " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_FORWARD , " Nunchuk Shift Forward " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_BACKWARD , " Nunchuk Shift Backward " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_TILT_FRONT , " Nunchuk Tilt Front " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_TILT_BACK , " Nunchuk Tilt Back " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_TILT_LEFT , " Nunchuk Tilt Left " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_TILT_RIGHT , " Nunchuk Tilt Right " ) ;
text_buttons_ . insert ( CLASSIC_BTN_X , " Classic X " ) ;
text_buttons_ . insert ( CLASSIC_BTN_Y , " Classic Y " ) ;
text_buttons_ . insert ( CLASSIC_BTN_A , " Classic A " ) ;
text_buttons_ . insert ( CLASSIC_BTN_B , " Classic B " ) ;
text_buttons_ . insert ( CLASSIC_BTN_L , " Classic L " ) ;
text_buttons_ . insert ( CLASSIC_BTN_R , " Classic R " ) ;
text_buttons_ . insert ( CLASSIC_BTN_ZL , " Classic ZL " ) ;
text_buttons_ . insert ( CLASSIC_BTN_ZR , " Classic ZR " ) ;
text_buttons_ . insert ( CLASSIC_BTN_MINUS , " Classic Minus " ) ;
text_buttons_ . insert ( CLASSIC_BTN_PLUS , " Classic Plus " ) ;
text_buttons_ . insert ( CLASSIC_BTN_HOME , " Classic Home " ) ;
text_buttons_ . insert ( CLASSIC_BTN_UP , " Classic Up " ) ;
text_buttons_ . insert ( CLASSIC_BTN_DOWN , " Classic Down " ) ;
text_buttons_ . insert ( CLASSIC_BTN_LEFT , " Classic Left " ) ;
text_buttons_ . insert ( CLASSIC_BTN_RIGHT , " Classic Right " ) ;
text_buttons_ . insert ( CLASSIC_BTN_LSTICK_UP , " Classic Left-Stick Up " ) ;
text_buttons_ . insert ( CLASSIC_BTN_LSTICK_DOWN , " Classic Left-Stick Down " ) ;
text_buttons_ . insert ( CLASSIC_BTN_LSTICK_LEFT , " Classic Left-Stick Left " ) ;
text_buttons_ . insert ( CLASSIC_BTN_LSTICK_RIGHT , " Classic Left-Stick Right " ) ;
text_buttons_ . insert ( CLASSIC_BTN_RSTICK_UP , " Classic Right-Stick Up " ) ;
text_buttons_ . insert ( CLASSIC_BTN_RSTICK_DOWN , " Classic Right-Stick Down " ) ;
text_buttons_ . insert ( CLASSIC_BTN_RSTICK_LEFT , " Classic Right-Stick Left " ) ;
text_buttons_ . insert ( CLASSIC_BTN_RSTICK_RIGHT , " Classic Right-Stick Right " ) ;
text_buttons_ . insert ( WIIMOTE_BTN_SHIFT_SHAKE , " Wiiremote Shift Shake " ) ;
text_buttons_ . insert ( NUNCHUK_BTN_SHIFT_SHAKE , " Nunchuk Shift Shake " ) ;
2010-08-25 20:10:44 +02:00
2010-09-01 17:37:09 +02:00
text_actions_ . insert ( WiimotedevShortcuts : : WiimotedevActiveDeactive , tr ( " Active/deactive Wiiremote " ) ) ;
2010-08-25 20:10:44 +02:00
text_actions_ . insert ( WiimotedevShortcuts : : PlayerNextTrack , tr ( " Next track " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerPreviousTrack , tr ( " Previous track " ) ) ;
2010-09-01 00:48:16 +02:00
text_actions_ . insert ( WiimotedevShortcuts : : PlayerPlay , tr ( " Play " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerStop , tr ( " Stop " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerIncVolume , tr ( " Increase volume " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerDecVolume , tr ( " Decrease volume " ) ) ;
2010-08-25 20:10:44 +02:00
text_actions_ . insert ( WiimotedevShortcuts : : PlayerMute , tr ( " Mute " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerPause , tr ( " Pause " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerTogglePause , tr ( " Play/Pause " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerSeekBackward , tr ( " Seek backward " ) ) ;
text_actions_ . insert ( WiimotedevShortcuts : : PlayerSeekForward , tr ( " Seek forward " ) ) ;
2010-09-01 00:48:16 +02:00
text_actions_ . insert ( WiimotedevShortcuts : : PlayerSeekForward , tr ( " Stop after " ) ) ;
2010-08-25 20:10:44 +02:00
text_actions_ . insert ( WiimotedevShortcuts : : PlayerShowOSD , tr ( " Show OSD " ) ) ;
2010-08-25 18:19:54 +02:00
settings_ . beginGroup ( WiimotedevShortcuts : : kSettingsGroup ) ;
2010-08-31 03:37:17 +02:00
ui_ - > wiimotedev_enable - > setChecked ( settings_ . value ( " enabled " , false ) . toBool ( ) ) ;
ui_ - > wiimotedev_active - > setChecked ( settings_ . value ( " use_active_action " , true ) . toBool ( ) ) ;
2010-08-25 18:19:54 +02:00
ui_ - > wiimotedev_focus - > setChecked ( settings_ . value ( " only_when_focused " , false ) . toBool ( ) ) ;
2010-08-31 03:37:17 +02:00
ui_ - > wiimotedev_notification - > setChecked ( settings_ . value ( " use_notification " , true ) . toBool ( ) ) ;
2010-08-25 18:19:54 +02:00
ui_ - > wiimotedev_device - > setValue ( settings_ . value ( " device " , 1 ) . toUInt ( ) ) ;
settings_ . endGroup ( ) ;
2010-08-26 17:37:07 +02:00
LoadSettings ( ) ;
connect ( ui_ - > list , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) , SLOT ( ItemClicked ( QTreeWidgetItem * ) ) ) ;
2010-09-01 01:50:21 +02:00
2010-08-29 17:21:50 +02:00
connect ( ui_ - > wiimotedev_add_action , SIGNAL ( clicked ( ) ) , this , SLOT ( AddAction ( ) ) ) ;
2010-08-26 17:37:07 +02:00
connect ( ui_ - > wiimotedev_delete_action , SIGNAL ( clicked ( ) ) , this , SLOT ( DeleteAction ( ) ) ) ;
connect ( ui_ - > wiimotedev_reload , SIGNAL ( clicked ( ) ) , this , SLOT ( DefaultSettings ( ) ) ) ;
}
2010-08-31 03:37:17 +02:00
void WiimotedevShortcutsConfig : : AddShortcut ( quint64 button , quint32 action ) {
2010-09-01 17:46:22 +02:00
foreach ( const Shortcut & shortcut , actions_ ) {
if ( shortcut . button = = button ) {
QMessageBox : : information ( this , tr ( " Information " ) , QString ( tr ( " Shortcut for %1 already exists " ) ) . arg ( GetReadableWiiremoteSequence ( button ) ) , QMessageBox : : Ok ) ;
return ;
}
}
2010-08-31 03:37:17 +02:00
Shortcut s ;
s . object = new QTreeWidgetItem ( ui_ - > list , QStringList ( ) < < GetReadableWiiremoteSequence ( button ) < < text_actions_ [ action ] ) ;
s . button = button ;
s . action = action ;
actions_ < < s ;
}
2010-08-26 17:37:07 +02:00
void WiimotedevShortcutsConfig : : LoadSettings ( ) {
2010-09-01 01:35:39 +02:00
settings_ . beginGroup ( WiimotedevShortcuts : : kSettingsGroup ) ;
bool first_conf = settings_ . value ( " first_conf " , true ) . toBool ( ) ;
settings_ . endGroup ( ) ;
if ( first_conf ) {
DefaultSettings ( ) ;
return ;
}
2010-08-25 20:10:44 +02:00
settings_ . beginGroup ( WiimotedevShortcuts : : kActionsGroup ) ;
2010-08-28 21:31:38 +02:00
settings_ . sync ( ) ;
2010-08-26 17:37:07 +02:00
ui_ - > list - > clear ( ) ;
2010-08-31 03:37:17 +02:00
actions_ . clear ( ) ;
2010-08-26 17:37:07 +02:00
2010-08-25 20:10:44 +02:00
quint64 fvalue , svalue ;
bool fvalid , svalid ;
foreach ( const QString & str , settings_ . allKeys ( ) ) {
fvalue = str . toULongLong ( & fvalid , 10 ) ;
svalue = settings_ . value ( str , 0 ) . toULongLong ( & svalid ) ;
2010-08-31 03:37:17 +02:00
if ( fvalid & & svalid )
AddShortcut ( fvalue , svalue ) ;
2010-08-25 20:10:44 +02:00
}
settings_ . endGroup ( ) ;
2010-08-31 03:37:17 +02:00
ui_ - > list - > sortItems ( 1 , Qt : : AscendingOrder ) ;
2010-08-26 17:37:07 +02:00
}
2010-08-25 20:10:44 +02:00
2010-08-26 17:37:07 +02:00
void WiimotedevShortcutsConfig : : DefaultSettings ( )
{
2010-08-31 03:37:17 +02:00
ui_ - > list - > clear ( ) ;
actions_ . clear ( ) ;
AddShortcut ( WIIMOTE_BTN_LEFT , WiimotedevShortcuts : : PlayerPreviousTrack ) ;
AddShortcut ( WIIMOTE_BTN_RIGHT , WiimotedevShortcuts : : PlayerNextTrack ) ;
AddShortcut ( WIIMOTE_BTN_SHIFT_LEFT , WiimotedevShortcuts : : PlayerPreviousTrack ) ;
AddShortcut ( WIIMOTE_BTN_SHIFT_RIGHT , WiimotedevShortcuts : : PlayerNextTrack ) ;
AddShortcut ( WIIMOTE_BTN_PLUS , WiimotedevShortcuts : : PlayerIncVolume ) ;
AddShortcut ( WIIMOTE_BTN_MINUS , WiimotedevShortcuts : : PlayerDecVolume ) ;
AddShortcut ( WIIMOTE_BTN_1 , WiimotedevShortcuts : : PlayerTogglePause ) ;
AddShortcut ( WIIMOTE_BTN_2 , WiimotedevShortcuts : : PlayerShowOSD ) ;
2010-09-01 17:37:09 +02:00
AddShortcut ( WIIMOTE_BTN_A , WiimotedevShortcuts : : WiimotedevActiveDeactive ) ;
2010-08-31 03:37:17 +02:00
ui_ - > list - > sortItems ( 1 , Qt : : AscendingOrder ) ;
2010-08-25 18:19:54 +02:00
}
2010-08-26 17:37:07 +02:00
void WiimotedevShortcutsConfig : : ItemClicked ( QTreeWidgetItem * selected_item ) {
selected_item_ = selected_item ;
2010-08-25 20:10:44 +02:00
}
QString WiimotedevShortcutsConfig : : GetReadableWiiremoteSequence ( quint64 value ) {
QMapIterator < quint64 , QString > text ( text_buttons_ ) ;
QStringList list ;
while ( text . hasNext ( ) ) {
text . next ( ) ;
if ( ( text . key ( ) & value ) = = text . key ( ) )
list < < text . value ( ) ;
}
2010-08-29 17:21:50 +02:00
2010-08-26 17:37:07 +02:00
QString output ;
2010-08-29 17:21:50 +02:00
if ( ! list . isEmpty ( ) ) {
for ( int i = 0 ; i < ( list . count ( ) - 1 ) ; + + i )
output + = list . at ( i ) + " + " ;
output + = list . last ( ) ;
2010-08-31 03:37:17 +02:00
} else
output = tr ( " Push Wiiremote button " ) ;
2010-08-25 20:10:44 +02:00
return output ;
}
2010-08-26 17:37:07 +02:00
2010-08-29 17:21:50 +02:00
void WiimotedevShortcutsConfig : : AddAction ( ) {
2010-08-31 22:35:11 +02:00
emit SetWiimotedevInterfaceActived ( false ) ;
2010-09-11 03:23:44 +02:00
WiimotedevShortcutGrabber grabber ( 0 , ui_ - > wiimotedev_device - > value ( ) , this ) ;
2010-09-01 17:46:22 +02:00
connect ( & grabber , SIGNAL ( AddShortcut ( quint64 , quint32 ) ) , this , SLOT ( AddShortcut ( quint64 , quint32 ) ) , Qt : : QueuedConnection ) ;
2010-08-29 18:24:19 +02:00
grabber . exec ( ) ;
2010-08-31 22:35:11 +02:00
emit SetWiimotedevInterfaceActived ( true ) ;
2010-09-01 17:37:09 +02:00
ui_ - > list - > sortItems ( 1 , Qt : : AscendingOrder ) ;
2010-08-29 17:21:50 +02:00
}
2010-08-26 17:37:07 +02:00
void WiimotedevShortcutsConfig : : DeleteAction ( ) {
for ( int i = 0 ; i < actions_ . count ( ) ; + + i ) {
if ( actions_ . at ( i ) . object = = selected_item_ ) {
delete selected_item_ ;
actions_ . removeAt ( i ) ;
break ;
}
}
}