File indexing completed on 2025-02-09 05:58:49
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 using namespace Qt::Literals::StringLiterals; 0009 0010 PollEditorBackend::PollEditorBackend(QObject *parent) 0011 : QObject(parent) 0012 { 0013 m_options.push_back(QString{}); 0014 m_options.push_back(QString{}); 0015 } 0016 0017 void PollEditorBackend::addOption() 0018 { 0019 m_options.push_back(QString{}); 0020 0021 checkValidity(); 0022 0023 Q_EMIT optionsChanged(); 0024 } 0025 0026 void PollEditorBackend::removeOption(int index) 0027 { 0028 m_options.removeAt(index); 0029 0030 checkValidity(); 0031 0032 Q_EMIT optionsChanged(); 0033 } 0034 0035 void PollEditorBackend::setOption(int index, const QString &name) 0036 { 0037 m_options[index] = name; 0038 0039 checkValidity(); 0040 } 0041 0042 QJsonObject PollEditorBackend::toJsonObject() const 0043 { 0044 QJsonArray optionsArray; 0045 0046 for (const auto &options : m_options) { 0047 optionsArray.push_back(options.toString()); 0048 } 0049 0050 QJsonObject pollObj; 0051 pollObj["options"_L1] = optionsArray; 0052 0053 pollObj["expires_in"_L1] = m_expiresIn; 0054 pollObj["multiple"_L1] = m_multipleChoice; 0055 pollObj["hide_totals"_L1] = m_hideTotals; 0056 0057 return pollObj; 0058 } 0059 0060 bool PollEditorBackend::isValid() const 0061 { 0062 return m_valid; 0063 } 0064 0065 void PollEditorBackend::checkValidity() 0066 { 0067 const bool isValid = std::all_of(m_options.cbegin(), m_options.cend(), [](const auto &option) { 0068 return !option.toString().isEmpty(); 0069 }); 0070 0071 if (isValid != m_valid) { 0072 m_valid = isValid; 0073 Q_EMIT validityChanged(); 0074 } 0075 } 0076 0077 #include "moc_polleditorbackend.cpp"