• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

streamwg.cpp

Go to the documentation of this file.
00001 /*  Stream Widget
00002     Copyright (C) 2003 Jasem Mutlaq (mutlaqja@ikarustech.com)
00003 
00004     This application is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008     
00009     2004-03-16: A class to handle video streaming.
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 //   streamBuffer   = NULL;
00048    streamWidth    = streamHeight = -1;
00049 //   streamFD       = -1;
00050    processStream  = colorFrame = false;
00051    
00052    //videoFrameLayout = new QVBoxLayout(videoFrame, 0, 0); 
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 //  delete streamBuffer;
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 /*void StreamWG::establishDataChannel(QString host, int port)
00089 {
00090         QString errMsg;
00091     struct sockaddr_in pin;
00092     struct hostent *serverHostName = gethostbyname(host.ascii());
00093     errMsg = QString("Connection to INDI host at %1 on port %2 failed.").arg(host).arg(port);
00094     
00095     memset(&pin, 0, sizeof(pin));
00096     pin.sin_family      = AF_INET;
00097     pin.sin_addr.s_addr     = ((struct in_addr *) (serverHostName->h_addr))->s_addr;
00098     pin.sin_port        = htons(port);
00099 
00100     if ( (streamFD = socket(AF_INET, SOCK_STREAM, 0)) == -1)
00101     {
00102      KMessageBox::error(0, i18n("Cannot create socket."));
00103      return;
00104     }
00105 
00106     if ( ::connect(streamFD, (struct sockaddr*) &pin, sizeof(pin)) == -1)
00107     {
00108       KMessageBox::error(0, errMsg);
00109       streamFD = -1;
00110       return;
00111     }
00112 
00113     // callback notified
00114     sNotifier = new QSocketNotifier( streamFD, QSocketNotifier::Read, this);
00115         QObject::connect( sNotifier, SIGNAL(activated(int)), this, SLOT(streamReceived()));
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 void StreamWG::allocateStreamBuffer()
00153 {
00154   if (streamWidth < 0 || streamHeight < 0) return;
00155   
00156   fprintf(stderr, "In allocate stream buffer \n");
00157   
00158   delete (streamBuffer);
00159   
00160   if (colorFrame)
00161     frameTotalBytes = streamWidth * streamHeight * 4;
00162   else
00163     frameTotalBytes = streamWidth * streamHeight;
00164   
00165    streamBuffer = new unsigned char[frameTotalBytes];
00166    
00167    fprintf(stderr, "We have a %s frame. width: %d -- height: %d -- totalBytes: %d\n", colorFrame ? "color" : "grey", streamWidth, streamHeight, frameTotalBytes);
00168   
00169 }
00170 
00171 void StreamWG::streamReceived()
00172 {
00173 
00174     char msg[1024];
00175     int nr=0, n=0;
00176     
00177     for (nr = 0; nr < frameTotalBytes; nr+=n)
00178     {
00179            n = read (streamFD, streamBuffer + nr, frameTotalBytes - nr);
00180        if (n <= 0)
00181        {
00182             if (n < 0)
00183             sprintf (msg, "INDI: input error.");
00184             else
00185             sprintf (msg, "INDI: agent closed connection.");
00186 
00187         stdDev->sNotifier->disconnect();
00188         close(stdDev->streamFD);
00189         KMessageBox::error(0, QString(msg));
00190             return;
00191        }
00192     }
00193     
00194     if (!processStream)
00195       return;
00196     
00197     streamFrame->newFrame(streamBuffer, streamWidth, streamHeight, colorFrame);
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     //set rwx for owner, rx for group, rx for other
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     { //need to upload to remote location
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    //delete (streamImage);
00288    //streamImage = NULL;
00289   
00290   //if (color)
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 */*ev*/)
00302 {
00303     
00304    if (streamImage)
00305    {
00306     if (streamImage->isNull()) return;
00307     //qPix = kPixIO.convertToPixmap(*streamImage);/*streamImage->smoothScale(width(), height()));*/
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"

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal