summaryrefslogtreecommitdiffstats
path: root/src/preferences/broadcastsettingsmodel.cpp
blob: 33ebcb895d8ad71800be1596a2c40d1a0ef627cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "preferences/broadcastsettingsmodel.h"

#include <preferences/broadcastsettings.h>

#include "moc_broadcastsettingsmodel.cpp"

namespace {
const int kColumnEnabled = 0;
const int kColumnName = 1;
const int kColumnStatus = 2;
}

BroadcastSettingsModel::BroadcastSettingsModel() {
}

void BroadcastSettingsModel::resetFromSettings(BroadcastSettingsPointer pSettings) {
    if (!pSettings) {
        return;
    }

    if (!m_profiles.isEmpty()) {
        beginRemoveRows(QModelIndex(), 0, m_profiles.size()-1);
        endRemoveRows();
        m_profiles.clear();
    }

    const QList<BroadcastProfilePtr> profiles = pSettings->profiles();
    for (BroadcastProfilePtr profile : profiles) {
        BroadcastProfilePtr copy = profile->valuesCopy();
        copy->setConnectionStatus(profile->connectionStatus());
        connect(profile.data(),
                &BroadcastProfile::statusChanged,
                copy.data(),
                &BroadcastProfile::relayStatus);
        connect(profile.data(),
                &BroadcastProfile::connectionStatusChanged,
                copy.data(),
                &BroadcastProfile::relayConnectionStatus);
        addProfileToModel(copy);
    }
}

bool BroadcastSettingsModel::addProfileToModel(BroadcastProfilePtr profile) {
    if (!profile)
        return false;

    int position = m_profiles.size();
    beginInsertRows(QModelIndex(), position, position);

    // It is best to avoid using QSharedPointer::data(), especially when
    // passing it to another function, as it puts the associated pointer
    // at risk of being manually deleted.
    // However it's fine with Qt's connect because it can be trusted that
    // it won't delete the pointer.
    connect(profile.data(),
            &BroadcastProfile::profileNameChanged,
            this,
            &BroadcastSettingsModel::onProfileNameChanged);
    connect(profile.data(),
            &BroadcastProfile::connectionStatusChanged,
            this,
            &BroadcastSettingsModel::onConnectionStatusChanged);
    m_profiles.insert(profile->getProfileName(), BroadcastProfilePtr(profile));

    endInsertRows();
    return true;
}

void BroadcastSettingsModel::deleteProfileFromModel(BroadcastProfilePtr profile) {
    if (!profile)
        return;

    QString name = profile->getProfileName();
    int position = 0;
    for (auto it = m_profiles.constBegin(); it != m_profiles.constEnd(); ++it, ++position) {
        if (it.key() == name) {
            beginRemoveRows(QModelIndex(), position, position);
            endRemoveRows();
        }
    }
    m_profiles.remove(profile->getProfileName());
}

BroadcastProfilePtr BroadcastSettingsModel::getProfileByName(
        const QString& profileName) {
    return m_profiles.value(profileName, BroadcastProfilePtr());
}

int BroadcastSettingsModel::rowCount(const QModelIndex& parent) const {
    Q_UNUSED(parent);
    return m_profiles.size();
}

int BroadcastSettingsModel::columnCount(const QModelIndex& parent) const {
    Q_UNUSED(parent);
    return 3;
}

QVariant BroadcastSettingsModel::data(const QModelIndex& index, int role) const {
    int rowIndex = index.row();
    if (!index.isValid() || rowIndex >= m_profiles.size())
        return QVariant();

    auto it = m_profiles.constBegin() + rowIndex;
    if (it != m_profiles.constEnd()) {
        BroadcastProfilePtr profile = it.value();
        int column = index.column();
        if (column == kColumnEnabled) {
            if (role == Qt::CheckStateRole) {
                return (profile->getEnabled() == true ? Qt::Checked : Qt::Unchecked);
            } else if (role == Qt::TextAlignmentRole) {
                return Qt::AlignCenter;
            }
        }
        else if (column == kColumnName && role == Qt::DisplayRole) {
            return profile->getProfileName();
        }
        else if (column == kColumnStatus) {
            if (role == Qt::DisplayRole) {
                return connectionStatusString(profile);
            } else if (role == Qt::BackgroundRole) {
                return QBrush(connectionStatusBgColor(profile));
            } else if (role == Qt::ForegroundRole &&
                    profile->connectionStatus() != BroadcastProfile::STATUS_UNCONNECTED) {
                return QBrush(Qt::black);
            } else if (role == Qt::TextAlignmentRole) {
                return Qt::AlignCenter;
            }
        }
    }

    return QVariant();
}

QVariant BroadcastSettingsModel::headerData(int section, Qt::Orientation orientation,
        int role) const {
    if (orientation == Qt::Horizontal) {
        if (role == Qt::DisplayRole) {
            if (section == kColumnEnabled) {
                return tr("Enabled");
            } else if (section == kColumnName) {
                return tr("Name");
            } else if (section == kColumnStatus) {
                return tr("Status");
            }
        }
    }
    return QVariant();
}

Qt::ItemFlags BroadcastSettingsModel::flags(const QModelIndex& index) const {
    if (index.column() == kColumnEnabled)
        return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;

    if (index.column() == kColumnName)
        return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEnabled;
}

bool BroadcastSettingsModel::setData(const QModelIndex& index, const QVariant& value, int role) {
    if (index.isValid()) {
        auto it = m_profiles.constBegin() + index.row();
        if (it != m_profiles.constEnd()) {
            BroadcastProfilePtr profile = it.value();
            if (index.column() == kColumnEnabled && role == Qt::CheckStateRole) {
                profile->setEnabled(value.toBool());
            }
            if (index.column() == kColumnName && role == Qt::EditRole) {
                QString newName = value.toString();
                newName = newName.trimmed();

                if (!newName.isNull() && !newName.isEmpty())
                    profile->setProfileName(newName);
            }
        }
    }
    return true;
}

QAbstractItemDelegate* BroadcastSettingsModel::delegateForColumn(const int i, QObject* parent) {
    Q_UNUSED(i);
    Q_UNUSED(parent);
    return nullptr;
}

QString BroadcastSettingsModel::connectionStatusString(BroadcastProfilePtr profile) {
    int status = profile->connectionStatus();
    switch(status) {
        case BroadcastProfile::STATUS_UNCONNECTED:
            return tr("Disconnected");
        case BroadcastProfile::STATUS_CONNECTING:
            return tr("Connecting...");
        case BroadcastProfile::STATUS_CONNECTED:
            return tr("Connected");
        case BroadcastProfile::STATUS_FAILURE:
            return tr("Failed");

        default:
            return tr("Unknown");
    }
}

QColor BroadcastSettingsModel::connectionStatusBgColor(BroadcastProfilePtr profile) {
    // Manual colors below were picked using Google's color picker (query: colorpicker)
    int status = profile->connectionStatus();
        switch(status) {
            case BroadcastProfile::STATUS_CONNECTING:
                return QColor(25, 224, 255); // turquoise blue
            case BroadcastProfile::STATUS_CONNECTED:
                return QColor(96, 255, 81); // warm green
            case BroadcastProfile::STATUS_FAILURE:
                return QColor(255, 228, 56); // toned-down yellow

            default:
                return Qt::transparent;
        }
}

void BroadcastSettingsModel::onProfileNameChanged(const QString& oldName, const QString& newName) {
    if (!m_profiles.contains(oldName))
        return;

    BroadcastProfilePtr profile = m_profiles.take(oldName);
    if (profile) {
        m_profiles.insert(newName, profile);
    }

    // Refresh the whole name column
    QModelIndex start = this->index(0, kColumnName);
    QModelIndex end = this->index(this->rowCount()-1, kColumnName);
    emit dataChanged(start, end);
}

void BroadcastSettingsModel::onConnectionStatusChanged(