TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

faxrenderer.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /***************************************************************************
  2. * Copyright (C) 2005 by Stefan Kebekus *
  3. * kebekus@kde.org *
  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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
  19. ***************************************************************************/
  20. #include <config.h>
  21. #include <kmessagebox.h>
  22. #include <kdebug.h>
  23. #include <klocale.h>
  24. #include <tqfileinfo.h>
  25. #include <tqpainter.h>
  26. #include "documentWidget.h"
  27. #include "faxrenderer.h"
  28. #include "faxmultipage.h"
  29. //#define KF_DEBUG
  30. FaxRenderer::FaxRenderer(TQWidget* par)
  31. : DocumentRenderer(par)
  32. {
  33. #ifdef KF_DEBUG
  34. kdError() << "FaxRenderer( parent=" << par << " )" << endl;
  35. #endif
  36. }
  37. FaxRenderer::~FaxRenderer()
  38. {
  39. #ifdef KF_DEBUG
  40. kdDebug() << "~FaxRenderer" << endl;
  41. #endif
  42. // Wait for all access to this documentRenderer to finish
  43. mutex.lock();
  44. mutex.unlock();
  45. }
  46. void FaxRenderer::drawPage(double resolution, RenderedDocumentPage* page)
  47. {
  48. #ifdef KF_DEBUG
  49. kdDebug() << "FaxRenderer::drawPage(documentPage*) called, page number " << page->getPageNumber() << endl;
  50. #endif
  51. // Paranoid safety checks
  52. if (page == 0) {
  53. kdError() << "FaxRenderer::drawPage(documentPage*) called with argument == 0" << endl;
  54. return;
  55. }
  56. if (page->getPageNumber() == 0) {
  57. kdError() << "FaxRenderer::drawPage(documentPage*) called for a documentPage with page number 0" << endl;
  58. return;
  59. }
  60. // Wait for all access to this documentRenderer to finish
  61. mutex.lock();
  62. // more paranoid safety checks
  63. if (page->getPageNumber() > numPages) {
  64. kdError() << "FaxRenderer::drawPage(documentPage*) called for a documentPage with page number " << page->getPageNumber()
  65. << " but the current fax file has only " << numPages << " pages." << endl;
  66. mutex.unlock();
  67. return;
  68. }
  69. TQImage img = fax.page(page->getPageNumber() - 1);
  70. SimplePageSize psize = pageSizes[page->getPageNumber() - 1];
  71. if (psize.isValid()) {
  72. TQPainter *foreGroundPaint = page->getPainter();
  73. if (foreGroundPaint != 0) {
  74. // Compute an image for the page.
  75. // WARNING: It may be tempting to compute the image size in
  76. // pixel, using page->height() and page->width(). DON'T DO
  77. // THAT. KViewShell uses transformations e.g. to rotate the
  78. // page, and sets the argument 'resolution' accordingly. Similar
  79. // problems occur if KViewShell required a shrunken version of
  80. // the page, e.g. to print multiple pages on one sheet of paper.
  81. int width_in_pixel = tqRound(resolution * psize.width().getLength_in_inch());
  82. int height_in_pixel = tqRound(resolution * psize.height().getLength_in_inch());
  83. img = img.smoothScale(width_in_pixel, height_in_pixel);
  84. foreGroundPaint->drawImage(0, 0, img);
  85. page->returnPainter(foreGroundPaint);
  86. }
  87. } else
  88. kdError() << "FaxRenderer::drawPage() called, but page size for page " << page->getPageNumber() << " is invalid." << endl;
  89. // To indicate that the page was drawn, we set the appropriate flas in the page structure
  90. page->isEmpty = false;
  91. mutex.unlock();
  92. }
  93. bool FaxRenderer::setFile(const TQString &fname, const KURL &)
  94. {
  95. #ifdef KF_DEBUG
  96. kdDebug() << "FaxRenderer::setFile(" << fname << ") called" << endl;
  97. #endif
  98. // Wait for all access to this documentRenderer to finish
  99. mutex.lock();
  100. // If fname is the empty string, then this means: "close".
  101. if (fname.isEmpty()) {
  102. kdDebug() << "FaxRenderer::setFile( ... ) called with empty filename. Closing the file." << endl;
  103. mutex.unlock();
  104. return true;
  105. }
  106. // Paranoid saftey checks: make sure the file actually exists, and
  107. // that it is a file, not a directory. Otherwise, show an error
  108. // message and exit..
  109. TQFileInfo fi(fname);
  110. TQString filename = fi.absFilePath();
  111. if (!fi.exists() || fi.isDir()) {
  112. KMessageBox::error( parentWidget,
  113. i18n("<qt><strong>File error.</strong> The specified file '%1' does not exist.</qt>").arg(filename),
  114. i18n("File Error"));
  115. // the return value 'false' indicates that this operation was not successful.
  116. mutex.unlock();
  117. return false;
  118. }
  119. // Now we assume that the file is fine and load the file into the
  120. // fax member. We abort on error and give an error message.
  121. bool ok = fax.loadImage(filename);
  122. // It can happen that fax.loadImage() returns with 'ok == true', but
  123. // still the file could NOT be loaded. This happens, e.g. for TIFF
  124. // file that do NOT contain FAX, but other image formats. We handle
  125. // that case here also.
  126. if ( (!ok) || (fax.numPages() == 0)) {
  127. // Unfortunately, it can happen that fax.loadImage() fails WITHOUT
  128. // leaving an error message in fax.errorString(). We try to handle
  129. // this case gracefully.
  130. if (fax.errorString().isEmpty())
  131. KMessageBox::error( parentWidget,
  132. i18n("<qt><strong>File error.</strong> The specified file '%1' could not be loaded.</qt>").arg(filename),
  133. i18n("File Error"));
  134. else
  135. KMessageBox::detailedError( parentWidget,
  136. i18n("<qt><strong>File error.</strong> The specified file '%1' could not be loaded.</qt>").arg(filename),
  137. fax.errorString(),
  138. i18n("File Error"));
  139. clear();
  140. mutex.unlock();
  141. return false;
  142. }
  143. // Set the number of pages page sizes
  144. numPages = fax.numPages();
  145. // Set the page size for the first page in the pageSizes array.
  146. // The rest of the page sizes will be calculated on demand by the drawPage function.
  147. pageSizes.resize(numPages);
  148. Length w,h;
  149. if (numPages != 0) {
  150. for(TQ_UINT16 pg=0; pg < numPages; pg++) {
  151. TQSize pageSize = fax.page_size(pg);
  152. TQPoint dpi = fax.page_dpi(pg);
  153. double dpix = dpi.x();
  154. double dpiy = dpi.y();
  155. if (dpix*dpiy < 1.0) {
  156. kdError() << "File invalid resolutions, dpi x = " << dpix << ", dpi y = " << dpiy << ". This information will be ignored and 75 DPI assumed." << endl;
  157. dpix = dpiy = 75.0;
  158. }
  159. w.setLength_in_inch(pageSize.width() / dpix);
  160. h.setLength_in_inch(pageSize.height() / dpiy);
  161. pageSizes[pg].setPageSize(w, h);
  162. }
  163. }
  164. // the return value 'true' indicates that this operation was not successful.
  165. mutex.unlock();
  166. return true;
  167. }
  168. #include "faxrenderer.moc"