1 #include <QtGlobal>
2 #include <QRadioButton>
3 #include "Preferences.h"
4 
5 #include "LDViewErrors.h"
6 
7 #include <LDLoader/LDLError.h>
8 #include <TCFoundation/TCStringArray.h>
9 #include <TCFoundation/mystring.h>
10 #include <TCFoundation/TCLocalStrings.h>
11 #include <TCFoundation/TCUserDefaults.h>
12 #include <LDLib/LDUserDefaultsKeys.h>
13 
14 #include <QLabel>
15 #include <QStatusBar>
16 #include <QRegExp>
17 #include <QTreeWidgetItem>
18 #include <QHeaderView>
19 #include "misc.h"
20 
LDViewErrors(QWidget * parent,Preferences * preferences)21 LDViewErrors::LDViewErrors(QWidget *parent, Preferences *preferences)
22 	:QMainWindow(parent),ErrorPanel(),
23 	preferences(preferences),
24 	errors(new LDLErrorArray),
25 	listViewPopulated(false)
26 {
27 	setupUi(this);
28     connect( generalErrorButton, SIGNAL( clicked() ), this, SLOT( generalError() ) );
29     connect( parseErrorButton, SIGNAL( clicked() ), this, SLOT( parseError() ) );
30     connect( fileNotFoundButton, SIGNAL( clicked() ), this, SLOT( fileNotFound() ) );
31     connect( singularMatrixButton, SIGNAL( clicked() ), this, SLOT( singularMatrix() ) );
32     connect( partDeterminantButton, SIGNAL( clicked() ), this, SLOT( partDeterminant() ) );
33     connect( concaveQuadButton, SIGNAL( clicked() ), this, SLOT( concaveQuad() ) );
34     connect( badVertexOrderButton, SIGNAL( clicked() ), this, SLOT( badVertexOrder() ) );
35     connect( colinearPointsButton, SIGNAL( clicked() ), this, SLOT( colinearPoints() ) );
36     connect( MPDErrorButton, SIGNAL( clicked() ), this, SLOT( MPDError() ) );
37     connect( whitespaceButton, SIGNAL( clicked() ), this, SLOT( whitespace() ) );
38     connect( partrenamedButton, SIGNAL( clicked() ), this, SLOT( partrenamed() ) );
39     connect( unofficialPartButton, SIGNAL( clicked() ), this, SLOT( unofficialpart() ) );
40     connect( modelLoopButton, SIGNAL( clicked() ), this, SLOT( modelLoop() ) );
41     connect( metaCommandProblemButton, SIGNAL( clicked() ), this, SLOT( metaCommand() ) );
42     connect( showWarningsButton, SIGNAL( clicked() ), this, SLOT( showWarnings() ) );
43     connect( identicalVerticesButton, SIGNAL( clicked() ), this, SLOT( identicalVertices() ) );
44     connect( BFCErrorButton, SIGNAL( clicked() ), this, SLOT( BFCError() ) );
45     connect( BFCWarningButton, SIGNAL( clicked() ), this, SLOT( BFCWarning() ) );
46     connect( showAllButton, SIGNAL( clicked() ), this, SLOT( showAllError() ) );
47     connect( showNoneButton, SIGNAL( clicked() ), this, SLOT( showNoneError() ) );
48     connect( nonFlatQuadButton, SIGNAL( clicked() ), this, SLOT( nonFlatQuad() ) );
49 
50 //	errorListView->setColumnWidthMode(0, QListView::Maximum);
51 	errorListView->header()->hide();
52 //	errorListView->setSorting(-1);
53 	reflectSettings();
54 	messageText = new QLabel(statusBar());
55 	statusBar()->addWidget(messageText,1);
56 }
57 
reflectSettings(void)58 void LDViewErrors::reflectSettings(void)
59 {
60 	preferences->setButtonState(generalErrorButton,
61 		preferences->getShowError(LDLEGeneral));
62         preferences->setButtonState(parseErrorButton,
63 		preferences->getShowError(LDLEParse));
64 	preferences->setButtonState(fileNotFoundButton,
65 		preferences->getShowError(LDLEFileNotFound));
66 //	preferences->setButtonState(colorErrorButton,
67 //		preferences->getShowError(LDLEColor));
68 	preferences->setButtonState(singularMatrixButton,
69 		preferences->getShowError(LDLEMatrix));
70 	preferences->setButtonState(partDeterminantButton,
71 		preferences->getShowError(LDLEPartDeterminant));
72 	preferences->setButtonState(concaveQuadButton,
73 		preferences->getShowError(LDLEConcaveQuad));
74 	preferences->setButtonState(badVertexOrderButton,
75 		preferences->getShowError(LDLEVertexOrder));
76 //	preferences->setButtonState(concaveQuadSplitButton,
77 //		preferences->getShowError(LDLEConcaveQuadSplit));
78 	preferences->setButtonState(colinearPointsButton,
79 		preferences->getShowError(LDLEColinear));
80 //	preferences->setButtonState(openGLErrorButton,
81 //		preferences->getShowError(LDLEOpenGL));
82 	preferences->setButtonState(BFCWarningButton,
83 		preferences->getShowError(LDLEBFCWarning));
84 	preferences->setButtonState(BFCErrorButton,
85 		preferences->getShowError(LDLEBFCError));
86 	preferences->setButtonState(MPDErrorButton,
87 		preferences->getShowError(LDLEMPDError));
88 	preferences->setButtonState(whitespaceButton,
89 		preferences->getShowError(LDLEWhitespace));
90 	preferences->setButtonState(partrenamedButton,
91 		preferences->getShowError(LDLEMovedTo));
92 	preferences->setButtonState(unofficialPartButton,
93 		preferences->getShowError(LDLEUnofficialPart));
94 	preferences->setButtonState(modelLoopButton,
95 		preferences->getShowError(LDLEModelLoop));
96 	preferences->setButtonState(metaCommandProblemButton,
97 		preferences->getShowError(LDLEMetaCommand));
98 	preferences->setButtonState(showWarningsButton,
99 		TCUserDefaults::longForKey(SHOW_WARNINGS_KEY, 0) ? 1 : 0);
100 	preferences->setButtonState(identicalVerticesButton,
101 		preferences->getShowError(LDLEMatchingPoints));
102 	preferences->setButtonState(nonFlatQuadButton,
103 		preferences->getShowError(LDLENonFlatQuad));
104 }
105 
setValues(bool value)106 void LDViewErrors::setValues(bool value)
107 {
108 	for(int b=LDLEFirstError;b<=LDLELastError;b++)
109 	{
110 		preferences->setShowError(b,value);
111 	}
112 }
113 
showWarnings(void)114 void LDViewErrors::showWarnings(void)
115 {
116 	 TCUserDefaults::setLongForKey(TCUserDefaults::longForKey(SHOW_WARNINGS_KEY, 0) ? 0 :1 ,
117 		SHOW_WARNINGS_KEY);
118         clearListView();
119         populateListView();
120 }
121 
~LDViewErrors(void)122 LDViewErrors::~LDViewErrors(void)
123 {
124 	if (errors)
125 	{
126 		errors->release();
127 	}
128 }
129 
clear(void)130 void LDViewErrors::clear(void)
131 {
132 	errors->removeAll();
133 	clearListView();
134 }
135 
clearListView(void)136 void LDViewErrors::clearListView(void)
137 {
138 	errorListView->clear();
139 	listViewPopulated = false;
140 }
141 
addError(LDLError * error)142 void LDViewErrors::addError(LDLError *error)
143 {
144 	errors->addObject(error);
145 }
146 
populateListView(void)147 int LDViewErrors::populateListView(void)
148 {
149 	int errorCount = 0;
150 	int warningCount = 0;
151 	QString buf;
152 
153 	if (!listViewPopulated)
154 	{
155 		int i;
156 		int count = errors->getCount();
157 
158 		for (i = count - 1; i >= 0; i--)
159 		{
160 			LDLError *error = (*errors)[i];
161 			if (addErrorToListView((*errors)[i]))
162 			{
163 				if (error->getLevel() == LDLAWarning)
164 				{
165 					warningCount++;
166 				}
167 				else
168 				{
169 					errorCount++;
170 				}
171 			}
172 		}
173 		listViewPopulated = true;
174     	if (errorCount > 0)
175     	{
176         	if (errorCount == 1)
177         	{
178             	buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeOneError"));
179         	}
180         	else
181         	{
182             	buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeNErrors"));
183 #if QT_VERSION >= 0x60000
184 				buf.replace(QRegularExpression("%."), QString::number(errorCount));
185 #else
186 				buf.replace(QRegExp("%."), QString::number(errorCount));
187 #endif
188         	}
189         	if (warningCount > 0)
190         	{
191             	buf += ", ";
192         	}
193     	}
194     	if (warningCount > 0)
195     	{
196         	if (warningCount == 1)
197         	{
198             	buf += QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeOneWarning"));
199         	}
200         	else
201         	{
202                	buf += QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeNWarnings"));
203 #if QT_VERSION >= 0x60000
204 				buf.replace(QRegularExpression("%."), QString::number(warningCount));
205 #else
206 				buf.replace(QRegExp("%."), QString::number(warningCount));
207 #endif
208         	}
209     	}
210 		messageText->setText(buf);
211 	}
212 	return errorCount;
213 }
214 
addErrorToListView(LDLError * error)215 bool LDViewErrors::addErrorToListView(LDLError *error)
216 {
217 	const char *string;
218 	QTreeWidgetItem *parent;
219 	QString buf, qstring;
220 	if (!TCUserDefaults::longForKey(SHOW_WARNINGS_KEY, 0) &&
221 		(error->getLevel() == LDLAWarning))
222 	{
223 		return false;
224 	}
225 	if (!showsErrorType(error->getType()))
226 	{
227 		return false;
228 	}
229 	wcstoqstring(qstring, error->getWMessage());
230 	parent = addErrorLine(NULL, qstring, error);
231 	if (parent)
232 	{
233 		string = error->getFilename();
234 		if (string)
235 		{
236 			buf = QString("File: ")+string;
237 		}
238 		else
239 		{
240 			buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeUnknownFile"));
241 		}
242 		addErrorLine(parent, buf, error);
243 		string = error->getFormattedFileLine();
244 		if (string)
245 		{
246 			int lineNumber = error->getLineNumber();
247 
248 			if (lineNumber > 0)
249 			{
250 				buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeLine#"));
251 #if QT_VERSION >= 0x60000
252 				buf.replace(QRegularExpression("%d"), QString::number(lineNumber));
253 #else
254 				buf.replace(QRegExp("%d"), QString::number(lineNumber));
255 #endif
256 			}
257 			else
258 			{
259 				buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeUnknownLine#"));
260 			}
261 			addErrorLine(parent, buf, error);
262 			char *tempString = copyString(string);
263 			stripCRLF(tempString);
264 			buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeLine"));
265 #if QT_VERSION >= 0x60000
266 			buf.replace(QRegularExpression("%s"),QString(tempString));
267 #else
268 			buf.replace(QRegExp("%s"),QString(tempString));
269 #endif
270 			delete tempString;
271 		}
272 		else
273 		{
274 			buf = QString::fromWCharArray(TCLocalStrings::get(L"ErrorTreeUnknownLine"));
275 		}
276 		addErrorLine(parent, buf, error);
277 		ucstringVector extraInfo = error->getUCExtraInfo();
278 		if (extraInfo.size() > 0)
279 		{
280 			for (ucstringVector::iterator it = extraInfo.begin();
281 				 it != extraInfo.end(); it++)
282 			{
283 				QString tmp;
284 				ucstringtoqstring(tmp, *it);
285 				addErrorLine(parent, tmp, error);
286 			}
287 		}
288 	}
289 	return true;
290 }
291 
showsErrorType(LDLErrorType errorType)292 bool LDViewErrors::showsErrorType(LDLErrorType errorType)
293 {
294 	return preferences->getShowError(errorType);
295 }
296 
addErrorLine(QTreeWidgetItem * parent,QString line,LDLError * error,int)297 QTreeWidgetItem *LDViewErrors::addErrorLine(QTreeWidgetItem *parent,
298 	QString line, LDLError * error, int /*imageIndex*/)
299 {
300 	QTreeWidgetItem *item;
301 
302 	if (parent)
303 	{
304 		if (parent->childCount() > 0)
305 		{
306 			QTreeWidgetItem *lastChild = parent->child(parent->childCount()-1);
307 			item = new QTreeWidgetItem(parent, lastChild);
308 		}
309 		else
310 		{
311 			item = new QTreeWidgetItem(parent);
312 		}
313 		item->setText(0, line);
314 		item->setIcon(0, QIcon( ":/images/Icons/error_info.png" ));
315 	}
316 	else
317 	{
318 		item = new QTreeWidgetItem(errorListView);
319 		item->setText(0, line);
320         switch (error->getType())
321         {
322             case LDLEGeneral:
323             case LDLEBFCError:
324 			case LDLEBFCWarning:
325             case LDLEMPDError:
326 		    case LDLEWhitespace:
327 			case LDLEMetaCommand:
328             case LDLEParse:
329                     item->setIcon(0,
330                         QIcon( ":/images/Icons/error_parse.png"));
331                     break;
332             case LDLEMatrix:
333                     item->setIcon(0,
334                         QIcon( ":/images/Icons/error_matrix.png"));
335                     break;
336             case LDLEFileNotFound:
337                     item->setIcon(0,
338                         QIcon( ":/images/Icons/error_fnf.png"));
339                     break;
340             case LDLEMatchingPoints:
341                     item->setIcon(0,
342                         QIcon( ":/images/Icons/error_matching_points.png"));
343                     break;
344             case LDLEConcaveQuad:
345                     item->setIcon(0,
346                         QIcon( ":/images/Icons/error_concave_quad.png"));
347                     break;
348             case LDLEColinear:
349                     item->setIcon(0,
350                         QIcon( ":/images/Icons/error_colinear.png"));
351                     break;
352             case LDLEVertexOrder:
353                     item->setIcon(0,
354                         QIcon( ":/images/Icons/error_vertex_order.png"));
355                     break;
356             case LDLENonFlatQuad:
357                     item->setIcon(0,
358                         QIcon( ":/images/Icons/error_non_flat_quad.png"));
359                     break;
360             case LDLEPartDeterminant:
361                     item->setIcon(0,
362                         QIcon( ":/images/Icons/error_determinant.png"));
363                     break;
364 			case LDLEMovedTo:
365 			case LDLEUnofficialPart:
366 					item->setIcon(0,
367                         QIcon( ":/images/Icons/error_info.png"));
368 					break;
369 			case LDLEModelLoop:
370 					item->setIcon(0,
371 						QIcon( ":/images/Icons/error_loop.png"));
372 					break;
373         }
374 	}
375 	return item;
376 }
377 
doErrorClick(QCheckBox * button,LDLErrorType errorType)378 void LDViewErrors::doErrorClick(QCheckBox *button, LDLErrorType errorType)
379 {
380 	preferences->setShowError(errorType, button->checkState());
381 	clearListView();
382 	populateListView();
383 }
384 
showAllError()385 void LDViewErrors::showAllError()
386 {
387 	setValues(true);
388 	reflectSettings();
389 	clearListView();
390 	populateListView();
391 }
392 
showNoneError()393 void LDViewErrors::showNoneError()
394 {
395 	setValues(false);
396 	reflectSettings();
397 	clearListView();
398 	populateListView();
399 }
400 
401