Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //productsmodel.cpp
- #include "productsmodel.h"
- #include "products.h"
- #include "product.h"
- #include <QDebug>
- #include <cstdlib>
- #include <QDateTime>
- ProductsModel::ProductsModel(Products *products, QObject *parent) : QAbstractTableModel(parent), m_products(products)
- {
- srand(QDateTime::currentMSecsSinceEpoch());
- connect(products, SIGNAL(updated()), this, SIGNAL(layoutChanged()));
- }
- int ProductsModel::rowCount(const QModelIndex&) const
- {
- return m_products->count();
- }
- int ProductsModel::columnCount(const QModelIndex&) const
- {
- return 5;
- }
- QVariant ProductsModel::data(const QModelIndex &index, int role) const
- {
- if (!index.isValid())
- return QVariant();
- if (role == Qt::DisplayRole)
- {
- Product *prod = m_products->at(index.row());
- switch (index.column())
- {
- case 0:
- return m_products->at(index.row())->name();
- case 1:
- return QString::fromLocal8Bit("%1 €").arg(prod->price());
- case 2:
- return rand()% 1000 + 1;
- case 3:
- {
- double diff = prod->price()-prod->oldPrice();
- QString plop = QString("%1").arg(diff, 0 ,'f', 2);
- if (diff >= 0)
- plop = "+"+plop;
- return plop;
- }
- case 4:
- {
- double ratio = (prod->price()-prod->oldPrice())/prod->oldPrice()*100;
- QChar c(0x2666);
- if (ratio > 0)
- c = QChar(0x25b2);
- if (ratio < 0)
- c = QChar(0x25bc);
- return QString("%1 %2%").arg(c).arg(ratio, 0 ,'f', 1);
- }
- default:
- return QVariant();
- }
- }
- if (role == Qt::TextAlignmentRole)
- {
- switch (index.column())
- {
- case 0:
- return static_cast<int>(Qt::AlignLeft|Qt::AlignVCenter);
- default:
- return static_cast<int>(Qt::AlignRight|Qt::AlignVCenter);
- }
- }
- return QVariant();
- }
- Qt::ItemFlags ProductsModel::flags(const QModelIndex &index) const
- {
- if (!index.isValid())
- return Qt::NoItemFlags;
- else
- return Qt::ItemIsEnabled;
- }
- QVariant ProductsModel::headerData(int section, Qt::Orientation orientation, int role) const
- {
- if (orientation == Qt::Horizontal)
- {
- switch (section)
- {
- case 1:
- return "Prix";
- case 2:
- return "Cote";
- case 3:
- return "Différence";
- case 4:
- return "Variation";
- }
- }
- return QVariant();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement