kviewshell
sizePreview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
00025 _width = _height = 50.0;
00026 orientation = 0;
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
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
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
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
00089 int lineSpacing = (int)(7.0*displayedWidth/_width + 0.5);
00090 if (lineSpacing <= 3)
00091 lineSpacing = 3;
00092 int interWordSpace = (int)(4.0*displayedWidth/_width + 0.5);
00093 if (interWordSpace <= 1)
00094 interWordSpace = 2;
00095
00096 KRandomSequence rnd(1);
00097
00098 p.setClipRect(textBox);
00099 p.setPen(Qt::black);
00100 int count = 1;
00101 for (int y = vOffset+margin+lineSpacing; y <= vOffset+displayedHeight-margin; y += lineSpacing) {
00102
00103
00104
00105 rnd.setSeed(count);
00106
00107
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
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"