swift
namevariantpairlistmodel.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QModelIndex>
7 #include <Qt>
8 #include <QtGlobal>
9 
11 #include "gui/models/columns.h"
13 #include "misc/namevariantpair.h"
15 
16 using namespace swift::misc;
17 
18 namespace swift::gui::models
19 {
20  CNameVariantPairModel::CNameVariantPairModel(bool withIcon, QObject *parent)
21  : CListModelBase("ViewNameVariantPairList", parent)
22  {
23  this->setIconMode(withIcon);
24 
25  // force strings for translation in resource files
26  (void)QT_TRANSLATE_NOOP("ViewNameVariantPairList", "name");
27  (void)QT_TRANSLATE_NOOP("ViewNameVariantPairList", "value");
28  }
29 
31  {
32  this->m_columns.clear();
33  if (withIcon) { this->m_columns.addColumn(CColumn(CNameVariantPair::IndexPixmap)); }
34  this->m_columns.addColumn(CColumn::standardString("name", CNameVariantPair::IndexName));
35  this->m_columns.addColumn(CColumn("value", CNameVariantPair::IndexVariant, new CDefaultFormatter()));
36 
37  // default sort order
38  this->setSortColumnByPropertyIndex(CNameVariantPair::IndexName);
39  this->m_sortOrder = Qt::AscendingOrder;
40  }
41 
42  bool CNameVariantPairModel::addOrUpdateByName(const QString &name, const swift::misc::CVariant &value,
43  const CIcon &icon, bool skipEqualValues)
44  {
45  int index = this->getRowIndexForName(name);
46  CNameVariantPair pair(name, value, icon);
47 
48  if (index < 0)
49  {
50  // not in the list yet, append
51  this->push_back(pair);
52  return true;
53  }
54  else
55  {
56  // already in list, update
57  if (skipEqualValues && this->containsNameValue(name, value)) { return false; }
58  this->update(index, pair);
59  return true;
60  }
61  }
62 
63  int CNameVariantPairModel::getRowIndexForName(const QString &name) const
64  {
65  int rowIndex = this->m_container.getIndexForName(name);
66  return rowIndex;
67  }
68 
69  void CNameVariantPairModel::removeByName(const QString &name)
70  {
71  int rowIndex = this->getRowIndexForName(name);
72  if (rowIndex < 0) return;
73  QModelIndex i = this->index(rowIndex, 0);
74  this->remove(this->at(i));
75  }
76 
77  bool CNameVariantPairModel::containsName(const QString &name) const { return this->m_container.containsName(name); }
78 
79  bool CNameVariantPairModel::containsNameValue(const QString &name, const swift::misc::CVariant &value) const
80  {
81  int rowIndex = this->getRowIndexForName(name);
82  if (rowIndex < 0) { return false; }
83  QModelIndex i = this->index(rowIndex, 0);
84  const CNameVariantPair cv = this->at(i);
85  return value == CVariant::from(cv);
86  }
87 } // namespace swift::gui::models
Single column.
Definition: columns.h:26
static CColumn standardString(const QString &headerName, const swift::misc::CPropertyIndex &propertyIndex, int alignment=CDefaultFormatter::alignDefault())
Get a standard string object formatted column.
Definition: columns.cpp:57
void addColumn(const CColumn &column)
Add a column.
Definition: columns.cpp:100
Column formatter default implementation, also serving as interface.
virtual const ObjectType & at(const QModelIndex &index) const
Object at row position.
virtual void push_back(const ObjectType &object)
Similar to ContainerType::push_back.
virtual void remove(const ObjectType &object)
Remove object.
virtual int update(const ContainerType &container, bool sort=true)
Update by new container.
virtual bool setSortColumnByPropertyIndex(const swift::misc::CPropertyIndex &propertyIndex)
Set column for sorting.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const final
bool containsName(const QString &name) const
Contains name already?
bool addOrUpdateByName(const QString &name, const swift::misc::CVariant &value, const swift::misc::CIcon &icon, bool skipEqualValues)
Add our update a value.
int getRowIndexForName(const QString &name) const
Current row index of given name.
bool containsNameValue(const QString &name, const swift::misc::CVariant &value) const
Contains name / value?
void removeByName(const QString &name)
Remove by given name.
void setIconMode(bool withIcon)
Icon on / off.
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition: icon.h:39
Value / variant pair.
int getIndexForName(const QString &name) const
Get name's index, -1 if not found.
bool containsName(const QString &name) const
Contains name.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
Models to be used with views, mainly QTableView.
Free functions in swift::misc.