// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // One poll, drawn after NeoChat's messagecontent/PollComponent.qml: the question // with a small mark beside it, and one row per answer carrying a checkbox, a // count and a bar under it. // // The bar and the count are shown only once the poll has ended or is disclosed. // Before that, an undisclosed poll shows who picked what and nothing else, which // is what the Matrix spec asks for and what an earlier row of plain buttons did // not: it drew the totals in the label, so every viewer could read them. import QtQuick import QtQuick.Layouts import QtQuick.Controls as QQC2 import org.kde.kirigami as Kirigami import org.kde.kirigamiaddons.delegates as Delegates import koutnet.app ColumnLayout { id: root // answers: [{id, body}], counts: [int], myVotes: [id], totalVotes: int required property var poll required property string chatId required property string eventId readonly property bool disclosed: poll && poll.disclosed === true readonly property bool ended: false // hidden until the poll has been read back with its totals readonly property bool showTotals: root.disclosed || root.ended readonly property int totalCount: root.poll && root.poll.totalVotes !== undefined ? root.poll.totalVotes : (root.poll && root.poll.counts ? root.poll.counts.reduce(function(a, b) { return a + b }, 0) : 0) Layout.fillWidth: true spacing: 0 RowLayout { Layout.fillWidth: true Kirigami.Icon { Layout.alignment: Qt.AlignTop implicitWidth: Kirigami.Units.iconSizes.smallMedium implicitHeight: implicitWidth source: "amarok_playcount" } QQC2.Label { id: questionLabel Layout.fillWidth: true Layout.alignment: Qt.AlignTop Layout.topMargin: Kirigami.Units.smallSpacing Layout.bottomMargin: Kirigami.Units.smallSpacing text: root.poll ? (root.poll.question || "") : "" textFormat: Text.PlainText wrapMode: Text.Wrap } } Repeater { model: root.poll && root.poll.answers ? root.poll.answers : [] delegate: Delegates.RoundedItemDelegate { id: answerRow required property var modelData required property int index readonly property int count: root.poll && root.poll.counts ? (root.poll.counts[index] || 0) : 0 readonly property bool localChoice: root.poll && root.poll.myVotes ? root.poll.myVotes.indexOf(modelData.id) >= 0 : false // the answer with the most votes once they are visible; a tie is not a // winner, which is what the star is for readonly property bool isWinner: root.showTotals && answerRow.count > 0 && answerRow.count === root.highestCount readonly property int highestCount: { if (!root.poll || !root.poll.counts) return 0 return root.poll.counts.reduce(function(a, b) { return Math.max(a, b || 0) }, 0) } Layout.fillWidth: true Layout.leftMargin: -Kirigami.Units.smallSpacing Layout.rightMargin: -Kirigami.Units.smallSpacing highlighted: false topPadding: Kirigami.Units.smallSpacing bottomPadding: Kirigami.Units.smallSpacing enabled: !root.ended onClicked: matrixRooms.sendPollVote(root.chatId, root.eventId, answerRow.modelData.id || "") contentItem: ColumnLayout { Layout.fillWidth: true spacing: Kirigami.Units.smallSpacing / 2 RowLayout { Layout.fillWidth: true QQC2.CheckBox { Layout.alignment: Qt.AlignTop enabled: !root.ended checked: answerRow.localChoice onClicked: answerRow.clicked() } QQC2.Label { Layout.alignment: Qt.AlignTop Layout.fillWidth: true text: answerRow.modelData.body || "" textFormat: Text.PlainText wrapMode: Text.Wrap } Kirigami.Icon { Layout.alignment: Qt.AlignTop implicitWidth: Kirigami.Units.iconSizes.small implicitHeight: implicitWidth visible: answerRow.isWinner source: "favorite-favorited" } QQC2.Label { Layout.alignment: Qt.AlignTop visible: root.showTotals horizontalAlignment: Text.AlignRight text: i18ncp("@info a number of votes, %1 is how many", "%1 Vote", "%1 Votes", answerRow.count) } } QQC2.ProgressBar { Layout.fillWidth: true // empty until the totals may be shown, so an undisclosed poll // has no bar to read a number off to: Math.max(1, root.totalCount) value: root.showTotals ? answerRow.count : 0 } } } } QQC2.Label { Layout.fillWidth: true Layout.topMargin: Kirigami.Units.smallSpacing / 2 visible: root.showTotals text: root.ended ? i18ncp("@info how many people voted in a poll that has ended, %1 is how many", "Based on votes by %1 user (ended)", "Based on votes by %1 users (ended)", root.totalCount) : i18ncp("@info how many people voted in a poll, %1 is how many", "Based on votes by %1 user", "Based on votes by %1 users", root.totalCount) font.pointSize: questionLabel.font.pointSize * 0.8 color: AppPalette.disabledText } }