Skip to content

Commit ad436d6

Browse files
committed
CLightningFastViewerWidget: int -> qsizetype
1 parent 1f42a81 commit ad436d6

File tree

2 files changed

+53
-55
lines changed

2 files changed

+53
-55
lines changed

plugins/viewer/textviewer/src/clightningfastviewer.cpp

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void CLightningFastViewerWidget::paintEvent(QPaintEvent*)
105105
}
106106
else
107107
{
108-
drawTextLine(painter, static_cast<int>(line), y, _fontMetrics);
108+
drawTextLine(painter, line, y, _fontMetrics);
109109
}
110110
y += _lineHeight;
111111
}
@@ -267,13 +267,12 @@ void CLightningFastViewerWidget::keyPressEvent(QKeyEvent* event)
267267
else
268268
{
269269
// Find previous line
270-
int currentLine = findLineContainingOffset(cursorPos);
270+
const qsizetype currentLine = findLineContainingOffset(cursorPos);
271271
if (currentLine > 0)
272272
{
273273
// Try to maintain column position
274-
int colInLine = cursorPos - _lineOffsets[currentLine];
275-
newPos = qMin((qsizetype)(_lineOffsets[currentLine - 1] + colInLine),
276-
(qsizetype)(_lineOffsets[currentLine - 1] + _lineLengths[currentLine - 1] - 1));
274+
const qsizetype colInLine = cursorPos - _lineOffsets[currentLine];
275+
newPos = qMin(_lineOffsets[currentLine - 1] + colInLine, _lineOffsets[currentLine - 1] + _lineLengths[currentLine - 1] - 1);
277276
}
278277
}
279278
break;
@@ -285,13 +284,12 @@ void CLightningFastViewerWidget::keyPressEvent(QKeyEvent* event)
285284
else
286285
{
287286
// Find next line
288-
int currentLine = findLineContainingOffset(cursorPos);
289-
if (currentLine >= 0 && currentLine < static_cast<int>(_lineOffsets.size()) - 1)
287+
const qsizetype currentLine = findLineContainingOffset(cursorPos);
288+
if (currentLine >= 0 && currentLine < static_cast<qsizetype>(_lineOffsets.size()) - 1)
290289
{
291290
// Try to maintain column position
292-
int colInLine = cursorPos - _lineOffsets[currentLine];
293-
newPos = qMin((qsizetype)(_lineOffsets[currentLine + 1] + colInLine),
294-
(qsizetype)(_lineOffsets[currentLine + 1] + _lineLengths[currentLine + 1] - 1));
291+
const qsizetype colInLine = cursorPos - _lineOffsets[currentLine];
292+
newPos = qMin(_lineOffsets[currentLine + 1] + colInLine, _lineOffsets[currentLine + 1] + _lineLengths[currentLine + 1] - 1);
295293
}
296294
}
297295
break;
@@ -313,7 +311,7 @@ void CLightningFastViewerWidget::keyPressEvent(QKeyEvent* event)
313311
else
314312
{
315313
// Find start of current line
316-
int currentLine = findLineContainingOffset(cursorPos);
314+
const qsizetype currentLine = findLineContainingOffset(cursorPos);
317315
if (currentLine >= 0)
318316
{
319317
newPos = _lineOffsets[currentLine];
@@ -333,10 +331,10 @@ void CLightningFastViewerWidget::keyPressEvent(QKeyEvent* event)
333331
else
334332
{
335333
// Find end of current line
336-
int currentLine = findLineContainingOffset(cursorPos);
334+
const qsizetype currentLine = findLineContainingOffset(cursorPos);
337335
if (currentLine >= 0)
338336
{
339-
newPos = qMin(maxOffset, (qsizetype)(_lineOffsets[currentLine] + _lineLengths[currentLine] - 1));
337+
newPos = qMin(maxOffset, _lineOffsets[currentLine] + _lineLengths[currentLine] - 1);
340338
}
341339
}
342340
break;
@@ -361,17 +359,17 @@ void CLightningFastViewerWidget::keyPressEvent(QKeyEvent* event)
361359
}
362360
}
363361

364-
int CLightningFastViewerWidget::totalLines() const
362+
qsizetype CLightningFastViewerWidget::totalLines() const
365363
{
366364
if (_mode == HEX)
367365
{
368366
if (_data.isEmpty())
369367
return 0;
370-
return static_cast<int>((_data.size() + (qsizetype)_bytesPerLine - 1) / (qsizetype)_bytesPerLine);
368+
return (_data.size() + _bytesPerLine - 1) / _bytesPerLine;
371369
}
372370
else
373371
{
374-
return static_cast<int>(_lineOffsets.size());
372+
return static_cast<qsizetype>(_lineOffsets.size());
375373
}
376374
}
377375

@@ -494,26 +492,26 @@ void CLightningFastViewerWidget::drawHexLine(QPainter& painter, qsizetype offset
494492
}
495493
}
496494

497-
void CLightningFastViewerWidget::drawTextLine(QPainter& painter, int lineIndex, int y, const QFontMetrics& fm)
495+
void CLightningFastViewerWidget::drawTextLine(QPainter& painter, qsizetype lineIndex, int y, const QFontMetrics& fm)
498496
{
499-
if (lineIndex < 0 || lineIndex >= static_cast<int>(_lineOffsets.size()))
497+
if (lineIndex < 0 || lineIndex >= static_cast<qsizetype>(_lineOffsets.size()))
500498
return;
501499

502500
const int hScroll = horizontalScrollBar()->value();
503501

504-
const int lineStart = _lineOffsets[lineIndex];
505-
const int lineLen = _lineLengths[lineIndex];
502+
const qsizetype lineStart = _lineOffsets[lineIndex];
503+
const qsizetype lineLen = _lineLengths[lineIndex];
506504
QString lineText = _text.mid(lineStart, lineLen);
507505

508506
// Draw the line character by character to handle selection
509507
int x = Layout::LEFT_MARGIN_PIXELS - hScroll;
510-
for (int i = 0; i < lineText.length(); ++i)
508+
for (qsizetype i = 0; i < lineText.length(); ++i)
511509
{
512510
qsizetype charOffset = lineStart + i;
513511

514512
if (isSelected(charOffset))
515513
{
516-
int charWidth = fm.horizontalAdvance(lineText[i]);
514+
const int charWidth = fm.horizontalAdvance(lineText[i]);
517515
QRect selRect(x, y, charWidth, _lineHeight);
518516
painter.fillRect(selRect, palette().highlight());
519517
painter.setPen(palette().highlightedText().color());
@@ -548,25 +546,25 @@ void CLightningFastViewerWidget::updateScrollBarsAndHexLayout()
548546
{
549547
// In text mode, estimate max line width based on character count
550548
// For monospace fonts, this is much faster than measuring each line
551-
int maxChars = 0;
549+
qsizetype maxChars = 0;
552550
for (size_t i = 0; i < _lineOffsets.size(); ++i)
553551
{
554-
const int lineChars = _lineLengths[i];
552+
const qsizetype lineChars = _lineLengths[i];
555553

556554
// Quick scan for tabs in this line to adjust estimate
557-
int tabCount = 0;
558-
for (int j = _lineOffsets[i], end = j + lineChars; j < end; ++j)
555+
qsizetype tabCount = 0;
556+
for (qsizetype j = _lineOffsets[i], end = j + lineChars; j < end; ++j)
559557
{
560558
if (_text[j] == '\t') [[unlikely]]
561559
++tabCount;
562560
}
563561

564562
// Estimate: each tab expands based on measured _tabWidthInChars
565-
const int estimatedChars = lineChars + (tabCount * (_tabWidthInChars - 1));
563+
const qsizetype estimatedChars = lineChars + (tabCount * (_tabWidthInChars - 1));
566564
maxChars = qMax(maxChars, estimatedChars);
567565
}
568566

569-
const int maxWidth = maxChars * _charWidth;
567+
const qsizetype maxWidth = maxChars * _charWidth;
570568
horizontalScrollBar()->setRange(0, qMax(0, maxWidth - viewport()->width() + _charWidth * Layout::TEXT_HORIZONTAL_MARGIN_CHARS));
571569
}
572570
horizontalScrollBar()->setPageStep(viewport()->width());
@@ -639,14 +637,14 @@ qsizetype CLightningFastViewerWidget::textPosToOffset(const QPoint& pos) const
639637
return -1;
640638

641639
int x = pos.x() + horizontalScrollBar()->value();
642-
int lineStart = _lineOffsets[line];
643-
int lineLen = _lineLengths[line];
644-
QString lineText = _text.mid(lineStart, lineLen);
640+
const qsizetype lineStart = _lineOffsets[line];
641+
const qsizetype lineLen = _lineLengths[line];
642+
const QString lineText = _text.mid(lineStart, lineLen);
645643

646644
// Find character at position
647645
int currentX = Layout::LEFT_MARGIN_PIXELS;
648646

649-
for (int i = 0; i < lineText.length(); ++i)
647+
for (qsizetype i = 0; i < lineText.length(); ++i)
650648
{
651649
int charWidth = _fontMetrics.horizontalAdvance(lineText[i]);
652650
if (x < currentX + charWidth / 2)
@@ -660,11 +658,11 @@ qsizetype CLightningFastViewerWidget::textPosToOffset(const QPoint& pos) const
660658

661659
void CLightningFastViewerWidget::ensureVisible(qsizetype offset)
662660
{
663-
int line = 0;
661+
qsizetype line = 0;
664662

665663
if (_mode == HEX)
666664
{
667-
line = static_cast<int>(offset / _bytesPerLine);
665+
line = offset / _bytesPerLine;
668666
}
669667
else
670668
{
@@ -674,8 +672,8 @@ void CLightningFastViewerWidget::ensureVisible(qsizetype offset)
674672
return;
675673
}
676674

677-
int firstVisible = verticalScrollBar()->value();
678-
int visibleLines = viewport()->height() / _lineHeight;
675+
const int firstVisible = verticalScrollBar()->value();
676+
const int visibleLines = viewport()->height() / _lineHeight;
679677

680678
if (line < firstVisible)
681679
{
@@ -779,8 +777,8 @@ void CLightningFastViewerWidget::wrapTextIfNeeded()
779777
clearWrappingData();
780778

781779
// No wrapping - split on newlines only
782-
int start = 0;
783-
int newlinePos;
780+
qsizetype start = 0;
781+
qsizetype newlinePos;
784782

785783
while ((newlinePos = _text.indexOf('\n', start)) != -1)
786784
{
@@ -810,14 +808,14 @@ void CLightningFastViewerWidget::wrapTextIfNeeded()
810808
if (maxCharsPerLine <= 0)
811809
return; // Guard against too small viewport
812810

813-
int currentLineStart = 0;
814-
int currentCharCount = 0; // Character count in current line
815-
int lastBreakPos = -1; // Last position where we could break
816-
int lastBreakCharCount = 0; // Character count at last break position
811+
qsizetype currentLineStart = 0;
812+
qsizetype currentCharCount = 0; // Character count in current line
813+
qsizetype lastBreakPos = -1; // Last position where we could break
814+
qsizetype lastBreakCharCount = 0; // Character count at last break position
817815

818816
for (qsizetype i = 0, n = _text.length(); i < n; ++i)
819817
{
820-
QChar ch = _text[i];
818+
const QChar ch = _text[i];
821819

822820
// Handle explicit newlines
823821
if (ch == '\n')
@@ -832,11 +830,11 @@ void CLightningFastViewerWidget::wrapTextIfNeeded()
832830
}
833831

834832
// Calculate character width in monospace units
835-
int charWidth = 1; // Default for most characters
833+
qsizetype charWidth = 1; // Default for most characters
836834
if (ch == '\t')
837835
{
838836
// Tab width: advance to next multiple of _tabWidthInChars
839-
int nextTabStop = ((currentCharCount / _tabWidthInChars) + 1) * _tabWidthInChars;
837+
qsizetype nextTabStop = ((currentCharCount / _tabWidthInChars) + 1) * _tabWidthInChars;
840838
charWidth = nextTabStop - currentCharCount;
841839
}
842840
else if (ch.unicode() < 32 || ch.category() == QChar::Other_Control)
@@ -857,7 +855,7 @@ void CLightningFastViewerWidget::wrapTextIfNeeded()
857855
// Line is too long
858856
if (currentCharCount > maxCharsPerLine && i > currentLineStart)
859857
{
860-
int breakPos;
858+
qsizetype breakPos = -1;
861859

862860
// Try to break at last space
863861
if (lastBreakPos > currentLineStart)
@@ -914,13 +912,13 @@ void CLightningFastViewerWidget::updateFontMetrics()
914912
_tabWidthInChars = qMax(1, tabWidth / _charWidth); // Ensure at least 1
915913
}
916914

917-
int CLightningFastViewerWidget::findLineContainingOffset(qsizetype offset) const
915+
qsizetype CLightningFastViewerWidget::findLineContainingOffset(qsizetype offset) const
918916
{
919917
for (size_t i = 0; i < _lineOffsets.size(); ++i)
920918
{
921919
if (_lineOffsets[i] <= offset && offset < _lineOffsets[i] + _lineLengths[i])
922920
{
923-
return static_cast<int>(i);
921+
return static_cast<qsizetype>(i);
924922
}
925923
}
926924
return -1;

plugins/viewer/textviewer/src/clightningfastviewer.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class CLightningFastViewerWidget final : public QAbstractScrollArea
4242
};
4343

4444
// Common methods
45-
[[nodiscard]] int totalLines() const;
45+
[[nodiscard]] qsizetype totalLines() const;
4646
void updateScrollBarsAndHexLayout();
4747
void ensureVisible(qsizetype offset);
4848
void copySelection();
@@ -65,31 +65,31 @@ class CLightningFastViewerWidget final : public QAbstractScrollArea
6565
[[nodiscard]] qsizetype hexPosToOffset(const QPoint& pos) const;
6666

6767
// Text mode methods
68-
void drawTextLine(QPainter& painter, int lineIndex, int y, const QFontMetrics& fm);
68+
void drawTextLine(QPainter& painter, qsizetype lineIndex, int y, const QFontMetrics& fm);
6969
[[nodiscard]] qsizetype textPosToOffset(const QPoint& pos) const;
7070

7171
void wrapTextIfNeeded();
7272
void clearWrappingData();
73-
[[nodiscard]] int findLineContainingOffset(qsizetype offset) const;
73+
[[nodiscard]] qsizetype findLineContainingOffset(qsizetype offset) const;
7474

7575
private:
7676
Mode _mode = HEX;
7777

7878
// Hex mode data
7979
QByteArray _data;
80-
int _bytesPerLine = 16;
80+
qsizetype _bytesPerLine = 16;
8181

8282
// Text mode data
8383
QString _text;
84-
std::vector<int> _lineOffsets; // Starting character offset for each wrapped line
85-
std::vector<int> _lineLengths; // Length of each wrapped line
84+
std::vector<qsizetype> _lineOffsets; // Starting character offset for each wrapped line
85+
std::vector<qsizetype> _lineLengths; // Length of each wrapped line
8686
size_t _wordWrapParamsHash = 0;
8787
bool _wordWrap = true;
8888

8989
// Common display data
9090
int _lineHeight = 0;
9191
int _charWidth = 0;
92-
int _tabWidthInChars = 4; // Default tab width in character positions
92+
qsizetype _tabWidthInChars = 4; // Default tab width in character positions
9393
QFontMetrics _fontMetrics;
9494
Selection _selection;
9595
bool _initialized = false;

0 commit comments

Comments
 (0)