author | sheepluva |
Thu, 12 Jun 2014 13:43:44 +0200 | |
changeset 10283 | f5573ef8cda9 |
parent 10208 | f04fdb35fc33 |
permissions | -rw-r--r-- |
359 | 1 |
#include <QGridLayout> |
2 |
#include <QImage> |
|
3 |
#include <QPixmap> |
|
4 |
#include <QMessageBox> |
|
5 |
#include <QFile> |
|
6 |
#include <QTextStream> |
|
7 |
#include <QRegExp> |
|
8 |
#include <QDebug> |
|
9 |
#include "mainform.h" |
|
10 |
||
11 |
MyWindow::MyWindow(QWidget * parent, Qt::WFlags flags) |
|
8442 | 12 |
: QMainWindow(parent, flags) |
359 | 13 |
|
14 |
{ |
|
8442 | 15 |
QWidget * centralWidget = new QWidget(this); |
16 |
QGridLayout * mainlayout = new QGridLayout(centralWidget); |
|
17 |
mainlayout->setMargin(1); |
|
18 |
mainlayout->setSpacing(1); |
|
359 | 19 |
|
8442 | 20 |
sa_xy = new QScrollArea(centralWidget); |
21 |
xy = new PixLabel(); |
|
22 |
xy->setFixedSize(1024, 512); |
|
23 |
sa_xy->setWidget(xy); |
|
359 | 24 |
|
8442 | 25 |
mainlayout->addWidget(sa_xy, 0, 0, 1, 4); |
359 | 26 |
|
8442 | 27 |
setCentralWidget(centralWidget); |
359 | 28 |
|
8442 | 29 |
buttAdd = new QPushButton(centralWidget); |
30 |
buttAdd->setText(tr("Add")); |
|
31 |
mainlayout->addWidget(buttAdd, 1, 0); |
|
359 | 32 |
|
8442 | 33 |
buttCode = new QPushButton(centralWidget); |
34 |
buttCode->setText(tr("Code")); |
|
35 |
mainlayout->addWidget(buttCode, 1, 1); |
|
359 | 36 |
|
8442 | 37 |
buttSave = new QPushButton(centralWidget); |
38 |
buttSave->setText(tr("Save")); |
|
39 |
mainlayout->addWidget(buttSave, 1, 3); |
|
359 | 40 |
|
8442 | 41 |
buttLoad = new QPushButton(centralWidget); |
42 |
buttLoad->setText(tr("Load")); |
|
43 |
mainlayout->addWidget(buttLoad, 1, 2); |
|
359 | 44 |
|
8442 | 45 |
connect(buttAdd, SIGNAL(clicked()), xy, SLOT(AddRect())); |
46 |
connect(buttCode, SIGNAL(clicked()), this, SLOT(Code())); |
|
47 |
connect(buttSave, SIGNAL(clicked()), this, SLOT(Save())); |
|
48 |
connect(buttLoad, SIGNAL(clicked()), this, SLOT(Load())); |
|
359 | 49 |
} |
50 |
||
51 |
void MyWindow::Code() |
|
52 |
{ |
|
8442 | 53 |
if (xy->rects.size()) |
54 |
{ |
|
55 |
QFile f("template.pas"); |
|
56 |
if (!f.open(QIODevice::WriteOnly)) |
|
57 |
{ |
|
58 |
QMessageBox::information(this, tr("Error"), |
|
59 |
tr("Cannot save")); |
|
60 |
return ; |
|
61 |
} |
|
359 | 62 |
|
8442 | 63 |
QTextStream stream(&f); |
64 |
stream << QString("const Template0Points: array[0..%1] of TSDL_Rect =").arg(xy->rects.size() - 1) << endl; |
|
65 |
stream << " (" << endl; |
|
66 |
for(int i = 0; i < xy->rects.size(); i++) |
|
67 |
{ |
|
68 |
QRect r = xy->rects[i].normalized(); |
|
69 |
stream << QString(" (x: %1; y: %2; w: %3; h: %4),"). |
|
70 |
arg(r.x() * 4, 4).arg(r.y() * 4, 4).arg(r.width() * 4, 4).arg(r.height() * 4, 4) << endl; |
|
71 |
} |
|
72 |
stream << " );" << endl; |
|
73 |
f.close(); |
|
74 |
} |
|
359 | 75 |
} |
76 |
||
77 |
void MyWindow::Save() |
|
78 |
{ |
|
8442 | 79 |
Code(); |
359 | 80 |
} |
81 |
||
82 |
void MyWindow::Load() |
|
83 |
{ |
|
8442 | 84 |
QFile f("template.pas"); |
85 |
if (!f.open(QIODevice::ReadOnly)) |
|
86 |
{ |
|
87 |
QMessageBox::information(this, tr("Error"), |
|
88 |
tr("Cannot open file")); |
|
89 |
return ; |
|
90 |
} |
|
359 | 91 |
|
8442 | 92 |
QTextStream stream(&f); |
93 |
QStringList sl; |
|
94 |
while (!stream.atEnd()) |
|
95 |
{ |
|
96 |
sl << stream.readLine(); |
|
97 |
} |
|
98 |
xy->rects.clear(); |
|
99 |
for (int i = 0; i < sl.size(); ++i) |
|
100 |
{ |
|
10208
f04fdb35fc33
- Limit outline to leftX/rightX/topY instead of LAND_WIDTH/LAND_HEIGHT
unc0rr
parents:
8442
diff
changeset
|
101 |
QRegExp re("x:\\s*(\\d+);\\sy:\\s*(\\d+);\\sw:\\s*(\\d+);\\sh:\\s*(\\d+)"); |
8442 | 102 |
re.indexIn(sl.at(i)); |
103 |
QStringList coords = re.capturedTexts(); |
|
104 |
qDebug() << sl.at(i) << coords; |
|
105 |
if ((coords.size() == 5) && (coords[0].size())) |
|
106 |
xy->rects.push_back(QRect(coords[1].toInt() / 4, coords[2].toInt() / 4, coords[3].toInt() / 4, coords[4].toInt() / 4)); |
|
107 |
} |
|
108 |
f.close(); |
|
109 |
xy->repaint(); |
|
359 | 110 |
} |