File: VariableWin.cpp

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (209 lines) | stat: -rwxr-xr-x 7,540 bytes parent folder | download
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
/** Copyright (C) 2006, Ian Paul Larsen.
 **
 **  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.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License along
 **  with this program; if not, write to the Free Software Foundation, Inc.,
 **  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **/

#include "VariableWin.h"
#include "DataElement.h"
#include "MainWindow.h"

extern MainWindow * mainwin;

extern "C" {
extern char **symtable;
extern int numsyms;
}

VariableWin::VariableWin () {
    setColumnCount(3);
    setHeaderLabels(QStringList() << tr("Level - Name") << tr("Ty") << tr("Value"));
    resizeColumnToContents(COLUMNTYPE);
    sortByColumn(COLUMNNAME, Qt::AscendingOrder);
    setSortingEnabled(true);
    convert = new Convert(mainwin->locale);
    //hold translations to speed up things
    tr_stringType = tr("Type: String");
    tr_integerType = tr("Type: Integer");
    tr_floatType = tr("Type: Float");
    tr_arrayType = tr("Type: Array");
    tr_referenceType = tr("Type: Reference");
    tr_unknownType = tr("?");
}

VariableWin::~VariableWin() {
    delete(convert);
}

void VariableWin::setTypeAndValue(QTreeWidgetItem *r, DataElement *d) {
	// set thr type and value columns (2 and 3)
	QString var;
	if (r) {
		r->takeChildren();
		if (d!=NULL) {
			switch (d->type) {
			case T_STRING:{
				r->setText(COLUMNTYPE, QStringLiteral("S"));
				r->setData(COLUMNTYPE, Qt::ToolTipRole, tr_stringType);
				r->setData(COLUMNVALUE, Qt::UserRole + 1, d->stringval);
				r->setText(COLUMNVALUE, d->stringval);
				break;
			}
			case T_INT:{
				r->setText(COLUMNTYPE, QStringLiteral("I"));
				r->setData(COLUMNTYPE, Qt::ToolTipRole, tr_integerType);
				r->setData(COLUMNVALUE, Qt::UserRole + 1, (long long)d->intval);
				r->setText(COLUMNVALUE, convert->getString(d)); //to keep user choice about printing numbers
				break;
			}
			case T_FLOAT:{
				r->setText(COLUMNTYPE, QStringLiteral("F"));
				r->setData(COLUMNTYPE, Qt::ToolTipRole, tr_floatType);
				r->setData(COLUMNVALUE, Qt::UserRole + 1, d->floatval);
				r->setText(COLUMNVALUE, convert->getString(d)); //using user locale format
				break;
			}
			case T_REF:{
				r->setText(COLUMNTYPE, QStringLiteral("REF"));
				r->setData(COLUMNTYPE, Qt::ToolTipRole, tr_referenceType);
				var = QString::number(d->level) + QStringLiteral(" - ") + symtable[d->intval];
				r->setText(COLUMNVALUE, var);
				r->setData(COLUMNVALUE, Qt::UserRole + 1, var); //to do:00level+var
				break;
			}
			 case T_ARRAY:{
				r->setText(COLUMNTYPE, QStringLiteral("A"));
				r->setData(COLUMNTYPE, Qt::ToolTipRole, tr_arrayType);
				if (d->arrayRows()==1) {
					// 1d array
					var = QStringLiteral("[") + QString::number(d->arrayCols()) + QStringLiteral("]");
				} else {
					// 2d array
					var = QStringLiteral("[") + QString::number(d->arrayRows()) + QStringLiteral(",") + QString::number(d->arrayCols()) + QStringLiteral("]");
				}
				r->setText(COLUMNVALUE, var);
				r->setData(COLUMNVALUE, Qt::UserRole + 1, var); //to do:00level+var
				break;
			}
		   default:{
				r->setText(COLUMNTYPE, tr_unknownType);
				r->setData(COLUMNTYPE, Qt::ToolTipRole, QStringLiteral(""));
				r->setText(COLUMNVALUE, QStringLiteral(""));
				r->setData(COLUMNVALUE, Qt::UserRole + 1, QVariant());
			}
			}
		} else {
			r->setText(COLUMNTYPE, tr_unknownType);
			r->setData(COLUMNTYPE, Qt::ToolTipRole, QStringLiteral(""));
			r->setText(COLUMNVALUE, QStringLiteral(""));
			r->setData(COLUMNVALUE, Qt::UserRole + 1, QVariant());
		}
	}
}

void VariableWin::varWinAssign(Variables **variables, int varnum, int level, int x, int y) {
	// refresh an array's element
	QTreeWidgetItem *parentItem = NULL;
	QTreeWidgetItem *childItem = NULL;
	QString parentname, childname;
	DataElement *d;
	QList<QTreeWidgetItem *> list;


	if(*variables){
		Variable *v = (*variables)->getAt(varnum, level);
		parentname = QString::number(level) + QStringLiteral(" - ") + symtable[varnum];
		// see if v is on the list and change value or add
		list = findItems(parentname, Qt::MatchExactly | Qt::MatchRecursive, COLUMNNAME);

		if (list.size() > 0) {
			parentItem = (TreeWidgetItem *)list[0];
			// now find child or add
			if (v->data->arrayRows()==1) {
				childname = parentname + QStringLiteral("[") + QString::number(y) + QStringLiteral("]");
			} else {
				childname = parentname + QStringLiteral("[") + QString::number(x) + QStringLiteral(",") + QString::number(y) + QStringLiteral("]");
			}
			list = findItems(childname, Qt::MatchExactly | Qt::MatchRecursive, COLUMNNAME);
			if (list.size() > 0) {
				// get existing element
				childItem = (TreeWidgetItem *)list[0];
			} else {
				// create a new one
				childItem = new TreeWidgetItem();
				childItem->setText(COLUMNNAME, childname);
				childItem->setData(COLUMNNAME, Qt::UserRole + 1, childname);
				parentItem->addChild(childItem);
			}
			setTypeAndValue(childItem, v->data->arrayGetData(x,y));
		}
	}
}

void VariableWin::varWinAssign(Variables **variables, int varnum, int level) {
	// refresh variable/array content
	TreeWidgetItem *rowItem = NULL;
	QString name;

	if(*variables){
		Variable *v = (*variables)->getAt(varnum, level);
		name = QString::number(level) + QStringLiteral(" - ") + symtable[varnum];

		// see if v is on the list and change value or add
		QList<QTreeWidgetItem *> list = findItems(name, Qt::MatchExactly | Qt::MatchRecursive, COLUMNNAME);

		if (list.size() > 0) {
			// get existing element
			rowItem = (TreeWidgetItem *)list[0];
		} else {
			// create a new one
			rowItem = new TreeWidgetItem();
			rowItem->setText(COLUMNNAME, name);
			QString id = QString("%1").arg(v->data->level, 7, 10, QChar('0')) + symtable[varnum];
			rowItem->setData(COLUMNNAME, Qt::UserRole + 1, id);
			addTopLevelItem(rowItem);
		}
		setTypeAndValue(rowItem, v->data);
		//
		// if an array set initial values if any
		if (v->data->type==T_ARRAY) {
			for(int x = 0; x<v->data->arr->xdim;x++) {
				for(int y = 0; y<v->data->arr->ydim;y++) {
					DataElement *q = v->data->arrayGetData(x,y);
					DataElement::getError(true);
					if (q->type!=T_UNASSIGNED) {
						varWinAssign(variables, varnum, level, x, y);
					}
				}  
			}  
		}
	}
}

void VariableWin::varWinDropLevel(int level) {
    // when we return from a subroutine or function delete its variables
    QString name = QString::number(level) + QStringLiteral(" ");
    QList<QTreeWidgetItem *> items = findItems(name, Qt::MatchStartsWith, COLUMNNAME);
    for (int n=items.size()-1; n>=0; n--) {
        delete items[n];
    }
}

void VariableWin::clear() {
    // when window is cleared (Interpreter restarts) we reload convert settings
    // to make sure that convert will reflect the current settings (user may change those any time)
    delete (convert);
    convert = new Convert(mainwin->locale);
    QTreeWidget::clear();
}