-
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show line indicator instead of rectangle while dragging in tables (#5252
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "widgets/helper/TableStyles.hpp" | ||
|
||
#include <QAbstractItemView> | ||
#include <QPainter> | ||
#include <QStyleOption> | ||
#include <QTableView> | ||
#include <QWidget> | ||
|
||
namespace chatterino { | ||
|
||
TableRowDragStyle::TableRowDragStyle(QStyle *target) | ||
: QProxyStyle(target) | ||
{ | ||
} | ||
|
||
void TableRowDragStyle::applyTo(QTableView *view) | ||
{ | ||
auto *proxyStyle = new TableRowDragStyle(view->style()); | ||
proxyStyle->setParent(view); | ||
view->setStyle(proxyStyle); | ||
} | ||
|
||
void TableRowDragStyle::drawPrimitive(QStyle::PrimitiveElement element, | ||
const QStyleOption *option, | ||
QPainter *painter, | ||
const QWidget *widget) const | ||
{ | ||
if (element != QStyle::PE_IndicatorItemViewItemDrop) | ||
{ | ||
QProxyStyle::drawPrimitive(element, option, painter, widget); | ||
return; | ||
} | ||
|
||
const auto *view = dynamic_cast<const QAbstractItemView *>(widget); | ||
if (!view) | ||
{ | ||
assert(false && "TableStyle must be used on a QAbstractItemView"); | ||
return; | ||
} | ||
|
||
if (option->rect.isNull()) | ||
{ | ||
return; | ||
} | ||
|
||
// Get the direction a row is dragged in | ||
auto selected = view->currentIndex(); | ||
auto hovered = view->indexAt(option->rect.center()); | ||
if (!selected.isValid() || !hovered.isValid()) | ||
{ | ||
// This shouldn't happen as we're in a drag operation | ||
assert(false && "Got bad indices"); | ||
return; | ||
} | ||
|
||
int y = option->rect.top(); // move up | ||
if (hovered.row() >= selected.row()) | ||
{ | ||
y = option->rect.bottom(); // move down | ||
} | ||
|
||
painter->setPen({Qt::white, 2}); | ||
painter->drawLine(0, y, widget->width(), y); | ||
} | ||
|
||
} // namespace chatterino |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <QProxyStyle> | ||
|
||
class QTableView; | ||
|
||
namespace chatterino { | ||
|
||
/// @brief A custom style for drag operations of rows on tables | ||
/// | ||
/// This style overwrites how `PE_IndicatorItemViewItemDrop`, the drop | ||
/// indicator of item-views, is drawn. It's intended to be used on QTableViews | ||
/// where entire rows are moved (not individual cells). The indicator is shown | ||
/// as a line at the position where the dragged item should be inserted. If no | ||
/// such position exists, a red border is drawn around the viewport. | ||
class TableRowDragStyle : public QProxyStyle | ||
{ | ||
public: | ||
/// Applies the style to @a view | ||
static void applyTo(QTableView *view); | ||
|
||
void drawPrimitive(QStyle::PrimitiveElement element, | ||
const QStyleOption *option, QPainter *painter, | ||
const QWidget *widget = nullptr) const override; | ||
|
||
private: | ||
/// @param target The style to wrap. This is **not** the parent of this | ||
/// object. This object will become the parent of @a target. | ||
TableRowDragStyle(QStyle *target); | ||
}; | ||
|
||
} // namespace chatterino |