QTreeWidgetSortItem.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "QTreeWidgetSortItem.h"
  2. #include "rcglobal.h"
  3. /* 自己重新实现一个QTreeWidgetSortItem,主要是为了重载函数的排序功能,不适应默认的名字排序,而是使用
  4. * 每个项自带的tip字符串排序。这样做是因为对齐的时候,有些空白对齐项目,并没有名称,就会导致无法排序
  5. */
  6. int QTreeWidgetSortItem::s_sortType = 0;
  7. bool QTreeWidgetSortItem::s_syncOrder = false;
  8. QTreeWidgetSortItem::QTreeWidgetSortItem(int type):QTreeWidgetItem(type)
  9. {
  10. }
  11. QTreeWidgetSortItem::QTreeWidgetSortItem(const QStringList &strings, int type):QTreeWidgetItem(strings, type)
  12. {
  13. }
  14. QTreeWidgetSortItem::~QTreeWidgetSortItem()
  15. {
  16. }
  17. bool QTreeWidgetSortItem::operator<(const QTreeWidgetItem & other) const
  18. {
  19. if (s_sortType == 0)
  20. {
  21. if (!s_syncOrder) //按名称排序
  22. {
  23. //目录最大,放在文件前面。都是目录则按照名称排序
  24. if ((type() == RC_DIR) && (other.type() == RC_DIR))
  25. {
  26. goto cmp_name;
  27. }
  28. else if ((type() == RC_DIR) && (other.type() != RC_DIR))
  29. {
  30. return false;
  31. }
  32. else if ((type() != RC_DIR) && (other.type() == RC_DIR))
  33. {
  34. return true;
  35. }
  36. cmp_name:
  37. QString a = this->data(0, Item_RelativePath).toString();
  38. QString b = other.data(0, Item_RelativePath).toString();
  39. return (a.compare(b, Qt::CaseInsensitive) > 0);
  40. }
  41. else
  42. {
  43. //同步序号,按序号进行排序
  44. #if 0
  45. if ((type() == RC_DIR) && (other.type() == RC_DIR))
  46. {
  47. QString a = this->data(0, Item_RelativePath).toString();
  48. QString b = other.data(0, Item_RelativePath).toString();
  49. return !(a.compare(b, Qt::CaseInsensitive) > 0);
  50. }
  51. else if ((type() == RC_DIR) && (other.type() != RC_DIR))
  52. {
  53. return true;
  54. }
  55. else if ((type() != RC_DIR) && (other.type() == RC_DIR))
  56. {
  57. return false;
  58. }
  59. cmp_index:
  60. #endif
  61. //同步对方的操作。
  62. int a = this->data(0, Item_Index).toInt();
  63. int b = other.data(0, Item_Index).toInt();
  64. return(a > b);
  65. }
  66. }
  67. else if (s_sortType == 1) //按大小排序
  68. {
  69. //目录最大,放在文件前面。都是目录则按照名称排序
  70. if ((type() == RC_DIR) && (other.type() == RC_DIR))
  71. {
  72. qint64 a = this->data(0, DIR_ITEM_MAXSIZE_FILE).toULongLong();
  73. qint64 b = other.data(0, DIR_ITEM_MAXSIZE_FILE).toULongLong();
  74. return(a > b);
  75. #if 0
  76. QString a = this->data(0, Item_RelativePath).toString();
  77. QString b = other.data(0, Item_RelativePath).toString();
  78. return (a.compare(b, Qt::CaseInsensitive) > 0);
  79. #endif
  80. }
  81. else if ((type() == RC_DIR) && (other.type() != RC_DIR))
  82. {
  83. return false;
  84. }
  85. else if ((type() != RC_DIR) && (other.type() == RC_DIR))
  86. {
  87. return true;
  88. }
  89. int leftSize = this->text(1).toInt();
  90. int rightSize = other.text(1).toInt();
  91. return(leftSize > rightSize);
  92. }
  93. else if (s_sortType == 2) //by 修改日期
  94. {
  95. //目录最大,放在文件前面。都是目录则按照名称排序
  96. if ((type() == RC_DIR) && (other.type() == RC_DIR))
  97. {
  98. QString a = this->data(0, Item_RelativePath).toString();
  99. QString b = other.data(0, Item_RelativePath).toString();
  100. return (a.compare(b, Qt::CaseInsensitive) > 0);
  101. }
  102. else if ((type() == RC_DIR) && (other.type() != RC_DIR))
  103. {
  104. return false;
  105. }
  106. else if ((type() != RC_DIR) && (other.type() == RC_DIR))
  107. {
  108. return true;
  109. }
  110. QString a = this->text(2);
  111. QString b = other.text(2);
  112. return(a.compare(b, Qt::CaseInsensitive) > 0);
  113. }
  114. return false;
  115. }