1 /* antimicro Gamepad to KB+M event mapper
2  * Copyright (C) 2015 Travis Nickles <nickles.travis@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "joycontrolstickeditdialoghelper.h"
19 
JoyControlStickEditDialogHelper(JoyControlStick * stick,QObject * parent)20 JoyControlStickEditDialogHelper::JoyControlStickEditDialogHelper(JoyControlStick *stick, QObject *parent) :
21     QObject(parent)
22 {
23     Q_ASSERT(stick);
24 
25     this->stick = stick;
26 }
27 
setPendingSlots(QHash<JoyControlStick::JoyStickDirections,JoyButtonSlot * > * tempSlots)28 void JoyControlStickEditDialogHelper::setPendingSlots(QHash<JoyControlStick::JoyStickDirections,
29                                                       JoyButtonSlot *> *tempSlots)
30 {
31     pendingSlots.clear();
32 
33     QHashIterator<JoyControlStick::JoyStickDirections, JoyButtonSlot*> iter(*tempSlots);
34     while (iter.hasNext())
35     {
36         iter.next();
37 
38         JoyButtonSlot *slot = iter.value();
39         JoyControlStick::JoyStickDirections tempDir = iter.key();
40         pendingSlots.insert(tempDir, slot);
41     }
42 }
43 
clearPendingSlots()44 void JoyControlStickEditDialogHelper::clearPendingSlots()
45 {
46     pendingSlots.clear();
47 }
48 
setFromPendingSlots()49 void JoyControlStickEditDialogHelper::setFromPendingSlots()
50 {
51     if (!pendingSlots.isEmpty())
52     {
53         QHashIterator<JoyControlStick::JoyStickDirections, JoyButtonSlot*> iter(pendingSlots);
54         while (iter.hasNext())
55         {
56             iter.next();
57 
58             JoyButtonSlot *slot = iter.value();
59             if (slot)
60             {
61                 JoyControlStick::JoyStickDirections tempDir = iter.key();
62                 JoyControlStickButton *button = stick->getDirectionButton(tempDir);
63                 if (button)
64                 {
65                     button->clearSlotsEventReset(false);
66                     button->setAssignedSlot(slot->getSlotCode(), slot->getSlotCodeAlias(),
67                                             slot->getSlotMode());
68                 }
69 
70                 slot->deleteLater();
71             }
72         }
73     }
74 }
75 
clearButtonsSlotsEventReset()76 void JoyControlStickEditDialogHelper::clearButtonsSlotsEventReset()
77 {
78     QHash<JoyControlStick::JoyStickDirections, JoyControlStickButton*> *buttons = stick->getButtons();
79     QHashIterator<JoyControlStick::JoyStickDirections, JoyControlStickButton*> iter(*buttons);
80     while (iter.hasNext())
81     {
82         JoyControlStickButton *button = iter.next().value();
83         if (button)
84         {
85             button->clearSlotsEventReset();
86         }
87     }
88 }
89 
updateControlStickDelay(int value)90 void JoyControlStickEditDialogHelper::updateControlStickDelay(int value)
91 {
92     int temp = value * 10;
93     if (stick->getStickDelay() != temp)
94     {
95         stick->setStickDelay(temp);
96     }
97 }
98