25 class ModeChangeHandler
28 ModeChangeHandler(
Remote *remote) {
31 virtual ~ModeChangeHandler(){}
33 virtual bool handleModeButton(
const QString &button) = 0;
35 virtual QStringList availableModeSwitchButtons()
const = 0;
36 virtual void addMode(
Mode *mode)
const = 0;
37 virtual void handleModeButtonAssignment(
const QString &button){
40 virtual void handleModeButtonAssignments(){};
47 class GroupModeChangeHandler :
public ModeChangeHandler
51 GroupModeChangeHandler(
Remote* remote) : ModeChangeHandler(remote) {}
69 bool handleModeButton(
const QString& button) {
71 int size = m_remote->m_modeList.size();
72 int currentModeHasButton = m_remote->currentMode()->button() == button;
74 int index= currentModeHasButton ? m_remote->m_modeList.indexOf(m_remote->currentMode()) +1 : 0;
76 for(; index < size ; ++index) {
77 if (m_remote->m_modeList.at(index)->button() == button) {
79 m_remote->setCurrentMode(m_remote->m_modeList.at(index));
84 if (currentModeHasButton) {
87 m_remote->setCurrentMode(m_remote->masterMode());
92 kDebug() <<
"Mode with button " << button <<
" not available. Mode is not changed.";
100 QStringList availableModeSwitchButtons()
const {
104 retList.append(button.
name());
109 void addMode(
Mode* mode)
const {
110 m_remote->m_modeList.append(mode);
114 class CycleModeChangeHandler :
public ModeChangeHandler
118 CycleModeChangeHandler(
Remote* remote) : ModeChangeHandler(remote) {}
120 bool handleModeButton(
const QString& button) {
122 if(!m_remote->previousModeButton().isEmpty() && m_remote->previousModeButton() == button){
123 index = m_remote->m_modeList.indexOf(m_remote->currentMode());
124 index = index > 0 ? index-1 : m_remote->m_modeList.size()-1;
125 }
else if (! m_remote->nextModeButton().isEmpty() && m_remote->nextModeButton() == button){
126 index = m_remote->m_modeList.indexOf(m_remote->currentMode());
127 index = index < m_remote->m_modeList.size() -1 ? index+1 : 0;
128 }
else if((m_remote->currentMode()->button().isEmpty() || m_remote->currentMode()->button() != button )){
129 index = indexOfModeButton(button);
132 m_remote->setCurrentMode(m_remote->allModes().at(index));
142 QStringList availableModeSwitchButtons()
const {
145 retList << button.
name();
147 retList.removeAll(m_remote->nextModeButton());
148 retList.removeAll(m_remote->previousModeButton());
149 foreach(
const Mode *mode, m_remote->m_modeList){
150 retList.removeAll(mode->
button());
155 void addMode(
Mode* mode)
const {
156 foreach(
Mode *m, m_remote->m_modeList){
158 kDebug() <<
"mode "<< m->
name() <<
" has already assigned the button " << mode->
button();
162 m_remote->m_modeList.append(mode);
165 virtual void handleModeButtonAssignment(
const QString &button) {
166 if(button.isEmpty()){
169 foreach(
Mode* mode, m_remote->allModes()){
170 if(mode->
button() == button){
176 int indexOfModeButton(
const QString &button){
177 if(button.isEmpty()){
180 for (
int index=0; index < m_remote->m_modeList.size() ; ++index) {
181 kDebug()<<
"index " << index <<
" size " << m_remote->m_modeList.size();
182 if (m_remote->m_modeList.at(index)->button() == button) {
189 virtual void handleModeButtonAssignments(){
190 handleModeButtonAssignment(m_remote->nextModeButton());
191 handleModeButtonAssignment(m_remote->previousModeButton());
192 int size = m_remote->m_modeList.size();
193 for(
int index=0; index < size; ++index){
194 QString buttonToValidate = m_remote->m_modeList.at(index)->button();
195 if(! buttonToValidate.isEmpty()){
196 for(
int counter=index+1; counter< size; ++counter){
197 Mode *mode = m_remote->m_modeList.at(counter);
198 if(!mode->
button().isEmpty() && mode->
button() == buttonToValidate){
218 m_remoteName = remote;
219 m_modechangeHandler = 0;
222 bool hasMaster =
false;
224 foreach(
Mode *mode, m_modeList) {
225 if (mode->
name() == QLatin1String(
"Master" )) {
240 while (!m_modeList.isEmpty()) {
241 Mode *mode = m_modeList.first();
242 m_modeList.remove(0);
252 int oldPos = m_modeList.indexOf(mode);
255 m_modeList.remove(oldPos);
256 m_modeList.insert(oldPos - 1, mode);
261 int oldPos = m_modeList.indexOf(mode);
263 if (oldPos < (m_modeList.count() - 1)) {
264 m_modeList.remove(oldPos);
265 m_modeList.insert(oldPos + 1, mode);
275 if(mode !=
masterMode() && mode->
name() != QLatin1String(
"Master" )){
276 m_modechangeHandler->addMode(mode);
281 if (mode->
name() == QLatin1String(
"Master" )) {
282 kDebug() <<
"cannot delete the Master mode";
286 if (m_defaultMode == mode) {
288 foreach(
Mode *tmp, m_modeList) {
289 if (tmp->
name() == QLatin1String(
"Master" )) {
296 m_modeList.remove(m_modeList.indexOf(mode));
301 foreach(
Mode *mode, m_modeList) {
303 if (mode->
name() == QLatin1String(
"Master" )) {
307 kDebug() <<
"Master mode not found";
314 return m_defaultMode;
318 if (!m_modeList.contains(mode)) {
319 m_modeList.append(mode);
322 m_defaultMode = mode;
326 foreach(
Mode *mode, m_modeList) {
327 if (mode->
name() == modeName) {
335 foreach(
Mode *mode, m_modeList) {
336 if (mode->
name() == modeName) {
344 if (m_currentMode != 0) {
345 return m_currentMode;
348 return m_defaultMode;
352 m_currentMode = mode;
360 return m_modechangeHandler->handleModeButton(button);
364 return m_modechangeHandler->type();
368 delete m_modechangeHandler;
374 m_modechangeHandler->handleModeButtonAssignments();
378 QStringList buttonList = m_modechangeHandler->availableModeSwitchButtons();
379 if(mode && !mode->
button().isEmpty() && !buttonList.contains(mode->
button())){
380 buttonList.append(mode->
button());
386 QStringList buttonList = m_modechangeHandler->availableModeSwitchButtons();
397 return m_nextModeButton;
401 m_nextModeButton = button;
402 m_modechangeHandler->handleModeButtonAssignment(button);
406 return m_previousModeButton;
410 m_previousModeButton = button;
411 m_modechangeHandler->handleModeButtonAssignment(button);
void setButton(const QString &button)
void setNextModeButton(const QString &button)
static QStringList allRemoteNames()
Get the Names of the available remotes in the system.
void removeMode(Mode *mode)
Remove the given Mode from this Remote and delete it.
Mode * modeByName(const QString &name) const
QVector< Mode * > allModes() const
friend class CycleModeChangeHandler
ModeChangeMode modeChangeMode() const
void setPreviousModeButton(const QString &button)
Mode * currentMode() const
friend class GroupModeChangeHandler
QString previousModeButton() const
QString nextModeButton() const
void addMode(Mode *mode)
Add the given Mode to this Remote.
void moveModeUp(Mode *mode)
~Remote()
Destrys the Remote and all of its Modes.
Mode * masterMode() const
void setDefaultMode(Mode *mode)
Mode * defaultMode() const
void setModeChangeMode(ModeChangeMode modeChangeMode)
QStringList availableModeSwitchButtons(const Mode *mode=0) const
Get buttons available for this mode (Free buttons + the current button for this mode) ...
void setCurrentMode(Mode *mode)
bool nextMode(const QString &button)
QStringList availableModeCycleButtons() const
Get buttons available for switching to the next or previous mode (Free buttons + the current nextMode...
void moveModeDown(Mode *mode)