00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "GUI/QtDisplayStream4.H"
00039
00040 #include "Component/GlobalOpts.H"
00041 #include "Component/ModelOptionDef.H"
00042 #include "GUI/QtImageStack.qt.H"
00043 #include "Image/Image.H"
00044 #include "Image/Pixels.H"
00045 #include "QtUtil/ImageConvert4.H"
00046 #include "QtUtil/Util.H"
00047 #include "Raster/GenericFrame.H"
00048 #include "Transport/FrameInfo.H"
00049 #include "Transport/FrameOstreamFactory.H"
00050 #include "Transport/TransportOpts.H"
00051 #include "Util/sformat.H"
00052
00053 #include <QtGui/QApplication>
00054 #include <QtGui/QPixmap>
00055 #include <QtCore/QReadWriteLock>
00056
00057
00058 const ModelOptionDef OPT_QdisplayEcho =
00059 { MODOPT_ARG_STRING, "QdisplayEcho", &MOC_OUTPUT, OPTEXP_CORE,
00060 "Optional output destination for screen-grabs of the Qt display frames.",
00061 "qdisplay-echo", '\0', "<raster|display|mpeg|none>", "none" };
00062
00063
00064 const ModelOptionDef OPT_QdisplayPrefDims =
00065 { MODOPT_ARG(Dims), "QdisplayPrefDims", &MOC_OUTPUT, OPTEXP_CORE,
00066 "Preferred scaled dimensions for Qt display frames, "
00067 "or 0x0 for no preference",
00068 "qdisplay-pref-dims", '\0', "<WxH>", "640x480" };
00069
00070
00071 const ModelOptionDef OPT_QdisplayPrefMaxDims =
00072 { MODOPT_ARG(Dims), "QdisplayPrefMaxDims", &MOC_OUTPUT, OPTEXP_CORE,
00073 "If an image larger than these dimensions is sent to a Qt display "
00074 "frame, initially scale it down to smaller than these dimensions "
00075 "(you can always scale it up later by using the buttons on the "
00076 "Qt display widget",
00077 "qdisplay-pref-max-dims", '\0', "<WxH>", "1600x1200" };
00078
00079 #define EVT_WRITEFRAME (QEvent::User)
00080 #define EVT_REMOVEFRAME ((QEvent::Type)(int(QEvent::User) + 1))
00081
00082 namespace
00083 {
00084
00085 void *qapp_thread_run(void *c)
00086 {
00087 QtDisplayStream *d = reinterpret_cast<QtDisplayStream *>(c);
00088 return d->run();
00089 }
00090
00091
00092 class QEventWriteFrame : public QEvent
00093 {
00094 public:
00095 QEventWriteFrame(const GenericFrame& fram, const std::string& shortnam, const FrameInfo& auxinf)
00096 : QEvent(EVT_WRITEFRAME), frame(fram), shortname(shortnam), auxinfo(auxinf) { }
00097 virtual ~QEventWriteFrame() { }
00098
00099 const GenericFrame frame;
00100 const std::string shortname;
00101 const FrameInfo auxinfo;
00102 };
00103
00104
00105 class QEventRemoveFrame : public QEvent
00106 {
00107 public:
00108 QEventRemoveFrame(const std::string& shortnam) : QEvent(EVT_REMOVEFRAME), shortname(shortnam) { }
00109 virtual ~QEventRemoveFrame() { }
00110
00111 const std::string shortname;
00112 };
00113 }
00114
00115
00116
00117 class EventFilt : public QObject {
00118 public:
00119 EventFilt(QtDisplayStream *master) : m(master) { }
00120
00121 virtual ~EventFilt() { }
00122
00123 protected:
00124 virtual bool eventFilter(QObject *obj, QEvent *event)
00125 {
00126 if (obj == m->itsWidget) {
00127 if (event->type() == EVT_WRITEFRAME)
00128 {
00129 QEventWriteFrame *e = static_cast<QEventWriteFrame*>(event);
00130 m->itsWidget->addFrame(e->frame, e->shortname, e->auxinfo);
00131 if (!m->itsShown && !m->itsTestMode.getVal()) { m->itsShown = true; m->itsWidget->show(); }
00132 return true;
00133 }
00134 else if (event->type() == EVT_REMOVEFRAME)
00135 {
00136 QEventRemoveFrame *e = static_cast<QEventRemoveFrame*>(event);
00137 m->itsWidget->removeFrame(e->shortname);
00138 return true;
00139 }
00140 else
00141 return false;
00142 }
00143
00144
00145 return QObject::eventFilter(obj, event);
00146 }
00147
00148 private:
00149 QtDisplayStream *m;
00150 };
00151
00152
00153
00154 QtDisplayStream::QtDisplayStream(OptionManager& mgr,
00155 const std::string& descrName,
00156 const std::string& tagName) :
00157 FrameOstream(mgr, descrName, tagName),
00158 itsQtEcho(&OPT_QdisplayEcho, this),
00159 itsPreferredDims(&OPT_QdisplayPrefDims, this),
00160 itsPreferredMaxDims(&OPT_QdisplayPrefMaxDims, this),
00161 itsTestMode(&OPT_TestMode, this),
00162 itsWidget(0), itsFilter(0),
00163 itsShown(false),
00164 itsFrameNumber(-1),
00165 itsEcho(), itsReady(false)
00166 { }
00167
00168
00169 void QtDisplayStream::start1()
00170 {
00171
00172 if (0 != pthread_create(&runner, NULL, &qapp_thread_run, this)) LFATAL("couldn't create thread for QApplication");
00173
00174 while(itsReady == false) usleep(10000);
00175 }
00176
00177
00178 QtDisplayStream::~QtDisplayStream()
00179 {
00180 itsWidget->deleteLater();
00181 }
00182
00183
00184 void* QtDisplayStream::run()
00185 {
00186
00187 const char* dpy = getenv("DISPLAY");
00188 const bool havedpy = (dpy != 0 && dpy[0] != '\0');
00189 if (!havedpy) {
00190 if (setenv("DISPLAY", ":0.0", 1) == 0) LINFO("DISPLAY environment variable not set; assuming DISPLAY=\":0.0\"");
00191 else LFATAL("DISPLAY environment variable not set; can't use Qt without a DISPLAY");
00192 }
00193
00194
00195 int argc = 1; const char* argv[1] = { "QtDisplayStream" };
00196 QApplication *a = new QApplication(argc, argv2qt(argc, argv));
00197
00198
00199 itsWidget = new QtImageStack;
00200 itsWidget->setPreferredDims(itsPreferredDims.getVal());
00201 itsWidget->setPreferredMaxDims(itsPreferredMaxDims.getVal());
00202
00203
00204 itsFilter = new EventFilt(this);
00205 itsWidget->installEventFilter(itsFilter);
00206
00207
00208 itsReady = true;
00209
00210
00211 const int result = a->exec();
00212 LDEBUG("QApplication exit status %d", result);
00213 return reinterpret_cast<void*>(result);
00214 }
00215
00216
00217 void QtDisplayStream::paramChanged(ModelParamBase* const param,
00218 const bool valueChanged,
00219 ParamClient::ChangeStatus* status)
00220 {
00221 FrameOstream::paramChanged(param, valueChanged, status);
00222
00223 if (param == &itsQtEcho && itsQtEcho.getVal().length() > 0)
00224 {
00225 if (itsQtEcho.getVal().compare("none") == 0)
00226 {
00227 if (itsEcho.is_valid()) this->removeSubComponent(*itsEcho);
00228 itsEcho = nub::soft_ref<FrameOstream>();
00229 }
00230 else
00231 {
00232 itsEcho = makeFrameOstream(itsQtEcho.getVal(), this->getManager());
00233 this->addSubComponent(itsEcho);
00234 itsEcho->exportOptions(MC_RECURSE);
00235 }
00236 }
00237 }
00238
00239
00240 bool QtDisplayStream::setFrameNumber(int n)
00241 {
00242 if (itsFrameNumber != n)
00243 {
00244 if (itsEcho.is_valid())
00245 {
00246 QPixmap pixmap = QPixmap::grabWidget(itsWidget);
00247 Image<PixRGB<byte> > img = convertToImage4(pixmap);
00248 itsEcho->setFrameNumber(n);
00249 itsEcho->writeRGB(img, "qtecho");
00250 }
00251
00252 itsFrameNumber = n;
00253 }
00254
00255 return itsWidget->setFrameNumber(n);
00256 }
00257
00258
00259 void QtDisplayStream::writeFrame(const GenericFrame& frame,
00260 const std::string& shortname,
00261 const FrameInfo& auxinfo)
00262 {
00263 QEventWriteFrame *event = new QEventWriteFrame(frame, shortname, auxinfo);
00264 QApplication::postEvent(itsWidget, event);
00265 }
00266
00267
00268 bool QtDisplayStream::isVoid() const
00269 {
00270 return itsWidget->isClosed();
00271 }
00272
00273
00274 void QtDisplayStream::closeStream(const std::string& shortname)
00275 {
00276 QEventRemoveFrame *event = new QEventRemoveFrame(shortname);
00277 QApplication::postEvent(itsWidget, event);
00278 }
00279
00280
00281 bool QtDisplayStream::isClosed() const
00282 {
00283 return itsWidget->isClosed();
00284 }
00285
00286
00287
00288
00289
00290
00291