File indexing completed on 2024-11-10 05:11:10

0001 /*
0002  * Copyright 2018 by Marco Martin <mart@kde.org>
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *    http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  *
0016  */
0017 
0018 #include "sessiondatamap.h"
0019 #include "abstractskillview.h"
0020 #include "sessiondatamodel.h"
0021 
0022 #include <QDebug>
0023 #include <QJSValue>
0024 #include <cstddef>
0025 
0026 SessionDataMap::SessionDataMap(const QString &skillId, AbstractSkillView *parent)
0027     : QQmlPropertyMap(this, parent),
0028       m_skillId(skillId),
0029       m_view(parent)
0030 {
0031     m_updateTimer = new QTimer(this);
0032     m_updateTimer->setSingleShot(true);
0033     m_updateTimer->setInterval(250); //arbitrary
0034     connect(m_updateTimer, &QTimer::timeout, this, [this]() {
0035         if (!m_propertiesToUpdate.isEmpty()) {
0036             m_view->writeProperties(m_skillId, m_propertiesToUpdate);
0037         }
0038         for (auto k : m_propertiesToDelete) {
0039             m_view->deleteProperty(m_skillId, k);
0040         }
0041         m_propertiesToUpdate.clear();
0042         m_propertiesToDelete.clear();
0043     });
0044 }
0045 
0046 SessionDataMap::~SessionDataMap()
0047 {
0048 }
0049 
0050 QVariant SessionDataMap::updateValue(const QString &key, const QVariant &newValue)
0051 {
0052     if (value(key).canConvert<SessionDataModel *>()) {
0053         qWarning() << "Can't replace a model from the client side";
0054         return value(key);
0055     }
0056 
0057     if (newValue.isNull() || !newValue.isValid() ) {
0058         m_propertiesToDelete << key;
0059     } else {
0060         m_propertiesToUpdate[key] = newValue;
0061     }
0062 
0063     m_updateTimer->start();
0064 
0065     return QQmlPropertyMap::updateValue(key, newValue);
0066 }
0067 
0068 void SessionDataMap::insertAndNotify(const QString &key, const QVariant &value)
0069 {
0070     insert(key, value);
0071     emit valueChanged(key, value);
0072 }
0073 
0074 void SessionDataMap::clearAndNotify(const QString &key)
0075 {
0076     clear(key);
0077     emit dataCleared(key);
0078 }
0079 
0080 #include "moc_sessiondatamap.cpp"