00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "marginwidget.h"
00021 #include "marginpreview.h"
00022 #include "marginvaluewidget.h"
00023 #include "kprinter.h"
00024
00025 #include <qcombobox.h>
00026 #include <qcheckbox.h>
00027 #include <qlayout.h>
00028 #include <qlabel.h>
00029 #include <klocale.h>
00030 #include <kglobal.h>
00031
00032 MarginWidget::MarginWidget(QWidget *parent, const char* name, bool allowMetricUnit)
00033 : QWidget(parent, name), m_default(4, 0), m_pagesize( 2 )
00034 {
00035 m_symetric = m_block = false;
00036 m_pagesize[ 0 ] = 595;
00037 m_pagesize[ 1 ] = 842;
00038 m_landscape = false;
00039
00040 m_custom = new QCheckBox(i18n("&Use custom margins"), this);
00041 m_top = new MarginValueWidget(0, 0.0, this);
00042 m_bottom = new MarginValueWidget(m_top, 0.0, this);
00043 m_left = new MarginValueWidget(m_bottom, 0.0, this);
00044 m_right = new MarginValueWidget(m_left, 0.0, this);
00045 m_top->setLabel(i18n("&Top:"), Qt::AlignLeft|Qt::AlignVCenter);
00046 m_bottom->setLabel(i18n("&Bottom:"), Qt::AlignLeft|Qt::AlignVCenter);
00047 m_left->setLabel(i18n("Le&ft:"), Qt::AlignLeft|Qt::AlignVCenter);
00048 m_right->setLabel(i18n("&Right:"), Qt::AlignLeft|Qt::AlignVCenter);
00049 m_units = new QComboBox(this);
00050 m_units->insertItem(i18n("Pixels (1/72nd in)"));
00051 if ( allowMetricUnit )
00052 {
00053 m_units->insertItem(i18n("Inches (in)"));
00054 m_units->insertItem(i18n("Centimeters (cm)"));
00055 m_units->insertItem( i18n( "Millimeters (mm)" ) );
00056 }
00057 m_units->setCurrentItem(0);
00058 connect(m_units, SIGNAL(activated(int)), m_top, SLOT(setMode(int)));
00059 connect(m_units, SIGNAL(activated(int)), m_bottom, SLOT(setMode(int)));
00060 connect(m_units, SIGNAL(activated(int)), m_left, SLOT(setMode(int)));
00061 connect(m_units, SIGNAL(activated(int)), m_right, SLOT(setMode(int)));
00062 m_preview = new MarginPreview(this);
00063 m_preview->setMinimumSize(60, 80);
00064 m_preview->setPageSize(m_pagesize[ 0 ], m_pagesize[ 1 ]);
00065 connect(m_preview, SIGNAL(marginChanged(int,float)), SLOT(slotMarginPreviewChanged(int,float)));
00066 connect(m_top, SIGNAL(marginChanged(float)), SLOT(slotMarginValueChanged()));
00067 connect(m_bottom, SIGNAL(marginChanged(float)), SLOT(slotMarginValueChanged()));
00068 connect(m_left, SIGNAL(marginChanged(float)), SLOT(slotMarginValueChanged()));
00069 connect(m_right, SIGNAL(marginChanged(float)), SLOT(slotMarginValueChanged()));
00070 slotMarginValueChanged();
00071 connect(m_custom, SIGNAL(toggled(bool)), m_top, SLOT(setEnabled(bool)));
00072 connect(m_custom, SIGNAL(toggled(bool)), m_left, SLOT(setEnabled(bool)));
00073
00074 connect(m_custom, SIGNAL(toggled(bool)), SLOT(slotCustomMarginsToggled(bool)));
00075 connect(m_custom, SIGNAL(toggled(bool)), m_preview, SLOT(enableRubberBand(bool)));
00076 m_top->setEnabled(false);
00077 m_bottom->setEnabled(false);
00078 m_left->setEnabled(false);
00079 m_right->setEnabled(false);
00080
00081
00082 QGridLayout *l3 = new QGridLayout(this, 7, 2, 0, 10);
00083 l3->addWidget(m_custom, 0, 0);
00084 l3->addWidget(m_top, 1, 0);
00085 l3->addWidget(m_bottom, 2, 0);
00086 l3->addWidget(m_left, 3, 0);
00087 l3->addWidget(m_right, 4, 0);
00088 l3->addRowSpacing(5, 10);
00089 l3->addWidget(m_units, 6, 0);
00090 l3->addMultiCellWidget(m_preview, 0, 6, 1, 1);
00091
00092 if ( allowMetricUnit )
00093 {
00094 int mode = (KGlobal::locale()->measureSystem() == KLocale::Metric ? 2 : 1);
00095 m_top->setMode(mode);
00096 m_bottom->setMode(mode);
00097 m_left->setMode(mode);
00098 m_right->setMode(mode);
00099 m_units->setCurrentItem(mode);
00100 }
00101 }
00102
00103 MarginWidget::~MarginWidget()
00104 {
00105 }
00106
00107 void MarginWidget::slotCustomMarginsToggled(bool b)
00108 {
00109 m_bottom->setEnabled(b && !m_symetric);
00110 m_right->setEnabled(b && !m_symetric);
00111 if (!b)
00112 resetDefault();
00113 }
00114
00115 void MarginWidget::setSymetricMargins(bool on)
00116 {
00117 if (on == m_symetric)
00118 return;
00119
00120 m_symetric = on;
00121 m_bottom->setEnabled(on && m_custom->isChecked());
00122 m_right->setEnabled(on && m_custom->isChecked());
00123 if (on)
00124 {
00125 connect(m_top, SIGNAL(marginChanged(float)), m_bottom, SLOT(setMargin(float)));
00126 connect(m_left, SIGNAL(marginChanged(float)), m_right, SLOT(setMargin(float)));
00127 m_bottom->setMargin(m_top->margin());
00128 m_right->setMargin(m_left->margin());
00129 }
00130 else
00131 {
00132 disconnect(m_top, 0, m_bottom, 0);
00133 disconnect(m_left, 0, m_right, 0);
00134 }
00135 m_preview->setSymetric(on);
00136 }
00137
00138 void MarginWidget::slotMarginValueChanged()
00139 {
00140 if (m_block)
00141 return;
00142 m_preview->setMargins(m_top->margin(), m_bottom->margin(), m_left->margin(), m_right->margin());
00143 }
00144
00145 void MarginWidget::slotMarginPreviewChanged(int type, float value)
00146 {
00147 m_block = true;
00148 switch (type)
00149 {
00150 case MarginPreview::TMoving:
00151 m_top->setMargin(value);
00152 break;
00153 case MarginPreview::BMoving:
00154 m_bottom->setMargin(value);
00155 break;
00156 case MarginPreview::LMoving:
00157 m_left->setMargin(value);
00158 break;
00159 case MarginPreview::RMoving:
00160 m_right->setMargin(value);
00161 break;
00162 }
00163 m_block = false;
00164 }
00165
00166 void MarginWidget::setPageSize(float w, float h)
00167 {
00168
00169 int dpi = m_top->resolution();
00170 m_pagesize[ 0 ] = w;
00171 m_pagesize[ 1 ] = h;
00172 if (m_landscape)
00173 m_preview->setPageSize((m_pagesize[ 1 ]*dpi)/72, (m_pagesize[ 0 ]*dpi)/72);
00174 else
00175 m_preview->setPageSize((m_pagesize[ 0 ]*dpi)/72, (m_pagesize[ 1 ]*dpi)/72);
00176 }
00177
00178 float MarginWidget::top() const
00179 {
00180 return m_top->margin();
00181 }
00182
00183 float MarginWidget::bottom() const
00184 {
00185 return m_bottom->margin();
00186 }
00187
00188 float MarginWidget::left() const
00189 {
00190 return m_left->margin();
00191 }
00192
00193 float MarginWidget::right() const
00194 {
00195 return m_right->margin();
00196 }
00197
00198 void MarginWidget::setTop(float value)
00199 {
00200 m_top->setMargin(value);
00201 }
00202
00203 void MarginWidget::setBottom(float value)
00204 {
00205 m_bottom->setMargin(value);
00206 }
00207
00208 void MarginWidget::setLeft(float value)
00209 {
00210 m_left->setMargin(value);
00211 }
00212
00213 void MarginWidget::setRight(float value)
00214 {
00215 m_right->setMargin(value);
00216 }
00217
00218 void MarginWidget::setResolution(int dpi)
00219 {
00220 m_top->setResolution(dpi);
00221 m_bottom->setResolution(dpi);
00222 m_left->setResolution(dpi);
00223 m_right->setResolution(dpi);
00224 }
00225
00226 void MarginWidget::setDefaultMargins(float t, float b, float l, float r)
00227 {
00228 int dpi = m_top->resolution();
00229 m_default[0] = (t*dpi)/72;
00230 m_default[1] = (b*dpi)/72;
00231 m_default[2] = (l*dpi)/72;
00232 m_default[3] = (r*dpi)/72;
00233 if (!m_custom->isChecked())
00234 resetDefault();
00235 }
00236
00237 void MarginWidget::resetDefault()
00238 {
00239 m_top->setMargin(m_landscape ? m_default[2] : m_default[0]);
00240 m_bottom->setMargin(m_landscape ? m_default[3] : m_default[1]);
00241 m_left->setMargin(m_landscape ? m_default[1] : m_default[2]);
00242 m_right->setMargin(m_landscape ? m_default[0] : m_default[3]);
00243 }
00244
00245 void MarginWidget::setCustomEnabled(bool on)
00246 {
00247 m_custom->setChecked(on);
00248 }
00249
00250 bool MarginWidget::isCustomEnabled() const
00251 {
00252 return m_custom->isChecked();
00253 }
00254
00255 void MarginWidget::setOrientation(int orient)
00256 {
00257 m_landscape = (orient == KPrinter::Landscape);
00258 setPageSize(m_pagesize[ 0 ], m_pagesize[ 1 ]);
00259 }
00260
00261 #include "marginwidget.moc"