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