tools/templates/mainform.cpp
changeset 14354 20178fa3a74a
parent 14353 5cc671f988e7
child 14355 bc29fc6df7a2
equal deleted inserted replaced
14353:5cc671f988e7 14354:20178fa3a74a
     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)
       
    12         : QMainWindow(parent, flags)
       
    13 
       
    14 {
       
    15     QWidget * centralWidget = new QWidget(this);
       
    16     QGridLayout * mainlayout = new QGridLayout(centralWidget);
       
    17     mainlayout->setMargin(1);
       
    18     mainlayout->setSpacing(1);
       
    19 
       
    20     sa_xy = new QScrollArea(centralWidget);
       
    21     xy = new PixLabel();
       
    22     xy->setFixedSize(1024, 512);
       
    23     sa_xy->setWidget(xy);
       
    24 
       
    25     mainlayout->addWidget(sa_xy, 0, 0, 1, 4);
       
    26 
       
    27     setCentralWidget(centralWidget);
       
    28 
       
    29     buttAdd = new QPushButton(centralWidget);
       
    30     buttAdd->setText(tr("Add"));
       
    31     mainlayout->addWidget(buttAdd, 1, 0);
       
    32 
       
    33     buttCode = new QPushButton(centralWidget);
       
    34     buttCode->setText(tr("Code"));
       
    35     mainlayout->addWidget(buttCode, 1, 1);
       
    36 
       
    37     buttSave = new QPushButton(centralWidget);
       
    38     buttSave->setText(tr("Save"));
       
    39     mainlayout->addWidget(buttSave, 1, 3);
       
    40 
       
    41     buttLoad = new QPushButton(centralWidget);
       
    42     buttLoad->setText(tr("Load"));
       
    43     mainlayout->addWidget(buttLoad, 1, 2);
       
    44 
       
    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()));
       
    49 }
       
    50 
       
    51 void MyWindow::Code()
       
    52 {
       
    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         }
       
    62 
       
    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     }
       
    75 }
       
    76 
       
    77 void MyWindow::Save()
       
    78 {
       
    79     Code();
       
    80 }
       
    81 
       
    82 void MyWindow::Load()
       
    83 {
       
    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     }
       
    91 
       
    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     {
       
   101         QRegExp re("x:\\s*(\\d+);\\sy:\\s*(\\d+);\\sw:\\s*(\\d+);\\sh:\\s*(\\d+)");
       
   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();
       
   110 }