00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "imageposition.h"
00021
00022 #include <qpainter.h>
00023 #include <kstandarddirs.h>
00024
00025 static void draw3DPage(QPainter *p, QRect r)
00026 {
00027
00028 p->fillRect(r,Qt::white);
00029
00030 p->setPen(Qt::black);
00031 p->moveTo(r.left(),r.bottom());
00032 p->lineTo(r.right(),r.bottom());
00033 p->lineTo(r.right(),r.top());
00034 p->setPen(Qt::darkGray);
00035 p->lineTo(r.left(),r.top());
00036 p->lineTo(r.left(),r.bottom());
00037 p->setPen(Qt::gray);
00038 p->moveTo(r.left()+1,r.bottom()-1);
00039 p->lineTo(r.right()-1,r.bottom()-1);
00040 p->lineTo(r.right()-1,r.top()+1);
00041 }
00042
00043 ImagePosition::ImagePosition(QWidget *parent, const char *name)
00044 : QWidget(parent,name)
00045 {
00046 position_ = Center;
00047 setMinimumSize(sizeHint());
00048 setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
00049 pix_.load(locate("data", "kdeprint/preview-mini.png"));
00050 }
00051
00052 ImagePosition::~ImagePosition()
00053 {
00054 }
00055
00056 void ImagePosition::setPosition(const char *type)
00057 {
00058 int pos(Center);
00059 if (strcmp(type,"top-left") == 0) pos = TopLeft;
00060 else if (strcmp(type,"top") == 0) pos = Top;
00061 else if (strcmp(type,"top-right") == 0) pos = TopRight;
00062 else if (strcmp(type,"left") == 0) pos = Left;
00063 else if (strcmp(type,"center") == 0) pos = Center;
00064 else if (strcmp(type,"right") == 0) pos = Right;
00065 else if (strcmp(type,"bottom-left") == 0) pos = BottomLeft;
00066 else if (strcmp(type,"bottom") == 0) pos = Bottom;
00067 else if (strcmp(type,"bottom-right") == 0) pos = BottomRight;
00068 setPosition((PositionType)pos);
00069 }
00070
00071 void ImagePosition::setPosition(PositionType type)
00072 {
00073 if (position_ != type) {
00074 position_ = type;
00075 update();
00076 }
00077 }
00078
00079 void ImagePosition::setPosition(int horiz, int vert)
00080 {
00081 int type = vert*3+horiz;
00082 setPosition((PositionType)type);
00083 }
00084
00085 QString ImagePosition::positionString() const
00086 {
00087 switch (position_) {
00088 case TopLeft: return "top-left";
00089 case Top: return "top";
00090 case TopRight: return "top-right";
00091 case Left: return "left";
00092 case Center: return "center";
00093 case Right: return "right";
00094 case BottomLeft: return "bottom-left";
00095 case Bottom: return "bottom";
00096 case BottomRight: return "bottom-right";
00097 }
00098 return "center";
00099 }
00100
00101 void ImagePosition::paintEvent(QPaintEvent*)
00102 {
00103 int horiz, vert, x, y;
00104 int margin = 5;
00105 int pw(width()), ph(height()), px(0), py(0);
00106
00107 if (pw > ((ph * 3) / 4))
00108 {
00109 pw = (ph * 3) / 4;
00110 px = (width() - pw) / 2;
00111 }
00112 else
00113 {
00114 ph = (pw * 4) / 3;
00115 py = (height() - ph) / 2;
00116 }
00117 QRect page(px, py, pw, ph), img(0, 0, pix_.width(), pix_.height());
00118
00119
00120 horiz = position_%3;
00121 vert = position_/3;
00122 switch (horiz) {
00123 case 0: x = page.left()+margin; break;
00124 default:
00125 case 1: x = (page.left()+page.right()-img.width())/2; break;
00126 case 2: x = page.right()-margin-img.width(); break;
00127 }
00128 switch (vert) {
00129 case 0: y = page.top()+margin; break;
00130 default:
00131 case 1: y = (page.top()+page.bottom()-img.height())/2; break;
00132 case 2: y = page.bottom()-margin-img.height(); break;
00133 }
00134 img.moveTopLeft(QPoint(x,y));
00135
00136
00137 QPainter p(this);
00138 draw3DPage(&p,page);
00139
00140
00141
00142
00143
00144
00145 p.drawPixmap(x, y, pix_);
00146
00147 p.end();
00148 }
00149
00150 QSize ImagePosition::sizeHint() const
00151 {
00152 return QSize(60, 80);
00153 }