dragabletable.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "dragabletable.h"
  2. #include <QDrag>
  3. #include <QMimeData>
  4. #include <QUuid>
  5. #include <QDebug>
  6. #include <QtEvents>
  7. #include <QPainter>
  8. #include <QHeaderView>
  9. #include "../PaintHelper/painthelper.h"
  10. DragableTable::DragableTable(QWidget *parent)
  11. : QTableView(parent)
  12. , m_UUID(QUuid::createUuid())
  13. , m_bSignalOnly(false)
  14. , m_bDragEnable(true)
  15. , m_nTargetRow(-1)
  16. , m_bIsUpon(false)
  17. {
  18. SetDragTargetRow(-1);
  19. setAcceptDrops(true);
  20. }
  21. void DragableTable::SetDragEnable(bool value)
  22. {
  23. setAcceptDrops(value);
  24. m_bDragEnable = value;
  25. }
  26. //某行之下或之上
  27. void DragableTable::SetDragTargetRow(int row, bool isUpon)
  28. {
  29. if(!m_bDragEnable) return;
  30. DragableTableDelegate *pDelegate = dynamic_cast<DragableTableDelegate*>(itemDelegate());
  31. if(pDelegate == nullptr) return;
  32. pDelegate->SetDragTarget(row, isUpon);
  33. // setProperty("dragTargetRow", row);
  34. // setProperty("isUpon", isUpon);
  35. m_nTargetRow = row;
  36. m_bIsUpon = isUpon;
  37. update();
  38. }
  39. void DragableTable::mouseMoveEvent(QMouseEvent *event)
  40. {
  41. QTableView::mouseMoveEvent(event);
  42. if(!m_bDragEnable) return;
  43. if(!m_indexPressed.isValid()) return;
  44. //启动Drag事件
  45. QDrag *drag = new QDrag(this);
  46. QMimeData *mimeData = new QMimeData();
  47. mimeData->setText(m_UUID.toString());
  48. drag->setMimeData(mimeData);
  49. drag->exec(Qt::MoveAction, Qt::CopyAction);
  50. m_indexPressed = QModelIndex();
  51. SetDragTargetRow(-1);
  52. }
  53. void DragableTable::mousePressEvent(QMouseEvent *event)
  54. {
  55. QTableView::mousePressEvent(event);
  56. if(!m_bDragEnable) return;
  57. //只允许左键按住
  58. if(event->button() != Qt::LeftButton) return;
  59. //根据触发鼠标位置获得QModelIndex
  60. m_indexPressed = indexAt(event->pos());
  61. }
  62. void DragableTable::mouseReleaseEvent(QMouseEvent *event)
  63. {
  64. QTableView::mouseReleaseEvent(event);
  65. if(!m_bDragEnable) return;
  66. m_indexPressed = QModelIndex();
  67. SetDragTargetRow(-1);
  68. }
  69. void DragableTable::dragEnterEvent(QDragEnterEvent *event)
  70. {
  71. if(!m_bDragEnable) return;
  72. const QMimeData *mimeData = event->mimeData();
  73. if(mimeData == nullptr || !mimeData->hasText()) return;
  74. if(event->mimeData()->text() != m_UUID.toString()) return;
  75. {//下面内容和dragMoveEvent里的一样, 避免dragMoveEvent未被调用而直接进入dropEvent
  76. QModelIndex index = indexAt(event->pos());
  77. int row = -1;
  78. //如拖拽到表格的空白位置, 视为目标是最后一行之下
  79. if(!index.isValid())
  80. {
  81. row = model()->rowCount() - 1;
  82. SetDragTargetRow(row, false);
  83. }
  84. else
  85. {
  86. QRect indexRect = visualRect(index);
  87. int rowHeight = verticalHeader()->sectionSize(index.row());
  88. int center = indexRect.top() + rowHeight/2;
  89. SetDragTargetRow(index.row(), event->pos().y()<=center);
  90. }
  91. }
  92. event->accept();
  93. }
  94. void DragableTable::dragMoveEvent(QDragMoveEvent *event)
  95. {
  96. if(!m_bDragEnable) return;
  97. QModelIndex index = indexAt(event->pos());
  98. int row = -1;
  99. //如拖拽到表格的空白位置, 视为目标是最后一行之下
  100. if(!index.isValid())
  101. {
  102. row = model()->rowCount() - 1;
  103. SetDragTargetRow(row, false);
  104. }
  105. else
  106. {
  107. QRect indexRect = visualRect(index);
  108. int rowHeight = verticalHeader()->sectionSize(index.row());
  109. int center = indexRect.top() + rowHeight/2;
  110. SetDragTargetRow(index.row(), event->pos().y()<=center);
  111. }
  112. }
  113. void DragableTable::dropEvent(QDropEvent *event)
  114. {
  115. if(!m_bDragEnable) return;
  116. /*----------------------------------------------------------------
  117. * 设置QDrag::exec()的返回值, 必须配合accept使用
  118. * 也可以直接调用acceptProposedAction方法来将返回值设为调用QDrag::exec时传入的defaultDropAction
  119. * event->setDropAction(Qt::MoveAction);
  120. * event->accept();
  121. * 不管之前执行了accept还是acceptProposedAction, 只要执行了event->ignore, QDrag::exec()的返回值都会是Qt::IgnoreAction
  122. * event->ignore();
  123. ----------------------------------------------------------------*/
  124. do
  125. {
  126. int sourceRow = m_indexPressed.row();
  127. if(m_nTargetRow == -1) break;
  128. if(sourceRow == m_nTargetRow) break;
  129. //计算实际要插入到的行: 如果是插入到"之下", 则要加1; 如果把上方的行往下移, 则要减1
  130. int insertedRow = m_nTargetRow + (m_bIsUpon?0:1) - ((sourceRow>m_nTargetRow)?0:1);
  131. if(sourceRow == insertedRow) break;
  132. //发送信号
  133. emit sig_Drop(sourceRow, insertedRow);
  134. if(m_bSignalOnly) break;
  135. QStandardItemModel *pModel = dynamic_cast<QStandardItemModel*>(model());
  136. if(pModel == nullptr) break;
  137. QList<QStandardItem*> targetRowItems = pModel->takeRow(sourceRow);
  138. if(!targetRowItems.isEmpty()) pModel->insertRow(insertedRow, targetRowItems);
  139. /*----------------------------------------------------------------
  140. * 另一种思路:先交换源行和目标行的数据, 交换后目标行去到了源行, 其他行不受影响; 再将目标行移到当前源行位置之后
  141. ----------------------------------------------------------------*/
  142. }while (false);
  143. m_indexPressed = QModelIndex();
  144. SetDragTargetRow(-1);
  145. event->acceptProposedAction();
  146. }
  147. DragableTableDelegate::DragableTableDelegate(QObject *parent)
  148. : BaseItemDelegate(parent)
  149. // , m_pTableView(parentTable)
  150. , m_nTargetRow(-1)
  151. , m_bIsUpon(false)
  152. {
  153. }
  154. void DragableTableDelegate::SetDragTarget(int targetRow, bool isUpon)
  155. {
  156. m_nTargetRow = targetRow;
  157. m_bIsUpon = isUpon;
  158. }
  159. void DragableTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  160. {
  161. BaseItemDelegate::paint(painter,option,index);
  162. if(m_nTargetRow == -1) return;
  163. PainterEx *painterEx = static_cast<PainterEx*>(painter);
  164. //绘制第一列单元格圆圈和线
  165. if(index.row() == m_nTargetRow && index.column() == 0)
  166. {
  167. int y = m_bIsUpon?option.rect.top():option.rect.bottom();
  168. painterEx->DrawCircle(QPoint(km_nRadius, y), km_nRadius, Qt::transparent, QPen(QColor(86,135,228), 2));
  169. painterEx->setPen(QPen(QColor(86,135,228), 2));
  170. painterEx->drawLine(option.rect.left()+km_nRadius*2, y, option.rect.right(), y);
  171. }
  172. //如果指示器绘制在当前行的下方, 因为头部圆圈会跨到下一行, 所以在绘制下一行的时候, 又绘制一次
  173. if(!m_bIsUpon && (index.row() == m_nTargetRow+1) && index.column() == 0)
  174. {
  175. int y = option.rect.top() - 1;
  176. painterEx->DrawCircle(QPoint(km_nRadius, y), km_nRadius, Qt::transparent, QPen(QColor(86,135,228), 2));
  177. painterEx->setPen(QPen(QColor(86,135,228), 2));
  178. painterEx->drawLine(option.rect.left()+km_nRadius*2, y, option.rect.right(), y);
  179. }
  180. //绘制后面单元格的线
  181. if(index.row() == m_nTargetRow && index.column() != 0)
  182. {
  183. painterEx->DrawBorder(option.rect, QPen(QColor(86,135,228), 2), m_bIsUpon?PainterEx::RectBorderTop:PainterEx::RectBorderBottom);
  184. }
  185. // //绘制拖拽指示器(绘制2*列数次, 防止被覆盖)
  186. // if(index.row() == m_nTargetRow||index.row() == m_nTargetRow+1)
  187. // {
  188. // painter->setRenderHint(QPainter::Antialiasing);
  189. // painter->setPen(QPen(QColor(86,135,228), 2));
  190. // painter->setBrush(Qt::transparent);
  191. // //int rowCount = m_pTableView->model()->rowCount();
  192. // int colCount = m_pTableView->model()->columnCount();
  193. // QPoint targetRowRect_topLeft = m_pTableView->visualRect(m_pTableView->model()->index(row,0)).topLeft();
  194. // QPoint targetRowRect_bottomRight = m_pTableView->visualRect(m_pTableView->model()->index(row,colCount-1)).bottomRight();
  195. // QRect targetRowRect(targetRowRect_topLeft, targetRowRect_bottomRight);
  196. // //头部圆圈
  197. // int radius = 8;
  198. // int y = m_bIsUpon?targetRowRect.top():targetRowRect.bottom();
  199. // QPoint topleft = QPoint(targetRowRect.left(), y - radius);
  200. // painter->drawEllipse(QRect(topleft, QSize(radius*2, radius*2)));
  201. // painter->drawLine(targetRowRect.left()+radius*2, y, targetRowRect.right(), y);
  202. // }
  203. }