Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- RCSS LOGO CONVERTER by Balázs Kóti (ShuffleSource)
- This file is part of RCSS Logo Converter.
- RCSS Logo Converter is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- RCSS Logo Converter is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with RCSS Logo Converter. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "mainwindow.h"
- #include <math.h>
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("PNG images (*.png)"));
- //check if file exists
- if (!QFileInfo(fileName).exists()){
- exit(1);
- }
- createMultilineInput();
- }
- MainWindow::~MainWindow()
- {
- delete textarea;
- delete img;
- delete dlgMultiLine;
- }
- void MainWindow::createMultilineInput()
- {
- dlgMultiLine = new QDialog(this);
- dlgMultiLine->setWindowTitle("Pattern for code");
- QGridLayout *gridLayout = new QGridLayout(dlgMultiLine);
- txtMultiline = new QTextEdit();
- //default text:
- txtMultiline->setText("std::snprintf(buf, 1024, \"(team_graphic (%d %d %s))\",@ROW , @COL ,\"@DATA\");\r\nthisclient->sndCmd(buf);");
- txtMultiline->setObjectName(QString::fromUtf8("txtMultiline"));
- gridLayout->addWidget(txtMultiline, 0, 0, 1, 1);
- QDialogButtonBox *buttonBox = new QDialogButtonBox();
- buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
- buttonBox->setOrientation(Qt::Horizontal);
- buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
- gridLayout->addWidget(buttonBox, 1, 0, 1, 1);
- dlgMultiLine->setFixedSize(600,200);
- connect(buttonBox,SIGNAL(accepted()),this,SLOT(process()));
- connect(buttonBox,SIGNAL(rejected()),this,SLOT(stop()));
- dlgMultiLine->show();
- delete gridLayout;
- }
- void MainWindow::process()
- {
- dlgMultiLine->hide();
- textarea = new QTextEdit;
- //load image to pixmap
- QImage imgA = QImage(fileName);
- //get width and height of pixmap
- float pmWidth = imgA.width();
- float pmHeight = imgA.height();
- float scale = (256/pmWidth)<=(64/pmHeight) ? (256/pmWidth) : (64/pmHeight);
- float scaledWidth = pmWidth*scale;
- float scaledHeight = pmHeight*scale;
- scaledWidth=ceil(scaledWidth);
- scaledHeight=ceil(scaledHeight);
- //scaling
- img = new QImage(imgA.scaled(scaledWidth,scaledHeight));
- //get width and height of pixmap again
- pmWidth = img->width();
- pmHeight = img->height();
- QProgressDialog progress("Generating code...", "", 0, pmWidth*pmHeight/64, this);
- progress.setWindowModality(Qt::WindowModal);
- progress.setCancelButton(0);
- progress.show();
- int wLimit = ((int)pmWidth % 8) == 0 ? pmWidth/8 : ceil(pmWidth/8);
- int hLimit = ((int)pmHeight % 8) == 0 ? pmHeight/8 : ceil(pmHeight/8);
- QRgb pix=0;
- //tiles
- for(int tW = 0; tW < wLimit; tW++){
- for(int tH = 0; tH < hLimit; tH++){
- pix = 0;
- tile = new QImage(8,8,QImage::Format_ARGB32);
- progress.setValue(progress.value()+1);
- //subpixels in tiles
- for(int i=0; i < 8; i++){
- for(int j=0; j < 8; j++){
- if( ( (tW*8+i)<pmWidth ) && ((tH*8+j)<pmHeight)){
- pix = img->pixel(tW*8 + i,tH*8 + j);
- tile->setPixel(i,j,pix);
- }
- else{pix=0; tile->setPixel(i,j,pix);}
- }
- }
- //save tiles to files
- //tile->save("tmp/tile_"+QString::number(tW)+"_"+QString::number(tH)+".xpm","XPM");
- QByteArray ba;
- QBuffer buffer(&ba);
- buffer.open(QIODevice::WriteOnly);
- //write to buffer
- tile->save(&buffer,"XPM");
- buffer.close();
- QString line = QString(ba);
- //process text with regular expressions
- line.replace(QRegExp(".*/"),"");
- line.replace(QRegExp(".*=."),"");
- line.replace(QRegExp(".;"),"");
- line.replace(QRegExp(","),"");
- line.replace(QRegExp("\n"),"");
- line.replace(QRegExp("\""),"\\\"");
- QString final = txtMultiline->toPlainText();
- //replacing variables with values
- final.replace(QRegExp("@DATA"),line);
- final.replace(QRegExp("@ROW"),QString::number(tW));
- final.replace(QRegExp("@COL"),QString::number(tH));
- textarea->append(final);
- delete tile;
- }
- }
- progress.hide();
- textarea->setGeometry(100,100,600,500);
- textarea->show();
- delete txtMultiline;
- }
- void MainWindow::stop(){
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement