File indexing completed on 2024-12-08 12:55:20
0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #include "polleditorbackend.h" 0005 0006 #include <QJsonArray> 0007 0008 PollEditorBackend::PollEditorBackend(QObject *parent) 0009 : QObject(parent) 0010 { 0011 m_options.push_back(QString{}); 0012 m_options.push_back(QString{}); 0013 } 0014 0015 void PollEditorBackend::addOption() 0016 { 0017 m_options.push_back(QString{}); 0018 0019 checkValidity(); 0020 0021 Q_EMIT optionsChanged(); 0022 } 0023 0024 void PollEditorBackend::removeOption(int index) 0025 { 0026 m_options.removeAt(index); 0027 0028 checkValidity(); 0029 0030 Q_EMIT optionsChanged(); 0031 } 0032 0033 void PollEditorBackend::setOption(int index, const QString &name) 0034 { 0035 m_options[index] = name; 0036 0037 checkValidity(); 0038 } 0039 0040 QJsonObject PollEditorBackend::toJsonObject() const 0041 { 0042 QJsonArray optionsArray; 0043 0044 for (const auto &options : m_options) { 0045 optionsArray.push_back(options.toString()); 0046 } 0047 0048 QJsonObject pollObj; 0049 pollObj["options"] = optionsArray; 0050 0051 pollObj["expires_in"] = m_expiresIn; 0052 pollObj["multiple"] = m_multipleChoice; 0053 pollObj["hide_totals"] = m_hideTotals; 0054 0055 return pollObj; 0056 } 0057 0058 bool PollEditorBackend::isValid() const 0059 { 0060 return m_valid; 0061 } 0062 0063 void PollEditorBackend::checkValidity() 0064 { 0065 const bool isValid = std::all_of(m_options.cbegin(), m_options.cend(), [](const auto &option) { 0066 return !option.toString().isEmpty(); 0067 }); 0068 0069 if (isValid != m_valid) { 0070 m_valid = isValid; 0071 Q_EMIT validityChanged(); 0072 } 0073 }