+/***************************************************************************
+ * Copyright (C) 2006 by Michal Sojka *
+ * sojkam1@fel.cvut.cz *
+ * *
+ * This program 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 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program 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 this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#include "mainwindow.h"
+#include <QTextStream>
+#include <QDataStream>
+#include <QFile>
+#include <QClipboard>
+
+MainWindow::MainWindow(const string &tty)
+ : QDialog(), m_commThread(tty), m_fileNum(0), m_image()
+{
+ ui.setupUi(this);
+
+ imageLabel = new QLabel;
+ ui.scrollArea->setWidget(imageLabel);
+ ui.progressBar->setValue(0);
+ ui.progressBar->setMaximum(15000);
+ ui.saveButton->setDisabled(true);
+
+ connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
+ connect(&m_commThread, SIGNAL(bytesReceived(int)), ui.progressBar, SLOT(setValue(int)));
+ connect(&m_commThread, SIGNAL(imageAvailable(OscImage*)), this, SLOT(processImage(OscImage*)));
+ connect(ui.prefixLine, SIGNAL(textChanged(const QString&)), this, SLOT(generateFileName(const QString&)));
+ connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(saveImage()));
+
+ m_commThread.start();
+}
+
+
+MainWindow::~MainWindow()
+{
+}
+
+void MainWindow::processImage( OscImage * img )
+{
+ bool ret;
+ ui.saveButton->setEnabled(true);
+ ui.progressBar->setMaximum(img->size());
+ ui.progressBar->setValue(img->size());
+ ui.progressBar->update();
+
+ m_fileNum++;
+ generateFileName( ui.prefixLine->text());
+
+ if (m_image) delete m_image;
+ m_image = img;
+
+ QPixmap pixmap;
+ ret = pixmap.loadFromData(*m_image);
+ if (!ret) cerr << "Can't display image!"<<endl;
+ QApplication::clipboard()->setPixmap(pixmap);
+ imageLabel->setPixmap(pixmap);
+ imageLabel->adjustSize();
+}
+
+
+
+void MainWindow::resetFileCounter( )
+{
+ m_fileNum = 0;
+}
+
+void MainWindow::generateFileName( const QString & prefix )
+{
+ QString fname;
+ QTextStream ts(&fname);
+
+ ts.setFieldWidth(3);
+ ts.setPadChar('0') ;
+ ts << prefix << m_fileNum << ".tif";
+ ui.fileLine->setText(fname);
+}
+
+void MainWindow::saveImage()
+{
+ QFile file(ui.fileLine->text());
+ QDataStream ds(&file);
+
+ file.open(QIODevice::WriteOnly);
+ ds.writeRawData(m_image->data(), m_image->size());
+ file.close();
+}