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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/*
* 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 "elementcontainermodel.h"
#include "src/event.h"
#include "src/ext/variant.h"
#include "src/model.h"
#include "align.h"
#include "guiassert.h"
#include "overload.h"
namespace scram::gui::model {
template <class T>
ElementContainerModel::ElementContainerModel(const T &container, Model *model,
QObject *parent)
: QAbstractItemModel(parent)
{
m_elements.reserve(container.size());
m_elementToIndex.reserve(container.size());
for (const auto &elementPtr : container) {
m_elementToIndex.emplace(elementPtr.get(), m_elements.size());
m_elements.push_back(elementPtr.get());
}
using E = typename T::value_type::element_type;
connect(model, OVERLOAD(Model, added, E *), this,
&ElementContainerModel::addElement);
connect(model, OVERLOAD(Model, removed, E *), this,
&ElementContainerModel::removeElement);
}
void ElementContainerModel::connectElement(Element *element)
{
connect(element, &Element::labelChanged, this, [this, element] {
QModelIndex index =
createIndex(getElementIndex(element), columnCount() - 1, element);
emit dataChanged(index, index);
});
connect(element, &Element::idChanged, this, [this, element] {
QModelIndex index = createIndex(getElementIndex(element), 0, element);
emit dataChanged(index, index);
});
}
QModelIndex ElementContainerModel::index(int row, int column,
const QModelIndex &parent) const
{
GUI_ASSERT(parent.isValid() == false, {});
return createIndex(row, column, getElement(row));
}
Element *ElementContainerModel::getElement(int index) const
{
GUI_ASSERT(index < m_elements.size(), nullptr);
return m_elements[index];
}
int ElementContainerModel::getElementIndex(Element *element) const
{
auto it = m_elementToIndex.find(element);
GUI_ASSERT(it != m_elementToIndex.end(), -1);
return it->second;
}
void ElementContainerModel::addElement(Element *element)
{
int index = m_elements.size();
beginInsertRows({}, index, index);
m_elementToIndex.emplace(element, index);
m_elements.push_back(element);
endInsertRows();
connectElement(element);
}
void ElementContainerModel::removeElement(Element *element)
{
GUI_ASSERT(m_elementToIndex.count(element), );
int index = m_elementToIndex.find(element)->second;
int lastIndex = m_elements.size() - 1;
auto *lastElement = m_elements.back();
// The following is basically a swap with the last item.
beginRemoveRows({}, lastIndex, lastIndex);
m_elementToIndex.erase(element);
m_elements.pop_back();
endRemoveRows();
if (index != lastIndex) {
m_elements[index] = lastElement;
m_elementToIndex[lastElement] = index;
emit dataChanged(createIndex(index, 0, lastElement),
createIndex(index, columnCount() - 1, lastElement));
}
disconnect(element, 0, this, 0);
}
int ElementContainerModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_elements.size();
}
BasicEventContainerModel::BasicEventContainerModel(Model *model,
QObject *parent)
: ElementContainerModel(model->basicEvents(), model, parent)
{
for (Element *element : elements())
connectElement(element);
}
int BasicEventContainerModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : 4;
}
QVariant BasicEventContainerModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (role == Qt::InitialSortOrderRole && section == 2)
return Qt::DescendingOrder;
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
return ElementContainerModel::headerData(section, orientation, role);
switch (section) {
case 0:
return tr("ID");
case 1:
//: The flavor type of a basic event.
return tr("Flavor");
case 2:
//: In PRA context, probability may be unavailability or unreliability.
return tr("Probability");
case 3:
return tr("Label");
}
GUI_ASSERT(false && "unexpected header section", {});
}
QVariant BasicEventContainerModel::data(const QModelIndex &index,
int role) const
{
if (!index.isValid())
return {};
if (role == Qt::TextAlignmentRole && index.column() == 2)
return ALIGN_NUMBER_IN_TABLE;
if (role == Qt::UserRole)
return QVariant::fromValue(index.internalPointer());
if (role != Qt::DisplayRole)
return {};
auto *basicEvent = static_cast<BasicEvent *>(index.internalPointer());
switch (index.column()) {
case 0:
return basicEvent->id();
case 1:
return BasicEvent::flavorToString(basicEvent->flavor());
case 2:
return basicEvent->probability<QVariant>();
case 3:
return basicEvent->label();
}
GUI_ASSERT(false && "unexpected column", {});
}
void BasicEventContainerModel::connectElement(Element *element)
{
ElementContainerModel::connectElement(element);
connect(static_cast<BasicEvent *>(element), &BasicEvent::flavorChanged,
this, [this, element] {
QModelIndex index =
createIndex(getElementIndex(element), 1, element);
emit dataChanged(index, index);
});
connect(static_cast<BasicEvent *>(element), &BasicEvent::expressionChanged,
this, [this, element] {
QModelIndex index =
createIndex(getElementIndex(element), 2, element);
emit dataChanged(index, index);
});
}
HouseEventContainerModel::HouseEventContainerModel(Model *model,
QObject *parent)
: ElementContainerModel(model->houseEvents(), model, parent)
{
for (Element *element : elements())
connectElement(element);
}
int HouseEventContainerModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : 3;
}
QVariant HouseEventContainerModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
return ElementContainerModel::headerData(section, orientation, role);
switch (section) {
case 0:
return tr("ID");
case 1:
//: House event Boolean state.
return tr("State");
case 2:
return tr("Label");
}
GUI_ASSERT(false && "unexpected header section", {});
}
QVariant HouseEventContainerModel::data(const QModelIndex &index,
int role) const
{
if (!index.isValid())
return {};
if (role == Qt::UserRole)
return QVariant::fromValue(index.internalPointer());
if (role != Qt::DisplayRole)
return {};
auto *houseEvent = static_cast<HouseEvent *>(index.internalPointer());
switch (index.column()) {
case 0:
return houseEvent->id();
case 1:
return houseEvent->state<QString>();
case 2:
return houseEvent->label();
}
GUI_ASSERT(false && "unexpected column", {});
}
void HouseEventContainerModel::connectElement(Element *element)
{
ElementContainerModel::connectElement(element);
connect(static_cast<HouseEvent *>(element), &HouseEvent::stateChanged, this,
[this, element] {
QModelIndex index =
createIndex(getElementIndex(element), 1, element);
emit dataChanged(index, index);
});
}
GateContainerModel::GateContainerModel(Model *model, QObject *parent)
: ElementContainerModel(model->gates(), model, parent)
{
for (Element *element : elements())
connectElement(element);
}
void GateContainerModel::connectElement(Element *element)
{
ElementContainerModel::connectElement(element);
connect(static_cast<Gate *>(element), &Gate::formulaChanged, this,
[this, element] {
int row = getElementIndex(element);
emit dataChanged(createIndex(row, 1, element),
createIndex(row, 2, element));
/// @todo Track gate formula changes more precisely.
beginResetModel();
endResetModel();
});
}
int GateContainerModel::columnCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return 4;
if (parent.parent().isValid())
return 0;
if (parent.column() == 0)
return 1;
return 0;
}
int GateContainerModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return ElementContainerModel::rowCount(parent);
if (parent.parent().isValid())
return 0;
if (parent.column() == 0)
return static_cast<Gate *>(parent.internalPointer())->numArgs();
return 0;
}
QModelIndex GateContainerModel::index(int row, int column,
const QModelIndex &parent) const
{
if (!parent.isValid())
return ElementContainerModel::index(row, column, parent);
GUI_ASSERT(parent.parent().isValid() == false, {});
auto value = reinterpret_cast<std::uintptr_t>(parent.internalPointer());
GUI_ASSERT(value && !(value & m_parentMask), {});
return createIndex(row, column,
reinterpret_cast<void *>(value | m_parentMask));
}
QModelIndex GateContainerModel::parent(const QModelIndex &index) const
{
GUI_ASSERT(index.isValid(), {});
auto value = reinterpret_cast<std::uintptr_t>(index.internalPointer());
GUI_ASSERT(value, {});
if (value & m_parentMask) {
auto *parent = reinterpret_cast<Gate *>(value & ~m_parentMask);
return createIndex(getElementIndex(parent), 0, parent);
}
return {};
}
QVariant GateContainerModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
return ElementContainerModel::headerData(section, orientation, role);
switch (section) {
case 0:
return tr("ID");
case 1:
//: Boolean operator of the Boolean formula.
return tr("Connective");
case 2:
//: The number of arguments in the Boolean formula.
return tr("Args");
case 3:
return tr("Label");
}
GUI_ASSERT(false && "unexpected header section", {});
}
QVariant GateContainerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return {};
auto value = reinterpret_cast<std::uintptr_t>(index.internalPointer());
if (role == Qt::UserRole) {
return QVariant::fromValue(
value & m_parentMask ? nullptr : index.internalPointer());
}
if (role != Qt::DisplayRole)
return {};
if (value & m_parentMask) {
auto *parent = reinterpret_cast<Gate *>(value & ~m_parentMask);
return QString::fromStdString(
ext::as<const mef::Event *>(parent->args().at(index.row()))->id());
}
auto *gate = static_cast<Gate *>(index.internalPointer());
switch (index.column()) {
case 0:
return gate->id();
case 1:
return gate->type<QString>();
case 2:
return gate->numArgs();
case 3:
return gate->label();
}
GUI_ASSERT(false && "unexpected column", {});
}
bool GateSortFilterProxyModel::filterAcceptsRow(int row,
const QModelIndex &parent) const
{
if (parent.isValid())
return true;
return QSortFilterProxyModel::filterAcceptsRow(row, parent);
}
bool GateSortFilterProxyModel::lessThan(const QModelIndex &lhs,
const QModelIndex &rhs) const
{
if (lhs.parent().isValid())
return false; // Don't sort arguments.
return QSortFilterProxyModel::lessThan(lhs, rhs);
}
} // namespace scram::gui::model
|