src/platform/qt/Action.cpp (view raw)
1/* Copyright (c) 2013-2018 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "Action.h"
7
8using namespace QGBA;
9
10Action::Action(QObject* parent)
11 : QObject(parent)
12{
13}
14
15Action::Action(Function function, const QString& name, const QString& visibleName, QObject* parent)
16 : QObject(parent)
17 , m_function(function)
18 , m_name(name)
19 , m_visibleName(visibleName)
20{
21}
22
23Action::Action(Action::BooleanFunction function, const QString& name, const QString& visibleName, QObject* parent)
24 : QObject(parent)
25 , m_booleanFunction(function)
26 , m_name(name)
27 , m_visibleName(visibleName)
28{
29}
30
31Action::Action(const QString& name, const QString& visibleName, QObject* parent)
32 : QObject(parent)
33 , m_name(name)
34 , m_visibleName(visibleName)
35{
36}
37
38Action::Action(const Action& other)
39 : QObject(other.parent())
40 , m_enabled(other.m_enabled)
41 , m_active(other.m_active)
42 , m_function(other.m_function)
43 , m_booleanFunction(other.m_booleanFunction)
44 , m_name(other.m_name)
45 , m_visibleName(other.m_visibleName)
46{
47}
48
49Action::Action(Action& other)
50 : QObject(other.parent())
51 , m_enabled(other.m_enabled)
52 , m_active(other.m_active)
53 , m_function(other.m_function)
54 , m_booleanFunction(other.m_booleanFunction)
55 , m_name(other.m_name)
56 , m_visibleName(other.m_visibleName)
57{
58}
59
60void Action::connect(Function func) {
61 m_booleanFunction = {};
62 m_function = func;
63}
64
65void Action::trigger(bool active) {
66 if (!m_enabled) {
67 return;
68 }
69
70 if (m_exclusive && !m_booleanFunction) {
71 active = true;
72 }
73
74 if (m_function && active) {
75 m_function();
76 }
77 if (m_booleanFunction) {
78 m_booleanFunction(active);
79 }
80
81 m_active = active;
82 emit activated(active);
83}
84
85void Action::setEnabled(bool e) {
86 if (m_enabled == e) {
87 return;
88 }
89 m_enabled = e;
90 emit enabled(e);
91}
92
93void Action::setActive(bool a) {
94 if (m_active == a) {
95 return;
96 }
97 m_active = a;
98 emit activated(a);
99}
100
101Action& Action::operator=(const Action& other) {
102 setParent(other.parent());
103 m_enabled = other.m_enabled;
104 m_active = other.m_active;
105 m_function = other.m_function;
106 m_booleanFunction = other.m_booleanFunction;
107 m_name = other.m_name;
108 m_visibleName = other.m_visibleName;
109 return *this;
110}