Advertisement
Norbi930523

mainwindow.cpp

May 22nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. /*
  2.     RCSS LOGO CONVERTER by Balázs Kóti (ShuffleSource)
  3.  
  4.     This file is part of RCSS Logo Converter.
  5.  
  6.     RCSS Logo Converter is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     RCSS Logo Converter is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with RCSS Logo Converter.  If not, see <http://www.gnu.org/licenses/>.
  18. */
  19.  
  20. #include "mainwindow.h"
  21. #include <math.h>
  22.  
  23. MainWindow::MainWindow(QWidget *parent)
  24.     : QMainWindow(parent)
  25. {
  26.     fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("PNG images (*.png)"));
  27.  
  28.     //check if file exists
  29.     if (!QFileInfo(fileName).exists()){
  30.         exit(1);
  31.     }
  32.  
  33.     createMultilineInput();
  34.  
  35. }
  36.  
  37. MainWindow::~MainWindow()
  38. {  
  39.     delete textarea;
  40.     delete img;
  41.     delete dlgMultiLine;
  42. }
  43.  
  44. void MainWindow::createMultilineInput()
  45. {
  46. dlgMultiLine =    new QDialog(this);
  47.     dlgMultiLine->setWindowTitle("Pattern for code");
  48.     QGridLayout *gridLayout = new QGridLayout(dlgMultiLine);
  49.  
  50.     txtMultiline = new QTextEdit();
  51.  
  52.     //default text:
  53.     txtMultiline->setText("std::snprintf(buf, 1024, \"(team_graphic (%d %d %s))\",@ROW , @COL ,\"@DATA\");\r\nthisclient->sndCmd(buf);");
  54.  
  55.     txtMultiline->setObjectName(QString::fromUtf8("txtMultiline"));
  56.     gridLayout->addWidget(txtMultiline, 0, 0, 1, 1);
  57.  
  58.     QDialogButtonBox *buttonBox = new QDialogButtonBox();
  59.     buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
  60.     buttonBox->setOrientation(Qt::Horizontal);
  61.     buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
  62.     gridLayout->addWidget(buttonBox, 1, 0, 1, 1);
  63.  
  64.     dlgMultiLine->setFixedSize(600,200);
  65.     connect(buttonBox,SIGNAL(accepted()),this,SLOT(process()));
  66.     connect(buttonBox,SIGNAL(rejected()),this,SLOT(stop()));
  67.  
  68.     dlgMultiLine->show();
  69.     delete gridLayout;
  70. }
  71.  
  72. void MainWindow::process()
  73. {
  74.     dlgMultiLine->hide();
  75.  
  76.     textarea = new QTextEdit;
  77.  
  78.     //load image to pixmap
  79.     QImage imgA = QImage(fileName);
  80.     //get width and height of pixmap
  81.     float pmWidth = imgA.width();
  82.     float pmHeight = imgA.height();
  83.     float scale = (256/pmWidth)<=(64/pmHeight) ? (256/pmWidth) : (64/pmHeight);
  84.     float scaledWidth = pmWidth*scale;
  85.     float scaledHeight = pmHeight*scale;
  86.     scaledWidth=ceil(scaledWidth);
  87.     scaledHeight=ceil(scaledHeight);
  88.     //scaling
  89.     img = new QImage(imgA.scaled(scaledWidth,scaledHeight));
  90.  
  91.     //get width and height of pixmap again
  92.     pmWidth = img->width();
  93.     pmHeight = img->height();
  94.  
  95.     QProgressDialog progress("Generating code...", "", 0, pmWidth*pmHeight/64, this);
  96.     progress.setWindowModality(Qt::WindowModal);
  97.     progress.setCancelButton(0);
  98.     progress.show();
  99.  
  100.     int wLimit = ((int)pmWidth % 8) == 0 ? pmWidth/8 : ceil(pmWidth/8);
  101.     int hLimit = ((int)pmHeight % 8) == 0 ? pmHeight/8 : ceil(pmHeight/8);
  102.  
  103.     QRgb pix=0;
  104.     //tiles
  105.     for(int tW = 0; tW < wLimit; tW++){
  106.     for(int tH = 0; tH < hLimit; tH++){
  107.         pix = 0;
  108.         tile = new QImage(8,8,QImage::Format_ARGB32);
  109.         progress.setValue(progress.value()+1);
  110.         //subpixels in tiles
  111.         for(int i=0; i < 8; i++){
  112.             for(int j=0; j < 8; j++){
  113.                 if( ( (tW*8+i)<pmWidth ) && ((tH*8+j)<pmHeight)){
  114.                 pix = img->pixel(tW*8 + i,tH*8 + j);
  115.                 tile->setPixel(i,j,pix);
  116.                 }
  117.                 else{pix=0; tile->setPixel(i,j,pix);}
  118.             }
  119.         }
  120.  
  121.         //save tiles to files
  122.         //tile->save("tmp/tile_"+QString::number(tW)+"_"+QString::number(tH)+".xpm","XPM");
  123.  
  124.         QByteArray ba;
  125.         QBuffer buffer(&ba);
  126.         buffer.open(QIODevice::WriteOnly);
  127.  
  128.         //write to buffer
  129.         tile->save(&buffer,"XPM");
  130.         buffer.close();
  131.  
  132.         QString line = QString(ba);
  133.  
  134.         //process text with regular expressions
  135.         line.replace(QRegExp(".*/"),"");
  136.         line.replace(QRegExp(".*=."),"");
  137.         line.replace(QRegExp(".;"),"");
  138.         line.replace(QRegExp(","),"");
  139.         line.replace(QRegExp("\n"),"");
  140.         line.replace(QRegExp("\""),"\\\"");
  141.  
  142.         QString final = txtMultiline->toPlainText();
  143.  
  144.         //replacing variables with values
  145.         final.replace(QRegExp("@DATA"),line);
  146.         final.replace(QRegExp("@ROW"),QString::number(tW));
  147.         final.replace(QRegExp("@COL"),QString::number(tH));
  148.  
  149.         textarea->append(final);
  150.         delete tile;
  151.     }
  152.     }
  153.  
  154.     progress.hide();
  155.     textarea->setGeometry(100,100,600,500);
  156.     textarea->show();
  157.     delete txtMultiline;
  158. }
  159.  
  160. void MainWindow::stop(){
  161.     exit(0);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement