summaryrefslogtreecommitdiffstats
path: root/src/kviewmpeg2.cpp
blob: 419d1ebfc48b4b5f7eca5e2e7f99a4a24498dfcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**************************************************************************
*   Copyright (C) 2005 by Jean-Michel Petit                               *
*   jm_petit@laposte.net                                                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
***************************************************************************/
#include "k9common.h"
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqlabel.h>
#include <tqimage.h>
#include <tqpaintdevice.h>
#include <tqslider.h>
#include <tqapplication.h>
#include <tqtoolbutton.h>
#include <tqlayout.h>

#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <tdelocale.h>
#include <kiconloader.h>
#include "kviewmpeg2.h"
#include "k9config.h"



k9Widget::k9Widget(TQWidget *parent):TQWidget(parent) {
    m_image=NULL;
}

void k9Widget::setImage(TQImage *_image) {
    m_image=_image;
    //paintEvent( NULL);
    repaint(FALSE);
}

void k9Widget::paintEvent( TQPaintEvent *_event) {
    setPaletteBackgroundColor(TQt::black);
    int top,left;
    if (m_image !=NULL) {
        TQPainter p(this);
        double wratio=(double)width()/(double)m_image->width();
        double hratio=(double)height()/(double)m_image->height();
        double ratio= wratio < hratio ? wratio:hratio;

        top =(int) (height() -m_image->height()*ratio)/2+1;
        left =(int) (width() -m_image->width()*ratio)/2 +1;

        p.scale(ratio,ratio);

        p.drawImage((int)(left/ratio),(int)(top/ratio),*m_image);

        p.end();
    }
}

kViewMPEG2::kViewMPEG2() {
    m_widget=NULL;
    m_GLwidget=NULL;
    m_layout=NULL;
    bPlay->setPixmap(SmallIcon("media-playback-start"));
    bStop->setPixmap(SmallIcon("media-playback-stop"));
    connect(&m_player  , TQT_SIGNAL(setPosition(uint32_t)), this, TQT_SLOT(setPosition(uint32_t)));
    connect(&m_player  , TQT_SIGNAL(setMax(uint32_t)), this, TQT_SLOT(setMax(uint32_t)));
    connect(&m_player  , TQT_SIGNAL(setMin(uint32_t)), this, TQT_SLOT(setMin(uint32_t)));
    lockSlider=false;
    stopped=true;

    m_layout=new TQGridLayout(label,1,1);

#ifdef HAVE_OPENGL

    readSettings();
#else

    m_prefUseGL=FALSE;
#endif
    //disable the option use_gl, so if k9copy crash, we restart without gl
    if (m_prefUseGL) {
    	k9Config config;
    	config.setUseGL( false);
    	config.save();
    }
    m_player.getDecoder()->setUseGL(m_prefUseGL);
    if (m_prefUseGL)  {
        connect(m_player.getDecoder()  , TQT_SIGNAL(ppmReady(uchar *,int,int,int)), this, TQT_SLOT(drawppm(uchar *,int,int,int)));
        
        m_GLwidget= k9GLWidget::createWidget(label);
        m_widget=NULL;
        m_layout->addWidget(m_GLwidget,0,0);
        
        //init ok, we can put the gl flag to true
    	k9Config config;
    	config.setUseGL( true);
    	config.save();
        
    } else {
        connect(m_player.getDecoder()  , TQT_SIGNAL(pixmapReady(TQImage *)), this, TQT_SLOT(drawPixmap(TQImage *)));
        m_widget=new k9Widget(label);
        m_layout->addWidget(m_widget,0,0);

        m_GLwidget=NULL;
    }

}


void kViewMPEG2::readSettings() {
    m_player.stop();

    //KSimpleConfig settings("K9Copy");
    k9Config config;
    m_prefUseGL=config.getUseGL();
    //m_prefUseGL=settings.readEntry("/options/useGL",0).toInt();

}


kViewMPEG2::~kViewMPEG2() {
    stopped=true;
    m_player.stop();
}


void kViewMPEG2::lock() {
    mutex.lock();
}

void kViewMPEG2::unlock() {
    mutex.unlock();
}

void kViewMPEG2::setPosition( uint32_t _position) {
    if (!lockSlider)
        slider->setValue(_position);
}

void kViewMPEG2::setMax( uint32_t _position) {
    slider->setMaxValue(_position);
}

void kViewMPEG2::setMin(uint32_t _position) {
    slider->setMinValue(_position);
}


/** No descriptions */
void kViewMPEG2::drawPixmap(TQImage *image) {
    if (tqApp==NULL)
        return;
    if (tqApp->tryLock()) {
        m_widget->setImage( image);
        if (tqApp!=NULL)
            tqApp->unlock();
        }
}

/** No descriptions */
void kViewMPEG2::drawppm(uchar *_buf,int _width,int _height,int _len) {
    if (tqApp==NULL)
        return;
//    if (tqApp->tryLock()) {
        m_GLwidget->setImage(_buf,_width,_height,_len);
 //       if (tqApp !=NULL)
  //          tqApp->unlock();
 //   } else
 //       free(_buf);
}


void kViewMPEG2::bPlayClick() {
    m_player.play();
}


void kViewMPEG2::open (k9DVD *_dvd,k9DVDTitle * title,int chapter) {
    m_player.open(_dvd->getdvd()->getDvd(), _dvd->getDevice(),title,chapter);
}


/** No descriptions */
void kViewMPEG2::setError(const TQString & err) {
    error=true;
    errMsg=err;
}


void kViewMPEG2::resizeEvent(TQResizeEvent *_event) {}

void kViewMPEG2::bStopClick() {
    m_player.stop();
}

void kViewMPEG2::sliderReleased() {
    pause=true;
    int i;
    i=slider->value();
    m_player.updatePos(i);
    lockSlider=false;

}

void kViewMPEG2::closeEvent( TQCloseEvent* ce ) {
    stopped=true;
    ce->accept();
    return;

}
/** No descriptions */
void kViewMPEG2::sliderPressed() {
    lockSlider=true;
}