khtml Library API Documentation

khtmlimage.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "khtmlimage.h"
00021 #include "khtmlview.h"
00022 #include "khtml_ext.h"
00023 #include "xml/dom_docimpl.h"
00024 #include "html/html_documentimpl.h"
00025 #include "html/html_elementimpl.h"
00026 #include "rendering/render_image.h"
00027 #include "misc/loader.h"
00028 
00029 #include <qvbox.h>
00030 #include <qtimer.h>
00031 
00032 #include <kio/job.h>
00033 #include <kinstance.h>
00034 #include <kmimetype.h>
00035 #include <klocale.h>
00036 
00037 K_EXPORT_COMPONENT_FACTORY( khtmlimagefactory /*NOT the part name, see Makefile.am*/, KHTMLImageFactory )
00038 
00039 KInstance *KHTMLImageFactory::s_instance = 0;
00040 
00041 KHTMLImageFactory::KHTMLImageFactory()
00042 {
00043     s_instance = new KInstance( "khtmlimage" );
00044 }
00045 
00046 KHTMLImageFactory::~KHTMLImageFactory()
00047 {
00048     delete s_instance;
00049 }
00050 
00051 KParts::Part *KHTMLImageFactory::createPartObject( QWidget *parentWidget, const char *widgetName,
00052                                                    QObject *parent, const char *name,
00053                                                    const char *, const QStringList & )
00054 {
00055     return new KHTMLImage( parentWidget, widgetName, parent, name );
00056 }
00057 
00058 KHTMLImage::KHTMLImage( QWidget *parentWidget, const char *widgetName,
00059                         QObject *parent, const char *name )
00060     : KParts::ReadOnlyPart( parent, name ), m_image( 0 )
00061 {
00062     setInstance( KHTMLImageFactory::instance() );
00063 
00064     QVBox *box = new QVBox( parentWidget, widgetName );
00065 
00066     m_khtml = new KHTMLPart( box, widgetName, this, "htmlimagepart", KHTMLPart::BrowserViewGUI );
00067     m_khtml->setAutoloadImages( true );
00068     m_khtml->widget()->installEventFilter(this);
00069 
00070     setWidget( box );
00071 
00072     // VBox can't take focus, so pass it on to sub-widget
00073     box->setFocusProxy( m_khtml->widget() );
00074 
00075     m_ext = new KHTMLImageBrowserExtension( this, "be" );
00076 
00077     // Remove unnecessary actions.
00078     KAction *encodingAction = actionCollection()->action( "setEncoding" );
00079     if ( encodingAction )
00080     {
00081         encodingAction->unplugAll();
00082         delete encodingAction;
00083     }
00084     KAction *viewSourceAction= actionCollection()->action( "viewDocumentSource" );
00085     if ( viewSourceAction )
00086     {
00087         viewSourceAction->unplugAll();
00088         delete viewSourceAction;
00089     }
00090 
00091     KAction *selectAllAction= actionCollection()->action( "selectAll" );
00092     if ( selectAllAction )
00093     {
00094         selectAllAction->unplugAll();
00095         delete selectAllAction;
00096     }
00097 
00098     // forward important signals from the khtml part
00099     connect(m_khtml->browserExtension(), SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)),
00100             m_ext, SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)));
00101 
00102     connect( m_khtml->browserExtension(), SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &,
00103              const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ), m_ext, SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &,
00104              const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ) );
00105 
00106     connect( m_khtml->browserExtension(), SIGNAL( enableAction( const char *, bool ) ),
00107              m_ext, SIGNAL( enableAction( const char *, bool ) ) );
00108 
00109     m_ext->setURLDropHandlingEnabled( true );
00110 }
00111 
00112 KHTMLImage::~KHTMLImage()
00113 {
00114     disposeImage();
00115 
00116     // important: delete the html part before the part or qobject destructor runs.
00117     // we now delete the htmlpart which deletes the part's widget which makes
00118     // _OUR_ m_widget 0 which in turn avoids our part destructor to delete the
00119     // widget ;-)
00120     // ### additional note: it _can_ be that the part has been deleted before:
00121     // when we're in a html frameset and the view dies first, then it will also
00122     // kill the htmlpart
00123     if ( m_khtml )
00124         delete static_cast<KHTMLPart *>( m_khtml );
00125 }
00126 
00127 bool KHTMLImage::openURL( const KURL &url )
00128 {
00129     static const QString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
00130 
00131     disposeImage();
00132 
00133     m_url = url;
00134 
00135     emit started( 0 );
00136 
00137     KParts::URLArgs args = m_ext->urlArgs();
00138     m_mimeType = args.serviceType;
00139 
00140     emit setWindowCaption( url.prettyURL() );
00141 
00142     m_khtml->begin( m_url, args.xOffset, args.yOffset );
00143     m_khtml->setAutoloadImages( true );
00144 
00145     DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() ); // ### hack ;-)
00146     if ( impl && m_ext->urlArgs().reload )
00147         impl->docLoader()->setCachePolicy( KIO::CC_Refresh );
00148 
00149     khtml::DocLoader *dl = impl ? impl->docLoader() : 0;
00150     m_image = dl->requestImage( m_url.url() );
00151     if ( m_image )
00152         m_image->ref( this );
00153 
00154     m_khtml->write( html.arg( m_url.url() ) );
00155     m_khtml->end();
00156 
00157     /*
00158     connect( khtml::Cache::loader(), SIGNAL( requestDone( khtml::DocLoader*, khtml::CachedObject *) ),
00159             this, SLOT( updateWindowCaption() ) );
00160             */
00161     return true;
00162 }
00163 
00164 bool KHTMLImage::closeURL()
00165 {
00166     disposeImage();
00167     return m_khtml->closeURL();
00168 }
00169 
00170 void KHTMLImage::notifyFinished( khtml::CachedObject *o )
00171 {
00172     if ( !m_image || o != m_image )
00173         return;
00174 
00175     const QPixmap &pix = m_image->pixmap();
00176     QString caption;
00177 
00178     KMimeType::Ptr mimeType;
00179     if ( !m_mimeType.isEmpty() )
00180         mimeType = KMimeType::mimeType( m_mimeType );
00181 
00182     if ( mimeType )
00183         caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00184                   .arg( pix.width() ).arg( pix.height() );
00185     else
00186         caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
00187 
00188     emit setWindowCaption( caption );
00189     emit completed();
00190     emit setStatusBarText(i18n("Done."));
00191 }
00192 
00193 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
00194 {
00195     // prevent the base implementation from emitting setWindowCaption with
00196     // our url. It destroys our pretty, previously caption. Konq saves/restores
00197     // the caption for us anyway.
00198     if ( e->activated() )
00199         return;
00200     KParts::ReadOnlyPart::guiActivateEvent(e);
00201 }
00202 
00203 /*
00204 void KHTMLImage::slotImageJobFinished( KIO::Job *job )
00205 {
00206     if ( job->error() )
00207     {
00208         job->showErrorDialog();
00209         emit canceled( job->errorString() );
00210     }
00211     else
00212     {
00213         if ( m_khtml->view()->contentsY() == 0 )
00214         {
00215             KParts::URLArgs args = m_ext->urlArgs();
00216             m_khtml->view()->setContentsPos( args.xOffset, args.yOffset );
00217         }
00218 
00219         emit completed();
00220 
00221         QTimer::singleShot( 0, this, SLOT( updateWindowCaption() ) );
00222     }
00223 }
00224 
00225 void KHTMLImage::updateWindowCaption()
00226 {
00227     if ( !m_khtml )
00228         return;
00229 
00230     DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
00231     if ( !impl )
00232         return;
00233 
00234     DOM::HTMLElementImpl *body = impl->body();
00235     if ( !body )
00236         return;
00237 
00238     DOM::NodeImpl *image = body->firstChild();
00239     if ( !image )
00240         return;
00241 
00242     khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
00243     if ( !renderImage )
00244         return;
00245 
00246     QPixmap pix = renderImage->pixmap();
00247 
00248     QString caption;
00249 
00250     KMimeType::Ptr mimeType;
00251     if ( !m_mimeType.isEmpty() )
00252         mimeType = KMimeType::mimeType( m_mimeType );
00253 
00254     if ( mimeType )
00255         caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00256                   .arg( pix.width() ).arg( pix.height() );
00257     else
00258         caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
00259 
00260     emit setWindowCaption( caption );
00261     emit completed();
00262     emit setStatusBarText(i18n("Done."));
00263 }
00264 */
00265 
00266 void KHTMLImage::disposeImage()
00267 {
00268     if ( !m_image )
00269         return;
00270 
00271     m_image->deref( this );
00272     m_image = 0;
00273 }
00274 
00275 bool KHTMLImage::eventFilter(QObject *, QEvent *e) {
00276     switch (e->type()) {
00277       case QEvent::DragEnter:
00278       case QEvent::DragMove:
00279       case QEvent::DragLeave:
00280       case QEvent::Drop:
00281         // simply forward all dnd events to the part widget,
00282     // konqueror will handle them properly there
00283         return QApplication::sendEvent(widget(), e);
00284       default: ;
00285     }
00286     return false;
00287 }
00288 
00289 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent, const char *name )
00290     : KParts::BrowserExtension( parent, name )
00291 {
00292     m_imgPart = parent;
00293 }
00294 
00295 int KHTMLImageBrowserExtension::xOffset()
00296 {
00297     return m_imgPart->doc()->view()->contentsX();
00298 }
00299 
00300 int KHTMLImageBrowserExtension::yOffset()
00301 {
00302     return m_imgPart->doc()->view()->contentsY();
00303 }
00304 
00305 void KHTMLImageBrowserExtension::print()
00306 {
00307     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00308 }
00309 
00310 void KHTMLImageBrowserExtension::reparseConfiguration()
00311 {
00312     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00313     m_imgPart->doc()->setAutoloadImages( true );
00314 }
00315 
00316 using namespace KParts;
00317 
00318 /* vim: et sw=4 ts=4
00319  */
00320 
00321 #include "khtmlimage.moc"
KDE Logo
This file is part of the documentation for khtml Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:37:20 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003