File: memory_view.cpp

package info (click to toggle)
cen64 0.3%2Bgit20200723-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,504 kB
  • sloc: ansic: 24,277; cpp: 793; asm: 772; makefile: 14
file content (93 lines) | stat: -rw-r--r-- 2,541 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// memory_view.cpp: CEN64D memory view (MVC).
//
// CEN64D: Cycle-Accurate Nintendo 64 Debugger
// Copyright (C) 2015, Tyler J. Stachecki.
//
// This file is subject to the terms and conditions defined in
// 'LICENSE', which is part of this source code package.
//

#include "memory_view.h"
#include <QBrush>
#include <QFont>
#include <QFontMetrics>
#include <QPainter>
#include <QSize>
#include <cstdio>

MemoryView::MemoryView(unsigned addressOctets) : addressOctets(addressOctets) {
  QFont monospacedFont("Courier New");
  monospacedFont.setFixedPitch(true);
  monospacedFont.setPointSize(10);
  setFont(monospacedFont);

  QFontMetrics metrics(monospacedFont);
  fontWidth = metrics.width('0');
  fontHeight = metrics.height();

  byteStart = 1 + (3 + addressOctets) * fontWidth;

  // Create the format string.
  sprintf(formatstr, " 0x%%.%ullX", addressOctets);
}

MemoryView::~MemoryView() {
}

void MemoryView::paintEvent(QPaintEvent* event) {
  QSize area = viewport()->size();
  char buf[32];
  int i, j;

  // TODO: Fonts don't seem to render exactly
  // where we want them, so add a fudge factor.
  float fudge = 2.0 / 3.0;
  unsigned start = fontHeight * fudge;

  QPainter painter(viewport());
  QBrush clear = QBrush(Qt::white);
  QBrush shaded = QBrush(QColor(0xE8, 0xE8, 0xE8));

  // Shade ever other line.
  painter.fillRect(0, 0, area.width(), area.height(), clear);

  for (i = fontHeight; i < area.height(); i += fontHeight * 2)
    painter.fillRect(0, i, area.width(), fontHeight, shaded);

  // Draw any values in the range.
  for (i = 0; i < area.height(); i += fontHeight) {
    sprintf(buf, formatstr, (unsigned long long) 0 +
      (i / fontHeight * bytesPerRow));

    painter.drawText(1, start + i, buf);

    for (j = 0; j < bytesPerRow; j++) {
      sprintf(buf, " %02X ", j);
      painter.drawText(byteStart + j  * fontWidth * 3, start + i, buf);

      buf[1] = '\0';
      buf[0] = isprint(j) ? j : '.';
      painter.drawText(byteStart + bytesPerRow * 3 * fontWidth +
        (j + 1) * fontWidth, start + i, buf);
    }
  }
}

void MemoryView::resizeEvent(QResizeEvent *event) {
  QSize area = viewport()->size();
  int i;

  // Determine how many bytes we can cram in the row without having
  // to resort to horizontal sliders. Make sure that we keep the byte
  // count to a power of two for simplicity's sake.
  for (bytesPerRow = 1, i = 2; ; i *= 2) {
    int check = byteStart + i * 3 * fontWidth + (i + 1) * fontWidth;

    if (check > area.width())
      break;

    bytesPerRow = i;
  }
}