summaryrefslogtreecommitdiffstats
path: root/src/devices/base/device_group.cpp
blob: db702649dd0138e4f3c4528be7363d2f1b3f10e6 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/***************************************************************************
 *   Copyright (C) 2005-2006 Nicolas Hadacek <hadacek@kde.org>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "device_group.h"

#if !defined(NO_KDE)
#  include <tqpainter.h>
#  include <kglobal.h>

TQColor Device::statusColor(Status status)
{
  switch (status.type()) {
    case Status::Future:         return TQt::blue;
    case Status::InProduction:   return TQt::green;
    case Status::Mature:
    case Status::NotRecommended: return TQColor("orange");
    case Status::EOL:            return TQt::red;
    case Status::Unknown:
    case Status::Nb_Types: break;
  }
  return TQt::black;
}

TQString coloredString(const TQString &text, TQColor color)
{
  return TQString("<font color=\"") + color.name() + "\">" + text + "</font>";
}

TQString supportedString(bool supported)
{
  return coloredString(supported ? i18n("Supported") : i18n("Unsupported"),
                       supported ? TQt::green :  TQt::red);
}

class Tick {
public:
  Tick() {}
  Tick(double value, double oValue) {
    s = KGlobal::locale()->formatNumber(value, 1);
    min = oValue;
  }
  TQString s;
  double min;
};

class TickMap : public TQMap<double, Tick>
{
public:
  TickMap() {}
  void add(double value, double oValue) {
    insert(value, Tick(value, oValue), false);
    (*this)[value].min = TQMIN((*this)[value].min, oValue);
  }
};

TQPixmap drawGraph(const TQValueVector<Device::RangeBox> &boxes)
{
  const uint w = 300, h = 200;
  TQPixmap pixmap(w, h);
  pixmap.fill(TQt::white);
  TQPainter p(&pixmap);
  TQFontMetrics f(p.font());
  TickMap xTicks, yTicks;
  xTicks.add(0.0, 0.0);
  yTicks.add(0.0, 0.0);
  for (uint i=0; i<boxes.count(); i++) {
//    qDebug("box #%i: %f=[%f %f] %f=[%f %f]", i, boxes[i].start.x, boxes[i].start.yMin,
//           boxes[i].start.yMax, boxes[i].end.x, boxes[i].end.yMin, boxes[i].end.yMax);
    xTicks.add(boxes[i].start.x, boxes[i].start.yMin);
    xTicks.add(boxes[i].start.x, boxes[i].start.yMax);
    xTicks.add(boxes[i].end.x, boxes[i].end.yMin);
    xTicks.add(boxes[i].end.x, boxes[i].end.yMax);
    yTicks.add(boxes[i].start.yMin, boxes[i].start.x);
    yTicks.add(boxes[i].start.yMax, boxes[i].start.x);
    yTicks.add(boxes[i].end.yMin, boxes[i].end.x);
    yTicks.add(boxes[i].end.yMax, boxes[i].end.x);
  }
  double xMax = 0.0, yMax = 0.0;
  int xStart = 0;
  int yStart = h-1 - f.lineSpacing();
  TickMap::const_iterator it = xTicks.begin();
  for (; it!=xTicks.end(); ++it) {
    xStart = TQMAX(xStart, f.width(it.data().s));
    xMax = TQMAX(xMax, it.key());
  }
  for (it = yTicks.begin(); it!=yTicks.end(); ++it)
    yMax = TQMAX(yMax, it.key());
  int xEnd = w-1 - f.width(xTicks[xMax].s)/2;
  TQRect rect = f.boundingRect(yTicks[yMax].s);
  int yEnd = rect.height()/2;

  // draw boxes
  p.setPen(TQt::lightGray);
  p.setBrush(TQt::lightGray);
  for (uint i=0; i<boxes.count(); i++) {
    double ax = double(xEnd - xStart)/xMax;
    double ay = double(yEnd - yStart)/yMax;
    TQPointArray pa(4);
    pa.setPoint(0, tqRound(ax*boxes[i].start.x), tqRound(ay*boxes[i].start.yMin));
    pa.setPoint(1, tqRound(ax*boxes[i].end.x), tqRound(ay*boxes[i].end.yMin));
    pa.setPoint(2, tqRound(ax*boxes[i].end.x), tqRound(ay*boxes[i].end.yMax));
    pa.setPoint(3, tqRound(ax*boxes[i].start.x), tqRound(ay*boxes[i].start.yMax));
    pa.translate(xStart, yStart);
    p.drawPolygon(pa);
  }

  // draw axis
  p.setPen(TQt::black);
  p.drawLine(xStart, yStart, w-1, yStart);
  p.drawLine(xStart, yStart, xStart, 0);

  // draw ticks and lines
  p.setPen(TQt::DotLine);
  for (it = yTicks.begin(); it!=yTicks.end(); ++it) {
    int y1 = yStart + tqRound(it.key()*(yEnd-yStart)/yMax);
    TQRect rect = f.boundingRect(it.data().s);
    p.drawText(xStart/2-rect.width()/2 , y1+rect.height()/2, it.data().s);
    int xmin = xStart + tqRound(it.data().min*(xEnd-xStart)/xMax);
    p.drawLine(xStart, y1, xmin, y1);
  }
  for (it = xTicks.begin(); it!=xTicks.end(); ++it) {
    int x1 = xStart + tqRound(it.key()*(xEnd-xStart)/xMax);
    TQRect rect = f.boundingRect(it.data().s);
    p.drawText(x1-rect.width()/2, h-1, it.data().s);
    int ymin = yStart + tqRound(it.data().min*(yEnd-yStart)/yMax);
    p.drawLine(x1, yStart, x1, ymin);
  }

  return pixmap;
}

TQPixmap Device::vddGraph(const TQString &xLabel, const TQString &yLabel,
                         const TQValueVector<Device::RangeBox> &boxes)
{
  uint sp = 10;
  TQPixmap graph = drawGraph(boxes);
  TQPainter p;
  TQFontMetrics f(p.font());
  TQPixmap pixmap(graph.width() + sp + f.width(xLabel), graph.height() + sp + f.lineSpacing());
  pixmap.fill(TQt::white);
  copyBlt(&pixmap, 0, f.lineSpacing() + sp, &graph, 0, 0, graph.width(), graph.height());
  p.begin(&pixmap);
  p.setPen(TQt::black);
  p.drawText(0, f.lineSpacing(), yLabel);
  p.drawText(pixmap.width()-1-f.width(xLabel), pixmap.height()-1, xLabel);
  return pixmap;
}

const Device::Package *Device::barPackage(const char *name, const Device::Data &data)
{
  for (uint i=0; i<data.packages().count(); i++)
    for (uint k=0; k<data.packages()[i].types.count(); k++)
      if ( Package::TYPE_DATA[data.packages()[i].types[k]].name==name ) return &data.packages()[i];
  return 0;
}

TQPixmap Device::pinsGraph(const Device::Package &package)
{
  TQPixmap pixmap;
  TQPainter p;
  TQFontMetrics fm(p.font());
  uint nb = package.pins.count();
  const int hspacing = 3, wspacing = 3, wmark = 10, wpin = 4;
  int theight = fm.ascent() + (fm.ascent()%2==0 ? 1 : 0);
  int height = hspacing + (nb/2)*(hspacing + theight);
  int wnumber = fm.width("1");
  wnumber = TQMAX(wnumber, fm.width(TQString::number(nb/2)));
  wnumber = TQMAX(wnumber, fm.width(TQString::number(nb/2+1)));
  wnumber = TQMAX(wnumber, fm.width(TQString::number(nb)));
  int bwidth = 4*wspacing + 2*wnumber + wmark;
  int lwidth = 0, rwidth = 0;
  for (uint k=0; k<nb/2; k++) {
    lwidth = TQMAX(lwidth, fm.width(package.pins[k]));
    rwidth = TQMAX(rwidth, fm.width(package.pins[nb-k-1]));
  }
  int bx = lwidth + wspacing + wpin;
  int width = bx + bwidth + wpin + wspacing + rwidth;
  pixmap.resize(width, height);
  pixmap.fill(TQt::white);
  p.begin(&pixmap);
  p.setPen(TQPen(TQt::black, 2));
  p.drawRect(bx, 1, bwidth, height-1);
  p.drawArc(bx+wspacing+wnumber+wspacing, -wmark/2+2, wmark, wmark, 0, -180*16);
  for (uint k=0; k<nb/2; k++) {
    int h = hspacing + theight/2 + k*(hspacing + theight);
    p.drawLine(bx-wpin-1, h, bx, h);
    p.drawLine(bx+bwidth, h, bx+bwidth+wpin, h);
    h += theight/2;
    TQString label = package.pins[k];
    p.drawText(bx-wpin-wspacing-fm.width(label), h, label);
    p.drawText(bx+bwidth+wpin+wspacing, h, package.pins[nb-k-1]);
    uint pin = (k+1);
    if ( pin==1 || pin==(nb/2) ) {
      p.drawText(bx+wspacing, h, TQString::number(pin));
      label = TQString::number(nb-k);
      p.drawText(bx+bwidth-wspacing-fm.width(label), h, label);
    }
  }
  p.end();
  return pixmap;
}

TQString Device::htmlInfo(const Device::Data &data, const TQString &deviceHref, const TQString &documentHtml)
{
  TQString doc;

  // title
  doc += "<h1>";
  bool first = true;
  FOR_EACH(Special, special) {
    for (uint k=0; k<data.frequencyRanges().count(); k++) {
      if ( data.frequencyRanges()[k].special!=special ) continue;
      if (first) first = false;
      else doc += " / ";
      doc += data.fname(special);
      break;
    }
  }
  doc += "</h1>";

  doc += "<table>";
  TQString status = coloredString(data.status().label(), statusColor(data.status()));
  doc += htmlTableRow(i18n("Status"), status);
  if ( data.alternatives().count() ) {
    TQString s;
    for (uint i=0; i<data.alternatives().count(); i++) {
      if ( i!=0 ) s += ", ";
      if ( deviceHref.isEmpty() ) s += data.alternatives()[i].upper();
      else {
        TQString href = deviceHref.arg(data.alternatives()[i].upper());
        s += TQString("<a href=\"%1\">%2</a>").arg(href).arg(data.alternatives()[i].upper());
      }
    }
    doc += htmlTableRow(i18n("Alternatives"), s);
  }
  doc += documentHtml;
  doc += "</table>";

  doc += "<hr />";
  doc += "<table>";
  doc += data.group().informationHtml(data);
  TQString s;
  for (uint i=0; i<data.packages().count(); i++)
    for (uint k=0; k<data.packages()[i].types.count(); k++)
      s += i18n(Package::TYPE_DATA[data.packages()[i].types[k]].label) + TQString("[%1] ").arg(data.packages()[i].pins.count());
  doc += htmlTableRow(i18n("Packaging"), s);
  doc += "</table>";

  return doc;
}

TQString Device::htmlPinDiagrams(const Device::Data &data, const TQString &imagePrefix, TQMimeSourceFactory *msf)
{
  TQString doc;
  // pins
  const Package *package = 0;
  for (uint i=0; Package::TYPE_DATA[i].name; i++) {
    if ( Package::TYPE_DATA[i].shape!=Package::Bar ) continue;
    package = barPackage(Package::TYPE_DATA[i].name, data);
    if (package) break;
  }
  if (package) {
    TQPixmap pix = pinsGraph(*package);
    doc += "<table cellpadding=\"3\"><tr bgcolor=\"gray\"><th align=\"center\">";
    for (uint k=0; k<package->types.count(); k++) {
      if ( k!=0 ) doc += " ";
      doc += i18n(Package::TYPE_DATA[package->types[k]].label);
      doc += "(" + TQString::number(package->pins.count()) + ")";
    }
    doc += "</th></tr><tr><td align=\"center\">";
    TQString label = data.name() + "_pins_graph.png";
    doc += "<img src=\"" + imagePrefix + label + "\" />";
    if (msf) msf->setPixmap(label, pix);
    doc += "</td></tr></table>";
  }
  return doc;
}

TQString Device::htmlVoltageFrequencyGraphs(const Device::Data &data, const TQString &imagePrefix, TQMimeSourceFactory *msf)
{
  TQString doc;
  FOR_EACH(Special, special) {
    for (uint k=0; k<data.frequencyRanges().count(); k++) {
      const Device::FrequencyRange &fr = data.frequencyRanges()[k];
      if ( fr.special!=special ) continue;
      doc += "<h3>" + data.fname(special) + " - " + i18n("Temperature range: ") + fr.operatingCondition.label() + "</h3>";
      TQString label = data.name() + "_" + data.fname(special) + "_"
        + fr.operatingCondition.key() + ".png";
      doc += "<img src=\"" + imagePrefix + label + "\" />";
      if (msf) msf->setPixmap(label, Device::vddGraph(i18n("F (MHz)"), i18n("Vdd (V)"), fr.vdds));
    }
  }
  return doc;
}

TQPixmap Device::memoryGraph(const TQValueList<MemoryGraphData> &r)
{
  TQValueList<MemoryGraphData> ranges = r;
  TQPixmap pixmap;
  TQPainter p;
  TQFontMetrics fm(p.font());
  // order
  qHeapSort(ranges);
  // add empty ranges
  TQValueList<MemoryGraphData>::iterator it;
  for (it=ranges.begin(); it!=ranges.end(); ) {
    TQValueList<MemoryGraphData>::iterator prev = it;
    ++it;
    if ( it==ranges.end() ) break;
    if ( (*prev).endAddress+1==(*it).startAddress ) continue;
    MemoryGraphData data;
    data.startAddress = (*prev).endAddress + 1;
    data.endAddress = (*it).startAddress-1;
    ranges.insert(it, data);
  }
  // compute widths and total height
  int theight = fm.ascent() + (fm.ascent()%2==0 ? 1 : 0);
  int hspacing = 5;
  int height = 1;
  int w1 = 0, w2 = 0;
  for (it=ranges.begin(); it!=ranges.end(); ++it) {
    w1 = TQMAX(w1, fm.width((*it).start));
    w1 = TQMAX(w1, fm.width((*it).end));
    w2 = TQMAX(w2, fm.width((*it).label));
    (*it).height = 2*hspacing + theight;
    if ( (*it).startAddress!=(*it).endAddress ) (*it).height += 2*theight;
    height += (*it).height;
  }
  int wspacing = 4;
  int width = wspacing + w1 + wspacing + wspacing + w2;
  pixmap.resize(width, height);
  pixmap.fill(TQt::white);
  p.begin(&pixmap);
  int h = 0;
  // draw ranges
  for (it=ranges.begin(); it!=ranges.end(); ++it) {
    p.setPen(TQPen(TQt::black, 1, TQt::DotLine));
    p.drawLine(0,h, width-1,h);
    p.setPen(TQPen(TQt::black, 1));
    p.setBrush((*it).label.isEmpty() ? TQt::gray : TQt::white);
    p.drawRect(0,h, wspacing+w1+wspacing,(*it).height+1);
    int hmid = h+(*it).height/2+theight/2;
    p.drawText(wspacing+w1+wspacing+wspacing,hmid, (*it).label);
    if ( (*it).startAddress==(*it).endAddress ) p.drawText(wspacing,hmid, (*it).start);
    else {
      p.drawText(wspacing,h+theight, (*it).start);
      p.drawText(wspacing,h+(*it).height-3, (*it).end);
    }
    h += (*it).height;
    p.setPen(TQPen(TQt::black, 1, TQt::DotLine));
    p.drawLine(0,h, width-1,h);
  }
  p.end();
  return pixmap;
}

#endif