~mzanetti/unity8/fix-left-edge-on-spread

« back to all changes in this revision

Viewing changes to plugins/Unity/Indicators/rootstateparser.cpp

  • Committer: Michael Zanetti
  • Date: 2015-01-12 11:21:17 UTC
  • mfrom: (1459.1.85 unity8)
  • Revision ID: michael.zanetti@canonical.com-20150112112117-0x9srs9dx0ndp60g
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 *
13
13
 * You should have received a copy of the GNU Lesser General Public License
14
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authors:
17
 
 *      Nick Dedekind <nick.dedekind@canonical.com>
18
15
 */
19
16
 
20
 
#include "rootactionstate.h"
21
 
#include "indicators.h"
22
 
 
23
 
#include <unitymenumodel.h>
24
 
#include <QVariant>
25
 
#include <QIcon>
 
17
#include "rootstateparser.h"
26
18
 
27
19
extern "C" {
28
20
#include <glib.h>
29
21
#include <gio/gio.h>
30
22
}
31
23
 
32
 
RootActionState::RootActionState(QObject *parent)
33
 
    : ActionStateParser(parent),
34
 
      m_menu(nullptr)
35
 
{
36
 
}
37
 
 
38
 
RootActionState::~RootActionState()
39
 
{
40
 
}
41
 
 
42
 
UnityMenuModel* RootActionState::menu() const
43
 
{
44
 
    return m_menu;
45
 
}
46
 
 
47
 
void RootActionState::setMenu(UnityMenuModel* menu)
48
 
{
49
 
    if (m_menu != menu) {
50
 
        if (m_menu) {
51
 
            m_menu->disconnect(this);
52
 
        }
53
 
        m_menu = menu;
54
 
 
55
 
        if (m_menu) {
56
 
            connect(m_menu, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SLOT(onModelRowsAdded(const QModelIndex&, int, int)));
57
 
            connect(m_menu, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SLOT(onModelRowsRemoved(const QModelIndex&, int, int)));
58
 
            connect(m_menu, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)), SLOT(onModelDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));
59
 
 
60
 
            connect(m_menu, SIGNAL(destroyed()), SLOT(reset()));
61
 
        }
62
 
        updateActionState();
63
 
        Q_EMIT menuChanged();
64
 
    }
65
 
}
66
 
 
67
 
void RootActionState::onModelRowsAdded(const QModelIndex& parent, int start, int end)
68
 
{
69
 
    Q_UNUSED(parent);
70
 
    if (start == 0 && end >= 0) {
71
 
        updateActionState();
72
 
    }
73
 
}
74
 
 
75
 
void RootActionState::onModelRowsRemoved(const QModelIndex& parent, int start, int end)
76
 
{
77
 
    Q_UNUSED(parent);
78
 
    if (start == 0 && end >= 0) {
79
 
        updateActionState();
80
 
    }
81
 
}
82
 
 
83
 
void RootActionState::onModelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
84
 
{
85
 
    Q_UNUSED(roles);
86
 
    if (!topLeft.isValid() || !bottomRight.isValid()) {
87
 
        return;
88
 
    }
89
 
 
90
 
    if (topLeft.row() <= 0 && bottomRight.row() >= 0) {
91
 
        updateActionState();
92
 
    }
93
 
}
94
 
 
95
 
void RootActionState::reset()
96
 
{
97
 
    m_cachedState.clear();
98
 
    m_menu = nullptr;
99
 
 
100
 
    Q_EMIT menuChanged();
101
 
    Q_EMIT updated();
102
 
}
103
 
 
104
 
void RootActionState::updateActionState()
105
 
{
106
 
    if (m_menu && m_menu->rowCount() > 0) {
107
 
        ActionStateParser* oldParser = m_menu->actionStateParser();
108
 
        m_menu->setActionStateParser(this);
109
 
 
110
 
        m_cachedState = m_menu->get(0, "actionState").toMap();
111
 
 
112
 
        m_menu->setActionStateParser(oldParser);
113
 
    } else {
114
 
        m_cachedState.clear();
115
 
    }
116
 
    Q_EMIT updated();
117
 
}
118
 
 
119
 
bool RootActionState::isValid() const
120
 
{
121
 
    return m_menu && m_menu->rowCount() > 0;
122
 
}
123
 
 
124
 
QString RootActionState::title() const
125
 
{
126
 
    if (!isValid()) return QString();
127
 
 
128
 
    return m_cachedState.value("title", QVariant::fromValue(QString())).toString();
129
 
}
130
 
 
131
 
QString RootActionState::leftLabel() const
132
 
{
133
 
    if (!isValid()) return QString();
134
 
 
135
 
    return m_cachedState.value("pre-label", QVariant::fromValue(QString())).toString();
136
 
}
137
 
 
138
 
QString RootActionState::rightLabel() const
139
 
{
140
 
    if (!isValid()) return QString();
141
 
 
142
 
    return m_cachedState.value("label", QVariant::fromValue(QString())).toString();
143
 
}
144
 
 
145
 
QStringList RootActionState::icons() const
146
 
{
147
 
    if (!isValid()) return QStringList();
148
 
 
149
 
    return m_cachedState.value("icons", QVariant::fromValue(QStringList())).toStringList();
150
 
}
151
 
 
152
 
QString RootActionState::accessibleName() const
153
 
{
154
 
    if (!isValid()) return QString();
155
 
 
156
 
    return m_cachedState.value("accessible-desc", QVariant::fromValue(QString())).toString();
157
 
}
158
 
 
159
 
bool RootActionState::indicatorVisible() const
160
 
{
161
 
    if (!isValid()) return false;
162
 
 
163
 
    return m_cachedState.value("visible", QVariant::fromValue(true)).toBool();
 
24
RootStateParser::RootStateParser(QObject* parent)
 
25
    : ActionStateParser(parent)
 
26
{
164
27
}
165
28
 
166
29
static QString iconUri(GIcon *icon)
209
72
    return uri;
210
73
}
211
74
 
212
 
QVariant RootActionState::toQVariant(GVariant* state) const
 
75
QVariant RootStateParser::toQVariant(GVariant* state) const
213
76
{
214
77
    if (!state) {
215
78
        return QVariant();
296
159
    }
297
160
    return ActionStateParser::toQVariant(state);
298
161
}
 
162
 
 
163
 
 
164
RootStateObject::RootStateObject(QObject* parent)
 
165
    : QObject(parent)
 
166
{
 
167
}
 
168
 
 
169
QString RootStateObject::title() const
 
170
{
 
171
    if (!valid()) return QString();
 
172
 
 
173
    return m_currentState.value("title", QVariant::fromValue(QString())).toString();
 
174
}
 
175
 
 
176
QString RootStateObject::leftLabel() const
 
177
{
 
178
    if (!valid()) return QString();
 
179
 
 
180
    return m_currentState.value("pre-label", QVariant::fromValue(QString())).toString();
 
181
}
 
182
 
 
183
QString RootStateObject::rightLabel() const
 
184
{
 
185
    if (!valid()) return QString();
 
186
 
 
187
    return m_currentState.value("label", QVariant::fromValue(QString())).toString();
 
188
}
 
189
 
 
190
QStringList RootStateObject::icons() const
 
191
{
 
192
    if (!valid()) return QStringList();
 
193
 
 
194
    return m_currentState.value("icons", QVariant::fromValue(QStringList())).toStringList();
 
195
}
 
196
 
 
197
QString RootStateObject::accessibleName() const
 
198
{
 
199
    if (!valid()) return QString();
 
200
 
 
201
    return m_currentState.value("accessible-desc", QVariant::fromValue(QString())).toString();
 
202
}
 
203
 
 
204
bool RootStateObject::indicatorVisible() const
 
205
{
 
206
    if (!valid()) return false;
 
207
 
 
208
    return m_currentState.value("visible", QVariant::fromValue(true)).toBool();
 
209
}
 
210
 
 
211
void RootStateObject::setCurrentState(const QVariantMap& newState)
 
212
{
 
213
    QString oldTitle = title();
 
214
    QString oldLeftLabel = leftLabel();
 
215
    QString oldRightLabel = rightLabel();
 
216
    QStringList oldIcons = icons();
 
217
    QString oldAccessibleName = accessibleName();
 
218
    bool oldIndicatorVisible = indicatorVisible();
 
219
 
 
220
    if (m_currentState != newState) {
 
221
        m_currentState = newState;
 
222
        Q_EMIT updated();
 
223
 
 
224
        if (oldTitle != title()) Q_EMIT titleChanged();
 
225
        if (oldLeftLabel != leftLabel()) Q_EMIT leftLabelChanged();
 
226
        if (oldRightLabel != rightLabel()) Q_EMIT rightLabelChanged();
 
227
        if (oldIcons != icons()) Q_EMIT iconsChanged();
 
228
        if (oldAccessibleName != accessibleName()) Q_EMIT accessibleNameChanged();
 
229
        if (oldIndicatorVisible != indicatorVisible()) Q_EMIT indicatorVisibleChanged();
 
230
    }
 
231
}