• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • khtml
 

khtml

  • khtml
khtmlimage.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "khtmlimage.h"
21 #include "khtmlview.h"
22 #include "khtml_ext.h"
23 #include "xml/dom_docimpl.h"
24 #include "html/html_documentimpl.h"
25 #include "html/html_elementimpl.h"
26 #include "rendering/render_image.h"
27 #include "misc/loader.h"
28 
29 #include <tqvbox.h>
30 #include <tqtimer.h>
31 
32 #include <kio/job.h>
33 #include <kinstance.h>
34 #include <kmimetype.h>
35 #include <klocale.h>
36 
37 K_EXPORT_COMPONENT_FACTORY( khtmlimagefactory /*NOT the part name, see Makefile.am*/, KHTMLImageFactory )
38 
39 KInstance *KHTMLImageFactory::s_instance = 0;
40 
41 KHTMLImageFactory::KHTMLImageFactory()
42 {
43  s_instance = new KInstance( "khtmlimage" );
44 }
45 
46 KHTMLImageFactory::~KHTMLImageFactory()
47 {
48  delete s_instance;
49 }
50 
51 KParts::Part *KHTMLImageFactory::createPartObject( TQWidget *parentWidget, const char *widgetName,
52  TQObject *parent, const char *name,
53  const char *className, const TQStringList & )
54 {
55  KHTMLPart::GUIProfile prof = KHTMLPart::DefaultGUI;
56  if ( strcmp( className, "Browser/View" ) == 0 )
57  prof = KHTMLPart::BrowserViewGUI;
58  return new KHTMLImage( parentWidget, widgetName, parent, name, prof );
59 }
60 
61 KHTMLImage::KHTMLImage( TQWidget *parentWidget, const char *widgetName,
62  TQObject *parent, const char *name, KHTMLPart::GUIProfile prof )
63  : KParts::ReadOnlyPart( parent, name ), m_image( 0 )
64 {
65  KHTMLPart* parentPart = ::tqqt_cast<KHTMLPart *>( parent );
66  setInstance( KHTMLImageFactory::instance(), prof == KHTMLPart::BrowserViewGUI && !parentPart );
67 
68  TQVBox *box = new TQVBox( parentWidget, widgetName );
69 
70  m_khtml = new KHTMLPart( box, widgetName, this, "htmlimagepart", prof );
71  m_khtml->setAutoloadImages( true );
72  m_khtml->widget()->installEventFilter(this);
73  connect( m_khtml->view(), TQT_SIGNAL( finishedLayout() ), this, TQT_SLOT( restoreScrollPosition() ) );
74 
75  setWidget( box );
76 
77  // VBox can't take focus, so pass it on to sub-widget
78  box->setFocusProxy( m_khtml->widget() );
79 
80  m_ext = new KHTMLImageBrowserExtension( this, "be" );
81 
82  // Remove unnecessary actions.
83  KAction *encodingAction = actionCollection()->action( "setEncoding" );
84  if ( encodingAction )
85  {
86  encodingAction->unplugAll();
87  delete encodingAction;
88  }
89  KAction *viewSourceAction= actionCollection()->action( "viewDocumentSource" );
90  if ( viewSourceAction )
91  {
92  viewSourceAction->unplugAll();
93  delete viewSourceAction;
94  }
95 
96  KAction *selectAllAction= actionCollection()->action( "selectAll" );
97  if ( selectAllAction )
98  {
99  selectAllAction->unplugAll();
100  delete selectAllAction;
101  }
102 
103  // forward important signals from the khtml part
104 
105  // forward opening requests to parent frame (if existing)
106  KHTMLPart *p = ::tqqt_cast<KHTMLPart *>(parent);
107  KParts::BrowserExtension *be = p ? p->browserExtension() : m_ext;
108  connect(m_khtml->browserExtension(), TQT_SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)),
109  be, TQT_SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)));
110 
111  connect( m_khtml->browserExtension(), TQT_SIGNAL( popupMenu( KXMLGUIClient *, const TQPoint &, const KURL &,
112  const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ), m_ext, TQT_SIGNAL( popupMenu( KXMLGUIClient *, const TQPoint &, const KURL &,
113  const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ) );
114 
115  connect( m_khtml->browserExtension(), TQT_SIGNAL( enableAction( const char *, bool ) ),
116  m_ext, TQT_SIGNAL( enableAction( const char *, bool ) ) );
117 
118  m_ext->setURLDropHandlingEnabled( true );
119 }
120 
121 KHTMLImage::~KHTMLImage()
122 {
123  disposeImage();
124 
125  // important: delete the html part before the part or qobject destructor runs.
126  // we now delete the htmlpart which deletes the part's widget which makes
127  // _OUR_ m_widget 0 which in turn avoids our part destructor to delete the
128  // widget ;-)
129  // ### additional note: it _can_ be that the part has been deleted before:
130  // when we're in a html frameset and the view dies first, then it will also
131  // kill the htmlpart
132  if ( m_khtml )
133  delete static_cast<KHTMLPart *>( m_khtml );
134 }
135 
136 bool KHTMLImage::openURL( const KURL &url )
137 {
138  static const TQString &html = KGlobal::staticQString( "<html><body><img src=\"%1\"></body></html>" );
139 
140  disposeImage();
141 
142  m_url = url;
143 
144  emit started( 0 );
145 
146  KParts::URLArgs args = m_ext->urlArgs();
147  m_mimeType = args.serviceType;
148 
149  emit setWindowCaption( url.prettyURL() );
150 
151  // Need to keep a copy of the offsets since they are cleared when emitting completed
152  m_xOffset = args.xOffset;
153  m_yOffset = args.yOffset;
154 
155  m_khtml->begin( m_url );
156  m_khtml->setAutoloadImages( true );
157 
158  DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_khtml->document().handle() ); // ### hack ;-)
159  if (!impl) return false;
160  if ( m_ext->urlArgs().reload )
161  impl->docLoader()->setCachePolicy( KIO::CC_Reload );
162 
163  khtml::DocLoader *dl = impl->docLoader();
164  m_image = dl->requestImage( m_url.url() );
165  if ( m_image )
166  m_image->ref( this );
167 
168  m_khtml->write( html.arg( m_url.url() ) );
169  m_khtml->end();
170 
171  /*
172  connect( khtml::Cache::loader(), TQT_SIGNAL( requestDone( khtml::DocLoader*, khtml::CachedObject *) ),
173  this, TQT_SLOT( updateWindowCaption() ) );
174  */
175  return true;
176 }
177 
178 bool KHTMLImage::closeURL()
179 {
180  disposeImage();
181  return m_khtml->closeURL();
182 }
183 
184 // This can happen after openURL returns, or directly from m_image->ref()
185 void KHTMLImage::notifyFinished( khtml::CachedObject *o )
186 {
187  if ( !m_image || o != m_image )
188  return;
189 
190  const TQPixmap &pix = m_image->pixmap();
191  TQString caption;
192 
193  KMimeType::Ptr mimeType;
194  if ( !m_mimeType.isEmpty() )
195  mimeType = KMimeType::mimeType( m_mimeType );
196 
197  if ( mimeType ) {
198  if (m_image && !m_image->suggestedTitle().isEmpty()) {
199  caption = i18n( "%1 (%2 - %3x%4 Pixels)" ).arg( m_image->suggestedTitle(), mimeType->comment() ).arg( pix.width() ).arg( pix.height() );
200  } else {
201  caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
202  .arg( pix.width() ).arg( pix.height() );
203  }
204  } else {
205  if (m_image && !m_image->suggestedTitle().isEmpty()) {
206  caption = i18n( "%1 (%2x%3 Pixels)" ).arg(m_image->suggestedTitle()).arg( pix.width() ).arg( pix.height() );
207  } else {
208  caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
209  }
210  }
211 
212  emit setWindowCaption( caption );
213  emit completed();
214  emit setStatusBarText(i18n("Done."));
215 }
216 
217 void KHTMLImage::restoreScrollPosition()
218 {
219  if ( m_khtml->view()->contentsY() == 0 ) {
220  m_khtml->view()->setContentsPos( m_xOffset, m_yOffset );
221  }
222 }
223 
224 void KHTMLImage::guiActivateEvent( KParts::GUIActivateEvent *e )
225 {
226  // prevent the base implementation from emitting setWindowCaption with
227  // our url. It destroys our pretty, previously caption. Konq saves/restores
228  // the caption for us anyway.
229  if ( e->activated() )
230  return;
231  KParts::ReadOnlyPart::guiActivateEvent(e);
232 }
233 
234 /*
235 void KHTMLImage::slotImageJobFinished( KIO::Job *job )
236 {
237  if ( job->error() )
238  {
239  job->showErrorDialog();
240  emit canceled( job->errorString() );
241  }
242  else
243  {
244  emit completed();
245  TQTimer::singleShot( 0, this, TQT_SLOT( updateWindowCaption() ) );
246  }
247 }
248 
249 void KHTMLImage::updateWindowCaption()
250 {
251  if ( !m_khtml )
252  return;
253 
254  DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
255  if ( !impl )
256  return;
257 
258  DOM::HTMLElementImpl *body = impl->body();
259  if ( !body )
260  return;
261 
262  DOM::NodeImpl *image = body->firstChild();
263  if ( !image )
264  return;
265 
266  khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
267  if ( !renderImage )
268  return;
269 
270  TQPixmap pix = renderImage->pixmap();
271 
272  TQString caption;
273 
274  KMimeType::Ptr mimeType;
275  if ( !m_mimeType.isEmpty() )
276  mimeType = KMimeType::mimeType( m_mimeType );
277 
278  if ( mimeType )
279  caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType->comment() )
280  .arg( pix.width() ).arg( pix.height() );
281  else
282  caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );
283 
284  emit setWindowCaption( caption );
285  emit completed();
286  emit setStatusBarText(i18n("Done."));
287 }
288 */
289 
290 void KHTMLImage::disposeImage()
291 {
292  if ( !m_image )
293  return;
294 
295  m_image->deref( this );
296  m_image = 0;
297 }
298 
299 bool KHTMLImage::eventFilter(TQObject *, TQEvent *e) {
300  switch (e->type()) {
301  case TQEvent::DragEnter:
302  case TQEvent::DragMove:
303  case TQEvent::DragLeave:
304  case TQEvent::Drop: {
305  // find out if this part is embedded in a frame, and send the
306  // event to its outside widget
307  KHTMLPart *p = ::tqqt_cast<KHTMLPart *>(parent());
308  if (p)
309  return TQApplication::sendEvent(p->widget(), e);
310  // otherwise simply forward all dnd events to the part widget,
311  // konqueror will handle them properly there
312  return TQApplication::sendEvent(widget(), e);
313  }
314  default: ;
315  }
316  return false;
317 }
318 
319 KHTMLImageBrowserExtension::KHTMLImageBrowserExtension( KHTMLImage *parent, const char *name )
320  : KParts::BrowserExtension( parent, name )
321 {
322  m_imgPart = parent;
323 }
324 
325 int KHTMLImageBrowserExtension::xOffset()
326 {
327  return m_imgPart->doc()->view()->contentsX();
328 }
329 
330 int KHTMLImageBrowserExtension::yOffset()
331 {
332  return m_imgPart->doc()->view()->contentsY();
333 }
334 
335 void KHTMLImageBrowserExtension::print()
336 {
337  static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
338 }
339 
340 void KHTMLImageBrowserExtension::reparseConfiguration()
341 {
342  static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
343  m_imgPart->doc()->setAutoloadImages( true );
344 }
345 
346 
347 void KHTMLImageBrowserExtension::disableScrolling()
348 {
349  static_cast<KHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->disableScrolling();
350 }
351 
352 using namespace KParts;
353 
354 /* vim: et sw=4 ts=4
355  */
356 
357 #include "khtmlimage.moc"
KParts::BrowserExtension
KURL
KXMLGUIClient
KParts::BrowserExtension::setURLDropHandlingEnabled
void setURLDropHandlingEnabled(bool enable)
KGlobal::staticQString
static const TQString & staticQString(const char *str)
KHTMLPart
This class is khtml&#39;s main class.
Definition: khtml_part.h:184
KParts::Part::widget
virtual TQWidget * widget()
KParts::Part
klocale.h
KHTMLPart::browserExtension
KParts::BrowserExtension * browserExtension() const
Returns a pointer to the KParts::BrowserExtension.
Definition: khtml_part.cpp:897
KInstance
KParts::GUIActivateEvent
KParts::URLArgs
KParts::URLArgs::xOffset
int xOffset
KAction
KHTMLPartBrowserExtension
This is the BrowserExtension for a KHTMLPart document.
Definition: khtml_ext.h:41
KParts::URLArgs::yOffset
int yOffset
KStdAccel::print
const KShortcut & print()
KParts
KParts::ReadOnlyPart::guiActivateEvent
virtual void guiActivateEvent(GUIActivateEvent *event)
KParts::URLArgs::serviceType
TQString serviceType
KURL::prettyURL
TQString prettyURL(int _trailing=0) const
KParts::ReadOnlyPart

khtml

Skip menu "khtml"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

khtml

Skip menu "khtml"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for khtml by doxygen 1.8.11
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |