tablewidgetreorderable.cpp
1 //------------------------------------------------------------------------------
2 // tablewidgetreorderable.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2019 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "tablewidgetreorderable.h"
24 
25 #include <QCursor>
26 #include <QDropEvent>
27 #include <QHeaderView>
28 #include <QPainter>
29 #include <QProxyStyle>
30 
31 class TableWidgetReorderableStyle : public QProxyStyle
32 {
33 public:
34  using QProxyStyle::QProxyStyle;
35 
36  void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override;
37 };
38 
39 void TableWidgetReorderableStyle::drawPrimitive(QStyle::PrimitiveElement element,
40  const QStyleOption *option, QPainter *painter, const QWidget *widget) const
41 {
42  if (element == PE_IndicatorItemViewItemDrop && !option->rect.isNull() && widget)
43  {
44  auto table = static_cast<const TableWidgetReorderable*>(widget);
45  QPoint cursorPos = QCursor::pos();
46  QPoint tablePos = table->mapFromGlobal(cursorPos);
47  tablePos.setX(tablePos.x() - table->verticalHeader()->width());
48  tablePos.setY(tablePos.y() - table->horizontalHeader()->height());
49  int targetRow = table->determineDragRow(tablePos.y());
50  QStyleOption newOption = *option;
51  if (targetRow < table->rowCount())
52  {
53  newOption.rect.setTop(table->rowViewportPosition(targetRow));
54  }
55  else
56  {
57  int lastRow = table->rowCount() - 1;
58  if (lastRow < 0)
59  return;
60  int bottom = table->rowViewportPosition(lastRow) + table->rowHeight(lastRow);
61  newOption.rect.setTop(bottom);
62  }
63  newOption.rect.setHeight(0);
64  newOption.rect.setLeft(0);
65  if (widget)
66  newOption.rect.setRight(widget->width());
67  QProxyStyle::drawPrimitive(element, &newOption, painter, widget);
68  }
69  else
70  {
71  QProxyStyle::drawPrimitive(element, option, painter, widget);
72  }
73 }
74 
75 TableWidgetReorderable::TableWidgetReorderable(QWidget *parent)
76  : QTableWidget(parent)
77 {
78  this->setStyle(new TableWidgetReorderableStyle());
79 }
80 
81 void TableWidgetReorderable::dropEvent(QDropEvent *event)
82 {
83  if (event->source() != this)
84  {
85  event->ignore();
86  return;
87  }
88  int newRow = determineDragRow(event->pos().y());
89  QList<QTableWidgetItem*> selectedItems = this->selectedItems();
90  QSet<int> uniqueRows;
91  for (QTableWidgetItem *item : selectedItems)
92  uniqueRows.insert(item->row());
93  for (int n = 0; n < uniqueRows.count(); ++n)
94  this->insertRow(newRow);
95 
96  int currentOldRow = -1;
97  int currentNewRow = newRow - 1;
98  QList<int> deleteRows;
99  for (QTableWidgetItem *selectedItem : selectedItems)
100  {
101  int column = selectedItem->column();
102  if (selectedItem->row() != currentOldRow)
103  {
104  currentOldRow = selectedItem->row();
105  deleteRows.append(currentOldRow);
106  currentNewRow++;
107  }
108  QWidget *cellWidget = this->cellWidget(currentOldRow, column);
109  this->takeItem(currentOldRow, column);
110  this->setItem(currentNewRow, column, selectedItem);
111  this->setCellWidget(currentNewRow, column, cellWidget);
112  }
113 
114  std::sort(deleteRows.begin(), deleteRows.end());
115  for (int i = deleteRows.count() - 1; i >= 0; --i)
116  this->removeRow(deleteRows[i]);
117  event->ignore();
118  emit rowsReordered();
119 }
120 
121 int TableWidgetReorderable::determineDragRow(int y) const
122 {
123  /*
124  Split the row height in half. If 'y' is in the upper half,
125  the row at the 'y' is targeted. If 'y' is in the lower half,
126  the row below is targeted. If we can't find any row, assume
127  we're targeting the end of the table.
128  */
129  int directRow = this->rowAt(y);
130  if (directRow < 0)
131  return this->rowCount();
132  int rowPosition = this->rowViewportPosition(directRow);
133  int rowHeight = this->rowHeight(directRow);
134  int rowLocalY = y - rowPosition;
135  return (rowLocalY < rowHeight / 2) ? directRow : directRow + 1;
136 }