Qucs-GUI  0.0.18
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
truthdiagram.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  truthdiagram.cpp
3  ------------------
4  begin : Sat Nov 12 2005
5  copyright : (C) 2005 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include <QtGui>
18 #include "truthdiagram.h"
19 #include "main.h"
20 
21 #include <math.h>
22 #include "qucs.h"
23 #include "schematic.h"
24 
25 
26 TruthDiagram::TruthDiagram(int _cx, int _cy) : TabDiagram(_cx, _cy)
27 {
28  x1 = 0; // no extension to select area
29  y1 = 0;
30  x2 = x3 = 150; // initial size of diagram
31  y2 = 200;
32  Name = "Truth";
33  xAxis.limit_min = 0.0; // scroll bar position (needs to be saved in file)
34 
35  calcDiagram();
36 }
37 
39 {
40 }
41 
42 // ------------------------------------------------------------
43 // calculates the text in the tabular
45 {
46  Lines.clear();
47  Texts.clear();
48  Arcs.clear();
49 
50  x1 = 0; // no scroll bar
51  x3 = x2;
52  QFontMetrics metrics(((Schematic*)QucsMain->DocumentTab->currentPage())->font()); // get size of text
53  int tHeight = metrics.lineSpacing();
54  QString Str;
55  int colWidth=0, x=6, y;
56 
57  if(y2 < (41 + MIN_SCROLLBAR_SIZE))
58  y2 = 41 + MIN_SCROLLBAR_SIZE;
59 
60  if(y2 < (tHeight + 8))
61  y2 = tHeight + 8;
62  y = y2 - tHeight - 6;
63 
64  // outer frame
65  Lines.append(new Line(0, y2, x2, y2, QPen(Qt::black,0)));
66  Lines.append(new Line(0, y2, 0, 0, QPen(Qt::black,0)));
67  Lines.append(new Line(x2, y2, x2, 0, QPen(Qt::black,0)));
68  Lines.append(new Line(0, 0, x2, 0, QPen(Qt::black,0)));
69  Lines.append(new Line(0, y+2, x2, y+2, QPen(Qt::black,2)));
70 
71  if(xAxis.limit_min < 0.0)
72  xAxis.limit_min = 0.0;
73 
74  Graph *firstGraph;
75  Graph *g = Graphs.first();
76  if(g == 0) { // no variables specified in diagram ?
77  Str = QObject::tr("no variables");
78  colWidth = checkColumnWidth(Str, metrics, colWidth, x, y2);
79  if(colWidth >= 0)
80  Texts.append(new Text(x-4, y2-2, Str)); // independent variable
81  return 0;
82  }
83 
84 
85  int NumAll=0; // how many numbers per column
86  int NumLeft=0; // how many numbers could not be written
87 
88  char *py;
89  int counting, invisibleCount=0;
90  int startWriting, z;
91 
92  while(g->cPointsX.isEmpty()) { // any graph with data ?
93  g = Graphs.next();
94  if(g == 0) break;
95  }
96 if(g) if(!g->cPointsX.isEmpty()) {
97  // ................................................
98  NumAll = g->cPointsX.getFirst()->count * g->countY; // number of values
99 
100  invisibleCount = NumAll - y/tHeight;
101  if(invisibleCount <= 0) xAxis.limit_min = 0.0;// height bigger than needed
102  else {
103  if(invisibleCount < int(xAxis.limit_min + 0.5))
104  xAxis.limit_min = double(invisibleCount); // adjust limit of scroll bar
105  NumLeft = invisibleCount - int(xAxis.limit_min + 0.5);
106  }
107 
108 
109  colWidth = 0;
110  Texts.append(new Text(x-4, y2-2, Str)); // independent variable
111  if(NumAll != 0) {
112  z = metrics.width("1");
113  colWidth = metrics.width("0");
114  if(z > colWidth) colWidth = z;
115  colWidth += 2;
116  counting = int(log(double(NumAll)) / log(2.0) + 0.9999); // number of bits
117 
118  if((x+colWidth*counting) >= x2) { // enough space for text ?
119  checkColumnWidth("0", metrics, 0, x2, y);
120  goto funcEnd;
121  }
122 
123  y = y2-tHeight-5;
124  startWriting = x;
125  for(z=int(xAxis.limit_min + 0.5); z<NumAll; z++) {
126  if(y < tHeight) break; // no room for more rows ?
127  startWriting = x;
128  for(int zi=counting-1; zi>=0; zi--) {
129  if(z & (1 << zi)) Str = "1";
130  else Str = "0";
131  Texts.append(new Text( startWriting, y, Str));
132  startWriting += colWidth;
133  }
134  y -= tHeight;
135  }
136  x = startWriting + 15;
137  }
138  Lines.append(new Line(x-8, y2, x-8, 0, QPen(Qt::black,2)));
139 
140 } // of "if no data in graphs"
141 
142 
143  int zi, digitWidth;
144  firstGraph = g;
145  // ................................................
146  // all dependent variables
147  for(g = Graphs.first(); g!=0; g = Graphs.next()) {
148  y = y2-tHeight-5;
149 
150  Str = g->Var;
151  colWidth = checkColumnWidth(Str, metrics, 0, x, y2);
152  if(colWidth < 0) goto funcEnd;
153  Texts.append(new Text(x, y2-2, Str)); // dependent variable
154 
155 
156  startWriting = int(xAxis.limit_min + 0.5); // when to reach visible area
157  if(g->cPointsX.getFirst()) {
158 
159  if(sameDependencies(g, firstGraph)) {
160 
161  if(g->Var.right(2) != ".X") { // not a digital variable ?
162  double *pdy = g->cPointsY - 2;
163  for(z = NumAll; z>0; z--) {
164  pdy += 2;
165  if(startWriting-- > 0) continue; // reached visible area ?
166  if(y < tHeight) break; // no room for more rows ?
167  Str = QString::number(sqrt((*pdy)*(*pdy) + (*(pdy+1))*(*(pdy+1))));
168 
169  colWidth = checkColumnWidth(Str, metrics, colWidth, x, y);
170  if(colWidth < 0) goto funcEnd;
171 
172  Texts.append(new Text(x, y, Str));
173  y -= tHeight;
174  }
175  }
176 
177  else { // digital variable !!!
178  py = (char*)g->cPointsY;
179  counting = strlen((char*)py); // count number of "bits"
180 
181  digitWidth = metrics.width("X") + 2;
182  if((x+digitWidth*counting) >= x2) { // enough space for "bit vector" ?
183  checkColumnWidth("0", metrics, 0, x2, y);
184  goto funcEnd;
185  }
186 
187  for(z = NumAll; z>0; z--) {
188  if(startWriting-- > 0) { // reached visible area ?
189  py += counting + 1;
190  continue;
191  }
192  if(y < tHeight) break; // no room for more rows ?
193 
194  zi = 0;
195  while(*py) {
196  Str = *(py++);
197  Texts.append(new Text(x + zi, y, Str));
198  zi += digitWidth;
199  }
200  py++;
201  y -= tHeight;
202  }
203 
204  digitWidth *= counting;
205  if(colWidth < digitWidth)
206  colWidth = digitWidth;
207  }
208 
209  } // of "if(sameDeps)"
210  else {
211  Str = QObject::tr("wrong dependency");
212  colWidth = checkColumnWidth(Str, metrics, colWidth, x, y);
213  if(colWidth < 0) goto funcEnd;
214  Texts.append(new Text(x, y, Str));
215  }
216  }
217  else { // no data in graph
218  Str = QObject::tr("no data");
219  colWidth = checkColumnWidth(Str, metrics, colWidth, x, y);
220  if(colWidth < 0) goto funcEnd;
221  Texts.append(new Text(x, y, Str));
222  }
223  x += colWidth+15;
224  if(g != Graphs.getLast()) // do not paint last line
225  Lines.append(new Line(x-8, y2, x-8, 0, QPen(Qt::black,0)));
226  }
227 
228 funcEnd:
229  if(invisibleCount > 0) { // could all numbers be written ?
230  x1 = 18; // extend the select area to the left
231 
232  zAxis.limit_max = double(NumAll); // number of data (rows)
233 
234  // calculate data for painting scroll bar
235  y = int(xAxis.limit_min + 0.5);
236  NumLeft = NumAll - NumLeft - y;
237 
238  // position of scroll bar in pixel
239  yAxis.numGraphs = (y2 - 39) * y / NumAll;
240 
241  // height of scroll bar
242  zAxis.numGraphs = (y2 - 39) * NumLeft / NumAll;
245  * y / NumAll;
247  }
248 
249  xAxis.numGraphs = NumLeft; // number of lines in the diagram
250  }
251 
252  return 1;
253 }
254 
255 // ------------------------------------------------------------
257 {
258  return new TruthDiagram();
259 }
260 
261 // ------------------------------------------------------------
262 Element* TruthDiagram::info(QString& Name, char* &BitmapFile, bool getNewOne)
263 {
264  Name = QObject::tr("Truth Table");
265  BitmapFile = (char *) "truth";
266 
267  if(getNewOne) return new TruthDiagram();
268  return 0;
269 }
Axis xAxis
Definition: diagram.h:101
int countY
Definition: graph.h:74
int y1
Definition: element.h:153
Definition: graph.h:57
QString Name
Definition: diagram.h:92
TruthDiagram(int _cx=0, int _cy=0)
#define MIN_SCROLLBAR_SIZE
Definition: diagram.h:31
int y2
Definition: element.h:153
int x1
Definition: element.h:153
bool sameDependencies(Graph *, Graph *)
Definition: diagram.cpp:1136
double limit_min
Definition: diagram.h:56
QString Var
Definition: graph.h:75
Definitions and declarations for the main application.
QucsApp * QucsMain
Definition: main.cpp:54
int numGraphs
Definition: diagram.h:52
Axis zAxis
Definition: diagram.h:101
Q3PtrList< Graph > Graphs
Definition: diagram.h:95
Q3PtrList< struct Arc > Arcs
Definition: diagram.h:96
Definition: element.h:82
int x3
Definition: diagram.h:100
int checkColumnWidth(const QString &, const QFontMetrics &, int, int, int)
Definition: diagram.cpp:1154
Definition: element.h:48
Superclass of all schematic drawing elements.
Definition: element.h:142
double limit_max
Definition: diagram.h:56
QTabWidget * DocumentTab
Definition: qucs.h:164
Q3PtrList< Text > Texts
Definition: diagram.h:98
static Element * info(QString &, char *&, bool getNewOne=false)
Axis yAxis
Definition: diagram.h:101
Diagram * newOne()
Q3PtrList< DataX > cPointsX
Definition: graph.h:71
int x2
Definition: element.h:153
double * cPointsY
Definition: graph.h:72
Q3PtrList< Line > Lines
Definition: diagram.h:97