• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

sizePreview.cpp

Go to the documentation of this file.
00001 // SizePreview.cpp
00002 //
00003 // Part of KVIEWSHELL - A framework for multipage text/gfx viewers
00004 //
00005 // (C) 2002 Stefan Kebekus
00006 // Distributed under the GPL
00007 
00008 // Add header files alphabetically
00009 
00010 #include <config.h>
00011 
00012 #include <kdebug.h>
00013 #include <krandomsequence.h>
00014 #include <qevent.h>
00015 #include <qpainter.h>
00016 
00017 #include "sizePreview.h"
00018 
00019 #include "simplePageSize.h"
00020 
00021 SizePreview::SizePreview( QWidget *parent, const char *name, WFlags)
00022     : QWidget( parent, name, WStaticContents | WNoAutoErase )
00023 {
00024   // Set a sane default
00025   _width = _height = 50.0;
00026   orientation = 0; // Portrait
00027 }
00028 
00029 void SizePreview::setSize(const SimplePageSize& size)
00030 {
00031   _width = size.width().getLength_in_mm();
00032   _height = size.height().getLength_in_mm();
00033 
00034   if (_width < 50.0)
00035     _width = 50.0;
00036   if (_width > 1200.0)
00037     _width = 1200.0;
00038 
00039   if (_height < 50.0)
00040     _height = 50.0;
00041   if (_height > 1200.0)
00042     _height = 1200.0;
00043 
00044   update();
00045 }
00046 
00047 void SizePreview::resizeEvent(QResizeEvent*)
00048 {
00049   update();
00050 }
00051 
00052 void SizePreview::paintEvent( QPaintEvent * )
00053 {
00054   int displayedWidth, displayedHeight;
00055 
00056   // Figure the width and height of the displayed page. Tricky.
00057   if (orientation == 0) {
00058     displayedWidth  = (int)(height() * (_width/_height) + 0.5);
00059     displayedHeight = (int)(width() * (_height/_width) + 0.5);
00060   } else {
00061     displayedHeight  = (int)(height() * (_width/_height) + 0.5);
00062     displayedWidth = (int)(width() * (_height/_width) + 0.5);
00063   }
00064   if (displayedWidth <= width()) 
00065     displayedHeight = height();
00066   else
00067     displayedWidth = width();
00068 
00069   int hOffset = (width()-displayedWidth)/2;
00070   int vOffset = (height()-displayedHeight)/2;
00071 
00072 
00073   // Now draw the graphics
00074   pixmap.resize(width(), height());
00075 
00076   QPainter p(&pixmap);
00077   p.fillRect(rect(), colorGroup().background());
00078   p.setPen(Qt::black);
00079   p.setBrush(Qt::white);
00080   p.drawRect(hOffset, vOffset, displayedWidth, displayedHeight);
00081 
00082   // mark the textbox; we assume 25mm margin
00083   int margin = (int)(25.0*displayedWidth/_width + 0.5);
00084   QRect textBox(hOffset+margin, vOffset+margin, displayedWidth-2*margin, displayedHeight-2*margin);
00085   p.setPen(Qt::lightGray);
00086   p.drawRect(textBox);
00087 
00088   // Draw some dummy "text", represented by black lines
00089   int lineSpacing    = (int)(7.0*displayedWidth/_width + 0.5); // equiv. 7 mm
00090   if (lineSpacing <= 3)
00091     lineSpacing = 3;
00092   int interWordSpace = (int)(4.0*displayedWidth/_width + 0.5); // equiv. 4 mm
00093   if (interWordSpace <= 1)
00094     interWordSpace = 2;
00095 
00096   KRandomSequence rnd(1); // to generate word widths
00097 
00098   p.setClipRect(textBox);
00099   p.setPen(Qt::black);
00100   int count = 1; // Counts lines
00101   for (int y = vOffset+margin+lineSpacing; y <= vOffset+displayedHeight-margin; y += lineSpacing) {
00102     // We start each line with its own seed.
00103     // This means that the random sequence for each line is always the same, and this
00104     // results in a more steady picture when the widget is resized.
00105     rnd.setSeed(count);
00106 
00107     // Every 10th line the end of a paragraph
00108     int endParagraph;
00109     if (count++ % 10 == 0)
00110       endParagraph = (int)(50.0*displayedWidth/_width + 0.5);
00111     else
00112       endParagraph = 0;
00113     for(int x = hOffset+margin; x <= hOffset+displayedWidth-margin-endParagraph; ) {
00114       double wordWidthMM = rnd.getDouble()*30.0+10.0;
00115       int wordWidth = (int)(wordWidthMM*displayedWidth/_width + 0.5); 
00116       p.drawLine(x, y, x+wordWidth, y);
00117       x += wordWidth+interWordSpace+1;
00118     }
00119   }
00120 
00121   p.end();
00122 
00123   // Copy the pixmap onto the widget
00124   bitBlt(this, rect().topLeft(), &pixmap, rect(), CopyROP);
00125   return;
00126 }
00127 
00128 void SizePreview::setOrientation(int ori)
00129 {
00130   orientation = ori;
00131   update();
00132 }
00133 
00134 #include "sizePreview.moc"

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal