QtBaseViewerSynchronization.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8521 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:57:35 +0100 (Mi, 10. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS QtBaseViewer - IMPLEMENTATION
00048 //
00049 //=============================================================================
00050 
00051 
00052 //== INCLUDES =================================================================
00053 
00054 #include "QtBaseViewer.hh"
00055 #include "QtGLGraphicsScene.hh"
00056 #include "QtGLGraphicsView.hh"
00057 #include <QHostInfo>
00058 
00059 //== NAMESPACES ===============================================================
00060 
00061 namespace ACG {
00062 namespace QtWidgets {
00063 
00064 
00065 //== IMPLEMENTATION ==========================================================
00066 
00067 
00068 void QtBaseViewer::sync_connect(const QtBaseViewer* _other)
00069 {
00070   connect(_other,
00071           SIGNAL(signalSetView(const GLMatrixd&, const GLMatrixd&)),
00072           this,
00073           SLOT(setView(const GLMatrixd&, const GLMatrixd&)));
00074 }
00075 
00076 
00077 //-----------------------------------------------------------------------------
00078 
00079 
00080 void QtBaseViewer::sync_disconnect(const QtBaseViewer* _other)
00081 {
00082   disconnect(_other,
00083              SIGNAL(signalSetView(const GLMatrixd&, const GLMatrixd&)),
00084              this,
00085              SLOT(setView(const GLMatrixd&, const GLMatrixd&)));
00086 }
00087 
00088 
00089 //-----------------------------------------------------------------------------
00090 
00091 
00092 void QtBaseViewer::sync_send(const GLMatrixd&, const GLMatrixd&)
00093 {
00094   const GLMatrixd m = glstate_->modelview();
00095   const GLMatrixd p = glstate_->projection();
00096 
00097   QByteArray   datagram;
00098   QDataStream  out(&datagram, QIODevice::WriteOnly);
00099   out.setVersion(5);
00100 
00101   quint64 myId = (quint64) winId();
00102 
00103   out << myId
00104       << m(0,0) << m(0,1) << m(0,2) << m(0,3)
00105       << m(1,0) << m(1,1) << m(1,2) << m(1,3)
00106       << m(2,0) << m(2,1) << m(2,2) << m(2,3)
00107       << m(3,0) << m(3,1) << m(3,2) << m(3,3)
00108       << p(0,0) << p(0,1) << p(0,2) << p(0,3)
00109       << p(1,0) << p(1,1) << p(1,2) << p(1,3)
00110       << p(2,0) << p(2,1) << p(2,2) << p(2,3)
00111       << p(3,0) << p(3,1) << p(3,2) << p(3,3)
00112       << glWidth() << glHeight()
00113       << projectionMode_
00114       << orthoWidth_;
00115 
00116   for (int h=0, n=sync_hosts_.size(); h<n; ++h)
00117     for (int i=6666; i<6676; ++i)
00118       if (i != socket_->localPort() || h != 0)
00119              socket_->writeDatagram( datagram, datagram.size(),sync_hosts_[h], i);
00120 }
00121 
00122 
00123 void QtBaseViewer::sync_receive()
00124 {
00125 GLMatrixd     m, p;
00126   int           w, h, pMode;
00127   quint64       id, myId = (quint64) winId();
00128 
00129 
00130   QByteArray  datagram( socket_->pendingDatagramSize(), 'x' );
00131   socket_->readDatagram( datagram.data(), datagram.size() );
00132 
00133 
00134   if (datagram.size() < 280)
00135      return;
00136 
00137   QDataStream  in( & datagram, QIODevice::ReadOnly);
00138   in.setVersion(5);
00139 
00140 
00141   in >> id;
00142   if (id == myId)
00143     return;
00144 
00145   in >> m(0,0) >> m(0,1) >> m(0,2) >> m(0,3)
00146      >> m(1,0) >> m(1,1) >> m(1,2) >> m(1,3)
00147      >> m(2,0) >> m(2,1) >> m(2,2) >> m(2,3)
00148      >> m(3,0) >> m(3,1) >> m(3,2) >> m(3,3)
00149      >> p(0,0) >> p(0,1) >> p(0,2) >> p(0,3)
00150      >> p(1,0) >> p(1,1) >> p(1,2) >> p(1,3)
00151      >> p(2,0) >> p(2,1) >> p(2,2) >> p(2,3)
00152      >> p(3,0) >> p(3,1) >> p(3,2) >> p(3,3)
00153      >> w >> h >> pMode >> orthoWidth_;
00154 
00155 
00156   blockSignals(true);
00157 
00158   makeCurrent();
00159 
00160   if (projectionMode_ != (ProjectionMode)pMode)
00161     projectionMode((ProjectionMode)pMode);
00162 
00163   glstate_->set_modelview(m);
00164 
00165   if (w>0 && h>0 &&
00166       action_["PasteDropSize"]->isChecked() )
00167   {
00168     glstate_->set_projection(p);
00169     glView_->setFixedSize(w,h);
00170     updateGeometry();
00171   }
00172 
00173   blockSignals(false);
00174 
00175   updateGL();
00176 }
00177 
00178 
00179 //-----------------------------------------------------------------------------
00180 
00181 
00182 void QtBaseViewer::setSynchronization(bool _b)
00183 {
00184   synchronized_ = _b;
00185   action_["Synchronize"]->setChecked( synchronized_ );
00186 
00187   if (synchronized_)
00188   {
00189     connect(socket_,
00190             SIGNAL(readyRead()),
00191             this,
00192             SLOT(sync_receive()));
00193 
00194     connect(this,
00195             SIGNAL(signalSetView(const GLMatrixd&, const GLMatrixd&)),
00196             this,
00197             SLOT(sync_send(const GLMatrixd&, const GLMatrixd&)));
00198   }
00199   else
00200   {
00201     disconnect(socket_,
00202                SIGNAL( readyRead() ),
00203                this,
00204                SLOT(sync_receive()));
00205 
00206     disconnect(this,
00207                SIGNAL(signalSetView(const GLMatrixd&, const GLMatrixd&)),
00208                this,
00209                SLOT(sync_send(const GLMatrixd&, const GLMatrixd&)));
00210   }
00211 
00212 }
00213 
00214 
00215 //----------------------------------------------------------------------------
00216 
00217 
00218 bool QtBaseViewer::add_sync_host(const QString& _name)
00219 {
00220   QHostAddress adr;
00221   if (adr.setAddress(_name))
00222   {
00223     add_sync_host(adr);
00224     return true;
00225   }
00226 
00227   // QT4
00228 
00229   // use DNS otherwise
00230   QHostInfo hi = QHostInfo::fromName( _name );
00231   QList<QHostAddress> list = hi.addresses();
00232   if (!list.empty())
00233   {
00234     add_sync_host(list.front());
00235     return true;
00236   }
00237 
00238 
00239   return false;
00240 }
00241 
00242 
00243 void QtBaseViewer::add_sync_host(QHostAddress& _adr)
00244 {
00245   sync_hosts_.push_back(_adr);
00246 }
00247 
00248 //=============================================================================
00249 } // namespace QtWidgets
00250 } // namespace ACG
00251 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .