Skip to content
Bitcoin Roots

v29.4-roots.2 · Commit 5: [PATCH 05/21] product: establish Roots identity and Qt experience

src/qt/qrimagewidget.cpp

Before: src/qt/qrimagewidget.cpp · After: src/qt/qrimagewidget.cpp

File metadata
index e912dafa6078620c11ce6e9cc1b0cfaddfe4ff09..77580f49a33b32bf6a508a9373980da93b104036 100644

Download original patch · Release notes

Diff rows 1–79 of 79. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/qt/qrimagewidget.cpp CHANGED
@@ -30,6 +30,17 @@ QRImageWidget::QRImageWidget(QWidget* parent)
30
  }
31
 
32
  bool QRImageWidget::setQR(const QString& data, const QString& text)
 
 
 
 
 
 
 
 
 
 
 
33
  {
34
  #ifdef USE_QRCODE
35
  setText("");
@@ -48,7 +59,8 @@ bool QRImageWidget::setQR(const QString& data, const QString& text)
48
  return false;
49
  }
50
 
51
- QImage qrImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
 
52
  qrImage.fill(0xffffff);
53
  unsigned char *p = code->data;
54
  for (int y = 0; y < code->width; ++y) {
@@ -59,25 +71,40 @@ bool QRImageWidget::setQR(const QString& data, const QString& text)
59
  }
60
  QRcode_free(code);
61
 
62
- const int qr_image_size = QR_IMAGE_SIZE + (text.isEmpty() ? 0 : 2 * QR_IMAGE_MARGIN);
63
- QImage qrAddrImage(qr_image_size, qr_image_size, QImage::Format_RGB32);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  qrAddrImage.fill(0xffffff);
65
  {
66
  QPainter painter(&qrAddrImage);
67
- painter.drawImage(QR_IMAGE_MARGIN, 0, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
68
 
69
  if (!text.isEmpty()) {
70
- QRect paddedRect = qrAddrImage.rect();
71
- paddedRect.setHeight(QR_IMAGE_SIZE + QR_IMAGE_TEXT_MARGIN);
72
-
73
- QFont font = GUIUtil::fixedPitchFont();
74
- font.setStretch(QFont::SemiCondensed);
75
- font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
76
- const qreal font_size = GUIUtil::calculateIdealFontSize(paddedRect.width() - 2 * QR_IMAGE_TEXT_MARGIN, text, font);
77
- font.setPointSizeF(font_size);
78
-
79
  painter.setFont(font);
80
- painter.drawText(paddedRect, Qt::AlignBottom | Qt::AlignCenter, text);
81
  }
82
  }
83
 
 
30
  }
31
 
32
  bool QRImageWidget::setQR(const QString& data, const QString& text)
33
+ {
34
+ QFont font{GUIUtil::fixedPitchFont()};
35
+ font.setStretch(QFont::SemiCondensed);
36
+ font.setLetterSpacing(QFont::AbsoluteSpacing, 1);
37
+ if (!text.isEmpty()) {
38
+ font.setPointSizeF(GUIUtil::calculateIdealFontSize(QR_IMAGE_SIZE, text, font));
39
+ }
40
+ return setQR(data, text, font);
41
+ }
42
+
43
+ bool QRImageWidget::setQR(const QString& data, const QString& text, const QFont& font)
44
  {
45
  #ifdef USE_QRCODE
46
  setText("");
 
59
  return false;
60
  }
61
 
62
+ // Preserve the four-module quiet zone required by QR readers before scaling.
63
+ QImage qrImage{code->width + 8, code->width + 8, QImage::Format_RGB32};
64
  qrImage.fill(0xffffff);
65
  unsigned char *p = code->data;
66
  for (int y = 0; y < code->width; ++y) {
 
71
  }
72
  QRcode_free(code);
73
 
74
+ int qr_image_width{QR_IMAGE_SIZE + (2 * QR_IMAGE_MARGIN)};
75
+ int qr_image_height{qr_image_width};
76
+ int qr_image_x_margin{QR_IMAGE_MARGIN};
77
+ int text_lines{0};
78
+ if (!text.isEmpty()) {
79
+ const int max_text_width{qr_image_width - (2 * QR_IMAGE_TEXT_MARGIN)};
80
+ const QFontMetrics fm{font};
81
+ const int text_width{GUIUtil::TextWidth(fm, text)};
82
+ if (text_width > max_text_width && text_width < max_text_width * 5 / 4) {
83
+ qr_image_width = text_width + (2 * QR_IMAGE_TEXT_MARGIN);
84
+ qr_image_x_margin = (qr_image_width - QR_IMAGE_SIZE) / 2;
85
+ text_lines = 1;
86
+ } else {
87
+ text_lines = (text_width + max_text_width - 1) / max_text_width;
88
+ }
89
+ qr_image_height += (fm.height() * text_lines) + QR_IMAGE_TEXT_MARGIN;
90
+ }
91
+ QImage qrAddrImage{qr_image_width, qr_image_height, QImage::Format_RGB32};
92
  qrAddrImage.fill(0xffffff);
93
  {
94
  QPainter painter(&qrAddrImage);
95
+ painter.drawImage(qr_image_x_margin, QR_IMAGE_MARGIN, qrImage.scaled(QR_IMAGE_SIZE, QR_IMAGE_SIZE));
96
 
97
  if (!text.isEmpty()) {
98
+ QRect padded_rect{qrAddrImage.rect()};
99
+ padded_rect.setHeight(padded_rect.height() - QR_IMAGE_TEXT_MARGIN);
100
+ QString text_wrapped{text};
101
+ const int chars_per_line{(text.size() + text_lines - 1) / text_lines};
102
+ for (int line{1}, pos{0}; line < text_lines; ++line) {
103
+ pos += chars_per_line;
104
+ text_wrapped.insert(pos, QChar{'\n'});
105
+ }
 
106
  painter.setFont(font);
107
+ painter.drawText(padded_rect, Qt::AlignBottom | Qt::AlignCenter, text_wrapped);
108
  }
109
  }
110