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
00030 #include <QtCore/QTimer>
00031
00032 #include <kjobuidelegate.h>
00033 #include <kio/job.h>
00034 #include <kcomponentdata.h>
00035 #include <kmimetype.h>
00036 #include <klocale.h>
00037 #include <kvbox.h>
00038 #include <kactioncollection.h>
00039
00040
00041
00042 K_EXPORT_COMPONENT_FACTORY( khtmlimagefactory, KHTMLImageFactory )
00043
00044 KComponentData *KHTMLImageFactory::s_componentData = 0;
00045
00046 KHTMLImageFactory::KHTMLImageFactory()
00047 {
00048 s_componentData = new KComponentData( "khtmlimage" );
00049 }
00050
00051 KHTMLImageFactory::~KHTMLImageFactory()
00052 {
00053 delete s_componentData;
00054 }
00055
00056 KParts::Part *KHTMLImageFactory::createPartObject( QWidget *parentWidget,
00057 QObject *parent,
00058 const char *className, const QStringList &args )
00059 {
00060 KHTMLPart::GUIProfile prof = KHTMLPart::DefaultGUI;
00061 if ( strcmp( className, "Browser/View" ) == 0 )
00062 prof = KHTMLPart::BrowserViewGUI;
00063 if (args.contains("Browser/View"))
00064 prof = KHTMLPart::BrowserViewGUI;
00065 return new KHTMLImage( parentWidget, parent, prof );
00066 }
00067
00068 KHTMLImage::KHTMLImage( QWidget *parentWidget,
00069 QObject *parent, KHTMLPart::GUIProfile prof )
00070 : KParts::ReadOnlyPart( parent ), m_image( 0 )
00071 {
00072 KHTMLPart* parentPart = qobject_cast<KHTMLPart*>( parent );
00073 setComponentData( KHTMLImageFactory::componentData(), prof == KHTMLPart::BrowserViewGUI && !parentPart );
00074
00075 KVBox *box = new KVBox( parentWidget );
00076 box->setAcceptDrops( true );
00077
00078 m_khtml = new KHTMLPart( box, this, prof );
00079 m_khtml->setAutoloadImages( true );
00080
00081 connect( m_khtml->view(), SIGNAL( finishedLayout() ), this, SLOT( restoreScrollPosition() ) );
00082
00083 setWidget( box );
00084
00085
00086 box->setFocusProxy( m_khtml->widget() );
00087
00088 m_ext = new KHTMLImageBrowserExtension( this );
00089 m_ext->setObjectName( "be" );
00090
00091
00092 delete actionCollection()->action( "setEncoding" );
00093 delete actionCollection()->action( "viewDocumentSource" );
00094 delete actionCollection()->action( "selectAll" );
00095
00096
00097
00098
00099 KHTMLPart *p = qobject_cast<KHTMLPart*>(parent);
00100 KParts::BrowserExtension *be = p ? p->browserExtension() : m_ext;
00101 connect(m_khtml->browserExtension(), SIGNAL(openUrlRequestDelayed(const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments &)),
00102 be, SIGNAL(openUrlRequestDelayed(const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments &)));
00103
00104 connect(m_khtml->browserExtension(), SIGNAL(popupMenu(QPoint,KUrl,mode_t,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)),
00105 this, SLOT(slotPopupMenu(QPoint,KUrl,mode_t,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)));
00106
00107 connect( m_khtml->browserExtension(), SIGNAL( enableAction( const char *, bool ) ),
00108 m_ext, SIGNAL( enableAction( const char *, bool ) ) );
00109
00110 m_ext->setURLDropHandlingEnabled( true );
00111 }
00112
00113 KHTMLImage::~KHTMLImage()
00114 {
00115 disposeImage();
00116
00117
00118
00119
00120
00121
00122
00123
00124 if ( m_khtml )
00125 delete static_cast<KHTMLPart *>( m_khtml );
00126 }
00127
00128 bool KHTMLImage::openUrl( const KUrl &url )
00129 {
00130 static const QString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
00131
00132 disposeImage();
00133
00134 setUrl(url);
00135
00136 emit started( 0 );
00137
00138 KParts::OpenUrlArguments args = arguments();
00139 m_mimeType = args.mimeType();
00140
00141 emit setWindowCaption( url.prettyUrl() );
00142
00143
00144 m_xOffset = args.xOffset();
00145 m_yOffset = args.yOffset();
00146
00147 m_khtml->begin( this->url() );
00148 m_khtml->setAutoloadImages( true );
00149
00150 DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() );
00151 if (!impl) return false;
00152
00153 if ( arguments().reload() )
00154 impl->docLoader()->setCachePolicy( KIO::CC_Reload );
00155
00156 khtml::DocLoader *dl = impl->docLoader();
00157 m_image = dl->requestImage( this->url().url() );
00158 if ( m_image )
00159 m_image->ref( this );
00160
00161 m_khtml->write( html.arg( this->url().url() ) );
00162 m_khtml->end();
00163
00164
00165
00166
00167
00168 return true;
00169 }
00170
00171 bool KHTMLImage::closeUrl()
00172 {
00173 disposeImage();
00174 return m_khtml->closeUrl();
00175 }
00176
00177
00178 void KHTMLImage::notifyFinished( khtml::CachedObject *o )
00179 {
00180 if ( !m_image || o != m_image )
00181 return;
00182
00183
00184 QString caption;
00185
00186 KMimeType::Ptr mimeType;
00187 if ( !m_mimeType.isEmpty() )
00188 mimeType = KMimeType::mimeType(m_mimeType, KMimeType::ResolveAliases);
00189
00190 if ( mimeType ) {
00191 if (m_image && !m_image->suggestedTitle().isEmpty()) {
00192 caption = i18n( "%1 (%2 - %3x%4 Pixels)", m_image->suggestedTitle(), mimeType->comment(), m_image->pixmap_size().width(), m_image->pixmap_size().height() );
00193 } else {
00194 caption = i18n( "%1 - %2x%3 Pixels" , mimeType->comment() ,
00195 m_image->pixmap_size().width() , m_image->pixmap_size().height() );
00196 }
00197 } else {
00198 if (m_image && !m_image->suggestedTitle().isEmpty()) {
00199 caption = i18n( "%1 (%2x%3 Pixels)" , m_image->suggestedTitle(), m_image->pixmap_size().width() , m_image->pixmap_size().height() );
00200 } else {
00201 caption = i18n( "Image - %1x%2 Pixels" , m_image->pixmap_size().width() , m_image->pixmap_size().height() );
00202 }
00203 }
00204
00205 emit setWindowCaption( caption );
00206 emit completed();
00207 emit setStatusBarText(i18n("Done."));
00208 }
00209
00210 void KHTMLImage::restoreScrollPosition()
00211 {
00212 if ( m_khtml->view()->contentsY() == 0 ) {
00213 m_khtml->view()->setContentsPos( m_xOffset, m_yOffset );
00214 }
00215 }
00216
00217 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
00218 {
00219
00220
00221
00222 if ( e->activated() )
00223 return;
00224 KParts::ReadOnlyPart::guiActivateEvent(e);
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
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 void KHTMLImage::disposeImage()
00284 {
00285 if ( !m_image )
00286 return;
00287
00288 m_image->deref( this );
00289 m_image = 0;
00290 }
00291
00292 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent )
00293 : KParts::BrowserExtension( parent )
00294 {
00295 m_imgPart = parent;
00296 }
00297
00298 int KHTMLImageBrowserExtension::xOffset()
00299 {
00300 return m_imgPart->doc()->view()->contentsX();
00301 }
00302
00303 int KHTMLImageBrowserExtension::yOffset()
00304 {
00305 return m_imgPart->doc()->view()->contentsY();
00306 }
00307
00308 void KHTMLImageBrowserExtension::print()
00309 {
00310 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
00311 }
00312
00313 void KHTMLImageBrowserExtension::reparseConfiguration()
00314 {
00315 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
00316 m_imgPart->doc()->setAutoloadImages( true );
00317 }
00318
00319
00320 void KHTMLImageBrowserExtension::disableScrolling()
00321 {
00322 static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->disableScrolling();
00323 }
00324
00325 void KHTMLImage::slotPopupMenu( const QPoint &global, const KUrl &url, mode_t mode,
00326 const KParts::OpenUrlArguments &origArgs,
00327 const KParts::BrowserArguments &browserArgs,
00328 KParts::BrowserExtension::PopupFlags flags,
00329 const KParts::BrowserExtension::ActionGroupMap& actionGroups )
00330 {
00331 KParts::OpenUrlArguments args = origArgs;
00332 args.setMimeType(m_mimeType);
00333 m_ext->popupMenu(global, url, mode, args, browserArgs, flags, actionGroups);
00334 }
00335
00336 #include "khtmlimage.moc"