00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00071 box->setFocusProxy( m_khtml->widget() );
00072
00073 m_ext = new KHTMLImageBrowserExtension( this, "be" );
00074
00075 connect( m_khtml->browserExtension(), SIGNAL( popupMenu( KXMLGUIClient *, const QPoint &, const KURL &, const QString &, mode_t ) ),
00076 this, SLOT( slotPopupMenu( KXMLGUIClient *, const QPoint &, const KURL &, const QString &, mode_t ) ) );
00077
00078 connect( m_khtml->browserExtension(), SIGNAL( enableAction( const char *, bool ) ),
00079 m_ext, SIGNAL( enableAction( const char *, bool ) ) );
00080
00081 m_ext->setURLDropHandlingEnabled( true );
00082 }
00083
00084 KHTMLImage::~KHTMLImage()
00085 {
00086
00087
00088
00089
00090
00091
00092
00093 if ( m_khtml )
00094 delete static_cast<KHTMLPart *>( m_khtml );
00095 }
00096
00097 bool KHTMLImage::openURL( const KURL &url )
00098 {
00099 static const QString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
00100
00101 m_url = url;
00102
00103 emit started( 0 );
00104
00105 KParts::URLArgs args = m_ext->urlArgs();
00106 m_mimeType = args.serviceType;
00107
00108 m_khtml->begin( m_url, args.xOffset, args.yOffset );
00109 m_khtml->setAutoloadImages( true );
00110
00111 DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() );
00112 if ( impl && m_ext->urlArgs().reload )
00113 impl->docLoader()->setCachePolicy( KIO::CC_Refresh );
00114
00115 m_khtml->write( html.arg( m_url.url() ) );
00116 m_khtml->end();
00117
00118 KIO::Job *job = khtml::Cache::loader()->jobForRequest( m_url.url() );
00119
00120 emit setWindowCaption( url.prettyURL() );
00121
00122 if ( job )
00123 {
00124 emit started( job );
00125
00126 connect( job, SIGNAL( result( KIO::Job * ) ),
00127 this, SLOT( slotImageJobFinished( KIO::Job * ) ) );
00128 }
00129 else
00130 {
00131 emit started( 0 );
00132 emit completed();
00133 QTimer::singleShot( 0, this, SLOT( updateWindowCaption() ) );
00134 }
00135
00136 return true;
00137 }
00138
00139 bool KHTMLImage::closeURL()
00140 {
00141 return m_khtml->closeURL();
00142 }
00143
00144 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
00145 {
00146 if ( e->activated() )
00147 emit setWindowCaption( m_url.prettyURL() );
00148 }
00149
00150 void KHTMLImage::slotPopupMenu( KXMLGUIClient *cl, const QPoint &pos, const KURL &u,
00151 const QString &, mode_t mode )
00152 {
00153 KAction *encodingAction = cl->actionCollection()->action( "setEncoding" );
00154 if ( encodingAction )
00155 cl->actionCollection()->take( encodingAction );
00156 KAction *viewSourceAction= cl->actionCollection()->action( "viewDocumentSource" );
00157 if ( viewSourceAction )
00158 cl->actionCollection()->take( viewSourceAction );
00159
00160 KAction *selectAllAction= cl->actionCollection()->action( "selectAll" );
00161 if ( selectAllAction )
00162 cl->actionCollection()->take( selectAllAction );
00163
00164 emit m_ext->popupMenu( cl, pos, u, m_mimeType, mode );
00165 }
00166
00167 void KHTMLImage::slotImageJobFinished( KIO::Job *job )
00168 {
00169 if ( job->error() )
00170 {
00171 job->showErrorDialog();
00172 emit canceled( job->errorString() );
00173 }
00174 else
00175 {
00176 if ( m_khtml->view()->contentsY() == 0 )
00177 {
00178 KParts::URLArgs args = m_ext->urlArgs();
00179 m_khtml->view()->setContentsPos( args.xOffset, args.yOffset );
00180 }
00181
00182 emit completed();
00183
00184 QTimer::singleShot( 0, this, SLOT( updateWindowCaption() ) );
00185 }
00186 }
00187
00188 void KHTMLImage::updateWindowCaption()
00189 {
00190 if ( !m_khtml )
00191 return;
00192
00193 DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
00194 if ( !impl )
00195 return;
00196
00197 DOM::HTMLElementImpl *body = impl->body();
00198 if ( !body )
00199 return;
00200
00201 DOM::NodeImpl *image = body->firstChild();
00202 if ( !image )
00203 return;
00204
00205 khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
00206 if ( !renderImage )
00207 return;
00208
00209 QPixmap pix = renderImage->pixmap();
00210
00211 QString caption;
00212
00213 KMimeType::Ptr mimeType;
00214 if ( !m_mimeType.isEmpty() )
00215 mimeType = KMimeType::mimeType( m_mimeType );
00216
00217 if ( mimeType )
00218 caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
00219 .arg( pix.width() ).arg( pix.height() );
00220 else
00221 caption = i18n( "Image - %2x%3 Pixels" ).arg( pix.width() ).arg( pix.height() );
00222
00223 emit setWindowCaption( caption );
00224 emit setStatusBarText(i18n("Done."));
00225 }
00226
00227 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent, const char *name )
00228 : KParts::BrowserExtension( parent, name )
00229 {
00230 m_imgPart = parent;
00231 }
00232
00233 int KHTMLImageBrowserExtension::xOffset()
00234 {
00235 return m_imgPart->doc()->view()->contentsX();
00236 }
00237
00238 int KHTMLImageBrowserExtension::yOffset()
00239 {
00240 return m_imgPart->doc()->view()->contentsY();
00241 }
00242
00243 void KHTMLImageBrowserExtension::print()
00244 {
00245 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00246 }
00247
00248 void KHTMLImageBrowserExtension::reparseConfiguration()
00249 {
00250 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00251 m_imgPart->doc()->setAutoloadImages( true );
00252 }
00253
00254 using namespace KParts;
00255
00256
00257
00258
00259 #include "khtmlimage.moc"