summaryrefslogtreecommitdiffstats
path: root/tdeprint/marginwidget.cpp
blob: 3ec1872f225df2fb95fb1f60364f5957618b7545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2001-2002 Michael Goffioul <tdeprint@swing.be>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "marginwidget.h"
#include "marginpreview.h"
#include "marginvaluewidget.h"
#include "kprinter.h"

#include <tqcombobox.h>
#include <tqcheckbox.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqwhatsthis.h>
#include <klocale.h>
#include <kglobal.h>

MarginWidget::MarginWidget(TQWidget *parent, const char* name, bool allowMetricUnit)
: TQWidget(parent, name), m_default(4, 0), m_pagesize( 2 )
{
	//WhatsThis strings.... (added by pfeifle@kde.org)
	TQString whatsThisTopMarginWidget = i18n( " <qt> "
			" <p><b>Top Margin</b></p>. "
			" <p>This spinbox/text edit field lets you control the top margin of your printout if the printing "
			" application does not define its margins internally. </p> "
			" <p>The setting works for instance for ASCII text file printing, or for printing from KMail and "
			" and Konqueror.. </p>"
			" <p><b>Note:</b></p>This margin setting is not intended for KOffice or OpenOffice.org printing, "
			" because these applications (or rather their users) are expected to do it by themselves. "
			" It also does not work for PostScript or PDF file, which in most cases have their margins hardcoded "
			" internally.</p> " 
			" <br> "
			" <hr> "
			" <p><em><b>Additional hint for power users:</b> This TDEPrint GUI element matches "
			" with the CUPS commandline job option parameter:</em> "
			" <pre>"
			"    -o page-top=...      # use values from \"0\" or higher. \"72\" is equal to 1 inch. "
			" </pre>"
			" </p> "
			" </qt>" );

	TQString whatsThisBottomMarginWidget = i18n( " <qt> "
			" <p><b>Bottom Margin</b></p>. "
			" <p>This spinbox/text edit field lets you control the bottom margin of your printout if the printing "
			" application does not define its margins internally. </p> "
			" <p>The setting works for instance for ASCII text file printing, or for printing from KMail and "
			" and Konqueror. </p>"
			" <p><b>Note:</b></p>This margin setting is not intended for KOffice or OpenOffice.org printing, "
			" because these applications (or rather their users) are expected to do it by themselves. "
			" It also does not work for PostScript or PDF file, which in most cases have their margins hardcoded "
			" internally.</p> "
			" <br> "
			" <hr> "
			" <p><em><b>Additional hint for power users:</b> This TDEPrint GUI element matches "
			" with the CUPS commandline job option parameter:</em> "
			" <pre>"
			"    -o page-bottom=...      # use values from \"0\" or higher. \"72\" is equal to 1 inch. "
			" </pre>"
			" </qt>" );

	TQString whatsThisLeftMarginWidget = i18n( " <qt> "
			" <p><b>Left Margin</b></p>. "
			" <p>This spinbox/text edit field lets you control the left margin of your printout if the printing "
			" application does not define its margins internally. </p> "
			" <p>The setting works for instance for ASCII text file printing, or for printing from KMail and "
			" and Konqueror. </p>"
			" <p><b>Note:</b></p>This margin setting is not intended for KOffice or OpenOffice.org printing, "
			" because these applications (or rather their users) are expected to do it by themselves. "
			" It also does not work for PostScript or PDF file, which in most cases have their margins hardcoded "
			" internally.</p> "
			" <br> "
			" <hr> "
			" <p><em><b>Additional hint for power users:</b> This TDEPrint GUI element matches "
			" with the CUPS commandline job option parameter:</em> "
			" <pre>"
			"    -o page-left=...      # use values from \"0\" or higher. \"72\" is equal to 1 inch. "
			" </pre>"
			" </qt>" );

	TQString whatsThisRightMarginWidget = i18n( " <qt> "
			" <p><b>Right Margin</b></p>. "
			" <p>This spinbox/text edit field lets you control the right margin of your printout if the printing "
			" application does not define its margins internally. </p> "
			" <p>The setting works for instance for ASCII text file printing, or for printing from KMail and "
			" and Konqueror. </p>"
			" <p><b>Note:</b></p>This margin setting is not intended for KOffice or OpenOffice.org printing, "
			" because these applications (or rather their users) are expected to do it by themselves. "
			" It also does not work for PostScript or PDF file, which in most cases have their margins hardcoded "
			" internally.</p> "
			" <br> "
			" <hr> "
			" <p><em><b>Additional hint for power users:</b> This TDEPrint GUI element matches "
			" with the CUPS commandline job option parameter:</em> "
			" <pre>"
			"    -o page-right=...      # use values from \"0\" or higher. \"72\" is equal to 1 inch. "
			" </pre>"
			" </qt>" );

	TQString whatsThisMeasurementUnitMarginWidget = i18n( " <qt> "
			" <p><b>Change Measurement Unit<b></p>. "
			" <p>You can change the units of measurement for the page"
			" margins here. Select from Millimeter, Centimeter, Inch or Pixels (1 pixel == 1/72 inch). "
			" </p> "
			" </qt>" );

	TQString whatsThisCheckboxMarginWidget = i18n( " <qt> "
			" <p><b>Custom Margins Checkbox</b></p>. "
			" <p>Enable this checkbox if you want to modify the margins of your printouts "
			" <p>You can change margin settings in 4 ways: "
			" <ul> "
			" <li>Edit the text fields. </li> "
			" <li>Click spinbox arrows. </li> "
			" <li>Scroll wheel of wheelmouses. </li> "
			" <li>Drag margins in preview frame with mouse. </li> "
			" </ul> "
			" <b>Note:</b> The margin setting does not work if you load such files directly into "
			" kprinter, which have their print margins hardcoded internally, like as most "
			" PDF or PostScript files. It works for all ASCII text files however. It also may not "
			" work with non-TDE applications which fail to "
			" fully utilize the TDEPrint framework, such as OpenOffice.org. </p> "
			" </qt>" );

	TQString whatsThisDragAndPreviewMarginWidget = i18n( " <qt> "
			" <p><b>\"Drag-your-Margins\" </p>. "
			" <p>Use your mouse to drag and set each margin on this little preview window. </p> "
			" </qt>" );

	m_symetric = m_block = false;
	m_pagesize[ 0 ] = 595;
	m_pagesize[ 1 ] = 842;
	m_landscape = false;

	m_custom = new TQCheckBox(i18n("&Use custom margins"), this);
	  TQWhatsThis::add(m_custom, whatsThisCheckboxMarginWidget);
	m_top = new MarginValueWidget(0, 0.0, this);
	  TQWhatsThis::add(m_top, whatsThisTopMarginWidget);
	m_bottom = new MarginValueWidget(m_top, 0.0, this);
	  TQWhatsThis::add(m_bottom, whatsThisBottomMarginWidget);
	m_left = new MarginValueWidget(m_bottom, 0.0, this);
	  TQWhatsThis::add(m_left, whatsThisLeftMarginWidget);
	m_right = new MarginValueWidget(m_left, 0.0, this);
	  TQWhatsThis::add(m_right, whatsThisRightMarginWidget);
	m_top->setLabel(i18n("&Top:"), Qt::AlignLeft|Qt::AlignVCenter);
	m_bottom->setLabel(i18n("&Bottom:"), Qt::AlignLeft|Qt::AlignVCenter);
	m_left->setLabel(i18n("Le&ft:"), Qt::AlignLeft|Qt::AlignVCenter);
	m_right->setLabel(i18n("&Right:"), Qt::AlignLeft|Qt::AlignVCenter);
	m_units = new TQComboBox(this);
	  TQWhatsThis::add(m_units, whatsThisMeasurementUnitMarginWidget);
	m_units->insertItem(i18n("Pixels (1/72nd in)"));
	if ( allowMetricUnit )
	{
		m_units->insertItem(i18n("Inches (in)"));
		m_units->insertItem(i18n("Centimeters (cm)"));
		m_units->insertItem( i18n( "Millimeters (mm)" ) );
	}
	m_units->setCurrentItem(0);
	connect(m_units, TQT_SIGNAL(activated(int)), m_top, TQT_SLOT(setMode(int)));
	connect(m_units, TQT_SIGNAL(activated(int)), m_bottom, TQT_SLOT(setMode(int)));
	connect(m_units, TQT_SIGNAL(activated(int)), m_left, TQT_SLOT(setMode(int)));
	connect(m_units, TQT_SIGNAL(activated(int)), m_right, TQT_SLOT(setMode(int)));
	m_preview = new MarginPreview(this);
	  TQWhatsThis::add(m_preview, whatsThisDragAndPreviewMarginWidget);
	m_preview->setMinimumSize(60, 80);
	m_preview->setPageSize(m_pagesize[ 0 ], m_pagesize[ 1 ]);
	connect(m_preview, TQT_SIGNAL(marginChanged(int,float)), TQT_SLOT(slotMarginPreviewChanged(int,float)));
	connect(m_top, TQT_SIGNAL(marginChanged(float)), TQT_SLOT(slotMarginValueChanged()));
	connect(m_bottom, TQT_SIGNAL(marginChanged(float)), TQT_SLOT(slotMarginValueChanged()));
	connect(m_left, TQT_SIGNAL(marginChanged(float)), TQT_SLOT(slotMarginValueChanged()));
	connect(m_right, TQT_SIGNAL(marginChanged(float)), TQT_SLOT(slotMarginValueChanged()));
	slotMarginValueChanged();
	connect(m_custom, TQT_SIGNAL(toggled(bool)), m_top, TQT_SLOT(setEnabled(bool)));
	connect(m_custom, TQT_SIGNAL(toggled(bool)), m_left, TQT_SLOT(setEnabled(bool)));
	//connect(m_custom, TQT_SIGNAL(toggled(bool)), m_units, TQT_SLOT(setEnabled(bool)));
	connect(m_custom, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotCustomMarginsToggled(bool)));
	connect(m_custom, TQT_SIGNAL(toggled(bool)), m_preview, TQT_SLOT(enableRubberBand(bool)));
	m_top->setEnabled(false);
	m_bottom->setEnabled(false);
	m_left->setEnabled(false);
	m_right->setEnabled(false);
	//m_units->setEnabled(false);

	TQGridLayout	*l3 = new TQGridLayout(this, 7, 2, 0, 10);
	l3->addWidget(m_custom, 0, 0);
	l3->addWidget(m_top, 1, 0);
	l3->addWidget(m_bottom, 2, 0);
	l3->addWidget(m_left, 3, 0);
	l3->addWidget(m_right, 4, 0);
	l3->addRowSpacing(5, 10);
	l3->addWidget(m_units, 6, 0);
	l3->addMultiCellWidget(m_preview, 0, 6, 1, 1);

	if ( allowMetricUnit )
	{
		int	mode = (TDEGlobal::locale()->measureSystem() == TDELocale::Metric ? 2 : 1);
		m_top->setMode(mode);
		m_bottom->setMode(mode);
		m_left->setMode(mode);
		m_right->setMode(mode);
		m_units->setCurrentItem(mode);
	}
}

MarginWidget::~MarginWidget()
{
}

void MarginWidget::slotCustomMarginsToggled(bool b)
{
	m_bottom->setEnabled(b && !m_symetric);
	m_right->setEnabled(b && !m_symetric);
	if (!b)
		resetDefault();
}

void MarginWidget::setSymetricMargins(bool on)
{
	if (on == m_symetric)
		return;

	m_symetric = on;
	m_bottom->setEnabled(on && m_custom->isChecked());
	m_right->setEnabled(on && m_custom->isChecked());
	if (on)
	{
		connect(m_top, TQT_SIGNAL(marginChanged(float)), m_bottom, TQT_SLOT(setMargin(float)));
		connect(m_left, TQT_SIGNAL(marginChanged(float)), m_right, TQT_SLOT(setMargin(float)));
		m_bottom->setMargin(m_top->margin());
		m_right->setMargin(m_left->margin());
	}
	else
	{
		disconnect(m_top, 0, m_bottom, 0);
		disconnect(m_left, 0, m_right, 0);
	}
	m_preview->setSymetric(on);
}

void MarginWidget::slotMarginValueChanged()
{
	if (m_block)
		return;
	m_preview->setMargins(m_top->margin(), m_bottom->margin(), m_left->margin(), m_right->margin());
}

void MarginWidget::slotMarginPreviewChanged(int type, float value)
{
	m_block = true;
	switch (type)
	{
		case MarginPreview::TMoving:
			m_top->setMargin(value);
			break;
		case MarginPreview::BMoving:
			m_bottom->setMargin(value);
			break;
		case MarginPreview::LMoving:
			m_left->setMargin(value);
			break;
		case MarginPreview::RMoving:
			m_right->setMargin(value);
			break;
	}
	m_block = false;
}

void MarginWidget::setPageSize(float w, float h)
{
	// takes care of the orientation and the resolution
	int	dpi = m_top->resolution();
	m_pagesize[ 0 ] = w;
	m_pagesize[ 1 ] = h;
	if (m_landscape)
		m_preview->setPageSize((m_pagesize[ 1 ]*dpi)/72, (m_pagesize[ 0 ]*dpi)/72);
	else
		m_preview->setPageSize((m_pagesize[ 0 ]*dpi)/72, (m_pagesize[ 1 ]*dpi)/72);
}

float MarginWidget::top() const
{
	return m_top->margin();
}

float MarginWidget::bottom() const
{
	return m_bottom->margin();
}

float MarginWidget::left() const
{
	return m_left->margin();
}

float MarginWidget::right() const
{
	return m_right->margin();
}

void MarginWidget::setTop(float value)
{
	m_top->setMargin(value);
}

void MarginWidget::setBottom(float value)
{
	m_bottom->setMargin(value);
}

void MarginWidget::setLeft(float value)
{
	m_left->setMargin(value);
}

void MarginWidget::setRight(float value)
{
	m_right->setMargin(value);
}

void MarginWidget::setResolution(int dpi)
{
	m_top->setResolution(dpi);
	m_bottom->setResolution(dpi);
	m_left->setResolution(dpi);
	m_right->setResolution(dpi);
}

void MarginWidget::setDefaultMargins(float t, float b, float l, float r)
{
	int	dpi = m_top->resolution();
	m_default[0] = (t*dpi)/72;
	m_default[1] = (b*dpi)/72;
	m_default[2] = (l*dpi)/72;
	m_default[3] = (r*dpi)/72;
	if (!m_custom->isChecked())
		resetDefault();
}

void MarginWidget::resetDefault()
{
	m_top->setMargin(m_landscape ? m_default[2] : m_default[0]);
	m_bottom->setMargin(m_landscape ? m_default[3] : m_default[1]);
	m_left->setMargin(m_landscape ? m_default[1] : m_default[2]);
	m_right->setMargin(m_landscape ? m_default[0] : m_default[3]);
}

void MarginWidget::setCustomEnabled(bool on)
{
	m_custom->setChecked(on);
}

bool MarginWidget::isCustomEnabled() const
{
	return m_custom->isChecked();
}

void MarginWidget::setOrientation(int orient)
{
	m_landscape = (orient == KPrinter::Landscape);
	setPageSize(m_pagesize[ 0 ], m_pagesize[ 1 ]);
}

#include "marginwidget.moc"