]> rtime.felk.cvut.cz Git - sojka/tekpic.git/blob - src/mainwindow.cpp
Change default port to ttyUSB0
[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      ui.statusBar->setText("Press \"hardcopy\" button on the scope...");
39      
40      connect(ui.quitButton, SIGNAL(clicked()), this, SLOT(close()));
41      connect(&m_commThread, SIGNAL(bytesReceived(int)), ui.progressBar, SLOT(setValue(int)));
42      connect(&m_commThread, SIGNAL(newImage()), this, SLOT(newImage()));
43      connect(&m_commThread, SIGNAL(imageAvailable(OscImage*)), this, SLOT(processImage(OscImage*)));
44      connect(ui.prefixLine, SIGNAL(textChanged(const QString&)), this, SLOT(generateFileName(const QString&)));
45      connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(saveImage()));
46      
47      m_commThread.start();
48 }
49
50
51
52 MainWindow::~MainWindow()
53 {
54 }
55
56 void MainWindow::processImage( OscImage * img )
57 {
58         bool ret;
59         ui.saveButton->setEnabled(true);
60         m_lastSize = img->size();
61         ui.progressBar->setMaximum(m_lastSize);
62         ui.progressBar->setValue(m_lastSize);
63         ui.progressBar->hide();
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)
71           ui.statusBar->setText("Cannot display the image! Unsupported format?");
72         else
73           ui.statusBar->setText("Ready");
74
75         QApplication::clipboard()->setPixmap(pixmap);
76         imageLabel->setPixmap(pixmap);
77         imageLabel->adjustSize();
78
79         m_fileNum++;
80         generateFileName( ui.prefixLine->text());
81
82 }
83
84
85
86 void MainWindow::resetFileCounter( )
87 {
88         m_fileNum = 0;
89 }
90
91 void MainWindow::generateFileName( const QString & prefix )
92 {
93         QString fname;
94         QTextStream ts(&fname);
95         
96         ts.setFieldWidth(3); 
97         ts.setPadChar('0') ;
98
99         ts << prefix << m_fileNum << ".bmp";
100         ui.fileLine->setText(fname);
101 }
102
103 void MainWindow::saveImage()
104 {
105         QFile file(ui.fileLine->text());
106         QDataStream ds(&file);
107
108         file.open(QIODevice::WriteOnly);
109         ds.writeRawData(m_image->data(), m_image->size());
110         file.close();
111 }
112
113 void MainWindow::newImage()
114 {
115      ui.progressBar->show();
116      ui.statusBar->setText("Transferring...");
117         if (m_lastSize == 0)
118                 ui.progressBar->setMaximum(0);
119 }