File: reporttree.cpp

package info (click to toggle)
scram 0.16.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,016 kB
  • sloc: xml: 120,766; cpp: 23,966; python: 1,256; ansic: 100; makefile: 9
file content (122 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2017-2018 Olzhas Rakhimov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/// @file

#include <cstdint>

#include "reporttree.h"

#include "guiassert.h"

namespace scram::gui {

ReportTree::ReportTree(const std::vector<core::RiskAnalysis::Result> *results,
                       QObject *parent)
    : QAbstractItemModel(parent), m_results(*results)
{
}

int ReportTree::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return m_results.size();
    if (parent.parent().isValid())
        return 0;

    GUI_ASSERT(parent.row() < m_results.size(), 0);
    const core::RiskAnalysis::Result &result = m_results[parent.row()];
    if (result.importance_analysis)
        return 3;
    if (result.probability_analysis)
        return 2;
    GUI_ASSERT(result.fault_tree_analysis, 0);
    return 1;
}

int ReportTree::columnCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return 1;
    if (parent.parent().isValid())
        return 0;
    return 1;
}

QModelIndex ReportTree::index(int row, int column,
                              const QModelIndex &parent) const
{
    if (!parent.isValid())
        return createIndex(row, column, nullptr);
    GUI_ASSERT(parent.parent().isValid() == false, {});
    // Carry the (parent-row-index + 1) in internal pointer.
    return createIndex(row, column, reinterpret_cast<void *>(parent.row() + 1));
}

QModelIndex ReportTree::parent(const QModelIndex &index) const
{
    GUI_ASSERT(index.isValid(), {});
    if (index.internalPointer() == nullptr)
        return {};
    // Retrieve the parent row index from the internal pointer.
    return createIndex(reinterpret_cast<std::uintptr_t>(index.internalPointer())
                           - 1,
                       0, nullptr);
}

QVariant ReportTree::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || role != Qt::DisplayRole)
        return {};
    struct
    {
        QString operator()(const mef::Gate *gate)
        {
            return QString::fromStdString(gate->id());
        }

        QString operator()(const std::pair<const mef::InitiatingEvent &,
                                           const mef::Sequence &> &)
        {
            GUI_ASSERT(false && "unexpected analysis target", {});
            return {};
        }
    } nameExtractor;

    if (!index.parent().isValid()) {
        GUI_ASSERT(index.row() < m_results.size(), {});
        return std::visit(nameExtractor, m_results[index.row()].id.target);
    }
    GUI_ASSERT(index.parent().row() < m_results.size(), {});
    const core::RiskAnalysis::Result &result = m_results[index.parent().row()];
    switch (static_cast<Row>(index.row())) {
    case Row::Products:
        return tr("Products (%L1)")
            .arg(result.fault_tree_analysis->products().size());
    case Row::Probability:
        GUI_ASSERT(result.probability_analysis, {});
        return tr("Probability (%1)")
            .arg(result.probability_analysis->p_total());
    case Row::Importance:
        GUI_ASSERT(result.importance_analysis, {});
        return tr("Importance Factors (%L1)")
            .arg(result.importance_analysis->importance().size());
    }
    GUI_ASSERT(false && "Unexpected analysis report data", {});
}

} // namespace scram::gui