Commit c9997b22 authored by Michael Reeves's avatar Michael Reeves

Refactor class Selection

*All fields but bSelectionContainsData are now private.
*direct use of bSelectionContainsData is deprecated
*simplified logic guarding deleteSelection
--Q_ASSERT lines added to detect any possiable regression.
*m_selection is now always initialized so init doesn't need to do this.
parent 8bad3d58
......@@ -322,18 +322,37 @@ class Selection
{
public:
Selection(){}
private:
int firstLine = -1;
int firstPos = -1;
int lastLine = -1;
int firstPos = -1;
int lastPos = -1;
int oldLastLine = -1;
int oldFirstLine = -1;
int oldLastLine = -1;
public:
//private:
bool bSelectionContainsData = false;
public:
inline int getFirstLine() { return firstLine; };
inline int getLastLine() { return lastLine; };
inline int getFirstPos() { return firstPos; };
inline int getLastPos() { return lastPos; };
inline void invalidate() { firstLine = -1; };
inline bool isValidFirstLine() { return firstLine != -1; }
inline void clearOldSelection() { oldLastLine = -1, oldFirstLine = -1; };
inline int getOldLastLine() { return oldLastLine; };
inline int getOldFirstLine() { return oldFirstLine; };
inline bool selectionContainsData(void) { return bSelectionContainsData; };
bool isEmpty() { return firstLine == -1 || (firstLine == lastLine && firstPos == lastPos) || bSelectionContainsData == false; }
void reset()
{
oldFirstLine = firstLine;
oldLastLine = lastLine;
oldFirstLine = firstLine;
firstLine = -1;
lastLine = -1;
bSelectionContainsData = false;
......@@ -344,6 +363,7 @@ public:
oldLastLine = lastLine;
lastLine = l;
lastPos = p;
//bSelectionContainsData = (firstLine == lastLine && firstPos == lastPos);
}
bool within( int l, int p );
......
......@@ -224,10 +224,6 @@ void DiffTextWindow::init(
d->m_fastSelectorNofLines = 0;
d->m_lineNumberWidth = 0;
d->m_maxTextWidth = -1;
d->m_selection.reset();
d->m_selection.oldFirstLine = -1; // reset is not enough here.
d->m_selection.oldLastLine = -1;
d->m_selection.lastLine = -1;
d->m_pTextCodec = pTextCodec;
d->m_eLineEndStyle = eLineEndStyle;
......@@ -273,7 +269,7 @@ void DiffTextWindow::setFirstLine(int firstLine)
d->m_firstLine = newFirstLine;
if(d->m_bSelectionInProgress && d->m_selection.firstLine != -1)
if(d->m_bSelectionInProgress && d->m_selection.isValidFirstLine())
{
int line, pos;
convertToLinePos(d->m_lastKnownMousePos.x(), d->m_lastKnownMousePos.y(), line, pos);
......@@ -309,7 +305,7 @@ void DiffTextWindow::setHorizScrollOffset(int horizScrollOffset)
r = QRect(width() - xOffset - 2, 0, -(width() - xOffset), height()).normalized();
}
if(d->m_bSelectionInProgress && d->m_selection.firstLine != -1)
if(d->m_bSelectionInProgress && d->m_selection.isValidFirstLine())
{
int line, pos;
convertToLinePos(d->m_lastKnownMousePos.x(), d->m_lastKnownMousePos.y(), line, pos);
......@@ -445,7 +441,7 @@ void DiffTextWindow::mousePressEvent(QMouseEvent* e)
if((!d->m_pOptions->m_bRightToLeftLanguage && e->x() < xOffset) || (d->m_pOptions->m_bRightToLeftLanguage && e->x() > width() - xOffset))
{
emit setFastSelectorLine(convertLineToDiff3LineIdx(line));
d->m_selection.firstLine = -1; // Disable current d->m_selection
d->m_selection.invalidate(); // Disable current d->m_selection
}
else
{ // Selection
......@@ -543,7 +539,7 @@ void DiffTextWindow::mouseReleaseEvent(QMouseEvent* e)
if(d->m_delayedDrawTimer)
killTimer(d->m_delayedDrawTimer);
d->m_delayedDrawTimer = 0;
if(d->m_selection.firstLine != -1)
if(d->m_selection.isValidFirstLine())
{
emit selectionEnd();
}
......@@ -561,7 +557,7 @@ void DiffTextWindow::mouseMoveEvent(QMouseEvent* e)
convertToLinePos(e->x(), e->y(), line, pos);
d->m_lastKnownMousePos = e->pos();
if(d->m_selection.firstLine != -1)
if(d->m_selection.isValidFirstLine())
{
d->m_selection.end(line, pos);
......@@ -619,19 +615,19 @@ void DiffTextWindow::timerEvent(QTimerEvent*)
{
int fontHeight = fontMetrics().lineSpacing();
if(d->m_selection.oldLastLine != -1)
if(d->m_selection.getOldLastLine() != -1)
{
int lastLine;
int firstLine;
if(d->m_selection.oldFirstLine != -1)
if(d->m_selection.getOldFirstLine() != -1)
{
firstLine = min3(d->m_selection.oldFirstLine, d->m_selection.lastLine, d->m_selection.oldLastLine);
lastLine = max3(d->m_selection.oldFirstLine, d->m_selection.lastLine, d->m_selection.oldLastLine);
firstLine = min3(d->m_selection.getOldFirstLine(), d->m_selection.getLastLine(), d->m_selection.getOldLastLine());
lastLine = max3(d->m_selection.getOldFirstLine(), d->m_selection.getLastLine(), d->m_selection.getOldLastLine());
}
else
{
firstLine = min2(d->m_selection.lastLine, d->m_selection.oldLastLine);
lastLine = max2(d->m_selection.lastLine, d->m_selection.oldLastLine);
firstLine = min2(d->m_selection.getLastLine(), d->m_selection.getOldLastLine());
lastLine = max2(d->m_selection.getLastLine(), d->m_selection.getOldLastLine());
}
int y1 = (firstLine - d->m_firstLine) * fontHeight;
int y2 = min2(height(), (lastLine - d->m_firstLine + 1) * fontHeight);
......@@ -648,7 +644,7 @@ void DiffTextWindow::timerEvent(QTimerEvent*)
if(d->m_scrollDeltaX != 0 || d->m_scrollDeltaY != 0)
{
d->m_selection.end(d->m_selection.lastLine + d->m_scrollDeltaY, d->m_selection.lastPos + d->m_scrollDeltaX);
d->m_selection.end(d->m_selection.getLastLine() + d->m_scrollDeltaY, d->m_selection.getLastPos() + d->m_scrollDeltaX);
emit scroll(d->m_scrollDeltaX, d->m_scrollDeltaY);
killTimer(d->m_delayedDrawTimer);
d->m_delayedDrawTimer = startTimer(50);
......@@ -1140,11 +1136,9 @@ void DiffTextWindow::paintEvent(QPaintEvent* e)
p.end();
d->m_oldFirstLine = d->m_firstLine;
d->m_selection.oldLastLine = -1;
if(d->m_selection.oldFirstLine != -1)
d->m_selection.oldFirstLine = -1;
d->m_selection.clearOldSelection();
if(!bOldSelectionContainsData && d->m_selection.bSelectionContainsData)
if(!bOldSelectionContainsData && d->m_selection.selectionContainsData())
emit newSelection();
}
......
......@@ -1616,7 +1616,7 @@ void MergeResultWindow::timerEvent(QTimerEvent*)
if(m_scrollDeltaX != 0 || m_scrollDeltaY != 0)
{
m_selection.end(m_selection.lastLine + m_scrollDeltaY, m_selection.lastPos + m_scrollDeltaX);
m_selection.end(m_selection.getLastLine() + m_scrollDeltaY, m_selection.getLastPos() + m_scrollDeltaX);
emit scroll(m_scrollDeltaX, m_scrollDeltaY);
killTimer(m_delayedDrawTimer);
m_delayedDrawTimer = startTimer(50);
......@@ -1894,7 +1894,7 @@ void MergeResultWindow::paintEvent(QPaintEvent*)
if(m_pDiff3LineList == nullptr || !m_bPaintingAllowed)
return;
bool bOldSelectionContainsData = m_selection.bSelectionContainsData;
bool bOldSelectionContainsData = m_selection.selectionContainsData();
const QFontMetrics& fm = fontMetrics();
int fontWidth = fm.width('0');
......@@ -1982,7 +1982,7 @@ void MergeResultWindow::paintEvent(QPaintEvent*)
textLayout.drawCursor(&painter, QPointF(0, (m_cursorYPos - m_firstLine) * fontMetrics().lineSpacing()), m_cursorXPos);
}
if(!bOldSelectionContainsData && m_selection.bSelectionContainsData)
if(!bOldSelectionContainsData && m_selection.selectionContainsData())
emit newSelection();
}
......@@ -2088,7 +2088,7 @@ void MergeResultWindow::mousePressEvent(QMouseEvent* e)
line = max2(line, 0);
if(e->QInputEvent::modifiers() & Qt::ShiftModifier)
{
if(m_selection.firstLine == -1)
if(!m_selection.isValidFirstLine())
m_selection.start(line, pos);
m_selection.end(line, pos);
}
......@@ -2162,7 +2162,7 @@ void MergeResultWindow::mouseReleaseEvent(QMouseEvent* e)
m_delayedDrawTimer = 0;
}
if(m_selection.firstLine != -1)
if(m_selection.isValidFirstLine())
{
emit selectionEnd();
}
......@@ -2179,7 +2179,7 @@ void MergeResultWindow::mouseMoveEvent(QMouseEvent* e)
m_cursorXPos = pos;
m_cursorOldXPixelPos = m_cursorXPixelPos;
m_cursorYPos = line;
if(m_selection.firstLine != -1)
if(m_selection.isValidFirstLine())
{
m_selection.end(line, pos);
myUpdate(0);
......@@ -2604,7 +2604,7 @@ void MergeResultWindow::keyPressEvent(QKeyEvent* e)
int newCursorX = x;
if(bShift)
{
if(m_selection.firstLine == -1)
if(!m_selection.isValidFirstLine())
m_selection.start(m_cursorYPos, m_cursorXPos);
m_selection.end(y, newCursorX);
......@@ -2722,8 +2722,9 @@ QString MergeResultWindow::getSelection()
bool MergeResultWindow::deleteSelection2(QString& s, int& x, int& y,
MergeLineList::iterator& mlIt, MergeEditLineList::iterator& melIt)
{
if(m_selection.firstLine != -1 && m_selection.bSelectionContainsData)
if(m_selection.selectionContainsData())
{
Q_ASSERT(m_selection.isValidFirstLine());
deleteSelection();
y = m_cursorYPos;
calcIteratorFromLineNr(y, mlIt, melIt);
......@@ -2731,15 +2732,18 @@ bool MergeResultWindow::deleteSelection2(QString& s, int& x, int& y,
x = convertToPosInText(s, m_cursorXPos, m_pOptions->m_tabSize);
return true;
}
return false;
}
void MergeResultWindow::deleteSelection()
{
if(m_selection.firstLine == -1 || !m_selection.bSelectionContainsData)
if(!m_selection.selectionContainsData())
{
return;
}
Q_ASSERT(m_selection.isValidFirstLine());
setModified();
int line = 0;
......@@ -2808,8 +2812,8 @@ void MergeResultWindow::deleteSelection()
firstLineString += lineString.midRef(pos); // rest of line
melItFirst->setString(firstLineString);
}
if(line != firstLine || m_selection.lastPos - m_selection.firstPos == lineString.length())
if(line != firstLine || (m_selection.endPos() - m_selection.beginPos()) == lineString.length())
{
// Remove the line
if(mlIt->mergeEditLineList.size() > 1)
......@@ -2833,8 +2837,8 @@ void MergeResultWindow::deleteSelection()
void MergeResultWindow::pasteClipboard(bool bFromSelection)
{
if(m_selection.firstLine != -1)
deleteSelection();
//checking of m_selection if needed is done by deleteSelection no need for check here.
deleteSelection();
setModified();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment