]> rtime.felk.cvut.cz Git - sojka/tekpic.git/blob - src/mainwindow.cpp
Added support for PCX images. TDS224 doesn't have TIFF.
[sojka/tekpic.git] / src / mainwindow.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Michal Sojka   *
3  *   sojkam1@fel.cvut.cz   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "mainwindow.h"
21 #include <QTextStream>
22 #include <QDataStream>
23 #include <QFile>
24 #include <QClipboard>
25
26 MainWindow::MainWindow(const string &tty)
27  : QDialog(), m_commThread(tty), m_fileNum(0), m_lastSize(0), m_image()
28 {
29      ui.setupUi(this);
30      
31      imageLabel = new QLabel;
32      ui.scrollArea->setWidget(imageLabel);
33      ui.scrollArea->setMinimumWidth(320);
34      ui.progressBar->setValue(0);
35      ui.progressBar->hide();
36      ui.saveButton->setDisabled(true);
37      
38      connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
39      connect(&m_commThread, SIGNAL(bytesReceived(int)), ui.progressBar, SLOT(setValue(int)));
40      connect(&m_commThread, SIGNAL(newImage()), this, SLOT(newImage()));
41      connect(&m_commThread, SIGNAL(imageAvailable(OscImage*)), this, SLOT(processImage(OscImage*)));
42      connect(ui.prefixLine, SIGNAL(textChanged(const QString&)), this, SLOT(generateFileName(const QString&)));
43      connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(saveImage()));
44      
45      m_commThread.start();
46 }
47
48
49 MainWindow::~MainWindow()
50 {
51 }
52
53 void MainWindow::processImage( OscImage * img )
54 {
55         bool ret;
56         ui.saveButton->setEnabled(true);
57         m_lastSize = img->size();
58         ui.progressBar->setMaximum(m_lastSize);
59         ui.progressBar->setValue(m_lastSize);
60         ui.progressBar->hide();
61         
62         m_fileNum++;
63         generateFileName( ui.prefixLine->text());
64         
65         if (m_image) delete m_image;
66         m_image = img;
67         
68         QPixmap pixmap;
69         ret = pixmap.loadFromData(*m_image);
70         if (!ret) cerr << "Can't display image!"<<endl;
71         QApplication::clipboard()->setPixmap(pixmap);
72         imageLabel->setPixmap(pixmap);
73         imageLabel->adjustSize();
74 }
75
76
77
78 void MainWindow::resetFileCounter( )
79 {
80         m_fileNum = 0;
81 }
82
83 void MainWindow::generateFileName( const QString & prefix )
84 {
85         QString fname;
86         QTextStream ts(&fname);
87         
88         ts.setFieldWidth(3); 
89         ts.setPadChar('0') ;
90         ts << prefix << m_fileNum << ".tif";
91         ui.fileLine->setText(fname);
92 }
93
94 void MainWindow::saveImage()
95 {
96         QFile file(ui.fileLine->text());
97         QDataStream ds(&file);
98
99         file.open(QIODevice::WriteOnly);
100         ds.writeRawData(m_image->data(), m_image->size());
101         file.close();
102 }
103
104 void MainWindow::newImage()
105 {
106      ui.progressBar->show();
107         if (m_lastSize == 0)
108                 ui.progressBar->setMaximum(0);
109 }