summaryrefslogtreecommitdiffstats
path: root/kviewshell/pageSizeWidget.cpp
blob: 5446fd53867f842280fc94ebb5e2a272056508aa (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
// pageSizeWidget.cpp
//
// Part of KVIEWSHELL - A framework for multipage text/gfx viewers
//
// (C) 2002-2003 Stefan Kebekus
// Distributed under the GPL

// Add header files alphabetically

#include <config.h>

#include <kcombobox.h>
#include <kdebug.h>
#include <klocale.h>
#include <tqlineedit.h>
#include <tqvalidator.h>

#include "pageSize.h"
#include "pageSizeWidget.h"
#include "sizePreview.h"



// Constructs a pageSizeWidget_base which is a child of 'tqparent', with
// the name 'name' and widget flags set to 'f'.
pageSizeWidget::pageSizeWidget( TQWidget* tqparent,  const char* name, WFlags fl )
    : pageSizeWidget_base( tqparent,  name, fl )
{
  connect(&chosenSize, TQT_SIGNAL(sizeChanged(const SimplePageSize&)), previewer, TQT_SLOT(setSize(const SimplePageSize&)));

  // Set up the formatChoice TQComboBox
  formatChoice->insertItem(i18n("Custom Size"));
  formatChoice->insertStringList(chosenSize.pageSizeNames());

  // Activate the proper entry in the TQComboBox
  if (chosenSize.formatName().isNull()) {
    orientationChoice->setEnabled(false);
    formatChoice->setCurrentItem(0);
  } else {
    orientationChoice->setEnabled(true);
    formatChoice->setCurrentText(chosenSize.formatName());
  }
  paperSize(formatChoice->currentItem());

  connect(formatChoice, TQT_SIGNAL(activated(int)), this, TQT_SLOT(paperSize(int)));
  connect(orientationChoice, TQT_SIGNAL(activated(int)), this, TQT_SLOT(orientationChanged(int)));

  // Update the text fields when the user switches to a new unit, and
  // when the "custom format" is NOT selected.
  connect(widthUnits, TQT_SIGNAL(activated(int)), this, TQT_SLOT(unitsChanged(int)));
  connect(heightUnits, TQT_SIGNAL(activated(int)), this, TQT_SLOT(unitsChanged(int)));

  // Upate the chosen size whenever the user edits the input field. 
  connect(widthInput, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(input(const TQString &)));
  connect(heightInput, TQT_SIGNAL(textChanged(const TQString &)), this, TQT_SLOT(input(const TQString &)));

  // Allow entries between 0 and 1200. More filtering is done by the
  // pageSize class, which silently ignores values which are out of
  // range.
  widthInput->setValidator(new TQDoubleValidator(0.0, 1200.0, 1, TQT_TQOBJECT(this), "widthValidator"));
  heightInput->setValidator(new TQDoubleValidator(0.0, 1200.0, 1, TQT_TQOBJECT(this), "heightValidator"));
}


// Receives the output from the formatChoice combobox. A value of
// "zero" means that the "custom paper size" has been chosen.
void pageSizeWidget::paperSize(int index)
{
  widthInput->setEnabled(index == 0);
  heightInput->setEnabled(index == 0);
  orientationChoice->setEnabled(index != 0);
  
  if (index != 0) {
    chosenSize.setPageSize(formatChoice->currentText());
    chosenSize.setOrientation(orientationChoice->currentItem());
  }
  widthUnits->setCurrentText(chosenSize.preferredUnit());
  heightUnits->setCurrentText(chosenSize.preferredUnit());
  
  fillTextFields();
}


void pageSizeWidget::setPageSize(const TQString& sizeName)
{
  chosenSize.setPageSize(sizeName);

  int index = chosenSize.formatNumber();

  formatChoice->setCurrentItem(index+1);
  widthInput->setEnabled(index == -1);
  heightInput->setEnabled(index == -1);
  orientationChoice->setEnabled(index != -1);

  widthUnits->setCurrentText(chosenSize.preferredUnit());
  heightUnits->setCurrentText(chosenSize.preferredUnit());

  fillTextFields();
}


void pageSizeWidget::fillTextFields()
{				   
  // Warning! It is really necessary here to first generate both
  // strings, and then call setText() on the input widgets. Reason?
  // Calling setText() implicitly calls the input() method via the
  // textChanged()-Signal of the TQLineEdit widgets.
  TQString width  = chosenSize.widthString(widthUnits->currentText());
  TQString height = chosenSize.heightString(heightUnits->currentText());

  widthInput->setText(width);
  heightInput->setText(height);
}


void pageSizeWidget::unitsChanged(int)
{
  // Update the text fields, i.e. show the current size in the proper
  // units, but only if the "custom size" is NOT selected.
  if (formatChoice->currentItem() != 0)
    fillTextFields();
  else
    input(TQString());
}


void pageSizeWidget::setOrientation(int ori)
{
  orientationChoice->setCurrentItem(ori);
  orientationChanged();
}


void pageSizeWidget::orientationChanged(int orient)
{
  // Update the page preview
  chosenSize.setOrientation(orient);
}


void pageSizeWidget::input(const TQString &)
{
  chosenSize.setPageSize(widthInput->text(), widthUnits->currentText(), heightInput->text(), heightUnits->currentText());
}



#include "pageSizeWidget.moc"