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
|
/*
Copyright 2012-2013 Adam Reichold
This file is part of qpdfview.
qpdfview is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
qpdfview 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with qpdfview. If not, see <http://www.gnu.org/licenses/>.
*/
#include "formfieldwidgets.h"
#include <QGraphicsProxyWidget>
#include <QMutex>
#include <poppler-form.h>
#ifndef HAS_POPPLER_24
#define LOCK_FORM_FIELD QMutexLocker mutexLocker(m_mutex);
#else
#define LOCK_FORM_FIELD
#endif // HAS_POPPLER_24
namespace
{
bool hideOnEscape(QWidget* widget, QKeyEvent* event)
{
if(event->key() == Qt::Key_Escape)
{
widget->hide();
event->accept();
return true;
}
return false;
}
} // anonymous
namespace qpdfview
{
NormalTextFieldWidget::NormalTextFieldWidget(QMutex* mutex, Poppler::FormFieldText* formField, QWidget* parent) : QLineEdit(parent),
m_mutex(mutex),
m_formField(formField)
{
LOCK_FORM_FIELD
setText(m_formField->text());
setMaxLength(m_formField->maximumLength());
setAlignment(m_formField->textAlignment());
setEchoMode(m_formField->isPassword() ? QLineEdit::Password : QLineEdit::Normal);
connect(this, SIGNAL(textChanged(QString)), SLOT(on_textChanged(QString)));
connect(this, SIGNAL(textChanged(QString)), SIGNAL(wasModified()));
connect(this, SIGNAL(returnPressed()), SLOT(hide()));
}
void NormalTextFieldWidget::keyPressEvent(QKeyEvent* event)
{
if(!hideOnEscape(this, event))
{
QLineEdit::keyPressEvent(event);
}
}
void NormalTextFieldWidget::on_textChanged(const QString& text)
{
LOCK_FORM_FIELD
m_formField->setText(text);
}
MultilineTextFieldWidget::MultilineTextFieldWidget(QMutex* mutex, Poppler::FormFieldText* formField, QWidget* parent) : QPlainTextEdit(parent),
m_mutex(mutex),
m_formField(formField)
{
LOCK_FORM_FIELD
setPlainText(m_formField->text());
connect(this, SIGNAL(textChanged()), SLOT(on_textChanged()));
connect(this, SIGNAL(textChanged()), SIGNAL(wasModified()));
moveCursor(QTextCursor::End);
}
void MultilineTextFieldWidget::keyPressEvent(QKeyEvent* event)
{
if(!hideOnEscape(this, event))
{
QPlainTextEdit::keyPressEvent(event);
}
}
void MultilineTextFieldWidget::on_textChanged()
{
LOCK_FORM_FIELD
m_formField->setText(toPlainText());
}
ComboBoxChoiceFieldWidget::ComboBoxChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldChoice* formField, QWidget* parent) : QComboBox(parent),
m_mutex(mutex),
m_formField(formField)
{
LOCK_FORM_FIELD
addItems(m_formField->choices());
if(!m_formField->currentChoices().isEmpty())
{
setCurrentIndex(m_formField->currentChoices().at(0));
}
connect(this, SIGNAL(currentIndexChanged(int)), SLOT(on_currentIndexChanged(int)));
connect(this, SIGNAL(currentIndexChanged(int)), SIGNAL(wasModified()));
#ifdef HAS_POPPLER_22
if(m_formField->isEditable())
{
setEditable(true);
setInsertPolicy(QComboBox::NoInsert);
lineEdit()->setText(m_formField->editChoice());
connect(lineEdit(), SIGNAL(textChanged(QString)), SLOT(on_currentTextChanged(QString)));
connect(lineEdit(), SIGNAL(textChanged(QString)), SIGNAL(wasModified()));
connect(lineEdit(), SIGNAL(returnPressed()), SLOT(hide()));
}
else
{
connect(this, SIGNAL(activated(int)), SLOT(hide()));
}
#else
connect(this, SIGNAL(activated(int)), SLOT(hide()));
#endif // HAS_POPPLER_22
}
void ComboBoxChoiceFieldWidget::keyPressEvent(QKeyEvent* event)
{
if(!hideOnEscape(this, event))
{
QComboBox::keyPressEvent(event);
}
}
void ComboBoxChoiceFieldWidget::showPopup()
{
QComboBox::showPopup();
graphicsProxyWidget()->setZValue(1.0);
}
void ComboBoxChoiceFieldWidget::hidePopup()
{
QComboBox::hidePopup();
graphicsProxyWidget()->setZValue(0.0);
}
void ComboBoxChoiceFieldWidget::on_currentIndexChanged(int index)
{
LOCK_FORM_FIELD
m_formField->setCurrentChoices(QList< int >() << index);
}
void ComboBoxChoiceFieldWidget::on_currentTextChanged(const QString& text)
{
LOCK_FORM_FIELD
#ifdef HAS_POPPLER_22
m_formField->setEditChoice(text);
#endif // HAS_POPPLER_22
}
ListBoxChoiceFieldWidget::ListBoxChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldChoice* formField, QWidget* parent) : QListWidget(parent),
m_mutex(mutex),
m_formField(formField)
{
LOCK_FORM_FIELD
addItems(m_formField->choices());
setSelectionMode(m_formField->multiSelect() ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection);
foreach(int index, m_formField->currentChoices())
{
if(index >= 0 && index < count())
{
item(index)->setSelected(true);
}
}
connect(this, SIGNAL(itemSelectionChanged()), SLOT(on_itemSelectionChanged()));
connect(this, SIGNAL(itemSelectionChanged()), SIGNAL(wasModified()));
}
void ListBoxChoiceFieldWidget::keyPressEvent(QKeyEvent* event)
{
if(!hideOnEscape(this, event))
{
QListWidget::keyPressEvent(event);
}
}
void ListBoxChoiceFieldWidget::on_itemSelectionChanged()
{
LOCK_FORM_FIELD
QList< int > currentChoices;
for(int index = 0; index < count(); ++index)
{
if(item(index)->isSelected())
{
currentChoices.append(index);
}
}
m_formField->setCurrentChoices(currentChoices);
}
CheckBoxChoiceFieldWidget::CheckBoxChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldButton* formField, QWidget* parent) : QCheckBox(parent),
m_mutex(mutex),
m_formField(formField)
{
LOCK_FORM_FIELD
setChecked(m_formField->state());
connect(this, SIGNAL(toggled(bool)), SLOT(on_toggled(bool)));
connect(this, SIGNAL(toggled(bool)), SIGNAL(wasModified()));
}
void CheckBoxChoiceFieldWidget::keyPressEvent(QKeyEvent* event)
{
if(!hideOnEscape(this, event))
{
QCheckBox::keyPressEvent(event);
}
}
void CheckBoxChoiceFieldWidget::on_toggled(bool checked)
{
LOCK_FORM_FIELD
m_formField->setState(checked);
}
RadioChoiceFieldWidget::Siblings RadioChoiceFieldWidget::s_siblings;
RadioChoiceFieldWidget::RadioChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldButton* formField, QWidget* parent) : QRadioButton(parent),
m_mutex(mutex),
m_formField(formField)
{
LOCK_FORM_FIELD
s_siblings.insert(qMakePair(m_mutex, m_formField->id()), this);
setAutoExclusive(false);
setChecked(m_formField->state());
connect(this, SIGNAL(toggled(bool)), SLOT(on_toggled(bool)));
connect(this, SIGNAL(toggled(bool)), SIGNAL(wasModified()));
}
RadioChoiceFieldWidget::~RadioChoiceFieldWidget()
{
LOCK_FORM_FIELD
s_siblings.remove(qMakePair(m_mutex, m_formField->id()));
}
void RadioChoiceFieldWidget::keyPressEvent(QKeyEvent* event)
{
if(!hideOnEscape(this, event))
{
QRadioButton::keyPressEvent(event);
}
}
void RadioChoiceFieldWidget::on_toggled(bool checked)
{
LOCK_FORM_FIELD
m_formField->setState(checked);
if(checked)
{
const QList< int > siblings = m_formField->siblings();
#ifndef HAS_POPPLER_24
mutexLocker.unlock();
#endif // HAS_POPPLER_24
foreach(int id, siblings)
{
const QPair< QMutex*, int > key = qMakePair(m_mutex, id);
if(s_siblings.contains(key))
{
s_siblings.value(key)->setChecked(false);
}
}
}
}
} // qpdfview
|