| # kdeprint Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# imageposition.cpp

    00001 /*
    00002  *  This file is part of the KDE libraries
    00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
    00004  *
    00005  *  This library is free software; you can redistribute it and/or
    00006  *  modify it under the terms of the GNU Library General Public
    00007  *  License version 2 as published by the Free Software Foundation.
    00008  *
    00009  *  This library is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012  *  Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU Library General Public License
    00015  *  along with this library; see the file COPYING.LIB.  If not, write to
    00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017  *  Boston, MA 02111-1307, USA.
    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     // draw white page
    00028     p->fillRect(r,Qt::white);
    00029     // draw 3D border
    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     // compute img position
    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     // draw page
    00137     QPainter    p(this);
    00138     draw3DPage(&p,page);
    00139
    00140     // draw img
    00141     /*p.setPen(darkRed);
    00142     p.drawRect(img);
    00143     p.drawLine(img.topLeft(),img.bottomRight());
    00144     p.drawLine(img.topRight(),img.bottomLeft());*/
    00145     p.drawPixmap(x, y, pix_);
    00146
    00147     p.end();
    00148 }
    00149
    00150 QSize ImagePosition::sizeHint() const
    00151 {
    00152     return QSize(60, 80);
    00153 }

This file is part of the documentation for kdeprint Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:01 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003