listviewdelegate.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "listviewdelegate.h"
  2. #include <QFileSystemModel>
  3. ListViewDelegate::ListViewDelegate()
  4. {
  5. // TODO Auto-generated constructor stub
  6. map=new QMap<qint64, int>;
  7. }
  8. ListViewDelegate::~ListViewDelegate()
  9. {
  10. // TODO Auto-generated destructor stub
  11. }
  12. //alocate each item size in listview.
  13. QSize ListViewDelegate::sizeHint(const QStyleOptionViewItem & option ,
  14. const QModelIndex & index) const
  15. {
  16. return QStyledItemDelegate::sizeHint(option,index);
  17. /*
  18. QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
  19. QSize iconsize = icon.actualSize(option.decorationSize);
  20. QFont font = QApplication::font();
  21. QFontMetrics fm(font);
  22. return(QSize(iconsize.width(),iconsize.height()+fm.height() +8 ));*/
  23. }
  24. void ListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  25. const QModelIndex &index) const
  26. {
  27. QStyledItemDelegate::paint(painter,option,index);
  28. painter->save();
  29. //painter->drawRect(option.rect);
  30. QIcon icon;
  31. //if (QFileSystemModel::fileInfo(index).absolutePath().contains(basePath)){
  32. //icon=QIcon(":/icons16/clear");
  33. if (map->contains(index.internalId())){
  34. switch (map->value(index.internalId())){
  35. case FileModified: icon=QIcon(":/icons16/important");break;
  36. case FileUnknown: icon=QIcon(":/icons16/unknown");break;
  37. case FileAdded: icon=QIcon(":/icons16/add");break;
  38. case FileClear: icon=QIcon(":/icons16/clear");break;
  39. default: break;
  40. }
  41. }
  42. QSize iconsize = icon.actualSize(option.decorationSize);
  43. if (option.rect.width()<200 && option.rect.height()>=48 && index.column()==0)
  44. painter->drawPixmap(QPoint((option.rect.left()+option.rect.width()/2)-option.decorationSize.width()/2,option.rect.top()+option.decorationSize.height()-16),icon.pixmap(iconsize.width(),iconsize.height()));
  45. else if (index.column()==0)
  46. painter->drawPixmap(QPoint(option.rect.left()+5,option.rect.top()+option.decorationSize.height()-16),icon.pixmap(iconsize.width(),iconsize.height()));
  47. painter->restore();
  48. }
  49. void ListViewDelegate::setOverlayStatus(qint64 index,int status){
  50. /*if (status== FileClear)
  51. map->remove(index);
  52. else*/
  53. if(status == FileClear || FileModified || status == FileAdded || status == FileUnknown ){
  54. map->insert(index,status);
  55. }
  56. }
  57. void ListViewDelegate::removeOverlayStatus(qint64 index){
  58. map->remove(index);
  59. }
  60. int ListViewDelegate::testOverlayStatus(qint64 index){
  61. if (map->contains(index))
  62. return map->value(index);
  63. else
  64. return -1;
  65. }
  66. void ListViewDelegate::setBasePath(QString path){
  67. basePath=path;
  68. }
  69. void ListViewDelegate::setMap(QMap<qint64, int> *map){
  70. this->map=map;
  71. }