summaryrefslogtreecommitdiffstats
path: root/src/rip/videodvd/k3bvideodvdrippingtitlelistview.cpp
blob: e0ac27eb829185da8abb4d3621611e804523ae5b (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bvideodvdrippingtitlelistview.h"
#include "k3bvideodvdrippingpreview.h"

#include <k3btooltip.h>
#include <k3bapplication.h>
#include <k3bmediacache.h>

#include <k3bvideodvd.h>
#include <k3bvideodvdaudiostream.h>
#include <k3bvideodvdvideostream.h>
#include <k3bvideodvdsubpicturestream.h>

#include <tqsimplerichtext.h>
#include <tqfontmetrics.h>
#include <tqpainter.h>
#include <tqheader.h>
#include <tqtooltip.h>

#include <klocale.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kapplication.h>


static TQString audioStreamString( const K3bVideoDVD::Title& title, unsigned int maxLines = 9999, bool includeExtInfo = true )
{
  TQString s = "<p>";
  for( unsigned int i = 0; i < TQMIN( title.numAudioStreams(), maxLines ); ++i ) {
    if( i > 0 )
      s += "<br>";
    s += TQString::number(i+1) + ": " 
      + i18n("%1 %2Ch (%3<em>%4</em>)")
      .arg( K3bVideoDVD::audioFormatString( title.audioStream(i).format() ) )
      .arg( title.audioStream(i).channels() )
      .arg( title.audioStream(i).langCode().isEmpty()
	    ? i18n("unknown language")
	    : TDEGlobal::locale()->twoAlphaToLanguageName( title.audioStream(i).langCode() ) )
      .arg( includeExtInfo && title.audioStream(i).codeExtension() != K3bVideoDVD::AUDIO_CODE_EXT_UNSPECIFIED 
	    ? TQString(" ") + K3bVideoDVD::audioCodeExtensionString( title.audioStream(i).codeExtension() )
	    : TQString() );
  }
  if( title.numAudioStreams() > maxLines )
    s += "...";

  return s;
}


static TQString subpictureStreamString( const K3bVideoDVD::Title& title, unsigned int maxLines = 9999, bool includeExtInfo = true )
{
  TQString s = "<p>";
  for( unsigned int i = 0; i < TQMIN( title.numSubPictureStreams(), maxLines ); ++i ) {
    if( i > 0 )
      s += "<br>";
    s += TQString::number(i+1) + ": " 
      + TQString("%1 (%2<em>%3</em>)")
      .arg( title.subPictureStream(i).codeMode() == K3bVideoDVD::SUBPIC_CODE_MODE_RLE 
	    ? i18n("RLE")
	    : i18n("Extended") )
      .arg( title.subPictureStream(i).langCode().isEmpty()
	    ? i18n("unknown language")
	    : TDEGlobal::locale()->twoAlphaToLanguageName( title.subPictureStream(i).langCode() ) )
      .arg( includeExtInfo && title.subPictureStream(i).codeExtension() != K3bVideoDVD::SUBPIC_CODE_EXT_UNSPECIFIED 
	    ? TQString(" ") + K3bVideoDVD::subPictureCodeExtensionString( title.subPictureStream(i).codeExtension() )
	    : TQString() );
  }
  if( title.numSubPictureStreams() > maxLines )
    s += "...";

  return s;
}



class K3bVideoDVDRippingTitleListView::TitleViewItem : public K3bCheckListViewItem
{
public:
  TitleViewItem( K3bVideoDVDRippingTitleListView* parent, TQListViewItem* after, const K3bVideoDVD::Title& title ) 
    : K3bCheckListViewItem( parent, after ),
      m_title( title ) {

    setMarginVertical( 4 );
    setMarginHorizontal( 1, 2 );
    setMarginHorizontal( 2, 2 );
    setMarginHorizontal( 3, 2 );
    setMarginHorizontal( 4, 2 );
    setMarginHorizontal( 5, 2 );
    setChecked(true);

    m_previewSet = false;
  }

  const K3bVideoDVD::Title& videoDVDTitle() const { return m_title; }

  void setup() {
    widthChanged();    

    // set a valid height
    int maxH = 0;
    for( int c = 1; c <= 4; ++c ) {
      TQSimpleRichText rt( text(c), listView()->font() );
      rt.setWidth( 600 ); // way to big to avoid line breaks
      maxH = TQMAX( maxH, rt.height() );
    }

    setHeight( maxH + 2*marginVertical() );
  }

  int width( const TQFontMetrics& fm, const TQListView* lv, int c ) const {
    if( c == 0 )
      return K3bCheckListViewItem::width( fm, lv, c );
    else {
      TQSimpleRichText rt( text(c), lv->font() );
      rt.setWidth( 600 ); // way to big to avoid line breaks
      return rt.widthUsed() + 2*marginHorizontal( c );
    }
  }

  void setPreview( const TQImage& preview ) {
    m_preview = preview;
    m_scaledPreview = TQPixmap();

    m_previewSet = true;

    repaint();
  }

  const TQImage& preview() const {
    return m_preview;
  }

protected:
  void paintK3bCell( TQPainter* p, const TQColorGroup& cg, int col, int w, int align ) {
    p->save();

    if( col == 0 ) {
      // the check mark
      K3bCheckListViewItem::paintK3bCell( p, cg, col, w, align );
    }
    else if( col == 2 ) {
      if( isSelected() ) {
	p->fillRect( 0, 0, w, height(),
		     cg.brush( TQColorGroup::Highlight ) );
	p->setPen( cg.highlightedText() );
      }
      else {
	p->fillRect( 0, 0, w, height(), cg.base() ); 
	p->setPen( cg.text() );
      }

      // draw the preview
      int h = height();
      h -= 2*marginVertical();
      h -= 1; // the separator
      if( !m_preview.isNull() ) {
	if( m_scaledPreview.height() != h ) {
	  // recreate scaled preview
	  int preH = m_preview.height()*w/m_preview.width();
	  int preW = m_preview.width()*h/m_preview.height();
	  if( preH > h )
	    preH = m_preview.height()*preW/m_preview.width();
	  if( preW > w )
	    preW = m_preview.width()*preH/m_preview.height();
	  m_scaledPreview.convertFromImage( m_preview.smoothScale( preW, preH ), 0 );
	}

	// center the preview in the column
	int yPos = ( height() - m_scaledPreview.height() ) / 2;
	int xPos = ( w - m_scaledPreview.width() ) / 2;

	p->drawPixmap( xPos, yPos, m_scaledPreview );
      }
      else if( m_previewSet ) {
	int preW = 0;
	if( m_title.videoStream().displayAspectRatio()	== K3bVideoDVD::VIDEO_ASPECT_RATIO_4_3 )
	  preW = h*4/3;
	else
	  preW = h*16/9;

	p->drawRect( ( w - preW ) / 2, ( height() - h ) / 2, preW, h );
	TQPixmap noIcon = TDEApplication::kApplication()->iconLoader()->loadIcon( "no", TDEIcon::NoGroup, TDEIcon::SizeSmall, TDEIcon::DefaultState, 0, true );
	p->drawPixmap( ( w - noIcon.width() ) / 2, ( height() - noIcon.height() ) / 2, noIcon );
      }
      else {
	p->drawText( 0, 0, w, height(), TQt::AlignCenter, "..." );
      }
    }
    else {
      TQString s = text( col );
      if( s.isEmpty() )
	K3bCheckListViewItem::paintK3bCell( p, cg, col, w, align );
      else {
	TQColorGroup cg1( cg );
	if( isSelected() ) {
	  p->fillRect( 0, 0, w, height(),
		       cg.brush( TQColorGroup::Highlight ) );
	  cg1.setColor( TQColorGroup::Text, cg.highlightedText() );
	}
	else {
	  p->fillRect( 0, 0, w, height(), cg.base() ); 
	}

	// paint using TQSimpleRichText
	TQSimpleRichText rt( text(col), listView()->font() );
	rt.setWidth( 600 ); // way to big to avoid line breaks
	// normally we would have to clip the height to height()-2*marginVertical(). But if we do that
	// some characters are cut (such as p or q). It seems as if TQSimpleRichText does not properly 
	// calculate it's height...
	rt.draw( p, 0, marginVertical(), TQRect( 0, 0, w, height() ), cg1 );
      }
    }

    // draw the separator
    if( listView()->firstChild() != this ) {
      p->translate( -1*marginHorizontal(col), 0 );
      // FIXME: modify the value from palette().disabled().foreground() to be lighter (or darker, depending on the background color )
      p->setPen( TQt::lightGray );
      p->drawLine( 0, 0, w+2*marginHorizontal(col), 0 );
    }

    p->restore();
  }

private:
  TQString text( int col ) const {
    switch( col ) {
    case 1:
      // Title X + length
      return i18n("<p><b>Title %1 (%2)</b><br>"
		  "%3")
	.arg( m_title.titleNumber(), 2 )
	.arg( m_title.playbackTime().toString( false ) )
	.arg( i18n("%n chapter", "%n chapters", m_title.numPTTs() ) );

    case 3:
      // video stream info
      return TQString("<p>%1 %2x%3<br>%4%5")
	.arg( m_title.videoStream().mpegVersion() == 0 ? i18n("MPEG1") : i18n("MPEG2") )
	.arg( m_title.videoStream().pictureWidth() )
	.arg( m_title.videoStream().pictureHeight() )
	.arg( m_title.videoStream().displayAspectRatio() == K3bVideoDVD::VIDEO_ASPECT_RATIO_4_3 ? "4:3" : "16:9" )
	.arg( m_title.videoStream().letterboxed() ? TQString(" - <em>") + i18n("letterboxed") + TQString("</em>"): 
	      m_title.videoStream().permittedDf() == K3bVideoDVD::VIDEO_PERMITTED_DF_LETTERBOXED 
	      ? TQString(" - <em>") + i18n("anamorph") + TQString("</em>") : TQString() );

    case 4:
      // audio streams info
      if( m_title.numAudioStreams() > 0 )
	return audioStreamString( m_title, 2, false );
      else
	return "<p><small><em>" + i18n("No audio streams") + "</em>";

    case 5:
      // subpicture streams info
      if( m_title.numSubPictureStreams() > 0 )
	return subpictureStreamString( m_title, 2, false );
      else
	return "<p><small><em>" + i18n("No Subpicture streams") + "</em>";

    default:
      return K3bCheckListViewItem::text( col );
    }
  }

  K3bVideoDVD::Title m_title;

  bool m_previewSet;
  TQImage m_preview;
  TQPixmap m_scaledPreview;
};


class K3bVideoDVDRippingTitleListView::TitleToolTip : public K3bToolTip
{
public:
  TitleToolTip( K3bVideoDVDRippingTitleListView* view )
    : K3bToolTip( view->viewport() ),
      m_view( view ) {
  }

  void maybeTip( const TQPoint& pos ) {
    TitleViewItem* item = static_cast<TitleViewItem*>( m_view->itemAt( pos ) );
    TQPoint contentsPos = m_view->viewportToContents( pos );
    if( !item )
      return;
    int col = m_view->header()->sectionAt( contentsPos.x() );

    TQRect r = m_view->itemRect( item );
    int headerPos = m_view->header()->sectionPos( col );
    r.setLeft( headerPos );
    r.setRight( headerPos + m_view->header()->sectionSize( col ) );

    switch( col ) {
    case 2:
      if( !item->preview().isNull() ) {
	TQPixmap previewPix;
	if( previewPix.convertFromImage( item->preview() ) )
	  tip( r, previewPix, 0 );
      }
      break;
    case 4:
      if( item->videoDVDTitle().numAudioStreams() > 0 )
	tip( r, "<p><b>" + i18n("Audio Streams") + "</b><p>" + audioStreamString( item->videoDVDTitle() ), 0 );
      break;
    case 5:
      if( item->videoDVDTitle().numSubPictureStreams() > 0 )
	tip( r, "<p><b>" + i18n("Subpicture Streams") + "</b><p>" + subpictureStreamString( item->videoDVDTitle() ), 0 );
      break;
    }
  }

private:
  K3bVideoDVDRippingTitleListView* m_view;
};



K3bVideoDVDRippingTitleListView::K3bVideoDVDRippingTitleListView( TQWidget* parent )
  : K3bListView( parent )
{
  setFullWidth(true);
  setSorting(-1);
  setAllColumnsShowFocus( true );
  setSelectionModeExt( Single );

  addColumn( "" );
  addColumn( i18n("Title") );
  addColumn( i18n("Preview") );
  addColumn( i18n("Video") );
  addColumn( i18n("Audio") );
  addColumn( i18n("Subpicture") );

  header()->setClickEnabled( false );
  setColumnWidthMode( 0, TQListView::Manual );
  setColumnWidth( 0, 20 );
  header()->setResizeEnabled( false, 0 );

  m_toolTip = new TitleToolTip( this );

  m_previewGen = new K3bVideoDVDRippingPreview( TQT_TQOBJECT(this) );
  connect( m_previewGen, TQT_SIGNAL(previewDone(bool)),
	   this, TQT_SLOT(slotPreviewDone(bool)) );
}


K3bVideoDVDRippingTitleListView::~K3bVideoDVDRippingTitleListView()
{
  delete m_toolTip;
}


void K3bVideoDVDRippingTitleListView::setVideoDVD( const K3bVideoDVD::VideoDVD& dvd )
{
  clear();

  m_dvd = dvd;
  m_medium = k3bappcore->mediaCache()->medium( m_dvd.device() );
  m_itemMap.resize( dvd.numTitles() );

  for( unsigned int i = 0; i < dvd.numTitles(); ++i )
    m_itemMap[i] = new TitleViewItem( this, lastItem(), dvd.title(i) );

  m_currentPreviewTitle = 1;
  m_previewGen->generatePreview( m_dvd, 1 );
}


void K3bVideoDVDRippingTitleListView::slotPreviewDone( bool success )
{
  if( success )
    m_itemMap[m_currentPreviewTitle-1]->setPreview( m_previewGen->preview() );
  else
    m_itemMap[m_currentPreviewTitle-1]->setPreview( TQImage() );

  // cancel if we got hidden or if the medium changed.
  if( isVisible() && m_medium == k3bappcore->mediaCache()->medium( m_dvd.device() ) ) {
    ++m_currentPreviewTitle;
    if( m_currentPreviewTitle <= m_dvd.numTitles() )
      m_previewGen->generatePreview( m_dvd, m_currentPreviewTitle );
  }
}


void K3bVideoDVDRippingTitleListView::hideEvent( TQHideEvent* e )
{
  //
  // For now we do it the easy way: just stop the preview generation
  // once this view is hidden
  //
  m_previewGen->cancel();

  K3bListView::hideEvent( e );
}

#include "k3bvideodvdrippingtitlelistview.moc"