delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kioslave / thumbnail / htmlcreator.cpp
blobc6236064cb2390004bec9116e6931bc6d7cf01f7
1 /* This file is part of the KDE libraries
2 Copyright (C) 2000 Malte Starostik <malte@kde.org>
3 Copyright (C) 2006 Roberto Cappuccio <roberto.cappuccio@gmail.com>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "htmlcreator.h"
23 #include <time.h>
25 #include <QPixmap>
26 #include <QImage>
27 #include <QPainter>
29 #include <kapplication.h>
30 #include <khtml_part.h>
32 extern "C"
34 KDE_EXPORT ThumbCreator *new_creator()
36 return new HTMLCreator;
40 HTMLCreator::HTMLCreator()
41 : m_html(0)
45 HTMLCreator::~HTMLCreator()
47 delete m_html;
50 bool HTMLCreator::create(const QString &path, int width, int height, QImage &img)
52 if (!m_html)
54 m_html = new KHTMLPart;
55 connect(m_html, SIGNAL(completed()), SLOT(slotCompleted()));
56 m_html->setJScriptEnabled(false);
57 m_html->setJavaEnabled(false);
58 m_html->setPluginsEnabled(false);
59 m_html->setMetaRefreshEnabled(false);
60 m_html->setOnlyLocalReferences(true);
62 KUrl url;
63 url.setPath(path);
64 m_html->openUrl(url);
66 int t = startTimer(5000);
68 m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
70 killTimer(t);
72 // render the HTML page on a bigger pixmap and use smoothScale,
73 // looks better than directly scaling with the QPainter (malte)
74 QPixmap pix;
75 if (width > 400 || height > 600)
77 if (height * 3 > width * 4)
78 pix = QPixmap(width, width * 4 / 3);
79 else
80 pix = QPixmap(height * 3 / 4, height);
82 else
83 pix = QPixmap(400, 600);
85 // light-grey background, in case loadind the page failed
86 pix.fill( QColor( 245, 245, 245 ) );
88 int borderX = pix.width() / width,
89 borderY = pix.height() / height;
90 QRect rc(borderX, borderY, pix.width() - borderX * 2, pix.height() - borderY * 2);
92 QPainter p;
93 p.begin(&pix);
94 m_html->paint(&p, rc);
95 p.end();
97 img = pix.toImage();
99 m_html->closeUrl();
101 return true;
104 void HTMLCreator::timerEvent(QTimerEvent *)
106 m_eventLoop.quit();
109 void HTMLCreator::slotCompleted()
111 m_eventLoop.quit();
114 ThumbCreator::Flags HTMLCreator::flags() const
116 return DrawFrame;
119 #include "htmlcreator.moc"