File indexing completed on 2024-05-19 05:41:20

0001 #include "unitmodel.h"
0002 #include <QDebug>
0003 #include <QSettings>
0004 
0005 namespace GMTOOL
0006 {
0007 QHash<Unit::Category, QString> UnitModel::m_cat2Text({{Unit::CURRENCY, QObject::tr("Currency")},
0008                                                       {Unit::DISTANCE, QObject::tr("Distance")},
0009                                                       {Unit::TEMPERATURE, QObject::tr("Temperature")},
0010                                                       {Unit::MASS, QObject::tr("MASS")},
0011                                                       {Unit::MASS, QObject::tr("OTHER")}});
0012 
0013 //{Unit::VOLUME,tr("Volume")},
0014 
0015 CategoryModel::CategoryModel(QObject* parent) : QSortFilterProxyModel(parent)
0016 {
0017     setFilterRole(Qt::UserRole);
0018     setFilterKeyColumn(0);
0019     setDynamicSortFilter(true);
0020 }
0021 
0022 void CategoryModel::addUnit(Unit* unit)
0023 {
0024     auto unitModel= dynamic_cast<UnitModel*>(sourceModel());
0025     if(nullptr == unitModel)
0026         return;
0027     beginInsertRows(QModelIndex(), rowCount(), rowCount());
0028     unitModel->insertData(unit);
0029     endInsertRows();
0030 }
0031 
0032 QString CategoryModel::currentCategory() const
0033 {
0034     return m_currentCategory;
0035 }
0036 
0037 void CategoryModel::setCurrentCategory(const QString& currentCategory)
0038 {
0039     m_currentCategory= currentCategory;
0040     invalidateFilter();
0041     setFilterFixedString(m_currentCategory);
0042 }
0043 bool CategoryModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
0044 {
0045     QModelIndex index0= sourceModel()->index(sourceRow, 0, sourceParent);
0046 
0047     return (index0.data(Qt::UserRole).toString() == m_currentCategory);
0048 }
0049 
0050 //////////////////////
0051 /// \brief UnitModel::UnitModel
0052 /// \param parent
0053 //////////////////////
0054 UnitModel::UnitModel(QObject* parent) : QAbstractListModel(parent) {}
0055 
0056 Unit* UnitModel::indexToUnit(const QModelIndex& index) const
0057 {
0058     return getUnitByIndex(index.row());
0059 }
0060 
0061 QVariant UnitModel::data(const QModelIndex& index, int role) const
0062 {
0063     if(!index.isValid())
0064         return QVariant();
0065     auto unit= indexToUnit(index);
0066     if(nullptr == unit)
0067         return {};
0068     if(role == Qt::DisplayRole)
0069     {
0070         return unit->name();
0071     }
0072     else if(role == Category)
0073     {
0074         return getCatNameFromId(unit->currentCat());
0075     }
0076     else if(role == UnitRole)
0077     {
0078         return QVariant::fromValue(unit);
0079     }
0080 
0081     return QVariant();
0082 }
0083 
0084 int UnitModel::rowCount(const QModelIndex& parent) const
0085 {
0086     if(parent.isValid())
0087         return 0;
0088 
0089     int sum= 0;
0090     for(auto& list : m_data)
0091     {
0092         sum+= list.size();
0093     }
0094     return sum;
0095 }
0096 
0097 QVariant UnitModel::headerData(int section, Qt::Orientation orientation, int role) const
0098 {
0099     Q_UNUSED(section)
0100     Q_UNUSED(orientation)
0101     Q_UNUSED(role)
0102     return QVariant();
0103 }
0104 
0105 Unit* UnitModel::insertData(Unit* unit)
0106 {
0107     int position= 0;
0108     std::vector<Unit::Category> cats(
0109         {Unit::TEMPERATURE, Unit::DISTANCE, Unit::CURRENCY, Unit::VOLUME, Unit::MASS, Unit::CUSTOM});
0110 
0111     for(auto cat : cats)
0112     {
0113         auto tmplist= m_data[cat];
0114         if(cat <= unit->currentCat())
0115             position+= tmplist.size();
0116     }
0117     auto& list= m_data[unit->currentCat()];
0118     beginInsertRows(QModelIndex(), position, position);
0119     list.append(unit);
0120     endInsertRows();
0121     return unit;
0122 }
0123 
0124 Unit* UnitModel::getUnitFromIndex(const QModelIndex& i, int currentCat)
0125 {
0126     if(!i.isValid())
0127         return nullptr;
0128     auto list= m_data.value(static_cast<Unit::Category>(currentCat));
0129     return list.at(i.row());
0130 }
0131 
0132 QHash<Unit::Category, QString> UnitModel::cat2Text() const
0133 {
0134     return m_cat2Text;
0135 }
0136 
0137 void UnitModel::setCat2Text(const QHash<Unit::Category, QString>& cat2Text)
0138 {
0139     m_cat2Text= cat2Text;
0140 }
0141 
0142 QString UnitModel::getCatNameFromId(Unit::Category id) const
0143 {
0144     return m_cat2Text[id];
0145 }
0146 
0147 bool UnitModel::insertUnit(Unit::Category cat)
0148 {
0149     int sum= 0;
0150     auto const& keys= m_data.keys();
0151     for(auto& key : keys)
0152     {
0153         const auto& list= m_data[key];
0154         if(key <= cat)
0155         {
0156             sum+= list.size();
0157         }
0158     }
0159     auto& list= m_data[cat];
0160 
0161     beginInsertRows(QModelIndex(), sum, sum);
0162     Unit* unit= new Unit(tr("New Unit"), "", cat);
0163     list.append(unit);
0164     endInsertRows();
0165     emit modelChanged();
0166 
0167     return true;
0168 }
0169 
0170 int UnitModel::getIndex(Unit* unit)
0171 {
0172     int i= 0;
0173     for(auto& list : m_data)
0174     {
0175         auto pos= list.indexOf(unit);
0176         if(pos < 0)
0177         {
0178             i+= list.size();
0179         }
0180         else
0181         {
0182             i+= pos;
0183             return i;
0184         }
0185     }
0186     return -1;
0187 }
0188 Unit* UnitModel::getUnitByIndex(int r) const
0189 {
0190     if(r < 0)
0191         return nullptr;
0192 
0193     for(auto& list : m_data)
0194     {
0195         if(r >= list.size())
0196         {
0197             r-= list.size();
0198             continue;
0199         }
0200         else
0201         {
0202             return list.at(r);
0203         }
0204     }
0205     return nullptr;
0206 }
0207 } // namespace GMTOOL