marginvaluewidget.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., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, 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     // Force synchronization
00040     m_margin = toPixel(value(), m_mode);
00041     return m_margin;
00042 }
00043 
00044 void MarginValueWidget::setMargin(float m)
00045 {
00046     m_margin = m;
00047     double  v = toValue(m, m_mode);
00048     m_block = true;
00049     setValue(v);
00050     m_block = false;
00051     emit marginChanged( m_margin );
00052 }
00053 
00054 float MarginValueWidget::toPixel(double value, int mode)
00055 {
00056     switch (mode)
00057     {
00058         default:
00059         case Pixels: return (float)value;
00060         case IN: return (float)(value * m_dpi);
00061         case CM: return (float)(value * m_dpi / 2.54);
00062         case MM: return (float)(value * m_dpi / 25.4);
00063     }
00064 }
00065 
00066 double MarginValueWidget::toValue(float pix, int mode)
00067 {
00068     switch (mode)
00069     {
00070         default:
00071         case Pixels:
00072             return (double)pix;
00073         case IN:
00074             return (double(pix) / m_dpi);
00075         case CM:
00076             return (double(pix) * 2.54 / m_dpi);
00077         case MM:
00078             return ( double( pix ) * 25.4 / m_dpi );
00079     }
00080 }
00081 
00082 void MarginValueWidget::slotValueChanged(double v)
00083 {
00084     if (!m_block)
00085     {
00086         m_margin = toPixel( v, m_mode );
00087         emit marginChanged(margin());
00088     }
00089 }
00090 
00091 void MarginValueWidget::setMode(int m)
00092 {
00093     if (m != m_mode)
00094     {
00095         m_block = true;
00096         m_mode = m;
00097         double v = toValue(m_margin, m);
00098         if (m == Pixels)
00099         {
00100             setPrecision(0);
00101             setRange(0.0, 999.0, 1.0, false);
00102         }
00103         else
00104         {
00105             setPrecision(3);
00106             setRange(0.0, 999.0, 0.01, false);
00107         }
00108         setValue(v);
00109         m_block = false;
00110     }
00111 }
00112 
00113 void MarginValueWidget::setResolution(int dpi)
00114 {
00115     m_dpi = dpi;
00116     m_block = true;
00117     setMargin(m_margin);
00118     m_block = false;
00119 }
00120 
00121 int MarginValueWidget::resolution() const
00122 {
00123     return (int)(m_dpi+0.5);
00124 }
00125 
00126 #include "marginvaluewidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys