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., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, 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 *className, const QStringList & )
00054 {
00055   KHTMLPart::GUIProfile prof = KHTMLPart::DefaultGUI;
00056   if ( strcmp( className, "Browser/View" ) == 0 )
00057     prof = KHTMLPart::BrowserViewGUI;
00058   return new KHTMLImage( parentWidget, widgetName, parent, name, prof );
00059 }
00060 
00061 KHTMLImage::KHTMLImage( QWidget *parentWidget, const char *widgetName,
00062                         QObject *parent, const char *name, KHTMLPart::GUIProfile prof )
00063     : KParts::ReadOnlyPart( parent, name ), m_image( 0 )
00064 {
00065     KHTMLPart* parentPart = ::qt_cast<KHTMLPart *>( parent );
00066     setInstance( KHTMLImageFactory::instance(), prof == KHTMLPart::BrowserViewGUI && !parentPart );
00067 
00068     QVBox *box = new QVBox( parentWidget, widgetName );
00069 
00070     m_khtml = new KHTMLPart( box, widgetName, this, "htmlimagepart", prof );
00071     m_khtml->setAutoloadImages( true );
00072     m_khtml->widget()->installEventFilter(this);
00073     connect( m_khtml->view(), SIGNAL( finishedLayout() ), this, SLOT( restoreScrollPosition() ) );
00074 
00075     setWidget( box );
00076 
00077     // VBox can't take focus, so pass it on to sub-widget
00078     box->setFocusProxy( m_khtml->widget() );
00079 
00080     m_ext = new KHTMLImageBrowserExtension( this, "be" );
00081 
00082     // Remove unnecessary actions.
00083     KAction *encodingAction = actionCollection()->action( "setEncoding" );
00084     if ( encodingAction )
00085     {
00086         encodingAction->unplugAll();
00087         delete encodingAction;
00088     }
00089     KAction *viewSourceAction= actionCollection()->action( "viewDocumentSource" );
00090     if ( viewSourceAction )
00091     {
00092         viewSourceAction->unplugAll();
00093         delete viewSourceAction;
00094     }
00095 
00096     KAction *selectAllAction= actionCollection()->action( "selectAll" );
00097     if ( selectAllAction )
00098     {
00099         selectAllAction->unplugAll();
00100         delete selectAllAction;
00101     }
00102 
00103     // forward important signals from the khtml part
00104 
00105     // forward opening requests to parent frame (if existing)
00106     KHTMLPart *p = ::qt_cast<KHTMLPart *>(parent);
00107     KParts::BrowserExtension *be = p ? p->browserExtension() : m_ext;
00108     connect(m_khtml->browserExtension(), SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)),
00109             be, SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)));
00110 
00111     connect( m_khtml->browserExtension(), SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &,
00112              const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ), m_ext, SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &,
00113              const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ) );
00114 
00115     connect( m_khtml->browserExtension(), SIGNAL( enableAction( const char *, bool ) ),
00116              m_ext, SIGNAL( enableAction( const char *, bool ) ) );
00117 
00118     m_ext->setURLDropHandlingEnabled( true );
00119 }
00120 
00121 KHTMLImage::~KHTMLImage()
00122 {
00123     disposeImage();
00124 
00125     // important: delete the html part before the part or qobject destructor runs.
00126     // we now delete the htmlpart which deletes the part's widget which makes
00127     // _OUR_ m_widget 0 which in turn avoids our part destructor to delete the
00128     // widget ;-)
00129     // ### additional note: it _can_ be that the part has been deleted before:
00130     // when we're in a html frameset and the view dies first, then it will also
00131     // kill the htmlpart
00132     if ( m_khtml )
00133         delete static_cast<KHTMLPart *>( m_khtml );
00134 }
00135 
00136 bool KHTMLImage::openURL( const KURL &url )
00137 {
00138     static const QString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
00139 
00140     disposeImage();
00141 
00142     m_url = url;
00143 
00144     emit started( 0 );
00145 
00146     KParts::URLArgs args = m_ext->urlArgs();
00147     m_mimeType = args.serviceType;
00148 
00149     emit setWindowCaption( url.prettyURL() );
00150 
00151     // Need to keep a copy of the offsets since they are cleared when emitting completed
00152     m_xOffset = args.xOffset;
00153     m_yOffset = args.yOffset;
00154 
00155     m_khtml->begin( m_url );
00156     m_khtml->setAutoloadImages( true );
00157 
00158     DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() ); // ### hack ;-)
00159     if ( impl && m_ext->urlArgs().reload )
00160         impl->docLoader()->setCachePolicy( KIO::CC_Reload );
00161 
00162     khtml::DocLoader *dl = impl ? impl->docLoader() : 0;
00163     m_image = dl->requestImage( m_url.url() );
00164     if ( m_image )
00165         m_image->ref( this );
00166 
00167     m_khtml->write( html.arg( m_url.url() ) );
00168     m_khtml->end();
00169 
00170     /*
00171     connect( khtml::Cache::loader(), SIGNAL( requestDone( khtml::DocLoader*, khtml::CachedObject *) ),
00172             this, SLOT( updateWindowCaption() ) );
00173             */
00174     return true;
00175 }
00176 
00177 bool KHTMLImage::closeURL()
00178 {
00179     disposeImage();
00180     return m_khtml->closeURL();
00181 }
00182 
00183 // This can happen after openURL returns, or directly from m_image->ref()
00184 void KHTMLImage::notifyFinished( khtml::CachedObject *o )
00185 {
00186     if ( !m_image || o != m_image )
00187         return;
00188 
00189     const QPixmap &pix = m_image->pixmap();
00190     QString caption;
00191 
00192     KMimeType::Ptr mimeType;
00193     if ( !m_mimeType.isEmpty() )
00194         mimeType = KMimeType::mimeType( m_mimeType );
00195 
00196     if ( mimeType ) {
00197         if (m_image && !m_image->suggestedTitle().isEmpty()) {
00198             caption = i18n( "%1 (%2 - %3x%4 Pixels)" ).arg( m_image->suggestedTitle(), mimeType->comment() ).arg( pix.width() ).arg( pix.height() );
00199         } else {
00200             caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00201                 .arg( pix.width() ).arg( pix.height() );
00202         }
00203     } else {
00204         if (m_image && !m_image->suggestedTitle().isEmpty()) {
00205             caption = i18n( "%1 (%2x%3 Pixels)" ).arg(m_image->suggestedTitle()).arg( pix.width() ).arg( pix.height() );
00206         } else {
00207             caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
00208         }
00209     }
00210 
00211     emit setWindowCaption( caption );
00212     emit completed();
00213     emit setStatusBarText(i18n("Done."));
00214 }
00215 
00216 void KHTMLImage::restoreScrollPosition()
00217 {
00218     if ( m_khtml->view()->contentsY() == 0 ) {
00219         m_khtml->view()->setContentsPos( m_xOffset, m_yOffset );
00220     }
00221 }
00222 
00223 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
00224 {
00225     // prevent the base implementation from emitting setWindowCaption with
00226     // our url. It destroys our pretty, previously caption. Konq saves/restores
00227     // the caption for us anyway.
00228     if ( e->activated() )
00229         return;
00230     KParts::ReadOnlyPart::guiActivateEvent(e);
00231 }
00232 
00233 /*
00234 void KHTMLImage::slotImageJobFinished( KIO::Job *job )
00235 {
00236     if ( job->error() )
00237     {
00238         job->showErrorDialog();
00239         emit canceled( job->errorString() );
00240     }
00241     else
00242     {
00243         emit completed();
00244         QTimer::singleShot( 0, this, SLOT( updateWindowCaption() ) );
00245     }
00246 }
00247 
00248 void KHTMLImage::updateWindowCaption()
00249 {
00250     if ( !m_khtml )
00251         return;
00252 
00253     DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
00254     if ( !impl )
00255         return;
00256 
00257     DOM::HTMLElementImpl *body = impl->body();
00258     if ( !body )
00259         return;
00260 
00261     DOM::NodeImpl *image = body->firstChild();
00262     if ( !image )
00263         return;
00264 
00265     khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
00266     if ( !renderImage )
00267         return;
00268 
00269     QPixmap pix = renderImage->pixmap();
00270 
00271     QString caption;
00272 
00273     KMimeType::Ptr mimeType;
00274     if ( !m_mimeType.isEmpty() )
00275         mimeType = KMimeType::mimeType( m_mimeType );
00276 
00277     if ( mimeType )
00278         caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00279                   .arg( pix.width() ).arg( pix.height() );
00280     else
00281         caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
00282 
00283     emit setWindowCaption( caption );
00284     emit completed();
00285     emit setStatusBarText(i18n("Done."));
00286 }
00287 */
00288 
00289 void KHTMLImage::disposeImage()
00290 {
00291     if ( !m_image )
00292         return;
00293 
00294     m_image->deref( this );
00295     m_image = 0;
00296 }
00297 
00298 bool KHTMLImage::eventFilter(QObject *, QEvent *e) {
00299     switch (e->type()) {
00300       case QEvent::DragEnter:
00301       case QEvent::DragMove:
00302       case QEvent::DragLeave:
00303       case QEvent::Drop: {
00304         // find out if this part is embedded in a frame, and send the
00305     // event to its outside widget
00306     KHTMLPart *p = ::qt_cast<KHTMLPart *>(parent());
00307     if (p)
00308         return QApplication::sendEvent(p->widget(), e);
00309         // otherwise simply forward all dnd events to the part widget,
00310     // konqueror will handle them properly there
00311         return QApplication::sendEvent(widget(), e);
00312       }
00313       default: ;
00314     }
00315     return false;
00316 }
00317 
00318 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent, const char *name )
00319     : KParts::BrowserExtension( parent, name )
00320 {
00321     m_imgPart = parent;
00322 }
00323 
00324 int KHTMLImageBrowserExtension::xOffset()
00325 {
00326     return m_imgPart->doc()->view()->contentsX();
00327 }
00328 
00329 int KHTMLImageBrowserExtension::yOffset()
00330 {
00331     return m_imgPart->doc()->view()->contentsY();
00332 }
00333 
00334 void KHTMLImageBrowserExtension::print()
00335 {
00336     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00337 }
00338 
00339 void KHTMLImageBrowserExtension::reparseConfiguration()
00340 {
00341     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00342     m_imgPart->doc()->setAutoloadImages( true );
00343 }
00344 
00345 
00346 void KHTMLImageBrowserExtension::disableScrolling()
00347 {
00348     static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->disableScrolling();
00349 }
00350 
00351 using namespace KParts;
00352 
00353 /* vim: et sw=4 ts=4
00354  */
00355 
00356 #include "khtmlimage.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys