1 /***************************************************************************
2 * Copyright (C) 2004 by Hideki Ikemoto , (c) 2004 by 421 *
3 * ikemo@users.sourceforge.jp *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 /* Image Viewer Widget */
13 #include "kitaimgview.h"
14 #include "libkita/imgmanager.h"
16 #include <kpopupmenu.h>
18 #include <khtml_part.h>
19 #include <khtmlview.h>
26 #include <qapplication.h>
29 /*---------------------------------------------------------------*/
32 KitaImgView::KitaImgView( const KURL& url, const KURL& datURL, KActionCollection* action,
33 QWidget* parent, const char* name, WFlags fl )
34 : QScrollView( parent, name, fl )
42 m_message = QString::null;
46 m_imgwd = m_imght = 0;
47 m_custom_wd = m_custom_ht = 0;
49 viewport() ->setEraseColor( Qt::black );
50 viewport() ->setPaletteBackgroundColor( Qt::black );
54 KitaImgView::~KitaImgView()
56 if ( m_pixmap ) delete m_pixmap;
57 if ( m_popup ) delete m_popup;
58 if ( m_layout ) delete m_layout;
59 if ( m_htmlPart ) delete m_htmlPart;
64 const KURL KitaImgView::url() const
71 const KURL KitaImgView::datURL() const
78 void KitaImgView::showMessage( QString message )
85 /* show image */ /* public slot */
86 void KitaImgView::slotShowImage()
88 const int defaultsize = 0;
90 if ( m_size < -1 ) m_size = defaultsize; /* size is not initialized. */
92 bool usekhtmlpart = Kita::ImgManager::isAnimationGIF( m_url );
93 if ( m_htmlPart ) usekhtmlpart = TRUE;
94 if ( Kita::ImgManager::mosaic( m_url ) ) usekhtmlpart = FALSE;
96 if ( usekhtmlpart ) showImageWithKHTML();
98 if ( resizeImage() ) {
106 void KitaImgView::slotFitToWin()
108 if ( m_size == 0 ) return ;
116 void KitaImgView::slotOriginalSize()
118 if ( m_size == 100 ) return ;
126 void KitaImgView::slotSetSize( int size )
128 if ( m_size == size ) return ;
136 void KitaImgView::slotCustomSize( int wd, int ht )
138 const int minsize = 8;
140 if ( wd == m_custom_wd && ht == m_custom_ht ) return ;
142 m_custom_wd = QMAX( minsize, wd );
143 m_custom_ht = QMAX( minsize, ht );
150 void KitaImgView::slotZoomIn()
152 const int maxsize = 400;
154 if ( m_size >= maxsize ) return ;
156 /* current mode is "fit to win" or "custom size" */
157 if ( m_size <= 0 && m_imgwd ) m_size = QMIN( 100, m_imgwd * 100 / Kita::ImgManager::width( m_url ) );
165 void KitaImgView::slotZoomOut()
167 const int minsize = 10;
169 if ( m_size > 0 && m_size <= minsize ) return ;
171 /* current mode is "fit to win" or "custom size" */
172 if ( m_size <= 0 && m_imgwd ) m_size = QMIN( 100, m_imgwd * 100 / Kita::ImgManager::width( m_url ) );
180 /* virtual protected */
181 void KitaImgView::resizeEvent( QResizeEvent * e )
183 QScrollView::resizeEvent( e );
185 if ( m_htmlPart ) showImageWithKHTML();
187 else if ( m_pixmap ) {
188 if ( m_size == 0 ) resizeImage(); /* fit to window */
191 /* drawContents is called later. */
196 /* virtual protected */
197 void KitaImgView::drawContents( QPainter * p, int clipx, int clipy, int clipw, int cliph )
199 if ( m_htmlPart ) return ; /* use KHTMLPart */
201 if ( m_pixmap == NULL ) {
202 p->setPen( Qt::white );
203 if ( m_message != QString::null ) p->drawText( 16, 16, m_message );
206 p->fillRect( clipx, clipy, clipw, cliph, QColor( "black" ) );
208 int x = QMAX( 0, ( QMAX( width(), m_imgwd ) - m_imgwd ) / 2 );
209 int y = QMAX( 0, ( QMAX( height(), m_imght ) - m_imght ) / 2 );
210 int sx = QMAX( 0, clipx - x );
211 int sy = QMAX( 0, clipy - y );
212 int sw = QMIN( clipw, x + m_imgwd - clipx );
213 int sh = QMIN( cliph, y + m_imght - clipy );
215 if ( x + m_imgwd <= clipx || x >= clipx + clipw
216 || y + m_imght <= clipy || y >= clipy + cliph ) return ;
218 x = QMAX( x, clipx );
219 y = QMAX( y, clipy );
220 p->drawPixmap( x, y, *m_pixmap, sx, sy, sw, sh );
225 /* virtual protected */
226 void KitaImgView::contentsMousePressEvent( QMouseEvent * e )
228 m_dragScroll = FALSE;
229 if ( e->button() & Qt::RightButton ) {
231 slotPopupMenu( dummy, e->globalPos() );
234 else if ( e->button() & Qt::LeftButton ) { /* start dragscrolling */
236 QApplication::setOverrideCursor( KCursor::handCursor() );
237 m_dragx = e->globalX();
238 m_dragy = e->globalY();
241 QScrollView::contentsMousePressEvent( e );
244 /* Note that e->button() is always NoButton. */ /* virtual protected */
245 void KitaImgView::contentsMouseMoveEvent( QMouseEvent * e )
247 if( !m_dragScroll ) return;
250 int dx = m_dragx - e->globalX();
251 int dy = m_dragy - e->globalY();
253 m_dragx = e->globalX();
254 m_dragy = e->globalY();
257 /* virtual protected */
258 void KitaImgView::contentsMouseReleaseEvent( QMouseEvent * )
261 m_dragScroll = FALSE;
262 QApplication::restoreOverrideCursor();
268 bool KitaImgView::resizeImage()
270 const int mosaic_wd = 48;
271 const int mosaic_ht = 48;
273 /*-------------------------------------------*/
275 if ( Kita::ImgManager::code( m_url ) != 200 ) return FALSE;
277 QString path = Kita::ImgManager::getPath( m_url );
278 QImage orgimg = QImage( path );
279 if ( orgimg.isNull() ) return FALSE;
281 int imgwd = Kita::ImgManager::width( m_url );
282 int imght = Kita::ImgManager::height( m_url );
285 if ( Kita::ImgManager::mosaic( m_url ) )
286 orgimg = orgimg.scale( mosaic_wd, mosaic_ht, QImage::ScaleMin ).scale( imgwd, imght, QImage::ScaleMin );
291 getImgSize( wd, ht );
293 if ( wd != imgwd || ht != imght ) orgimg = orgimg.scale( wd, ht, QImage::ScaleMin );
295 /* convert to pixmap */
296 if ( m_pixmap == NULL ) m_pixmap = new QPixmap();
297 m_pixmap->convertFromImage( orgimg );
298 m_imgwd = m_pixmap->width();
299 m_imght = m_pixmap->height();
308 void KitaImgView::getImgSize( int& wd, int& ht )
310 int imgwd = Kita::ImgManager::width( m_url );
311 int imght = Kita::ImgManager::height( m_url );
316 if ( m_size == -1 ) { /* custom size */
319 } else if ( m_size == 0 ) { /* fit to window */
321 if ( width() < imgwd || height() < imght ) {
328 } else if ( m_size != 100 ) { /* % */
329 wd = imgwd * m_size / 100;
330 ht = imght * m_size / 100;
337 void KitaImgView::myResizeContents()
339 int wd = QMAX( width(), m_imgwd );
340 int ht = QMAX( height(), m_imght );
342 resizeContents( wd, ht );
344 if ( wd == width() ) setHScrollBarMode( AlwaysOff );
345 else setHScrollBarMode( AlwaysOn );
347 if ( ht == height() ) setVScrollBarMode( AlwaysOff );
348 else setVScrollBarMode( AlwaysOn );
352 if ( wd == width() ) m_htmlPart->view() ->setHScrollBarMode( AlwaysOff );
353 else m_htmlPart->view() ->setHScrollBarMode( AlwaysOn );
355 if ( ht == height() ) m_htmlPart->view() ->setVScrollBarMode( AlwaysOff );
356 else m_htmlPart->view() ->setVScrollBarMode( AlwaysOn );
362 void KitaImgView::slotPopupMenu( const QString& , const QPoint& global )
364 if ( !m_action ) return ;
366 if ( m_popup ) delete m_popup;
367 m_popup = new KPopupMenu( this );
369 if ( Kita::ImgManager::mosaic( m_url ) ) {
370 if ( m_action->action( "imgviewer_cancelmosaic" ) )
371 m_action->action( "imgviewer_cancelmosaic" ) ->plug( m_popup );
372 m_popup->insertSeparator();
375 if ( m_action->action( "imgviewer_fittowin" ) )
376 m_action->action( "imgviewer_fittowin" ) ->plug( m_popup );
378 if ( m_action->action( "imgviewer_original" ) )
379 m_action->action( "imgviewer_original" ) ->plug( m_popup );
381 if ( m_action->action( "imgviewer_selectsize" ) )
382 m_action->action( "imgviewer_selectsize" ) ->plug( m_popup );
384 if ( m_action->action( "imgviewer_zoomin" ) )
385 m_action->action( "imgviewer_zoomin" ) ->plug( m_popup );
387 if ( m_action->action( "imgviewer_zoomout" ) )
388 m_action->action( "imgviewer_zoomout" ) ->plug( m_popup );
390 m_popup->insertSeparator();
391 if ( m_action->action( "imgviewer_openbrowser" ) )
392 m_action->action( "imgviewer_openbrowser" ) ->plug( m_popup );
393 if ( m_action->action( "imgviewer_openthread" ) )
394 m_action->action( "imgviewer_openthread" ) ->plug( m_popup );
395 if ( m_action->action( "imgviewer_save" ) )
396 m_action->action( "imgviewer_save" ) ->plug( m_popup );
398 m_popup->exec( global );
402 /*----------------------------------------------*/
404 /* use KHTMLPart ( to show an Animation GIF ) */
407 bool KitaImgView::showImageWithKHTML()
409 if ( Kita::ImgManager::code( m_url ) != 200 ) return FALSE;
410 QString path = Kita::ImgManager::getPath( m_url );
412 /* create KHTMLPart */
414 m_htmlPart = new KHTMLPart( this );
415 m_layout = new QVBoxLayout( this, 0, 0, "BoxLayout" );
416 m_layout->addWidget( m_htmlPart->view() );
418 connect( m_htmlPart, SIGNAL( popupMenu( const QString&, const QPoint& ) ),
419 SLOT( slotPopupMenu( const QString&, const QPoint& ) ) );
425 QString sizetxt = QString::null;
427 int imgwd = Kita::ImgManager::width( m_url );
428 int imght = Kita::ImgManager::height( m_url );
432 getImgSize( wd, ht );
434 if ( m_size <= 0 ) { /* "fit to win" or "custom size" */
436 if ( ( double ) imght * wd / imgwd >= ht ) {
437 sizetxt = "height=" + QString().setNum( ht );
438 m_imgwd = ( int ) ( ( double ) imgwd * ht / imght );
441 sizetxt = "width=" + QString().setNum( wd );
443 m_imght = ( int ) ( ( double ) imght * wd / imgwd );
445 } else if ( m_size != 100 ) { /* % */
446 sizetxt = "width=" + QString().setNum( wd );
448 m_imght = ( int ) ( ( double ) imght * wd / imgwd );
451 int x = QMAX( 0, ( QMAX( width(), m_imgwd ) - m_imgwd ) / 2 );
452 int y = QMAX( 0, ( QMAX( height(), m_imght ) - m_imght ) / 2 );
454 QString style = QString( "body { margin:%1px %2px; background-color:black; }" )
457 QString text = "<html><head><style>" + style + "</style></head><body>";
458 text += "<img src=\"file:" + path + "\" " + sizetxt + " >";
459 text += "</body></html>";
461 m_htmlPart->setJScriptEnabled( false );
462 m_htmlPart->setJavaEnabled( false );
463 m_htmlPart->begin( "file:/dummy.htm" );
464 m_htmlPart->write( text );
466 m_htmlPart->view() ->show();