source: projectionDesigner/tag/ProjectionDesigner_1.1.5/projdesigner/src/gui/QProjectorWindow.cpp @ 2

Last change on this file since 2 was 2, checked in by Torben Dannhauer, 14 years ago
File size: 7.1 KB
Line 
1#include "ProjectionModel.h"
2#include "Channel.h"
3#include "Screen.h"
4#include "Warp.h"
5#include "RSync.h"
6#include "GUIControler.h"
7#include "gui/QProjectorWindow.h"
8
9#include <QtOpenGL>
10
11using namespace projection;
12
13bool QProjectorWindow::m_bInitialized = false;
14
15QProjectorWindow::QProjectorWindow(QWidget* pParent, QGLWidget* pSharedWidget, Qt::WFlags flag) : QGLWidget(pParent, pSharedWidget, flag|Qt::Window)
16{
17    m_pModel = NULL;
18    m_pChannel = NULL;
19
20    m_blendChannelIndex = 0;
21
22    resize(640, 480);
23
24    m_overlayFont.setFamily("Helvetica");
25    m_overlayFont.setPointSize(64);
26
27    m_infoFont.setFamily("Helvetica");
28    m_infoFont.setPointSize(14);
29
30    setFocusPolicy(Qt::StrongFocus);
31}
32
33QProjectorWindow::~QProjectorWindow()
34{
35}
36
37void QProjectorWindow::setModel(ProjectionModel* pModel)
38{
39    m_pModel = pModel;
40}
41
42void QProjectorWindow::setChannel(Channel* pChannel)
43{
44    m_pChannel = pChannel;
45        updateTitle();
46        updateGL();
47}
48
49void QProjectorWindow::updateTitle()
50{
51        if (m_pChannel)
52                setWindowTitle(m_pChannel->getName() + " - Projector Window");
53}
54
55void QProjectorWindow::initializeGL()
56{
57    if (!m_pModel->getRSync()->isServer() && m_bInitialized)
58    {
59        m_bInitialized = true;
60    }
61
62    glBlendFunc(GL_ONE, GL_ONE);
63    glClearColor(0.0, 0.0, 0.0, 0.0);
64}
65
66void QProjectorWindow::resizeGL(int width, int height)
67{
68    glViewport(0, 0, width, height);
69}
70
71void QProjectorWindow::paintGL()
72{
73    m_pModel->setRenderContext(this);
74
75    if (m_pChannel)
76    {
77        if (m_pModel->getDesignMode() == DESIGN_MODE_DISTORTIONMAP)
78            m_pChannel->draw(CHANNEL_DRAWMODE_DESIGN_DISTORTIONMAP);
79        else
80            m_pChannel->draw(CHANNEL_DRAWMODE_DESIGN_BLENDMAP);
81
82        if (m_pChannel->getOverlayName())
83        {
84            QFontMetrics fontMet(m_overlayFont);
85            renderText((width()-fontMet.width(m_pChannel->getName()))/2, (height()+fontMet.height()/2)/2, m_pChannel->getName(), m_overlayFont, 3000);
86        }
87
88        if (m_pModel->getRSync()->isServer() && m_pModel->getDesignMode() == DESIGN_MODE_BLENDMAP)
89        {
90            glPushAttrib(GL_CURRENT_BIT);
91            glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
92            Channel* pChannel = m_pModel->getChannel(m_blendChannelIndex);
93            if (pChannel)
94                renderText(16, 32, QString("%1 Blend factor : %2").arg(pChannel->getName()).arg(QString::number(m_pChannel->getBlendFactor(pChannel->getName()), 'f', 2)), m_infoFont, 4000);
95            renderText(16, 64, QString("[Space] : Select channel to edit blend factor."), m_infoFont, 4000);
96            renderText(16, 88, QString("[B] : Brighten the selected channel."), m_infoFont, 4000);
97            renderText(16, 112, QString("[D] : Darken the selected channel."), m_infoFont, 4000);
98            glPopAttrib();
99        }
100    }
101}
102
103void QProjectorWindow::keyReleaseEvent(QKeyEvent* pEvent)
104{
105    switch (pEvent->key())
106        {
107        case Qt::Key_Return:
108                if (pEvent->modifiers() & Qt::AltModifier)
109                {
110                        if (!isFullScreen())
111                                showFullScreen();
112                        else
113                                showNormal();
114                }
115                break;
116        case Qt::Key_Escape:
117        if (!m_pModel->getRSync()->isServer()) {
118            qApp->closeAllWindows();
119                return;
120        }
121        else
122            close();
123        break;
124        case Qt::Key_Space:
125        if (m_pModel) {
126            m_blendChannelIndex++;
127            if (m_blendChannelIndex >= m_pModel->getNumChannels())
128                m_blendChannelIndex = 0;
129            updateGL();
130        }
131        break;
132        case Qt::Key_B:
133        if (m_pModel) {
134            Channel* pChannel = m_pModel->getChannel(m_blendChannelIndex);
135            if (pChannel)
136            {
137                float blendFactor = m_pChannel->getBlendFactor(pChannel->getName());
138                        if ((pEvent->modifiers() & Qt::ShiftModifier) ||
139                    (pEvent->modifiers() & Qt::ControlModifier))
140                    blendFactor += 0.01f;
141                else
142                    blendFactor += 0.1f;
143                if (blendFactor > 1.0f)
144                    blendFactor = 1.0f;
145                m_pChannel->setBlendFactor(pChannel->getName(), blendFactor);
146                updateGL();
147            }
148        }
149        break;
150        case Qt::Key_D:
151        if (m_pModel) {
152            Channel* pChannel = m_pModel->getChannel(m_blendChannelIndex);
153            if (pChannel)
154            {
155                float blendFactor = m_pChannel->getBlendFactor(pChannel->getName());
156                        if ((pEvent->modifiers() & Qt::ShiftModifier) ||
157                    (pEvent->modifiers() & Qt::ControlModifier))
158                    blendFactor -= 0.01f;
159                else
160                    blendFactor -= 0.1f;
161                if (blendFactor < 0.0f)
162                    blendFactor = 0.0f;
163                m_pChannel->setBlendFactor(pChannel->getName(), blendFactor);
164                updateGL();
165            }
166        }
167        break;
168        }
169
170    if (m_pModel->getRSync()->isServer())
171    {
172        m_pModel->getGUI()->processChannelKeyEvent(m_pChannel, pEvent);
173    }
174}
175
176void QProjectorWindow::mousePressEvent(QMouseEvent* pEvent)
177{
178    if (m_pChannel)
179    {
180        if (m_pChannel->getCurrentWarp()->pick(pEvent->x(), pEvent->y()))
181                updateGL();
182    }
183}
184
185void QProjectorWindow::mouseMoveEvent(QMouseEvent* pEvent)
186{
187    if (pEvent->buttons() == Qt::LeftButton && m_pChannel)
188    {
189        if (m_pChannel->getCurrentWarp()->drag(pEvent->x(), pEvent->y())) {
190            m_pChannel->updateData();
191                updateGL();
192        }
193    }
194}
195
196void QProjectorWindow::wheelEvent(QWheelEvent* pEvent)
197{
198    if (m_pChannel)
199    {
200        if (pEvent->delta() > 0)
201            m_pChannel->getCurrentWarp()->setZoom(m_pChannel->getCurrentWarp()->getZoom()/1.2f);
202        else
203            m_pChannel->getCurrentWarp()->setZoom(m_pChannel->getCurrentWarp()->getZoom()*1.2f);
204        updateGL();
205    }
206}
207
208void QProjectorWindow::closeEvent(QCloseEvent* pEvent)
209{
210    if (m_pChannel)
211        m_pChannel->clearWarpBuffer();
212
213    QGLWidget::closeEvent(pEvent);
214}
215
216void QProjectorWindow::initFromDOMElement(const QDomElement& element)
217{
218    bool bShowFullScreen = false;
219
220    if (!element.isNull())
221        {
222                int posX = 16;
223                int posY = 16;
224                int width = 0;
225                int height = 0;
226
227                QDomElement geom = element.firstChildElement("Geometry");
228                posX = geom.attribute("posX").toInt();
229                posY = geom.attribute("posY").toInt();
230                width = geom.attribute("width").toInt();
231                height = geom.attribute("height").toInt();
232
233                move(posX, posY);
234                if (width!=0 && height!=0)
235                        resize(width, height);
236
237                bShowFullScreen = (geom.attribute("fullScreen").toLower() == "true");
238        }
239
240    if (!element.isNull() && element.attribute("show")=="true") {
241        if (bShowFullScreen)
242            showFullScreen();
243        else
244            showNormal();
245    } else
246        hide();
247}
248
249QDomElement QProjectorWindow::domElement(QDomDocument& doc) const
250{
251        QDomElement de = doc.createElement("ProjectorWindow");
252        de.setAttribute("show", isVisible()?"true":"false");
253
254        QDomElement geom = doc.createElement("Geometry");
255        geom.setAttribute("fullScreen", isFullScreen()?"true":"false");
256        geom.setAttribute("posX", x());
257        geom.setAttribute("posY", y());
258        geom.setAttribute("width", width());
259        geom.setAttribute("height", height());
260        de.appendChild(geom);
261
262        return de;
263}
Note: See TracBrowser for help on using the repository browser.