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