overlayer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "overlayer.h"
  2. Overlayer::Overlayer(QObject *parent) :
  3. QThread(parent)
  4. {
  5. map=new QMap<qint64, int>;
  6. model=new QFileSystemModel();
  7. count=0;
  8. processed=false;
  9. timer = new QTimer(this);
  10. connect(timer, SIGNAL(timeout()), this, SLOT(onTimerTimeout()));
  11. }
  12. void Overlayer::run(){
  13. if (!timer->isActive()){
  14. processChanges("");
  15. timer->start(5000);
  16. }
  17. }
  18. void Overlayer::processChanges(QString txt){
  19. QString aux,str_value;
  20. QStringList list;
  21. QStringList list2;
  22. QString dir;
  23. //QRegExp rx("\\? .*");
  24. QRegExp rx_modfied("M .*");
  25. QRegExp rx_unknown("\\? .*");
  26. QRegExp rx_added("A .*");
  27. QRegExp rx_clear("C .*");
  28. qint64 id;
  29. list=txt.split("\n");
  30. changedFiles=0;
  31. for (int i=0; i< list.size();i++)
  32. {
  33. aux=list.at(i);
  34. if (rx_modfied.exactMatch(aux)){
  35. str_value=aux.replace(0,2,"");
  36. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
  37. map->insert(id,ListViewDelegate::FileModified);
  38. list2=str_value.split(QDir::separator());
  39. for (int j=0; j< list2.size()-1;j++){
  40. if (j==0)
  41. dir=list2.at(j);
  42. else
  43. dir.append(QDir::separator()).append(list2.at(j));
  44. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+dir).internalId();
  45. map->insert(id,ListViewDelegate::FileModified);
  46. }
  47. }else if (rx_unknown.exactMatch(aux)){
  48. str_value=aux.replace(0,2,"");
  49. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
  50. map->insert(id,ListViewDelegate::FileUnknown);
  51. list2=str_value.split(QDir::separator());
  52. for (int j=0; j< list2.size()-1;j++){
  53. if (j==0)
  54. dir=list2.at(j);
  55. else
  56. dir.append(QDir::separator()).append(list2.at(j));
  57. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+dir).internalId();
  58. map->insert(id,ListViewDelegate::FileModified);
  59. }
  60. }else if (rx_added.exactMatch(aux)){
  61. str_value=aux.replace(0,2,"");
  62. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
  63. map->insert(id,ListViewDelegate::FileAdded);
  64. list2=str_value.split(QDir::separator());
  65. for (int j=0; j< list2.size()-1;j++){
  66. if (j==0)
  67. dir=list2.at(j);
  68. else
  69. dir.append(QDir::separator()).append(list2.at(j));
  70. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+dir).internalId();
  71. map->insert(id,ListViewDelegate::FileModified);
  72. }
  73. }else if (rx_clear.exactMatch(aux)){
  74. str_value=aux.replace(0,2,"");
  75. id=model->index(QDir::toNativeSeparators(basePath)+QDir::separator()+str_value).internalId();
  76. //map->remove(id);
  77. map->insert(id,ListViewDelegate::FileClear);
  78. }
  79. changedFiles++;
  80. }
  81. //qDebug() << "changed files" << changedFiles;
  82. processed=true;
  83. emit mapReady(map);
  84. }
  85. void Overlayer::setBasePath(QString path){
  86. basePath=path;
  87. count=0;
  88. emit commandRequired("ar-changes");
  89. }
  90. void Overlayer::setModel(QFileSystemModel *model){
  91. this->model=model;
  92. }
  93. void Overlayer::onTimerTimeout(){
  94. if (processed)
  95. processed=false;
  96. else if (changedFiles>0){
  97. paintClear();
  98. changedFiles=0;
  99. }
  100. //qDebug() << "Timer triggered";
  101. emit commandRequired("ar-changes");
  102. }
  103. void Overlayer::paintClear(){
  104. qint64 id;
  105. QDirIterator it(basePath, QDirIterator::Subdirectories);
  106. //qDebug() << "Painting clear path" << basePath;
  107. map->clear();
  108. while (it.hasNext()) {
  109. //qDebug() << it.filePath();
  110. it.next();
  111. id=model->index( it.filePath()).internalId();
  112. //map->remove(id);
  113. map->insert(id,ListViewDelegate::FileClear);
  114. }
  115. emit mapReady(map);
  116. }