00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include "streamwg.h"
00013 #include "indistd.h"
00014 #include "Options.h"
00015
00016 #include <kmessagebox.h>
00017 #include <klocale.h>
00018 #include <kdebug.h>
00019 #include <kpushbutton.h>
00020 #include <kiconloader.h>
00021 #include <ktempfile.h>
00022 #include <kio/netaccess.h>
00023 #include <kfiledialog.h>
00024 #include <kcombobox.h>
00025 #include <kurl.h>
00026
00027 #include <qsocketnotifier.h>
00028 #include <qimage.h>
00029 #include <qpainter.h>
00030 #include <qstringlist.h>
00031 #include <qdir.h>
00032 #include <qlayout.h>
00033
00034
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <fcntl.h>
00038
00039 #define STREAMBUFSIZ 1024
00040
00041 FILE *wfp;
00042
00043 StreamWG::StreamWG(INDIStdDevice *inStdDev, QWidget * parent, const char * name) : streamForm(parent, name)
00044 {
00045
00046 stdDev = inStdDev;
00047
00048 streamWidth = streamHeight = -1;
00049
00050 processStream = colorFrame = false;
00051
00052
00053 streamFrame = new VideoWG(videoFrame);
00054
00055 KIconLoader *icons = KGlobal::iconLoader();
00056
00057 playPix = icons->loadIcon( "player_play", KIcon::Toolbar );
00058 pausePix = icons->loadIcon( "player_pause", KIcon::Toolbar );
00059 capturePix = icons->loadIcon( "frame_image", KIcon::Toolbar );
00060
00061 playB->setPixmap(pausePix);
00062 captureB->setPixmap(capturePix);
00063
00064 imgFormatCombo->insertStrList(QImage::outputFormats());
00065
00066 connect(playB, SIGNAL(clicked()), this, SLOT(playPressed()));
00067 connect(captureB, SIGNAL(clicked()), this, SLOT(captureImage()));
00068
00069 }
00070
00071 StreamWG::~StreamWG()
00072 {
00073
00074 }
00075
00076 void StreamWG::closeEvent ( QCloseEvent * e )
00077 {
00078 stdDev->streamDisabled();
00079 processStream = false;
00080 e->accept();
00081 }
00082
00083 void StreamWG::setColorFrame(bool color)
00084 {
00085 colorFrame = color;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 void StreamWG::enableStream(bool enable)
00119 {
00120 if (enable)
00121 {
00122 processStream = true;
00123 show();
00124 }
00125 else
00126 {
00127 processStream = false;
00128 playB->setPixmap(pausePix);
00129 hide();
00130 }
00131
00132 }
00133
00134 void StreamWG::setSize(int wd, int ht)
00135 {
00136 streamWidth = wd;
00137 streamHeight = ht;
00138
00139 streamFrame->totalBaseCount = wd * ht;
00140
00141 resize(wd + layout()->margin() * 2 , ht + playB->height() + layout()->margin() * 2 + layout()->spacing());
00142 streamFrame->resize(wd, ht);
00143 }
00144
00145 void StreamWG::resizeEvent(QResizeEvent *ev)
00146 {
00147
00148 streamFrame->resize(ev->size().width() - layout()->margin() * 2, ev->size().height() - playB->height() - layout()->margin() * 2 - layout()->spacing());
00149
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 void StreamWG::playPressed()
00202 {
00203
00204 if (processStream)
00205 {
00206 playB->setPixmap(playPix);
00207 processStream = false;
00208 }
00209 else
00210 {
00211 playB->setPixmap(pausePix);
00212 processStream = true;
00213 }
00214
00215 }
00216
00217 void StreamWG::captureImage()
00218 {
00219 QString fname;
00220 QString fmt;
00221 KURL currentFileURL;
00222 QString currentDir = Options::fitsSaveDirectory();
00223 KTempFile tmpfile;
00224 tmpfile.setAutoDelete(true);
00225
00226 fmt = imgFormatCombo->currentText();
00227
00228 currentFileURL = KFileDialog::getSaveURL( currentDir, fmt );
00229
00230 if (currentFileURL.isEmpty()) return;
00231
00232 if ( currentFileURL.isValid() )
00233 {
00234 currentDir = currentFileURL.directory();
00235
00236 if ( currentFileURL.isLocalFile() )
00237 fname = currentFileURL.path();
00238 else
00239 fname = tmpfile.name();
00240
00241 if (fname.right(fmt.length()).lower() != fmt.lower())
00242 {
00243 fname += ".";
00244 fname += fmt.lower();
00245 }
00246
00247 streamFrame->qPix.save(fname, fmt.ascii());
00248
00249
00250 chmod( fname.ascii(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH );
00251
00252 if ( tmpfile.name() == fname )
00253 {
00254
00255 if ( ! KIO::NetAccess::upload( tmpfile.name(), currentFileURL, (QWidget*) 0 ) )
00256 {
00257 QString message = i18n( "Could not upload image to remote location: %1" ).arg( currentFileURL.prettyURL() );
00258 KMessageBox::sorry( 0, message, i18n( "Could not upload file" ) );
00259 }
00260 }
00261 }
00262 else
00263 {
00264 QString message = i18n( "Invalid URL: %1" ).arg( currentFileURL.url() );
00265 KMessageBox::sorry( 0, message, i18n( "Invalid URL" ) );
00266 }
00267
00268 }
00269
00270
00271 VideoWG::VideoWG(QWidget * parent, const char * name) : QFrame(parent, name, Qt::WNoAutoErase)
00272 {
00273 streamImage = NULL;
00274 grayTable=new QRgb[256];
00275 for (int i=0;i<256;i++)
00276 grayTable[i]=qRgb(i,i,i);
00277 }
00278
00279 VideoWG::~VideoWG()
00280 {
00281 delete (streamImage);
00282 delete [] (grayTable);
00283 }
00284
00285 void VideoWG::newFrame(unsigned char *buffer, int buffSiz, int w, int h)
00286 {
00287
00288
00289
00290
00291 if (buffSiz > totalBaseCount)
00292 streamImage = new QImage(buffer, w, h, 32, 0, 0, QImage::BigEndian);
00293 else
00294
00295 streamImage = new QImage(buffer, w, h, 8, grayTable, 256, QImage::IgnoreEndian);
00296
00297 update();
00298
00299 }
00300
00301 void VideoWG::paintEvent(QPaintEvent *)
00302 {
00303
00304 if (streamImage)
00305 {
00306 if (streamImage->isNull()) return;
00307
00308 qPix = kPixIO.convertToPixmap(streamImage->scale(width(), height()));
00309 delete (streamImage);
00310 streamImage = NULL;
00311 }
00312
00313 bitBlt(this, 0, 0, &qPix);
00314
00315 }
00316
00317 #include "streamwg.moc"