kdeprint Library API Documentation

marginvaluewidget.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <goffioul@imec.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 "marginvaluewidget.h"
00021 
00022 #include <math.h>
00023 
00024 MarginValueWidget::MarginValueWidget(KNumInput *below, double value, QWidget *parent, const char *name)
00025 : KDoubleNumInput(below, value, parent, name)
00026 {
00027     m_mode = Pixels;
00028     m_block = false;
00029     setPrecision(0);
00030     m_dpi = 72.0;
00031     m_margin = ( float )value;
00032     setMode(m_mode);
00033     setRange(0, 999, 1, false);
00034     connect(this, SIGNAL(valueChanged(double)), SLOT(slotValueChanged(double)));
00035 }
00036 
00037 float MarginValueWidget::margin()
00038 {
00039     return m_margin;
00040 }
00041 
00042 void MarginValueWidget::setMargin(float m)
00043 {
00044     m_margin = m;
00045     double  v = toValue(m, m_mode);
00046     m_block = true;
00047     setValue(v);
00048     m_block = false;
00049     emit marginChanged( m_margin );
00050 }
00051 
00052 float MarginValueWidget::toPixel(double value, int mode)
00053 {
00054     switch (mode)
00055     {
00056         default:
00057         case Pixels: return (float)value;
00058         case IN: return (float)(value * m_dpi);
00059         case CM: return (float)(value * m_dpi / 2.54);
00060         case MM: return (float)(value * m_dpi / 25.4);
00061     }
00062 }
00063 
00064 double MarginValueWidget::toValue(float pix, int mode)
00065 {
00066     switch (mode)
00067     {
00068         default:
00069         case Pixels:
00070             return (double)pix;
00071         case IN:
00072             return (double(pix) / m_dpi);
00073         case CM:
00074             return (double(pix) * 2.54 / m_dpi);
00075         case MM:
00076             return ( double( pix ) * 25.4 / m_dpi );
00077     }
00078 }
00079 
00080 void MarginValueWidget::slotValueChanged(double v)
00081 {
00082     if (!m_block)
00083     {
00084         m_margin = toPixel( v, m_mode );
00085         emit marginChanged(margin());
00086     }
00087 }
00088 
00089 void MarginValueWidget::setMode(int m)
00090 {
00091     if (m != m_mode)
00092     {
00093         m_block = true;
00094         m_mode = m;
00095         double v = toValue(m_margin, m);
00096         if (m == Pixels)
00097         {
00098             setPrecision(0);
00099             setRange(0.0, 999.0, 1.0, false);
00100         }
00101         else
00102         {
00103             setPrecision(3);
00104             setRange(0.0, 999.0, 0.01, false);
00105         }
00106         setValue(v);
00107         m_block = false;
00108     }
00109 }
00110 
00111 void MarginValueWidget::setResolution(int dpi)
00112 {
00113     m_dpi = dpi;
00114     m_block = true;
00115     setMargin(m_margin);
00116     m_block = false;
00117 }
00118 
00119 int MarginValueWidget::resolution() const
00120 {
00121     return (int)(m_dpi+0.5);
00122 }
00123 
00124 #include "marginvaluewidget.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:45:15 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003