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 #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 , 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
00073 box->setFocusProxy( m_khtml->widget() );
00074
00075 m_ext = new KHTMLImageBrowserExtension( this, "be" );
00076
00077
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
00099
00100
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
00121
00122
00123
00124
00125
00126
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() );
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
00163
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
00200
00201
00202 if ( e->activated() )
00203 return;
00204 KParts::ReadOnlyPart::guiActivateEvent(e);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
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
00286
00287 KHTMLPart *p = ::qt_cast<KHTMLPart *>(parent());
00288 if (p)
00289 return QApplication::sendEvent(p->widget(), e);
00290
00291
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
00329
00330
00331 #include "khtmlimage.moc"