Commit 2781ae2f authored by Eike Ziller's avatar Eike Ziller

BinEditor: Fix selection behavior and painting

* there factually always is a selection (cursor position is always
selected)
* since there always is a selection, always paint a selection
* since there factually always is a cursor position where you can type
(even if you have a selection), always paint a (block) cursor
* fix selection painting in hex region to match the column painting

Reverts c4a3dbe3 and fixes the selection
on find like originally proposed by Orgad

Change-Id: Ife5395a42d35ac50103a63c77cb54491afd7dd1e
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent d11e7ff7
......@@ -126,11 +126,11 @@ void BinEditorWidget::init()
2*m_addressBytes + (m_addressBytes - 1) / 2;
m_addressString = QString(addressStringWidth, QLatin1Char(':'));
QFontMetrics fm(fontMetrics());
m_margin = 4;
m_descent = fm.descent();
m_ascent = fm.ascent();
m_lineHeight = fm.lineSpacing();
m_charWidth = fm.width(QChar(QLatin1Char('M')));
m_margin = m_charWidth;
m_columnWidth = 2 * m_charWidth + fm.width(QChar(QLatin1Char(' ')));
m_numLines = m_size / m_bytesPerLine + 1;
m_numVisibleLines = viewport()->height() / m_lineHeight;
......@@ -638,7 +638,7 @@ int BinEditorWidget::find(const QByteArray &pattern_arg, int from,
if (pos >= 0) {
setCursorPosition(pos);
setCursorPosition(pos + (found == pos ? pattern.size() : hexPattern.size()), KeepAnchor);
setCursorPosition(pos + (found == pos ? pattern.size() : hexPattern.size()) - 1, KeepAnchor);
}
return pos;
}
......@@ -713,7 +713,7 @@ void BinEditorWidget::paintEvent(QPaintEvent *e)
QPainter painter(viewport());
const int topLine = verticalScrollBar()->value();
const int xoffset = horizontalScrollBar()->value();
const int x1 = -xoffset + m_margin + m_labelWidth - m_charWidth/2;
const int x1 = -xoffset + m_margin + m_labelWidth - m_charWidth/2 - 1;
const int x2 = -xoffset + m_margin + m_labelWidth + m_bytesPerLine * m_columnWidth + m_charWidth/2;
painter.drawLine(x1, 0, x1, viewport()->height());
painter.drawLine(x2, 0, x2, viewport()->height());
......@@ -836,7 +836,7 @@ void BinEditorWidget::paintEvent(QPaintEvent *e)
color = QColor(0xffef0b);
if (color.isValid()) {
painter.fillRect(item_x, y-m_ascent, m_columnWidth, m_lineHeight, color);
painter.fillRect(item_x - m_charWidth/2, y-m_ascent, m_columnWidth, m_lineHeight, color);
int printable_item_x = -xoffset + m_margin + m_labelWidth + m_bytesPerLine * m_columnWidth + m_charWidth
+ fm.width(printable.left(c));
painter.fillRect(printable_item_x, y-m_ascent,
......@@ -844,8 +844,8 @@ void BinEditorWidget::paintEvent(QPaintEvent *e)
m_lineHeight, color);
}
if (selStart < selEnd && !isFullySelected && pos >= selStart && pos < selEnd) {
selectionRect |= QRect(item_x, y-m_ascent, m_columnWidth, m_lineHeight);
if (!isFullySelected && pos >= selStart && pos <= selEnd) {
selectionRect |= QRect(item_x - m_charWidth/2, y-m_ascent, m_columnWidth, m_lineHeight);
int printable_item_x = -xoffset + m_margin + m_labelWidth + m_bytesPerLine * m_columnWidth + m_charWidth
+ fm.width(printable.left(c));
printableSelectionRect |= QRect(printable_item_x, y-m_ascent,
......@@ -856,11 +856,10 @@ void BinEditorWidget::paintEvent(QPaintEvent *e)
}
int x = -xoffset + m_margin + m_labelWidth;
bool cursorWanted = m_cursorPosition == m_anchorPosition;
if (isFullySelected) {
painter.save();
painter.fillRect(x, y-m_ascent, m_bytesPerLine*m_columnWidth, m_lineHeight, palette().highlight());
painter.fillRect(x - m_charWidth/2, y-m_ascent, m_bytesPerLine*m_columnWidth, m_lineHeight, palette().highlight());
painter.setPen(palette().highlightedText().color());
drawItems(&painter, x, y, itemString);
painter.restore();
......@@ -878,8 +877,7 @@ void BinEditorWidget::paintEvent(QPaintEvent *e)
}
}
if (cursor >= 0 && cursorWanted) {
if (cursor >= 0) {
int w = fm.boundingRect(itemString.mid(cursor*3, 2)).width();
QRect cursorRect(x + cursor * m_columnWidth, y - m_ascent, w + 1, m_lineHeight);
painter.save();
......@@ -919,7 +917,7 @@ void BinEditorWidget::paintEvent(QPaintEvent *e)
}
}
if (cursor >= 0 && !printable.isEmpty() && cursorWanted) {
if (cursor >= 0 && !printable.isEmpty()) {
QRect cursorRect(text_x + fm.width(printable.left(cursor)),
y-m_ascent,
fm.width(printable.at(cursor)),
......@@ -950,18 +948,14 @@ void BinEditorWidget::setCursorPosition(int pos, MoveMode moveMode)
pos = qMin(m_size-1, qMax(0, pos));
int oldCursorPosition = m_cursorPosition;
bool hadSelection = hasSelection();
m_lowNibble = false;
if (!hadSelection)
updateLines();
m_cursorPosition = pos;
if (moveMode == MoveAnchor) {
if (hadSelection)
updateLines(m_anchorPosition, oldCursorPosition);
updateLines(m_anchorPosition, oldCursorPosition);
m_anchorPosition = m_cursorPosition;
}
updateLines(hadSelection || hasSelection() ? oldCursorPosition : m_cursorPosition, m_cursorPosition);
updateLines(oldCursorPosition, m_cursorPosition);
ensureCursorVisible();
emit cursorPositionChanged(m_cursorPosition);
}
......@@ -1087,20 +1081,14 @@ bool BinEditorWidget::event(QEvent *e)
QString BinEditorWidget::toolTip(const QHelpEvent *helpEvent) const
{
// Selection if mouse is in, else 1 byte at cursor
int selStart = selectionStart();
int selEnd = selectionEnd();
int byteCount = selEnd - selStart;
if (byteCount < 1) {
selStart = posAt(helpEvent->pos());
selEnd = selStart + 1;
byteCount = 1;
}
int byteCount = selEnd - selStart + 1;
if (m_hexCursor == 0 || byteCount > 8)
return QString();
const QPoint &startPoint = offsetToPos(selStart);
const QPoint &endPoint = offsetToPos(selEnd);
const QPoint &endPoint = offsetToPos(selEnd + 1);
QRect selRect(startPoint, endPoint);
selRect.setHeight(m_lineHeight);
if (!selRect.contains(helpEvent->pos()))
......@@ -1385,10 +1373,7 @@ void BinEditorWidget::copy(bool raw)
{
int selStart = selectionStart();
int selEnd = selectionEnd();
if (selStart > selEnd)
qSwap(selStart, selEnd);
const int selectionLength = selEnd - selStart;
const int selectionLength = selEnd - selStart + 1;
if (selectionLength >> 22) {
QMessageBox::warning(this, tr("Copying Failed"),
tr("You cannot copy more than 4 MB of binary data."));
......@@ -1496,7 +1481,7 @@ void BinEditorWidget::redo()
void BinEditorWidget::contextMenuEvent(QContextMenuEvent *event)
{
const int selStart = selectionStart();
const int byteCount = selectionEnd() - selStart;
const int byteCount = selectionEnd() - selStart + 1;
QPointer<QMenu> contextMenu(new QMenu(this));
......
......@@ -108,7 +108,6 @@ public:
Core::IEditor *editor() const { return m_ieditor; }
void setEditor(Core::IEditor *ieditor) { m_ieditor = ieditor; }
bool hasSelection() const { return m_cursorPosition != m_anchorPosition; }
int selectionStart() const { return qMin(m_anchorPosition, m_cursorPosition); }
int selectionEnd() const { return qMax(m_anchorPosition, m_cursorPosition); }
......
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