1 | #include <QPainter> |
---|
2 | #include <QPainterPath> |
---|
3 | #include <QPolygon> |
---|
4 | #include <QPixmap> |
---|
5 | #include <QBitmap> |
---|
6 | |
---|
7 | #include <QFontDatabase> |
---|
8 | #include <QFontMetrics> |
---|
9 | |
---|
10 | #include "widget.h" |
---|
11 | |
---|
12 | using namespace QPunchCard; |
---|
13 | |
---|
14 | CardWidget::CardWidget(QWidget * parent, Qt::WFlags f) { |
---|
15 | QWidget(parent,f); |
---|
16 | |
---|
17 | setAutoFillBackground(false); |
---|
18 | |
---|
19 | QSizePolicy size(QSizePolicy::Expanding, QSizePolicy::Expanding); |
---|
20 | // Lochkartenmasse: bloederweise nur uchar als Datenbereich!? |
---|
21 | size.setHorizontalStretch(74); |
---|
22 | size.setVerticalStretch(33); |
---|
23 | size.setHeightForWidth(true); |
---|
24 | setSizePolicy(size); |
---|
25 | |
---|
26 | // create background picture |
---|
27 | qDebug("Creating picture from card"); |
---|
28 | qDebug() << "Loading returned " << background_picture.load("/tmp/anb.jpg"); |
---|
29 | background_picture.setMask( background_picture.createMaskFromColor(Qt::white, Qt::MaskOutColor) ); |
---|
30 | |
---|
31 | // mal ganz bloed: |
---|
32 | codec = CodecFactory::createCodec("o29_code"); |
---|
33 | |
---|
34 | // setup font |
---|
35 | QFontDatabase::addApplicationFont(":/fonts/BPdots.otf"); |
---|
36 | |
---|
37 | setQuality(HighQuality); |
---|
38 | updateGeometry(); |
---|
39 | } |
---|
40 | |
---|
41 | int CardWidget::heightForWidth(int width) const { |
---|
42 | return (int)( ((float)width / 7375) * 3250 ); |
---|
43 | } |
---|
44 | |
---|
45 | QSize CardWidget::minimumSizeHint() const { |
---|
46 | return QSize(73, 32); |
---|
47 | } |
---|
48 | |
---|
49 | QSize CardWidget::sizeHint() const { |
---|
50 | return QSize(737, 325); |
---|
51 | } |
---|
52 | |
---|
53 | void CardWidget::paintEvent(QPaintEvent*) { |
---|
54 | QPainter painter(this); |
---|
55 | // internal coordinate system will measure in thou (1/1000 inch) |
---|
56 | // so the painting size is |
---|
57 | QRect bound = QRect(0,0, 7375, 3250); |
---|
58 | // width: 7-3/8 inch, height: 3-1/4 inch |
---|
59 | |
---|
60 | // hole dimensions: |
---|
61 | QRect hole = QRect(0,0,55,125); |
---|
62 | |
---|
63 | // prepare for painting |
---|
64 | //if(quality() == HighQuality) |
---|
65 | painter.setRenderHint(QPainter::Antialiasing); |
---|
66 | painter.setWindow(bound); |
---|
67 | |
---|
68 | // paint punch card |
---|
69 | if(quality() == ThumbnailQuality) { |
---|
70 | // paint thumbnail punch card: |
---|
71 | // * no border, no round edges, var size edge |
---|
72 | // * only plain background |
---|
73 | painter.setPen(QPen(Qt::red, 1)); // black |
---|
74 | painter.setBrush(Qt::lightGray); // lightGray |
---|
75 | QPoint points[5] = { |
---|
76 | QPoint(0, 300), |
---|
77 | QPoint(0, 3250), |
---|
78 | QPoint(7375, 3250), |
---|
79 | QPoint(7375, 0), |
---|
80 | QPoint(200, 0) |
---|
81 | }; |
---|
82 | painter.drawPolygon(points, 5); |
---|
83 | } else { |
---|
84 | // highquality punch card |
---|
85 | QPainterPath plain_bg; |
---|
86 | QPainterPath edge; |
---|
87 | plain_bg.addRoundedRect(bound, 100.0, 100.0); |
---|
88 | QPolygon edge_polygon; |
---|
89 | edge_polygon << QPoint(0, 0) << QPoint(0, 300) << QPoint(200, 0); |
---|
90 | edge.addPolygon(edge_polygon); |
---|
91 | |
---|
92 | QPainterPath background; |
---|
93 | background = plain_bg - edge; |
---|
94 | painter.setPen(Qt::black); // lightGray |
---|
95 | //painter.drawRoundedRect(bound, 100.0, 100.0); |
---|
96 | painter.setBrush(Qt::lightGray); |
---|
97 | painter.drawPath(background); |
---|
98 | painter.drawPixmap(0, 0, 7375, 3250, this->background_picture); |
---|
99 | } |
---|
100 | |
---|
101 | // now iterate the columns of the card |
---|
102 | if(!card) { |
---|
103 | // keine Karte, nix machen... |
---|
104 | // vielleicht was drauf schreiben oder so... |
---|
105 | return; |
---|
106 | } |
---|
107 | |
---|
108 | int col, row; // counters |
---|
109 | QPoint hole_center; |
---|
110 | |
---|
111 | for(col=0; col < 80; col++) { |
---|
112 | // card.columns[i] would be the actual column |
---|
113 | // #1: Print cols out |
---|
114 | for(row = 0; row < 12; row++) { |
---|
115 | // card.columns[col][row] would it be... |
---|
116 | // move our hole around |
---|
117 | hole_center.rx() = 251 + col * 87; |
---|
118 | hole_center.ry() = 250 * (row + 1); |
---|
119 | hole.moveCenter(hole_center); |
---|
120 | |
---|
121 | painter.setPen(Qt::black); |
---|
122 | |
---|
123 | static const int transform[] = {9,8,7,6,5,4,3,2,1,0,11,12}; |
---|
124 | //qDebug("Will ausgeben: row=%d, transform=%d < SIZE=%d", row, transform[row], card->get(col).size()); |
---|
125 | bool is_punched = card->get(col).at( transform[row] ); |
---|
126 | if(quality() == ThumbnailQuality) { |
---|
127 | if(is_punched) |
---|
128 | painter.fillRect(hole, Qt::black); |
---|
129 | } else { |
---|
130 | if(is_punched) { |
---|
131 | painter.setPen(Qt::black); |
---|
132 | painter.fillRect(hole, Qt::white); |
---|
133 | } else { |
---|
134 | painter.setOpacity(0.1); |
---|
135 | painter.fillRect(hole, Qt::white); |
---|
136 | painter.setOpacity(1.0); |
---|
137 | } |
---|
138 | } // if quality |
---|
139 | } // for row |
---|
140 | |
---|
141 | // #2: Print out the Label line, if present |
---|
142 | if(this->codec && quality() == HighQuality) { |
---|
143 | bool can_encode = codec->canEncode(&card->get(col)); |
---|
144 | char c = codec->toAscii(&card->get(col)); |
---|
145 | QFont font("BPDots"); |
---|
146 | font.setFixedPitch(true); |
---|
147 | font.setPixelSize(150); |
---|
148 | QFontMetrics metrics(font); |
---|
149 | |
---|
150 | hole_center.rx() = 251 + col * 87; |
---|
151 | hole_center.ry() = 73; |
---|
152 | hole.moveCenter(hole_center); |
---|
153 | |
---|
154 | painter.setFont(font); |
---|
155 | if(can_encode) { |
---|
156 | painter.setPen(Qt::black); |
---|
157 | painter.drawText(hole, Qt::AlignCenter | Qt::TextDontClip, QString(c)); |
---|
158 | } else { |
---|
159 | painter.setPen(Qt::red); |
---|
160 | painter.drawText(hole, Qt::AlignCenter | Qt::TextDontClip, QString("?")); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | } // for col |
---|
165 | } // paintEvent |
---|
166 | |
---|
167 | QDebug QPunchCard::operator<<(QDebug dbg, const CardWidget &c) { |
---|
168 | dbg.nospace() << "This is CardWidget, holding " << (c.getCard() ? "this card" : "no card"); |
---|
169 | if(c.getCard()) |
---|
170 | dbg.nospace() << *c.getCard(); |
---|
171 | return dbg.nospace(); |
---|
172 | } |
---|