Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. //productsmodel.cpp
  2.  
  3. #include "productsmodel.h"
  4. #include "products.h"
  5. #include "product.h"
  6. #include <QDebug>
  7. #include <cstdlib>
  8. #include <QDateTime>
  9.  
  10. ProductsModel::ProductsModel(Products *products, QObject *parent) : QAbstractTableModel(parent), m_products(products)
  11. {
  12.     srand(QDateTime::currentMSecsSinceEpoch());
  13.     connect(products, SIGNAL(updated()), this, SIGNAL(layoutChanged()));
  14. }
  15.  
  16. int ProductsModel::rowCount(const QModelIndex&) const
  17. {
  18.     return m_products->count();
  19. }
  20.  
  21. int ProductsModel::columnCount(const QModelIndex&) const
  22. {
  23.     return 5;
  24. }
  25.  
  26. QVariant ProductsModel::data(const QModelIndex &index, int role) const
  27. {
  28.     if (!index.isValid())
  29.         return QVariant();
  30.    
  31.     if (role == Qt::DisplayRole)
  32.     {
  33.         Product *prod = m_products->at(index.row());
  34.         switch (index.column())
  35.         {
  36.         case 0:
  37.             return m_products->at(index.row())->name();
  38.         case 1:
  39.             return QString::fromLocal8Bit("%1 €").arg(prod->price());
  40.         case 2:
  41.             return rand()% 1000 + 1;
  42.         case 3:
  43.             {
  44.                 double diff = prod->price()-prod->oldPrice();
  45.                 QString plop = QString("%1").arg(diff, 0 ,'f', 2);
  46.                 if (diff >= 0)
  47.                     plop = "+"+plop;
  48.                 return plop;
  49.             }
  50.         case 4:
  51.             {
  52.                 double ratio = (prod->price()-prod->oldPrice())/prod->oldPrice()*100;
  53.                 QChar c(0x2666);
  54.                 if (ratio > 0)
  55.                     c = QChar(0x25b2);
  56.                 if (ratio < 0)
  57.                     c = QChar(0x25bc);
  58.                 return QString("%1 %2%").arg(c).arg(ratio, 0 ,'f', 1);
  59.             }
  60.         default:
  61.             return QVariant();
  62.         }
  63.     }
  64.     if (role == Qt::TextAlignmentRole)
  65.     {
  66.         switch (index.column())
  67.         {
  68.         case 0:
  69.             return static_cast<int>(Qt::AlignLeft|Qt::AlignVCenter);
  70.         default:
  71.             return static_cast<int>(Qt::AlignRight|Qt::AlignVCenter);
  72.         }
  73.     }
  74.     return QVariant();
  75. }
  76.  
  77. Qt::ItemFlags ProductsModel::flags(const QModelIndex &index) const
  78. {
  79.     if (!index.isValid())
  80.         return Qt::NoItemFlags;
  81.     else
  82.         return Qt::ItemIsEnabled;
  83. }
  84.  
  85. QVariant ProductsModel::headerData(int section, Qt::Orientation orientation, int role) const
  86. {
  87.     if (orientation == Qt::Horizontal)
  88.     {
  89.         switch (section)
  90.         {
  91.         case 1:
  92.             return "Prix";
  93.         case 2:
  94.             return "Cote";
  95.         case 3:
  96.             return "Différence";
  97.         case 4:
  98.             return "Variation";
  99.         }
  100.     }
  101.     return QVariant();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement