Skip to content

Commit 7b8a4e4

Browse files
committed
Reimplimented graphic view
Also bugfixes Also stuff
1 parent 486e852 commit 7b8a4e4

File tree

6 files changed

+129
-82
lines changed

6 files changed

+129
-82
lines changed

hpgl_cmd.cpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
hpgl_cmd::hpgl_cmd()
1212
{
13-
opcode = "NA";
14-
verts.clear();
13+
_opcode = "NA";
14+
coordList.clear();
1515
pen = 0;
1616
}
1717

@@ -23,22 +23,22 @@ hpgl_cmd::hpgl_cmd(QString text)
2323
qDebug() << "Processing command: ";
2424

2525
// Get opcode, first two characters
26-
opcode = text.mid(0, 2);
26+
_opcode = text.mid(0, 2);
2727

2828
// Parse opcode
29-
if (opcode == "IN")
29+
if (_opcode == "IN")
3030
{
3131
// Just opcode
3232
qDebug() << "IN";
3333
}
34-
else if (opcode == "SP")
34+
else if (_opcode == "SP")
3535
{
3636
pen = text.mid(2,1).toInt();
3737
qDebug() << "SP[" << QString::number(pen) << "]";
3838
}
39-
else if (opcode == "PU" || opcode == "PD")
39+
else if (_opcode == "PU" || _opcode == "PD")
4040
{
41-
qDebug() << opcode;
41+
qDebug() << _opcode;
4242
// need coords
4343
text.remove(0,2);
4444
int commaCount = text.count(',');
@@ -51,7 +51,7 @@ hpgl_cmd::hpgl_cmd(QString text)
5151
int newY = text.section(',', i, i).toInt();
5252
//i++;
5353
qDebug() << "Found x: " << newX << " y: " << newY;
54-
verts.push_back(hpgl_coord(newX, newY));
54+
coordList.push_back(hpgl_coord(newX, newY));
5555
}
5656
}
5757
}
@@ -61,6 +61,32 @@ hpgl_cmd::~hpgl_cmd()
6161
//
6262
}
6363

64+
QString hpgl_cmd::opcode()
65+
{
66+
return(_opcode);
67+
}
68+
69+
QList<hpgl_coord> hpgl_cmd::get_verts()
70+
{
71+
return coordList;
72+
}
73+
74+
QList<QLine> hpgl_cmd::line_list()
75+
{
76+
QList<QLine> lineList;
77+
lineList.clear();
78+
int x1, y1, x2, y2;
79+
for (int i = 0; i < (coordList.length()-1); i++)
80+
{
81+
x1 = coordList[i].get_x();
82+
y1 = coordList[i].get_y();
83+
x2 = coordList[i+1].get_x();
84+
y2 = coordList[i+1].get_y();
85+
lineList.push_back(QLine(x1, y1, x2, y2));
86+
}
87+
return lineList;
88+
}
89+
6490
int hpgl_cmd::printLen()
6591
{
6692
QString check = print();
@@ -70,18 +96,18 @@ int hpgl_cmd::printLen()
7096
QString hpgl_cmd::print()
7197
{
7298
QString retval = "";
73-
retval += opcode;
74-
if (opcode == "SP")
99+
retval += _opcode;
100+
if (_opcode == "SP")
75101
{
76102
retval += QString::number(pen);
77103
}
78104
else
79105
{
80-
for (int i = 0; i < verts.length(); i++)
106+
for (int i = 0; i < coordList.length(); i++)
81107
{
82-
hpgl_coord tmp = verts[i];
108+
hpgl_coord tmp = coordList[i];
83109
retval += tmp.print();
84-
if (i < (verts.length()-1))
110+
if (i < (coordList.length()-1))
85111
{
86112
retval += ",";
87113
}

hpgl_cmd.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#ifndef HPGLCMD_H
22
#define HPGLCMD_H
33

4-
#include <QDebug>
5-
#include <QString>
64
#include <QList>
5+
#include <QLine>
6+
#include <QPen>
7+
78
#include <string>
89

910
#include "hpgl_coord.h"
@@ -24,15 +25,17 @@ class hpgl_cmd
2425
void set_verts(QList<hpgl_coord> newVerts);
2526
QList<hpgl_coord> get_verts();
2627
QString print();
28+
QString opcode();
2729
int printLen();
30+
QList<QLine> line_list();
2831

2932
public slots:
3033
//
3134

3235
protected:
3336
//QChar opcode[2];
34-
QString opcode;
35-
QList<hpgl_coord> verts;
37+
QString _opcode;
38+
QList<hpgl_coord> coordList;
3639
int pen;
3740
};
3841

hpgl_obj.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,51 @@ hpgl_obj::~hpgl_obj()
3131
//
3232
}
3333

34+
QList<QLine> hpgl_obj::line_list_up()
35+
{
36+
QList<QLine> lineList;
37+
lineList.clear();
38+
QPoint lastPoint = QPoint(0, 0);
39+
for (int i = 0; i < cmdList.length(); i++)
40+
{
41+
if (cmdList[i].opcode() == "PU")
42+
{
43+
if (cmdList[i].line_list().isEmpty())
44+
{
45+
hpgl_cmd tmp = cmdList.at(i);
46+
int end_x = tmp.get_verts().last().get_x();
47+
int end_y = tmp.get_verts().last().get_y();
48+
QLine implicitLine = QLine(lastPoint.x(), lastPoint.y(),end_x , end_y);
49+
lineList.push_back(implicitLine);
50+
}
51+
else if (lastPoint != cmdList[i].line_list().last().p1())
52+
{
53+
lineList.push_back(QLine(lastPoint, cmdList[i].line_list().last().p1()));
54+
}
55+
lineList += cmdList[i].line_list();
56+
}
57+
if (!lineList.isEmpty())
58+
{
59+
lastPoint = lineList.last().p2();
60+
}
61+
}
62+
return lineList;
63+
}
64+
65+
QList<QLine> hpgl_obj::line_list_down()
66+
{
67+
QList<QLine> lineList;
68+
lineList.clear();
69+
for (int i = 0; i < cmdList.length(); i++)
70+
{
71+
if (cmdList[i].opcode() == "PD")
72+
{
73+
lineList += cmdList[i].line_list();
74+
}
75+
}
76+
return lineList;
77+
}
78+
3479
int hpgl_obj::printLen()
3580
{
3681
int retval = 0;

hpgl_obj.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#ifndef HPGLOBJ_H
22
#define HPGLOBJ_H
33

4-
#include <QDebug>
5-
#include <QString>
6-
#include <QList>
7-
84
#include "hpgl_cmd.h"
95

106
namespace std {
@@ -19,6 +15,8 @@ class hpgl_obj
1915
~hpgl_obj();
2016
QString print();
2117
int printLen();
18+
QList<QLine> line_list_up();
19+
QList<QLine> line_list_down();
2220

2321
public slots:
2422
//

mainwindow.cpp

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -255,71 +255,46 @@ void MainWindow::do_loadFile()
255255
}
256256
inputFile.setFileName(ui->lineEdit_filePath->text());
257257
inputFile.open(QIODevice::ReadOnly);
258-
items.clear();
258+
objList.clear();
259259
ui->textBrowser_read->clear();
260-
// while (!inputFile.atEnd())
261-
// {
262-
// QString buffer = "";
263-
// char tmp = 0;
264-
// while (tmp != ';' && !inputFile.atEnd())
265-
// {
266-
// inputFile.read(&tmp, 1);
267-
// buffer += tmp;
268-
// }
269-
// if (buffer.endsWith(";"))
270-
// {
271-
// hpgl_cmd aparser = hpgl_cmd(buffer);
272-
// cmdList.append(aparser);
273-
// ui->textBrowser_read->append(cmdList.last().print());
274-
// }
275-
// }
276260

277261
QString buffer = "";
278262
QTextStream fstream(&inputFile);
279263
buffer = fstream.readAll();
280-
264+
objList.push_back(hpgl_obj(buffer));
281265

282266
// Set up new graphics view.
283267

268+
QPen downPen;
269+
QPen upPen;
270+
271+
downPen.setColor(QColor(0, 0, 255));
272+
downPen.setWidth(2);
273+
downPen.setJoinStyle(Qt::RoundJoin);
274+
275+
upPen.setStyle(Qt::DotLine);
276+
upPen.setWidth(2);
277+
upPen.setColor(QColor(200, 100, 100));
278+
284279
plotScene.clear();
285280

286-
// int curX, curY;
287-
// for (int i = 0; i < cmdList.count(); i++)
288-
// {
289-
// if (cmdList.at(i)[0] == 'P' && cmdList.at(i)[1] == 'D')
290-
// {
291-
// ui->textBrowser_console->append("Found PD: " + cmdList.at(i));
292-
// int charIndex = 1;
293-
// int nextX, nextY;
294-
// while (cmdList.at(i)[charIndex] != ';')
295-
// {
296-
// charIndex++;
297-
// nextX = get_nextInt(cmdList.at(i), &charIndex);
298-
// charIndex++;
299-
// nextY = get_nextInt(cmdList.at(i), &charIndex);
300-
301-
// plotScene.addLine(curX, -curY, nextX, -nextY);
302-
// ui->textBrowser_console->append(timeStamp() + "Adding line [" +
303-
// QString::number(curX) + "," + QString::number(curY) +
304-
// "] to [" + QString::number(nextX) + "," +
305-
// QString::number(nextY) + "].");
306-
// curX = nextX;
307-
// curY = nextY;
308-
// }
309-
// }
310-
// else if (cmdList.at(i)[0] == 'P' && cmdList.at(i)[1] == 'U')
311-
// {
312-
// ui->textBrowser_console->append("Found PU: " + cmdList.at(i));
313-
// int charIndex = 1;
314-
// while (cmdList.at(i)[charIndex] != ';')
315-
// {
316-
// charIndex++;
317-
// curX = get_nextInt(cmdList.at(i), &charIndex);
318-
// charIndex++;
319-
// curY = get_nextInt(cmdList.at(i), &charIndex);
320-
// }
321-
// }
322-
// }
281+
QList<QLine> lines_down;
282+
lines_down.clear();
283+
QList<QLine> lines_up;
284+
lines_up.clear();
285+
for (int i = 0; i < objList.length(); i++)
286+
{
287+
lines_down = objList[i].line_list_down();
288+
lines_up = objList[i].line_list_up();
289+
for (int l = 0; l < lines_down.length(); l++)
290+
{
291+
plotScene.addLine(lines_down[l], downPen);
292+
}
293+
for (int l = 0; l < lines_up.length(); l++)
294+
{
295+
plotScene.addLine(lines_up[l], upPen);
296+
}
297+
}
323298

324299
ui->graphicsView_view->setSceneRect(plotScene.sceneRect());
325300
ui->graphicsView_view->show();
@@ -330,9 +305,9 @@ void MainWindow::do_plot()
330305
qDebug() << "Plotting file!";
331306
if (serialBuffer.isNull())
332307
{
333-
for (int i = 0; i < items.count(); i++)
308+
for (int i = 0; i < objList.count(); i++)
334309
{
335-
hpgl_obj obj = items.at(i);
310+
hpgl_obj obj = objList.at(i);
336311
int size = obj.printLen();
337312
// for (int d = 0; d < size; d++)
338313
// {
@@ -341,14 +316,14 @@ void MainWindow::do_plot()
341316
}
342317
return;
343318
}
344-
if (!serialBuffer->isOpen() || items.isEmpty())
319+
if (!serialBuffer->isOpen() || objList.isEmpty())
345320
{
346321
ui->textBrowser_console->append(timeStamp() + "Can't plot!");
347322
return;
348323
}
349-
for (int i = 0; i < items.count(); i++)
324+
for (int i = 0; i < objList.count(); i++)
350325
{
351-
hpgl_obj obj = items.at(i);
326+
hpgl_obj obj = objList.at(i);
352327
int size = obj.printLen();
353328
QString printThis = obj.print();
354329
// serialBuffer->write(cmdList.at(i).toStdString().c_str(), size);

mainwindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public slots:
5252
QSerialPortInfo serialPorts;
5353
QPointer<QSerialPort> serialBuffer;
5454
QFile inputFile;
55-
QList<hpgl_obj> items;
55+
QList<hpgl_obj> objList;
5656
QGraphicsScene plotScene;
5757
};
5858

0 commit comments

Comments
 (0)