1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(chatlog): use enum class instead of enum to avoid implicit casts

This commit is contained in:
Anthony Bilinski 2019-09-28 17:07:06 -07:00
parent e4f73011f9
commit 08b2c848e2
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
2 changed files with 32 additions and 32 deletions

View File

@ -142,7 +142,7 @@ ChatLog::~ChatLog()
void ChatLog::clearSelection() void ChatLog::clearSelection()
{ {
if (selectionMode == None) if (selectionMode == SelectionMode::None)
return; return;
for (int i = selFirstRow; i <= selLastRow; ++i) for (int i = selFirstRow; i <= selLastRow; ++i)
@ -153,7 +153,7 @@ void ChatLog::clearSelection()
selClickedCol = -1; selClickedCol = -1;
selClickedRow = -1; selClickedRow = -1;
selectionMode = None; selectionMode = SelectionMode::None;
emit selectionChanged(); emit selectionChanged();
updateMultiSelectionRect(); updateMultiSelectionRect();
@ -220,7 +220,7 @@ void ChatLog::mouseReleaseEvent(QMouseEvent* ev)
{ {
QGraphicsView::mouseReleaseEvent(ev); QGraphicsView::mouseReleaseEvent(ev);
selectionScrollDir = NoDirection; selectionScrollDir = AutoScrollDirection::NoDirection;
multiClickTimer->start(); multiClickTimer->start();
} }
@ -234,14 +234,14 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev)
if (ev->buttons() & Qt::LeftButton) { if (ev->buttons() & Qt::LeftButton) {
// autoscroll // autoscroll
if (ev->pos().y() < 0) if (ev->pos().y() < 0)
selectionScrollDir = Up; selectionScrollDir = AutoScrollDirection::Up;
else if (ev->pos().y() > height()) else if (ev->pos().y() > height())
selectionScrollDir = Down; selectionScrollDir = AutoScrollDirection::Down;
else else
selectionScrollDir = NoDirection; selectionScrollDir = AutoScrollDirection::NoDirection;
// select // select
if (selectionMode == None if (selectionMode == SelectionMode::None
&& (clickPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) { && (clickPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) {
QPointF sceneClickPos = mapToScene(clickPos.toPoint()); QPointF sceneClickPos = mapToScene(clickPos.toPoint());
ChatLine::Ptr line = findLineByPosY(scenePos.y()); ChatLine::Ptr line = findLineByPosY(scenePos.y());
@ -255,7 +255,7 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev)
content->selectionStarted(sceneClickPos); content->selectionStarted(sceneClickPos);
selectionMode = Precise; selectionMode = SelectionMode::Precise;
// ungrab mouse grabber // ungrab mouse grabber
if (scene->mouseGrabberItem()) if (scene->mouseGrabberItem())
@ -265,11 +265,11 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev)
selFirstRow = selClickedRow; selFirstRow = selClickedRow;
selLastRow = selClickedRow; selLastRow = selClickedRow;
selectionMode = Multi; selectionMode = SelectionMode::Multi;
} }
} }
if (selectionMode != None) { if (selectionMode != SelectionMode::None) {
ChatLineContent* content = getContentFromPos(scenePos); ChatLineContent* content = getContentFromPos(scenePos);
ChatLine::Ptr line = findLineByPosY(scenePos.y()); ChatLine::Ptr line = findLineByPosY(scenePos.y());
@ -280,12 +280,12 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev)
int col = content->getColumn(); int col = content->getColumn();
if (row == selClickedRow && col == selClickedCol) { if (row == selClickedRow && col == selClickedCol) {
selectionMode = Precise; selectionMode = SelectionMode::Precise;
content->selectionMouseMove(scenePos); content->selectionMouseMove(scenePos);
selGraphItem->hide(); selGraphItem->hide();
} else if (col != selClickedCol) { } else if (col != selClickedCol) {
selectionMode = Multi; selectionMode = SelectionMode::Multi;
lines[selClickedRow]->selectionCleared(); lines[selClickedRow]->selectionCleared();
} }
@ -293,7 +293,7 @@ void ChatLog::mouseMoveEvent(QMouseEvent* ev)
row = line->getRow(); row = line->getRow();
if (row != selClickedRow) { if (row != selClickedRow) {
selectionMode = Multi; selectionMode = SelectionMode::Multi;
lines[selClickedRow]->selectionCleared(); lines[selClickedRow]->selectionCleared();
} }
} else { } else {
@ -332,12 +332,12 @@ ChatLineContent* ChatLog::getContentFromPos(QPointF scenePos) const
bool ChatLog::isOverSelection(QPointF scenePos) const bool ChatLog::isOverSelection(QPointF scenePos) const
{ {
if (selectionMode == Precise) { if (selectionMode == SelectionMode::Precise) {
ChatLineContent* content = getContentFromPos(scenePos); ChatLineContent* content = getContentFromPos(scenePos);
if (content) if (content)
return content->isOverSelection(scenePos); return content->isOverSelection(scenePos);
} else if (selectionMode == Multi) { } else if (selectionMode == SelectionMode::Multi) {
if (selGraphItem->rect().contains(scenePos)) if (selGraphItem->rect().contains(scenePos))
return true; return true;
} }
@ -528,7 +528,7 @@ void ChatLog::mouseDoubleClickEvent(QMouseEvent* ev)
selClickedRow = content->getRow(); selClickedRow = content->getRow();
selFirstRow = content->getRow(); selFirstRow = content->getRow();
selLastRow = content->getRow(); selLastRow = content->getRow();
selectionMode = Precise; selectionMode = SelectionMode::Precise;
emit selectionChanged(); emit selectionChanged();
} }
@ -549,9 +549,9 @@ void ChatLog::mouseDoubleClickEvent(QMouseEvent* ev)
QString ChatLog::getSelectedText() const QString ChatLog::getSelectedText() const
{ {
if (selectionMode == Precise) { if (selectionMode == SelectionMode::Precise) {
return lines[selClickedRow]->content[selClickedCol]->getSelectedText(); return lines[selClickedRow]->content[selClickedCol]->getSelectedText();
} else if (selectionMode == Multi) { } else if (selectionMode == SelectionMode::Multi) {
// build a nicely formatted message // build a nicely formatted message
QString out; QString out;
@ -582,7 +582,7 @@ bool ChatLog::isEmpty() const
bool ChatLog::hasTextToBeCopied() const bool ChatLog::hasTextToBeCopied() const
{ {
return selectionMode != None; return selectionMode != SelectionMode::None;
} }
ChatLine::Ptr ChatLog::getTypingNotification() const ChatLine::Ptr ChatLog::getTypingNotification() const
@ -700,7 +700,7 @@ void ChatLog::selectAll()
clearSelection(); clearSelection();
selectionMode = Multi; selectionMode = SelectionMode::Multi;
selFirstRow = 0; selFirstRow = 0;
selLastRow = lines.size() - 1; selLastRow = lines.size() - 1;
@ -734,7 +734,7 @@ void ChatLog::reloadTheme()
void ChatLog::moveSelectionRectUpIfMulti(int offset) void ChatLog::moveSelectionRectUpIfMulti(int offset)
{ {
assert(offset >= 0); assert(offset >= 0);
if (selectionMode != Multi) { if (selectionMode != SelectionMode::Multi) {
return; return;
} }
if (selLastRow < offset) { // entire selection now out of bounds if (selLastRow < offset) { // entire selection now out of bounds
@ -756,7 +756,7 @@ void ChatLog::moveSelectionRectUpIfMulti(int offset)
void ChatLog::moveSelectionRectDownIfMulti(int offset) void ChatLog::moveSelectionRectDownIfMulti(int offset)
{ {
assert(offset >= 0); assert(offset >= 0);
if (selectionMode != Multi) { if (selectionMode != SelectionMode::Multi) {
return; return;
} }
const int lastLine = lines.size() - 1; const int lastLine = lines.size() - 1;
@ -889,7 +889,7 @@ void ChatLog::resizeEvent(QResizeEvent* ev)
void ChatLog::updateMultiSelectionRect() void ChatLog::updateMultiSelectionRect()
{ {
if (selectionMode == Multi && selFirstRow >= 0 && selLastRow >= 0) { if (selectionMode == SelectionMode::Multi && selFirstRow >= 0 && selLastRow >= 0) {
QRectF selBBox; QRectF selBBox;
selBBox = selBBox.united(lines[selFirstRow]->sceneBoundingRect()); selBBox = selBBox.united(lines[selFirstRow]->sceneBoundingRect());
selBBox = selBBox.united(lines[selLastRow]->sceneBoundingRect()); selBBox = selBBox.united(lines[selLastRow]->sceneBoundingRect());
@ -954,10 +954,10 @@ void ChatLog::onSelectionTimerTimeout()
const int scrollSpeed = 10; const int scrollSpeed = 10;
switch (selectionScrollDir) { switch (selectionScrollDir) {
case Up: case AutoScrollDirection::Up:
verticalScrollBar()->setValue(verticalScrollBar()->value() - scrollSpeed); verticalScrollBar()->setValue(verticalScrollBar()->value() - scrollSpeed);
break; break;
case Down: case AutoScrollDirection::Down:
verticalScrollBar()->setValue(verticalScrollBar()->value() + scrollSpeed); verticalScrollBar()->setValue(verticalScrollBar()->value() + scrollSpeed);
break; break;
default: default:
@ -1028,7 +1028,7 @@ void ChatLog::handleMultiClickEvent()
selClickedRow = content->getRow(); selClickedRow = content->getRow();
selFirstRow = content->getRow(); selFirstRow = content->getRow();
selLastRow = content->getRow(); selLastRow = content->getRow();
selectionMode = Precise; selectionMode = SelectionMode::Precise;
emit selectionChanged(); emit selectionChanged();
} }
@ -1047,7 +1047,7 @@ void ChatLog::focusInEvent(QFocusEvent* ev)
{ {
QGraphicsView::focusInEvent(ev); QGraphicsView::focusInEvent(ev);
if (selectionMode != None) { if (selectionMode != SelectionMode::None) {
selGraphItem->setBrush(QBrush(selectionRectColor)); selGraphItem->setBrush(QBrush(selectionRectColor));
for (int i = selFirstRow; i <= selLastRow; ++i) for (int i = selFirstRow; i <= selLastRow; ++i)
@ -1059,7 +1059,7 @@ void ChatLog::focusOutEvent(QFocusEvent* ev)
{ {
QGraphicsView::focusOutEvent(ev); QGraphicsView::focusOutEvent(ev);
if (selectionMode != None) { if (selectionMode != SelectionMode::None) {
selGraphItem->setBrush(QBrush(selectionRectColor.lighter(120))); selGraphItem->setBrush(QBrush(selectionRectColor.lighter(120)));
for (int i = selFirstRow; i <= selLastRow; ++i) for (int i = selFirstRow; i <= selLastRow; ++i)

View File

@ -132,14 +132,14 @@ private:
void handleMultiClickEvent(); void handleMultiClickEvent();
private: private:
enum SelectionMode enum class SelectionMode
{ {
None, None,
Precise, Precise,
Multi, Multi,
}; };
enum AutoScrollDirection enum class AutoScrollDirection
{ {
NoDirection, NoDirection,
Up, Up,
@ -161,13 +161,13 @@ private:
int selFirstRow = -1; int selFirstRow = -1;
int selLastRow = -1; int selLastRow = -1;
QColor selectionRectColor = Style::getColor(Style::SelectText); QColor selectionRectColor = Style::getColor(Style::SelectText);
SelectionMode selectionMode = None; SelectionMode selectionMode = SelectionMode::None;
QPointF clickPos; QPointF clickPos;
QGraphicsRectItem* selGraphItem = nullptr; QGraphicsRectItem* selGraphItem = nullptr;
QTimer* selectionTimer = nullptr; QTimer* selectionTimer = nullptr;
QTimer* workerTimer = nullptr; QTimer* workerTimer = nullptr;
QTimer* multiClickTimer = nullptr; QTimer* multiClickTimer = nullptr;
AutoScrollDirection selectionScrollDir = NoDirection; AutoScrollDirection selectionScrollDir = AutoScrollDirection::NoDirection;
int clickCount = 0; int clickCount = 0;
QPoint lastClickPos; QPoint lastClickPos;
Qt::MouseButton lastClickButton; Qt::MouseButton lastClickButton;