tools/templates/mainform.cpp
author unc0rr
Fri, 17 May 2013 00:08:05 +0400
changeset 9008 e9d88f2522f4
parent 8442 535a00ca0d35
child 10208 f04fdb35fc33
permissions -rw-r--r--
merge
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)
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    12
        : QMainWindow(parent, flags)
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    13
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    14
{
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    15
    QWidget * centralWidget = new QWidget(this);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    16
    QGridLayout * mainlayout = new QGridLayout(centralWidget);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    17
    mainlayout->setMargin(1);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    18
    mainlayout->setSpacing(1);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    19
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    20
    sa_xy = new QScrollArea(centralWidget);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    21
    xy = new PixLabel();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    22
    xy->setFixedSize(1024, 512);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    23
    sa_xy->setWidget(xy);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    24
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    25
    mainlayout->addWidget(sa_xy, 0, 0, 1, 4);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    26
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    27
    setCentralWidget(centralWidget);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    28
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    29
    buttAdd = new QPushButton(centralWidget);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    30
    buttAdd->setText(tr("Add"));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    31
    mainlayout->addWidget(buttAdd, 1, 0);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    32
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    33
    buttCode = new QPushButton(centralWidget);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    34
    buttCode->setText(tr("Code"));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    35
    mainlayout->addWidget(buttCode, 1, 1);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    36
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    37
    buttSave = new QPushButton(centralWidget);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    38
    buttSave->setText(tr("Save"));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    39
    mainlayout->addWidget(buttSave, 1, 3);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    40
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    41
    buttLoad = new QPushButton(centralWidget);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    42
    buttLoad->setText(tr("Load"));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    43
    mainlayout->addWidget(buttLoad, 1, 2);
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    44
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    45
    connect(buttAdd, SIGNAL(clicked()), xy, SLOT(AddRect()));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    46
    connect(buttCode, SIGNAL(clicked()), this, SLOT(Code()));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    47
    connect(buttSave, SIGNAL(clicked()), this, SLOT(Save()));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    48
    connect(buttLoad, SIGNAL(clicked()), this, SLOT(Load()));
359
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
{
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    53
    if (xy->rects.size())
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    54
    {
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    55
        QFile f("template.pas");
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    56
        if (!f.open(QIODevice::WriteOnly))
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    57
        {
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    58
            QMessageBox::information(this, tr("Error"),
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    59
                        tr("Cannot save"));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    60
            return ;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    61
        }
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    62
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    63
        QTextStream stream(&f);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    64
        stream << QString("const Template0Points: array[0..%1] of TSDL_Rect =").arg(xy->rects.size() - 1) << endl;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    65
        stream << "      (" << endl;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    66
        for(int i = 0; i < xy->rects.size(); i++)
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    67
        {
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    68
            QRect r = xy->rects[i].normalized();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    69
            stream << QString("       (x: %1; y: %2; w: %3; h: %4),").
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    70
                    arg(r.x() * 4, 4).arg(r.y() * 4, 4).arg(r.width() * 4, 4).arg(r.height() * 4, 4) << endl;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    71
        }
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    72
        stream << "      );" << endl;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    73
        f.close();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    74
    }
359
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
{
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
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
{
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    84
    QFile f("template.pas");
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    85
    if (!f.open(QIODevice::ReadOnly))
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    86
    {
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    87
        QMessageBox::information(this, tr("Error"),
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    88
                    tr("Cannot open file"));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    89
        return ;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    90
    }
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
    91
8442
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    92
    QTextStream stream(&f);
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    93
    QStringList sl;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    94
    while (!stream.atEnd())
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    95
    {
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    96
        sl << stream.readLine();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    97
    }
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    98
    xy->rects.clear();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
    99
    for (int i = 0; i < sl.size(); ++i)
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   100
    {
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   101
        QRegExp re("x:\\s+(\\d+);\\sy:\\s+(\\d+);\\sw:\\s+(\\d+);\\sh:\\s+(\\d+)");
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   102
        re.indexIn(sl.at(i));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   103
        QStringList coords = re.capturedTexts();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   104
        qDebug() << sl.at(i) << coords;
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   105
        if ((coords.size() == 5) && (coords[0].size()))
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   106
            xy->rects.push_back(QRect(coords[1].toInt() / 4, coords[2].toInt() / 4, coords[3].toInt() / 4, coords[4].toInt() / 4));
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   107
    }
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   108
    f.close();
535a00ca0d35 whitespaces and tabs again
koda
parents: 1774
diff changeset
   109
    xy->repaint();
359
59fbfc65fbda - New land templates
unc0rr
parents:
diff changeset
   110
}