summaryrefslogtreecommitdiffstats
path: root/src/k3bwelcomewidget.cpp
blob: 334834c05ff01be2b6c6b61165f0ab33bb8d6579 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 *
 * $Id: k3bwelcomewidget.cpp 676186 2007-06-16 08:53:46Z trueg $
 * Copyright (C) 2003-2007 Sebastian Trueg <[email protected]>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
 *
 * 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 "k3bwelcomewidget.h"
#include "k3b.h"
#include "k3bflatbutton.h"
#include <k3bstdguiitems.h>
#include "k3bapplication.h"
#include <k3bversion.h>
#include "k3bthememanager.h"

#include <tqpixmap.h>
#include <tqtoolbutton.h>
#include <tqlabel.h>
#include <tqpainter.h>
#include <tqsimplerichtext.h>
#include <tqptrlist.h>
#include <tqmap.h>
#include <tqtooltip.h>
#include <tqcursor.h>
#include <tqimage.h>

#include <kurl.h>
#include <kurldrag.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdeapplication.h>
#include <kiconloader.h>
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdepopupmenu.h>
#include <tdeaboutdata.h>
#include <tdeactionclasses.h>


static const char* s_allActions[] = {
  "file_new_data",
  "file_new_dvd",
  "file_continue_multisession",
  "_sep_",
  "file_new_audio",
  "_sep_",
  "file_new_mixed",
  "_sep_",
  "file_new_vcd",
  "file_new_video_dvd",
  "_sep_",
  "file_new_movix",
  "file_new_movix_dvd",
  "_sep_",
  "tools_copy_cd",
  "tools_copy_dvd",
  "_sep_",
  "tools_blank_cdrw",
  "tools_format_dvd",
  "_sep_",
  "tools_write_cd_image",
  "tools_write_dvd_iso",
  "_sep_",
  "tools_cdda_rip",
  "tools_videodvd_rip",
  "tools_videocd_rip",
  0
};

K3bWelcomeWidget::Display::Display( K3bWelcomeWidget* parent )
  : TQWidget( parent->viewport() )
{
  setWFlags( TQt::WNoAutoErase );

  TQFont fnt(font());
  fnt.setBold(true);
  fnt.setPointSize( 16 );
  m_header = new TQSimpleRichText( i18n("Welcome to K3b - The CD and DVD Kreator"), fnt );
  m_infoText = new TQSimpleRichText( TQString::fromUtf8("<qt align=\"center\">K3b %1 (c) 1999 - 2007 Sebastian Trüg")
				    .arg(kapp->aboutData()->version()), font() );

  // set a large width just to be sure no linebreak occurs
  m_header->setWidth( 800 );

  setAcceptDrops( true );
  setBackgroundMode( PaletteBase );
  m_rows = m_cols = 1;

  m_buttonMore = new K3bFlatButton( i18n("Further actions..."), this );
  connect( m_buttonMore, TQT_SIGNAL(pressed()), parent, TQT_SLOT(slotMoreActions()) );

  connect( k3bappcore->themeManager(), TQT_SIGNAL(themeChanged()), this, TQT_SLOT(slotThemeChanged()) );

  slotThemeChanged();
}


K3bWelcomeWidget::Display::~Display()
{
  delete m_header;
  delete m_infoText;
}


void K3bWelcomeWidget::Display::addAction( TDEAction* action )
{
  if( action ) {
    m_actions.append(action);
    rebuildGui();
  }
}


void K3bWelcomeWidget::Display::removeAction( TDEAction* action )
{
  if( action ) {
    m_actions.removeRef( action );
    rebuildGui();
  }
}


void K3bWelcomeWidget::Display::removeButton( K3bFlatButton* b )
{
  removeAction( m_buttonMap[b] );
}


void K3bWelcomeWidget::Display::rebuildGui( const TQPtrList<TDEAction>& actions )
{
  m_actions = actions;
  rebuildGui();
}


static void calculateButtons( int width, int numActions, int buttonWidth, int& cols, int& rows )
{
  // always try to avoid horizontal scrollbars
  int wa = width - 40;
  cols = TQMAX( 1, TQMIN( wa / (buttonWidth+4), numActions ) );
  rows = numActions/cols;
  int over = numActions%cols;
  if( over ) {
    rows++;
    // try to avoid useless cols
    while( over && cols - over - 1 >= rows-1 ) {
      --cols;
      over = numActions%cols;
    }
  }
}


void K3bWelcomeWidget::Display::rebuildGui()
{
  // step 1: delete all old buttons in the buttons TQPtrList<K3bFlatButton>
  m_buttonMap.clear();
  m_buttons.setAutoDelete(true);
  m_buttons.clear();

  int numActions = m_actions.count();
  if( numActions > 0 ) {

    // create buttons
    for( TQPtrListIterator<TDEAction> it( m_actions ); it.current(); ++it ) {
      TDEAction* a = it.current();

      K3bFlatButton* b = new K3bFlatButton( a, this );

      m_buttons.append( b );
      m_buttonMap.insert( b, a );
    }

    // determine the needed button size (since all buttons should be equal in size
    // we use the max of all sizes)
    m_buttonSize = m_buttons.first()->sizeHint();
    for( TQPtrListIterator<K3bFlatButton> it( m_buttons ); it.current(); ++it ) {
      m_buttonSize = m_buttonSize.expandedTo( it.current()->sizeHint() );
    }

    repositionButtons();
  }
}


void K3bWelcomeWidget::Display::repositionButtons()
{
  // calculate rows and columns
  calculateButtons( width(), m_actions.count(), m_buttonSize.width(), m_cols, m_rows );

  int availHor = width() - 40;
  int availVert = height() - 20 - 10 - m_header->height() - 10;
  availVert -= m_infoText->height() - 10;
  int leftMargin = 20 + (availHor - (m_buttonSize.width()+4)*m_cols)/2;
  int topOffset = m_header->height() + 20 + ( availVert - (m_buttonSize.height()+4)*m_rows - m_buttonMore->height() )/2;

  int row = 0;
  int col = 0;

  for( TQPtrListIterator<K3bFlatButton> it( m_buttons ); it.current(); ++it ) {
    K3bFlatButton* b = it.current();

    b->setGeometry( TQRect( TQPoint( leftMargin + (col*(m_buttonSize.width()+4) + 2 ),
				   topOffset + (row*(m_buttonSize.height()+4)) + 2 ),
			   m_buttonSize ) );
    b->show();

    col++;
    if( col == m_cols ) {
      col = 0;
      row++;
    }
  }
  if( col > 0 )
    ++row;

  m_buttonMore->setGeometry( TQRect( TQPoint( leftMargin + 2,
					    topOffset + (row*(m_buttonSize.height()+4)) + 2 ),
				    TQSize( m_cols*(m_buttonSize.width()+4) - 4, m_buttonMore->height() ) ) );
}


TQSizePolicy K3bWelcomeWidget::Display::sizePolicy () const
{
  return TQSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum, true );
}


int K3bWelcomeWidget::Display::heightForWidth( int w ) const
{
  int ow = m_infoText->width();
  m_infoText->setWidth( w );
  int h = m_infoText->height();
  m_infoText->setWidth( ow );

  int cols, rows;
  calculateButtons( w, m_actions.count(), m_buttonSize.width(), cols, rows );

  return (20 + m_header->height() + 20 + 10 + ((m_buttonSize.height()+4)*rows) + 4 + m_buttonMore->height() + 10 + h + 20);
}


TQSize K3bWelcomeWidget::Display::minimumSizeHint() const
{
  TQSize size( TQMAX(40+m_header->widthUsed(), 40+m_buttonSize.width()),
	      20 + m_header->height() + 20 + 10 + m_buttonSize.height() + 10 + m_infoText->height() + 20 );

  return size;
}


void K3bWelcomeWidget::Display::resizeEvent( TQResizeEvent* e )
{
  m_infoText->setWidth( width() - 20 );
  TQWidget::resizeEvent(e);
  repositionButtons();
  if( e->size() != m_bgPixmap.size() )
    updateBgPix();
}


void K3bWelcomeWidget::Display::slotThemeChanged()
{
  if( K3bTheme* theme = k3bappcore->themeManager()->currentTheme() )
    if( theme->backgroundMode() == K3bTheme::BG_SCALE )
      m_bgImage = theme->pixmap( K3bTheme::WELCOME_BG ).convertToImage();

  updateBgPix();
  update();
}

#include "fastscale/scale.h"
void K3bWelcomeWidget::Display::updateBgPix()
{
  if( K3bTheme* theme = k3bappcore->themeManager()->currentTheme() ) {
    if( theme->backgroundMode() == K3bTheme::BG_SCALE )
      m_bgPixmap.convertFromImage( ImageUtils::scale( m_bgImage, rect().width(), rect().height(), ImageUtils::SMOOTH_FAST ) );
    else
      m_bgPixmap = theme->pixmap( K3bTheme::WELCOME_BG );
  }
}


void K3bWelcomeWidget::Display::paintEvent( TQPaintEvent* )
{
  if( K3bTheme* theme = k3bappcore->themeManager()->currentTheme() ) {
    TQPainter p( this );
    p.setPen( theme->foregroundColor() );

    // draw the background including first filling with the bg color for transparent images
    p.fillRect( rect(), theme->backgroundColor() );
    p.drawTiledPixmap( rect(), m_bgPixmap );

    // rect around the header
    TQRect rect( 10, 10, TQMAX( m_header->widthUsed() + 20, width() - 20 ), m_header->height() + 20 );
    p.fillRect( rect, theme->backgroundColor() );
    p.drawRect( rect );

    // big rect around the whole thing
    p.drawRect( 10, 10, width()-20, height()-20 );

    // draw the header text
    TQColorGroup grp( colorGroup() );
    grp.setColor( TQColorGroup::Text, theme->foregroundColor() );
    int pos = 20;
    pos += TQMAX( (width()-40-m_header->widthUsed())/2, 0 );
    m_header->draw( &p, pos, 20, TQRect(), grp );

    // draw the info box
    //    int boxWidth = 20 + m_infoText->widthUsed();
    int boxHeight = 10 + m_infoText->height();
    TQRect infoBoxRect( 10/*TQMAX( (width()-20-m_infoText->widthUsed())/2, 10 )*/,
		       height()-10-boxHeight,
		       width()-20/*boxWidth*/,
		       boxHeight );
    p.fillRect( infoBoxRect, theme->backgroundColor() );
    p.drawRect( infoBoxRect );
    m_infoText->draw( &p, infoBoxRect.left()+5, infoBoxRect.top()+5, TQRect(), grp );
  }
}


void K3bWelcomeWidget::Display::dragEnterEvent( TQDragEnterEvent* event )
{
  event->accept( KURLDrag::canDecode(event) );
}


void K3bWelcomeWidget::Display::dropEvent( TQDropEvent* e )
{
  KURL::List urls;
  KURLDrag::decode( e, urls );
  emit dropped( urls );
}



K3bWelcomeWidget::K3bWelcomeWidget( K3bMainWindow* mw, TQWidget* parent, const char* name )
  : TQScrollView( parent, name ),
    m_mainWindow( mw )
{
  main = new Display( this );
  addChild( main );

  connect( main, TQT_SIGNAL(dropped(const KURL::List&)), m_mainWindow, TQT_SLOT(addUrls(const KURL::List&)) );

  connect( kapp, TQT_SIGNAL(appearanceChanged()), main, TQT_SLOT(update()) );
}


K3bWelcomeWidget::~K3bWelcomeWidget()
{
}


void K3bWelcomeWidget::loadConfig( TDEConfigBase* c )
{
  TQStringList sl = TDEConfigGroup( c, "Welcome Widget" ).readListEntry( "welcome_actions" );

  if( sl.isEmpty() ) {
    sl.append( "file_new_audio" );
    sl.append( "file_new_data" );
    sl.append( "file_new_dvd" );
    sl.append( "tools_copy_cd" );
    sl.append( "tools_write_cd_image" );
    sl.append( "tools_write_dvd_iso" );
  }

  TQPtrList<TDEAction> actions;
  for( TQStringList::const_iterator it = sl.begin(); it != sl.end(); ++it )
    if( TDEAction* a = m_mainWindow->actionCollection()->action( (*it).latin1() ) )
      actions.append(a);

  main->rebuildGui( actions );

  fixSize();
}


void K3bWelcomeWidget::saveConfig( TDEConfigBase* c )
{
  TDEConfigGroup grp( c, "Welcome Widget" );

  TQStringList sl;
  for( TQPtrListIterator<TDEAction> it( main->m_actions ); it.current(); ++it )
    sl.append( it.current()->name() );

  grp.writeEntry( "welcome_actions", sl );
}


void K3bWelcomeWidget::resizeEvent( TQResizeEvent* e )
{
  TQScrollView::resizeEvent( e );
  fixSize();
}


void K3bWelcomeWidget::showEvent( TQShowEvent* e )
{
  TQScrollView::showEvent( e );
  fixSize();
}


void K3bWelcomeWidget::fixSize()
{
  TQSize s = contentsRect().size();
  s.setWidth( TQMAX( main->minimumSizeHint().width(), s.width() ) );
  s.setHeight( TQMAX( main->heightForWidth(s.width()), s.height() ) );

  main->resize( s );
  viewport()->resize( s );
}


void K3bWelcomeWidget::contentsMousePressEvent( TQMouseEvent* e )
{
  if( e->button() == TQt::RightButton ) {
    TQMap<int, TDEAction*> map;
    TDEPopupMenu addPop;

    for ( int i = 0; s_allActions[i]; ++i ) {
        if ( s_allActions[i][0] != '_' ) {
            TDEAction* a = m_mainWindow->actionCollection()->action( s_allActions[i] );
            if ( a && !main->m_actions.containsRef(a) ) {
                map.insert( addPop.insertItem( a->iconSet(), a->text() ), a );
            }
        }
    }

    // menu identifiers in QT are always < 0 (when automatically generated)
    // and unique throughout the entire application!
    int r = 0;
    int removeAction = 0;

    TQWidget* widgetAtPos = viewport()->childAt(e->pos());
    if( widgetAtPos && widgetAtPos->inherits( "K3bFlatButton" ) ) {
      TDEPopupMenu pop;
      removeAction = pop.insertItem( SmallIcon("remove"), i18n("Remove Button") );
      if ( addPop.count() > 0 )
          pop.insertItem( i18n("Add Button"), &addPop );
      pop.insertSeparator();
      r = pop.exec( e->globalPos() );
    }
    else {
      addPop.insertTitle( i18n("Add Button"), -1, 0 );
      addPop.insertSeparator();
      r = addPop.exec( e->globalPos() );
    }

    if( r != 0 ) {
      if( r == removeAction )
	main->removeButton( static_cast<K3bFlatButton*>(widgetAtPos) );
      else
	main->addAction( map[r] );
    }

    fixSize();
  }
}


void K3bWelcomeWidget::slotMoreActions()
{
  TDEPopupMenu popup;

  for ( int i = 0; s_allActions[i]; ++i ) {
      if ( s_allActions[i][0] == '_' ) {
          (new TDEActionSeparator( TQT_TQOBJECT(&popup) ))->plug( &popup );
      }
      else {
          m_mainWindow->actionCollection()->action( s_allActions[i] )->plug( &popup );
      }
  }

  popup.exec( TQCursor::pos() );
}

#include "k3bwelcomewidget.moc"