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 
00028 #include <qvbox.h>
00029 #include <qtimer.h>
00030 
00031 #include <kio/job.h>
00032 #include <kinstance.h>
00033 #include <kmimetype.h>
00034 #include <klocale.h>
00035 
00036 K_EXPORT_COMPONENT_FACTORY( khtmlimagepart, KHTMLImageFactory )
00037 
00038 KInstance *KHTMLImageFactory::s_instance = 0;
00039 
00040 KHTMLImageFactory::KHTMLImageFactory()
00041 {
00042     s_instance = new KInstance( "khtmlimage" );
00043 }
00044 
00045 KHTMLImageFactory::~KHTMLImageFactory()
00046 {
00047     delete s_instance;
00048 }
00049 
00050 KParts::Part *KHTMLImageFactory::createPartObject( QWidget *parentWidget, const char *widgetName,
00051                                                    QObject *parent, const char *name,
00052                                                    const char *, const QStringList & )
00053 {
00054     return new KHTMLImage( parentWidget, widgetName, parent, name );
00055 }
00056 
00057 KHTMLImage::KHTMLImage( QWidget *parentWidget, const char *widgetName,
00058                         QObject *parent, const char *name )
00059     : KParts::ReadOnlyPart( parent, name )
00060 {
00061     setInstance( KHTMLImageFactory::instance() );
00062 
00063     QVBox *box = new QVBox( parentWidget, widgetName );
00064 
00065     m_khtml = new KHTMLPart( box, widgetName, this, "htmlimagepart" );
00066     m_khtml->setAutoloadImages( true );
00067 
00068     setWidget( box );
00069 
00070     // VBox can't take focus, so pass it on to sub-widget
00071     box->setFocusProxy( m_khtml->widget() );
00072 
00073     m_ext = new KHTMLImageBrowserExtension( this, "be" );
00074 
00075     // Remove unnecessary actions.
00076     KAction *encodingAction = actionCollection()->action( "setEncoding" );
00077     if ( encodingAction )
00078     {
00079         encodingAction->unplugAll();
00080         delete encodingAction;
00081     }
00082     KAction *viewSourceAction= actionCollection()->action( "viewDocumentSource" );
00083     if ( viewSourceAction )
00084     {
00085         viewSourceAction->unplugAll();
00086         delete viewSourceAction;
00087     }
00088 
00089     KAction *selectAllAction= actionCollection()->action( "selectAll" );
00090     if ( selectAllAction )
00091     {
00092         selectAllAction->unplugAll();
00093         delete selectAllAction;
00094     }
00095 
00096    connect( m_khtml->browserExtension(), SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &, const QString &, mode_t ) ),
00097              m_ext, SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &, const QString &, mode_t ) ) );
00098 
00099     connect( m_khtml->browserExtension(), SIGNAL( enableAction( const char *, bool ) ),
00100              m_ext, SIGNAL( enableAction( const char *, bool ) ) );
00101 
00102     m_ext->setURLDropHandlingEnabled( true );
00103 }
00104 
00105 KHTMLImage::~KHTMLImage()
00106 {
00107     // important: delete the html part before the part or qobject destructor runs.
00108     // we now delete the htmlpart which deletes the part's widget which makes
00109     // _OUR_ m_widget 0 which in turn avoids our part destructor to delete the
00110     // widget ;-)
00111     // ### additional note: it _can_ be that the part has been deleted before:
00112     // when we're in a html frameset and the view dies first, then it will also
00113     // kill the htmlpart
00114     if ( m_khtml )
00115         delete static_cast<KHTMLPart *>( m_khtml );
00116 }
00117 
00118 bool KHTMLImage::openURL( const KURL &url )
00119 {
00120     static const QString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
00121 
00122     m_url = url;
00123 
00124     emit started( 0 );
00125 
00126     KParts::URLArgs args = m_ext->urlArgs();
00127     m_mimeType = args.serviceType;
00128 
00129     m_khtml->begin( m_url, args.xOffset, args.yOffset );
00130     m_khtml->setAutoloadImages( true );
00131 
00132     DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() ); // ### hack ;-)
00133     if ( impl && m_ext->urlArgs().reload )
00134         impl->docLoader()->setCachePolicy( KIO::CC_Refresh );
00135 
00136     m_khtml->write( html.arg( m_url.url() ) );
00137     m_khtml->end();
00138 
00139     emit setWindowCaption( url.prettyURL() );
00140 
00141     connect( khtml::Cache::loader(), SIGNAL( requestDone( khtml::DocLoader*, khtml::CachedObject *) ),
00142             this, SLOT( updateWindowCaption() ) );
00143     return true;
00144 }
00145 
00146 bool KHTMLImage::closeURL()
00147 {
00148     return m_khtml->closeURL();
00149 }
00150 
00151 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
00152 {
00153     if ( e->activated() )
00154         emit setWindowCaption( m_url.prettyURL() );
00155 }
00156 
00157 void KHTMLImage::slotPopupMenu( KXMLGUIClient *cl, const QPoint &pos, const KURL &u,
00158                                 const QString &, mode_t mode )
00159 {
00160     emit m_ext->popupMenu( cl, pos, u, m_mimeType, mode );
00161 }
00162 
00163 void KHTMLImage::slotImageJobFinished( KIO::Job *job )
00164 {
00165     if ( job->error() )
00166     {
00167         job->showErrorDialog();
00168         emit canceled( job->errorString() );
00169     }
00170     else
00171     {
00172         if ( m_khtml->view()->contentsY() == 0 )
00173         {
00174             KParts::URLArgs args = m_ext->urlArgs();
00175             m_khtml->view()->setContentsPos( args.xOffset, args.yOffset );
00176         }
00177 
00178         emit completed();
00179 
00180         QTimer::singleShot( 0, this, SLOT( updateWindowCaption() ) );
00181     }
00182 }
00183 
00184 void KHTMLImage::updateWindowCaption()
00185 {
00186     if ( !m_khtml )
00187         return;
00188 
00189     DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
00190     if ( !impl )
00191         return;
00192 
00193     DOM::HTMLElementImpl *body = impl->body();
00194     if ( !body )
00195         return;
00196 
00197     DOM::NodeImpl *image = body->firstChild();
00198     if ( !image )
00199         return;
00200 
00201     khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
00202     if ( !renderImage )
00203         return;
00204 
00205     QPixmap pix = renderImage->pixmap();
00206 
00207     QString caption;
00208 
00209     KMimeType::Ptr mimeType;
00210     if ( !m_mimeType.isEmpty() )
00211         mimeType = KMimeType::mimeType( m_mimeType );
00212 
00213     if ( mimeType )
00214         caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00215                   .arg( pix.width() ).arg( pix.height() );
00216     else
00217         caption = i18n( "Image - %2x%3 Pixels" ).arg( pix.width() ).arg( pix.height() );
00218 
00219     emit setWindowCaption( caption );
00220     emit completed();
00221     emit setStatusBarText(i18n("Done."));
00222 }
00223 
00224 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent, const char *name )
00225     : KParts::BrowserExtension( parent, name )
00226 {
00227     m_imgPart = parent;
00228 }
00229 
00230 int KHTMLImageBrowserExtension::xOffset()
00231 {
00232     return m_imgPart->doc()->view()->contentsX();
00233 }
00234 
00235 int KHTMLImageBrowserExtension::yOffset()
00236 {
00237     return m_imgPart->doc()->view()->contentsY();
00238 }
00239 
00240 void KHTMLImageBrowserExtension::print()
00241 {
00242     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00243 }
00244 
00245 void KHTMLImageBrowserExtension::reparseConfiguration()
00246 {
00247     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00248     m_imgPart->doc()->setAutoloadImages( true );
00249 }
00250 
00251 using namespace KParts;
00252 
00253 /* vim: et sw=4 ts=4
00254  */
00255 
00256 #include "khtmlimage.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:16:37 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001