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

tdehtml

  • tdehtml
tdehtmlimage.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 "tdehtmlimage.h"
21 #include "tdehtmlview.h"
22 #include "tdehtml_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 <tdeio/job.h>
33 #include <kinstance.h>
34 #include <kmimetype.h>
35 #include <tdelocale.h>
36 
37 K_EXPORT_COMPONENT_FACTORY( tdehtmlimagefactory /*NOT the part name, see Makefile.am*/, TDEHTMLImageFactory )
38 
39 TDEInstance *TDEHTMLImageFactory::s_instance = 0;
40 
41 TDEHTMLImageFactory::TDEHTMLImageFactory()
42 {
43  s_instance = new TDEInstance( "tdehtmlimage" );
44 }
45 
46 TDEHTMLImageFactory::~TDEHTMLImageFactory()
47 {
48  delete s_instance;
49 }
50 
51 KParts::Part *TDEHTMLImageFactory::createPartObject( TQWidget *parentWidget, const char *widgetName,
52  TQObject *parent, const char *name,
53  const char *className, const TQStringList & )
54 {
55  TDEHTMLPart::GUIProfile prof = TDEHTMLPart::DefaultGUI;
56  if ( strcmp( className, "Browser/View" ) == 0 )
57  prof = TDEHTMLPart::BrowserViewGUI;
58  return new TDEHTMLImage( parentWidget, widgetName, parent, name, prof );
59 }
60 
61 TDEHTMLImage::TDEHTMLImage( TQWidget *parentWidget, const char *widgetName,
62  TQObject *parent, const char *name, TDEHTMLPart::GUIProfile prof )
63  : KParts::ReadOnlyPart( parent, name ), m_image( 0 )
64 {
65  TDEHTMLPart* parentPart = ::tqt_cast<TDEHTMLPart *>( parent );
66  setInstance( TDEHTMLImageFactory::instance(), prof == TDEHTMLPart::BrowserViewGUI && !parentPart );
67 
68  TQVBox *box = new TQVBox( parentWidget, widgetName );
69 
70  m_tdehtml = new TDEHTMLPart( box, widgetName, this, "htmlimagepart", prof );
71  m_tdehtml->setAutoloadImages( true );
72  m_tdehtml->widget()->installEventFilter(this);
73  connect( m_tdehtml->view(), TQ_SIGNAL( finishedLayout() ), this, TQ_SLOT( restoreScrollPosition() ) );
74 
75  setWidget( box );
76 
77  // VBox can't take focus, so pass it on to sub-widget
78  box->setFocusProxy( m_tdehtml->widget() );
79 
80  m_ext = new TDEHTMLImageBrowserExtension( this, "be" );
81 
82  // Remove unnecessary actions.
83  TDEAction *encodingAction = actionCollection()->action( "setEncoding" );
84  if ( encodingAction )
85  {
86  encodingAction->unplugAll();
87  delete encodingAction;
88  }
89  TDEAction *viewSourceAction= actionCollection()->action( "viewDocumentSource" );
90  if ( viewSourceAction )
91  {
92  viewSourceAction->unplugAll();
93  delete viewSourceAction;
94  }
95 
96  TDEAction *selectAllAction= actionCollection()->action( "selectAll" );
97  if ( selectAllAction )
98  {
99  selectAllAction->unplugAll();
100  delete selectAllAction;
101  }
102 
103  // forward important signals from the tdehtml part
104 
105  // forward opening requests to parent frame (if existing)
106  TDEHTMLPart *p = ::tqt_cast<TDEHTMLPart *>(parent);
107  KParts::BrowserExtension *be = p ? p->browserExtension() : m_ext;
108  connect(m_tdehtml->browserExtension(), TQ_SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)),
109  be, TQ_SIGNAL(openURLRequestDelayed(const KURL &, const KParts::URLArgs &)));
110 
111  connect( m_tdehtml->browserExtension(), TQ_SIGNAL( popupMenu( KXMLGUIClient *, const TQPoint &, const KURL &,
112  const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ), m_ext, TQ_SIGNAL( popupMenu( KXMLGUIClient *, const TQPoint &, const KURL &,
113  const KParts::URLArgs &, KParts::BrowserExtension::PopupFlags, mode_t) ) );
114 
115  connect( m_tdehtml->browserExtension(), TQ_SIGNAL( enableAction( const char *, bool ) ),
116  m_ext, TQ_SIGNAL( enableAction( const char *, bool ) ) );
117 
118  m_ext->setURLDropHandlingEnabled( true );
119 }
120 
121 TDEHTMLImage::~TDEHTMLImage()
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_tdehtml )
133  delete static_cast<TDEHTMLPart *>( m_tdehtml );
134 }
135 
136 bool TDEHTMLImage::openURL( const KURL &url )
137 {
138  static const TQString &html = TDEGlobal::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_tdehtml->begin( m_url );
156  m_tdehtml->setAutoloadImages( true );
157 
158  DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>( m_tdehtml->document().handle() ); // ### hack ;-)
159  if (!impl) return false;
160  if ( m_ext->urlArgs().reload )
161  impl->docLoader()->setCachePolicy( TDEIO::CC_Reload );
162 
163  tdehtml::DocLoader *dl = impl->docLoader();
164  m_image = dl->requestImage( m_url.url() );
165  if ( m_image )
166  m_image->ref( this );
167 
168  m_tdehtml->write( html.arg( m_url.url() ) );
169  m_tdehtml->end();
170 
171  /*
172  connect( tdehtml::Cache::loader(), TQ_SIGNAL( requestDone( tdehtml::DocLoader*, tdehtml::CachedObject *) ),
173  this, TQ_SLOT( updateWindowCaption() ) );
174  */
175  return true;
176 }
177 
178 bool TDEHTMLImage::closeURL()
179 {
180  disposeImage();
181  return m_tdehtml->closeURL();
182 }
183 
184 // This can happen after openURL returns, or directly from m_image->ref()
185 void TDEHTMLImage::notifyFinished( tdehtml::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 TDEHTMLImage::restoreScrollPosition()
218 {
219  if ( m_tdehtml->view()->contentsY() == 0 ) {
220  m_tdehtml->view()->setContentsPos( m_xOffset, m_yOffset );
221  }
222 }
223 
224 void TDEHTMLImage::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 TDEHTMLImage::slotImageJobFinished( TDEIO::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, TQ_SLOT( updateWindowCaption() ) );
246  }
247 }
248 
249 void TDEHTMLImage::updateWindowCaption()
250 {
251  if ( !m_tdehtml )
252  return;
253 
254  DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_tdehtml->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  tdehtml::RenderImage *renderImage = dynamic_cast<tdehtml::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 TDEHTMLImage::disposeImage()
291 {
292  if ( !m_image )
293  return;
294 
295  m_image->deref( this );
296  m_image = 0;
297 }
298 
299 bool TDEHTMLImage::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  TDEHTMLPart *p = ::tqt_cast<TDEHTMLPart *>(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 TDEHTMLImageBrowserExtension::TDEHTMLImageBrowserExtension( TDEHTMLImage *parent, const char *name )
320  : KParts::BrowserExtension( parent, name )
321 {
322  m_imgPart = parent;
323 }
324 
325 int TDEHTMLImageBrowserExtension::xOffset()
326 {
327  return m_imgPart->doc()->view()->contentsX();
328 }
329 
330 int TDEHTMLImageBrowserExtension::yOffset()
331 {
332  return m_imgPart->doc()->view()->contentsY();
333 }
334 
335 void TDEHTMLImageBrowserExtension::print()
336 {
337  static_cast<TDEHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->print();
338 }
339 
340 void TDEHTMLImageBrowserExtension::reparseConfiguration()
341 {
342  static_cast<TDEHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->reparseConfiguration();
343  m_imgPart->doc()->setAutoloadImages( true );
344 }
345 
346 
347 void TDEHTMLImageBrowserExtension::disableScrolling()
348 {
349  static_cast<TDEHTMLPartBrowserExtension *>( m_imgPart->doc()->browserExtension() )->disableScrolling();
350 }
351 
352 using namespace KParts;
353 
354 #include "tdehtmlimage.moc"
KParts::BrowserExtension
KParts::GUIActivateEvent
KParts::Part
KParts::Part::widget
virtual TQWidget * widget()
KParts::ReadOnlyPart
KParts::ReadOnlyPart::guiActivateEvent
virtual void guiActivateEvent(GUIActivateEvent *event)
KURL
KURL::prettyURL
TQString prettyURL(int _trailing=0) const
KXMLGUIClient
TDEAction
TDEGlobal::staticQString
static const TQString & staticQString(const char *str)
TDEHTMLPartBrowserExtension
This is the BrowserExtension for a TDEHTMLPart document.
Definition: tdehtml_ext.h:42
TDEHTMLPart
This class is tdehtml's main class.
Definition: tdehtml_part.h:184
TDEHTMLPart::browserExtension
KParts::BrowserExtension * browserExtension() const
Returns a pointer to the KParts::BrowserExtension.
Definition: tdehtml_part.cpp:896
TDEInstance
TDEStdAccel::name
TQString name(StdAccel id)
TDEStdAccel::print
const TDEShortcut & print()
KParts::URLArgs
KParts::URLArgs::xOffset
int xOffset
KParts::URLArgs::yOffset
int yOffset
KParts::URLArgs::serviceType
TQString serviceType
tdelocale.h

tdehtml

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

tdehtml

Skip menu "tdehtml"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdehtml by doxygen 1.9.1
This website is maintained by Timothy Pearson.