Results 1 to 6 of 6

Thread: Problems with a thumbnail viewer for Konqueror

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problems with a thumbnail viewer for Konqueror

    I am writing a program to render NFO files, and I want to write a thumbnail viewer so that you can select or mouse over NFO files in Konqueror and see them rendered correctly. To this end, I wrote a text codec for Qt3 for the CP/IBM 437 encoding. It works in the program.

    I copied the code for the HTML file preview in Konqueror. It works for NFO files, but the moment I uncomment the file.open() code it stops working.

    Qt Code:
    1. /***************************************************************************
    2.  * Copyright (C) 2007 by Lawrence Lee *
    3.  * valheru@facticius.net *
    4.  * *
    5.  * This program is free software; you can redistribute it and/or modify *
    6.  * it under the terms of the GNU General Public License as published by *
    7.  * the Free Software Foundation; either version 2 of the License, or *
    8.  * (at your option) any later version. *
    9.  * *
    10.  * This program 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 *
    13.  * GNU General Public License for more details. *
    14.  * *
    15.  * You should have received a copy of the GNU General Public License *
    16.  * along with this program; if not, write to the *
    17.  * Free Software Foundation, Inc., *
    18.  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
    19.  ***************************************************************************/
    20. #include <kstandarddirs.h>
    21. #include <kapplication.h>
    22. #include <kmimetype.h>
    23. #include <time.h>
    24. #include <qpixmap.h>
    25. #include <qimage.h>
    26. #include <qpainter.h>
    27. #include <khtml_part.h>
    28. #include <qfile.h>
    29. #include <qimage.h>
    30. #include <qtextstream.h>
    31. #include "knfoviewerthumbnail.h"
    32. #include "cp437codec.h"
    33.  
    34. extern "C"
    35. {
    36. ThumbCreator *new_creator()
    37. {
    38. return new KNfoViewerThumbnail();
    39. }
    40. }
    41.  
    42. /*
    43.  * This code is taken nearly verbatim from htmlcreator.cpp
    44.  * in kdebase/kioslave/thumbnail/ with minor changes to
    45.  * ensure the correct rendering of CP437 encoded characters.
    46.  */
    47. KNfoViewerThumbnail::KNfoViewerThumbnail() : m_html( 0 )
    48. {
    49. }
    50.  
    51. KNfoViewerThumbnail::~KNfoViewerThumbnail()
    52. {
    53. delete m_html;
    54. }
    55.  
    56. bool KNfoViewerThumbnail::create( const QString &path, int width, int height, QImage &img ){
    57. if (!m_html)
    58. {
    59. m_html = new KHTMLPart;
    60. connect(m_html, SIGNAL(completed()), SLOT(slotCompleted()));
    61. m_html->setJScriptEnabled(false);
    62. m_html->setJavaEnabled(false);
    63. m_html->setPluginsEnabled(false);
    64. m_html->setMetaRefreshEnabled(false);
    65. m_html->setOnlyLocalReferences(true);
    66. }
    67. KURL url;
    68. url.setPath( path );
    69. // QFile file( url.url() );
    70.  
    71. // if( !file.open( IO_ReadOnly ) )
    72. // return false;
    73.  
    74. // QString text;
    75. // QTextStream stream( &file );
    76. // CP437Codec codec;
    77. // stream.setCodec( &codec );
    78. //
    79. // while( !stream.atEnd() ){
    80. // text += stream.readLine() + "\n";
    81. // }
    82.  
    83. // m_html->begin();
    84. // m_html->write( htmlCode( text ) );
    85. // m_html->end();
    86.  
    87. m_html->openURL(url);
    88.  
    89. int t = startTimer(5000);
    90.  
    91. qApp->enter_loop();
    92.  
    93. killTimer(t);
    94.  
    95. // render the HTML page on a bigger pixmap and use smoothScale,
    96. // looks better than directly scaling with the QPainter (malte)
    97. QPixmap pix;
    98. if (width > 400 || height > 600)
    99. {
    100. if (height * 3 > width * 4)
    101. pix.resize(width, width * 4 / 3);
    102. else
    103. pix.resize(height * 3 / 4, height);
    104. }
    105. else
    106. pix.resize(400, 600);
    107.  
    108. // light-grey background, in case loadind the page failed
    109. pix.fill( QColor( 245, 245, 245 ) );
    110.  
    111. int borderX = pix.width() / width, borderY = pix.height() / height;
    112. QRect rc(borderX, borderY, pix.width() - borderX * 2,
    113. pix.height() - borderY * 2);
    114.  
    115. p.begin(&pix);
    116. m_html->paint(&p, rc);
    117. p.end();
    118.  
    119. img = pix.convertToImage();
    120.  
    121. m_html->closeURL();
    122.  
    123. return true;
    124. }
    125.  
    126. const QString KNfoViewerThumbnail::htmlCode( const QString &text )
    127. {
    128. QString code = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n\
    129. <html>\n\
    130. <head>\n\
    131. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n\
    132. <style type=\"text/css\" media=\"screen\"><!--\n\
    133. body {\n \
    134. color : #000000;\n\
    135. background-color: #ffffff;\n\
    136. margin: 0px;\n\
    137. }\n\
    138. #nfo {\n\
    139. color: #000000;\n\
    140. background-color: transparent;\n\
    141. text-align: center;\n\
    142. position: absolute;\n\
    143. top: 0px;\n\
    144. left: 0px;\n\
    145. width: 100%;\n\
    146. height: 100%;\n\
    147. overflow: visible;\n\
    148. visibility: visible;\n\
    149. display: block\n\
    150. }\n\
    151. #data {\n\
    152. font-size: 11px;\n\
    153. font-family: \"Andale Mono\";\n\
    154. line-height: 11px;\n\
    155. background-color: #ffffff;\n\
    156. color: #000000;\n\
    157. position: relative;\n\
    158. white-space: pre;\n\
    159. visibility : visible;\n\
    160. }\n\
    161. a {\n\
    162. color: #000000;\n\
    163. text-decoration: none;\n\
    164. }\n\
    165. a:hover {\n\
    166. color: #000000;\n\
    167. text-decoration: none;\n\
    168. }\n\
    169. --></style>\n\
    170. </head>\n\
    171. <body>\n\
    172. <div id\"nfo\">\n\
    173. <div id=\"data\">\n";
    174.  
    175. code += text;
    176. code += "<br></div></div><br/></body></html>";
    177.  
    178. return code;
    179. }
    180.  
    181. void KNfoViewerThumbnail::timerEvent(QTimerEvent *)
    182. {
    183. qApp->exit_loop();
    184. }
    185.  
    186. void KNfoViewerThumbnail::slotCompleted()
    187. {
    188. qApp->exit_loop();
    189. }
    190.  
    191. ThumbCreator::Flags KNfoViewerThumbnail::flags() const
    192. {
    193. return DrawFrame;
    194. }
    195.  
    196. #include "knfoviewerthumbnail.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by Valheru; 20th January 2008 at 02:18. Reason: updated contents

  2. #2
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with a thumbnail viewer for Konqueror

    Upon further examination it appears that it is "failing" on the line

    Qt Code:
    1. if( !file.open( IO_ReadOnly ) )
    2. return false;
    To copy to clipboard, switch view to plain text mode 

    So evidently it can't read the file. If I comment that out and use m_file->openURL() it renders, albiet with incorrect encoding.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a thumbnail viewer for Konqueror

    Can you open the file if you use "QFile file( path );"?

  4. #4
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with a thumbnail viewer for Konqueror

    Nope, it doesn't

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with a thumbnail viewer for Konqueror

    Could you post a sample path value?

  6. #6
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with a thumbnail viewer for Konqueror

    Ugh, that was retarded. In the end it was a linker error. It wasn't immediately apparent because the library compiled fine. It was just crashing at runtime because I hadn't linked it correctly.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.