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     
00100     // forward opening requests to parent frame (if existing)
00101     KHTMLPart *p = ::qt_cast<KHTMLPart *>(parent);
00102     KParts::BrowserExtension *be = p ? p->browserExtension() : m_ext;
00103     connect(m_khtml->browserExtension(), SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)),
00104             be, SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)));
00105 
00106     connect( m_khtml->browserExtension(), SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &,
00107              const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ), m_ext, SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &,
00108              const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ) );
00109 
00110     connect( m_khtml->browserExtension(), SIGNAL( enableAction( const char *, bool ) ),
00111              m_ext, SIGNAL( enableAction( const char *, bool ) ) );
00112 
00113     m_ext->setURLDropHandlingEnabled( true );
00114 }
00115 
00116 KHTMLImage::~KHTMLImage()
00117 {
00118     disposeImage();
00119 
00120     // important: delete the html part before the part or qobject destructor runs.
00121     // we now delete the htmlpart which deletes the part's widget which makes
00122     // _OUR_ m_widget 0 which in turn avoids our part destructor to delete the
00123     // widget ;-)
00124     // ### additional note: it _can_ be that the part has been deleted before:
00125     // when we're in a html frameset and the view dies first, then it will also
00126     // kill the htmlpart
00127     if ( m_khtml )
00128         delete static_cast<KHTMLPart *>( m_khtml );
00129 }
00130 
00131 bool KHTMLImage::openURL( const KURL &url )
00132 {
00133     static const QString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
00134 
00135     disposeImage();
00136 
00137     m_url = url;
00138 
00139     emit started( 0 );
00140 
00141     KParts::URLArgs args = m_ext->urlArgs();
00142     m_mimeType = args.serviceType;
00143 
00144     emit setWindowCaption( url.prettyURL() );
00145 
00146     m_khtml->begin( m_url, args.xOffset, args.yOffset );
00147     m_khtml->setAutoloadImages( true );
00148 
00149     DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() ); // ### hack ;-)
00150     if ( impl && m_ext->urlArgs().reload )
00151         impl->docLoader()->setCachePolicy( KIO::CC_Reload );
00152 
00153     khtml::DocLoader *dl = impl ? impl->docLoader() : 0;
00154     m_image = dl->requestImage( m_url.url() );
00155     if ( m_image )
00156         m_image->ref( this );
00157 
00158     m_khtml->write( html.arg( m_url.url() ) );
00159     m_khtml->end();
00160 
00161     /*
00162     connect( khtml::Cache::loader(), SIGNAL( requestDone( khtml::DocLoader*, khtml::CachedObject *) ),
00163             this, SLOT( updateWindowCaption() ) );
00164             */
00165     return true;
00166 }
00167 
00168 bool KHTMLImage::closeURL()
00169 {
00170     disposeImage();
00171     return m_khtml->closeURL();
00172 }
00173 
00174 void KHTMLImage::notifyFinished( khtml::CachedObject *o )
00175 {
00176     if ( !m_image || o != m_image )
00177         return;
00178 
00179     const QPixmap &pix = m_image->pixmap();
00180     QString caption;
00181 
00182     KMimeType::Ptr mimeType;
00183     if ( !m_mimeType.isEmpty() )
00184         mimeType = KMimeType::mimeType( m_mimeType );
00185 
00186     if ( mimeType )
00187         caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00188                   .arg( pix.width() ).arg( pix.height() );
00189     else
00190         caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
00191 
00192     emit setWindowCaption( caption );
00193     emit completed();
00194     emit setStatusBarText(i18n("Done."));
00195 }
00196 
00197 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
00198 {
00199     // prevent the base implementation from emitting setWindowCaption with
00200     // our url. It destroys our pretty, previously caption. Konq saves/restores
00201     // the caption for us anyway.
00202     if ( e->activated() )
00203         return;
00204     KParts::ReadOnlyPart::guiActivateEvent(e);
00205 }
00206 
00207 /*
00208 void KHTMLImage::slotImageJobFinished( KIO::Job *job )
00209 {
00210     if ( job->error() )
00211     {
00212         job->showErrorDialog();
00213         emit canceled( job->errorString() );
00214     }
00215     else
00216     {
00217         if ( m_khtml->view()->contentsY() == 0 )
00218         {
00219             KParts::URLArgs args = m_ext->urlArgs();
00220             m_khtml->view()->setContentsPos( args.xOffset, args.yOffset );
00221         }
00222 
00223         emit completed();
00224 
00225         QTimer::singleShot( 0, this, SLOT( updateWindowCaption() ) );
00226     }
00227 }
00228 
00229 void KHTMLImage::updateWindowCaption()
00230 {
00231     if ( !m_khtml )
00232         return;
00233 
00234     DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
00235     if ( !impl )
00236         return;
00237 
00238     DOM::HTMLElementImpl *body = impl->body();
00239     if ( !body )
00240         return;
00241 
00242     DOM::NodeImpl *image = body->firstChild();
00243     if ( !image )
00244         return;
00245 
00246     khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
00247     if ( !renderImage )
00248         return;
00249 
00250     QPixmap pix = renderImage->pixmap();
00251 
00252     QString caption;
00253 
00254     KMimeType::Ptr mimeType;
00255     if ( !m_mimeType.isEmpty() )
00256         mimeType = KMimeType::mimeType( m_mimeType );
00257 
00258     if ( mimeType )
00259         caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00260                   .arg( pix.width() ).arg( pix.height() );
00261     else
00262         caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
00263 
00264     emit setWindowCaption( caption );
00265     emit completed();
00266     emit setStatusBarText(i18n("Done."));
00267 }
00268 */
00269 
00270 void KHTMLImage::disposeImage()
00271 {
00272     if ( !m_image )
00273         return;
00274 
00275     m_image->deref( this );
00276     m_image = 0;
00277 }
00278 
00279 bool KHTMLImage::eventFilter(QObject *, QEvent *e) {
00280     switch (e->type()) {
00281       case QEvent::DragEnter:
00282       case QEvent::DragMove:
00283       case QEvent::DragLeave:
00284       case QEvent::Drop: {
00285         // find out if this part is embedded in a frame, and send the
00286     // event to its outside widget
00287     KHTMLPart *p = ::qt_cast<KHTMLPart *>(parent());
00288     if (p)
00289         return QApplication::sendEvent(p->widget(), e);
00290         // otherwise simply forward all dnd events to the part widget,
00291     // konqueror will handle them properly there
00292         return QApplication::sendEvent(widget(), e);
00293       }
00294       default: ;
00295     }
00296     return false;
00297 }
00298 
00299 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent, const char *name )
00300     : KParts::BrowserExtension( parent, name )
00301 {
00302     m_imgPart = parent;
00303 }
00304 
00305 int KHTMLImageBrowserExtension::xOffset()
00306 {
00307     return m_imgPart->doc()->view()->contentsX();
00308 }
00309 
00310 int KHTMLImageBrowserExtension::yOffset()
00311 {
00312     return m_imgPart->doc()->view()->contentsY();
00313 }
00314 
00315 void KHTMLImageBrowserExtension::print()
00316 {
00317     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00318 }
00319 
00320 void KHTMLImageBrowserExtension::reparseConfiguration()
00321 {
00322     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00323     m_imgPart->doc()->setAutoloadImages( true );
00324 }
00325 
00326 using namespace KParts;
00327 
00328 /* vim: et sw=4 ts=4
00329  */
00330 
00331 #include "khtmlimage.moc"
KDE Logo
This file is part of the documentation for khtml Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:34:43 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003