File: register_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 (124 lines) | stat: -rw-r--r-- 3,362 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// register_view.cpp: CEN64D register 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 "register_view.h"
#include <QBrush>
#include <QColor>
#include <QFont>
#include <QPainter>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QScrollBar>
#include <QSize>
#include <cstdio>
#include <cstring>

RegisterView::RegisterView(const char **registers,
  unsigned numRegisters, unsigned octets) : model(numRegisters),
  registers(registers), numRegisters(numRegisters), octets(octets) {
  unsigned i;

  QFont monospacedFont("Courier New");
  monospacedFont.setFixedPitch(true);
  monospacedFont.setPointSize(10);
  setFont(monospacedFont);

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

  // Find the strlen of the longest mnemonic.
  for (longestMnemonic = 0, i = 0; i < numRegisters; i++) {
    size_t len = strlen(registers[i]);

    if (len > longestMnemonic)
      longestMnemonic = len;
  }

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

RegisterView::~RegisterView() {
}

unsigned RegisterView::getMaximumHeight() const {
  return fontHeight * numRegisters;
}

unsigned RegisterView::getMaximumWidth() const {
  return 1 + (3 + octets + longestMnemonic) * fontWidth;
}

RegisterModel& RegisterView::getModel() {
  return model;
}

void RegisterView::paintEvent(QPaintEvent* event) {
  QSize area = maximumViewportSize();
  char buf[32];
  int i;

  unsigned hs = horizontalScrollBar()->value();
  unsigned vs = verticalScrollBar()->value();
  unsigned hsDW = hs / fontWidth;
  unsigned hsMW = hs % fontWidth;
  unsigned vsDH = vs / fontHeight;
  unsigned vsMH = vs % fontHeight;

  // 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);

  if (vsDH & 0x1)
    painter.fillRect(0, 0, area.width(), fontHeight - vsMH, shaded);

  for (i = (vsDH & 0x1 ? fontHeight : 0) + fontHeight - vsMH;
    i < area.height(); i+= fontHeight * 2) {
    painter.fillRect(0, i, area.width(), fontHeight, shaded);
  }

  // Draw any values in the range.
  unsigned rs = vsDH;
  unsigned rf = vs + fontHeight * (numRegisters - rs);

  if (rf > numRegisters)
    rf = numRegisters;

  for (i = -vsMH; i < area.height() && rs < rf; i += fontHeight) {
    size_t len = strlen(registers[rs]);
    unsigned j;

    // Write the mnemonic, pad with spaces to align.
    strcpy(buf, registers[rs]);
    for (j = len; j < longestMnemonic; j++)
      buf[j] = ' ';

    // Write the values, draw the string.
    sprintf(buf + longestMnemonic, formatstr, model.getIndex(rs++));
    painter.drawText(1 - hsMW, start + i, buf + hsDW);
  }
}

void RegisterView::resizeEvent(QResizeEvent *event) {
  QSize area = maximumViewportSize();

  horizontalScrollBar()->setRange(0, getMaximumWidth() - area.width());
  verticalScrollBar()->setRange(0, getMaximumHeight() - area.height());
}