[1f4adec] | 1 | /* |
---|
| 2 | |
---|
| 3 | Copyright 2012-2013 Adam Reichold |
---|
| 4 | |
---|
| 5 | This file is part of qpdfview. |
---|
| 6 | |
---|
| 7 | qpdfview is free software: you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU General Public License as published by |
---|
| 9 | the Free Software Foundation, either version 2 of the License, or |
---|
| 10 | (at your option) any later version. |
---|
| 11 | |
---|
| 12 | qpdfview is distributed in the hope that it will be useful, |
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | GNU General Public License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU General Public License |
---|
| 18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "formfieldwidgets.h" |
---|
| 23 | |
---|
| 24 | #include <QGraphicsProxyWidget> |
---|
| 25 | #include <QMutex> |
---|
| 26 | |
---|
| 27 | #include <poppler-form.h> |
---|
| 28 | |
---|
| 29 | #ifndef HAS_POPPLER_24 |
---|
| 30 | |
---|
| 31 | #define LOCK_FORM_FIELD QMutexLocker mutexLocker(m_mutex); |
---|
| 32 | |
---|
| 33 | #else |
---|
| 34 | |
---|
| 35 | #define LOCK_FORM_FIELD |
---|
| 36 | |
---|
| 37 | #endif // HAS_POPPLER_24 |
---|
| 38 | |
---|
| 39 | namespace |
---|
| 40 | { |
---|
| 41 | |
---|
| 42 | bool hideOnEscape(QWidget* widget, QKeyEvent* event) |
---|
| 43 | { |
---|
| 44 | if(event->key() == Qt::Key_Escape) |
---|
| 45 | { |
---|
| 46 | widget->hide(); |
---|
| 47 | |
---|
| 48 | event->accept(); |
---|
| 49 | return true; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | return false; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | } // anonymous |
---|
| 56 | |
---|
| 57 | namespace qpdfview |
---|
| 58 | { |
---|
| 59 | |
---|
| 60 | NormalTextFieldWidget::NormalTextFieldWidget(QMutex* mutex, Poppler::FormFieldText* formField, QWidget* parent) : QLineEdit(parent), |
---|
| 61 | m_mutex(mutex), |
---|
| 62 | m_formField(formField) |
---|
| 63 | { |
---|
| 64 | LOCK_FORM_FIELD |
---|
| 65 | |
---|
| 66 | setText(m_formField->text()); |
---|
| 67 | setMaxLength(m_formField->maximumLength()); |
---|
| 68 | setAlignment(m_formField->textAlignment()); |
---|
| 69 | setEchoMode(m_formField->isPassword() ? QLineEdit::Password : QLineEdit::Normal); |
---|
| 70 | |
---|
| 71 | connect(this, SIGNAL(textChanged(QString)), SLOT(on_textChanged(QString))); |
---|
| 72 | connect(this, SIGNAL(textChanged(QString)), SIGNAL(wasModified())); |
---|
| 73 | |
---|
| 74 | connect(this, SIGNAL(returnPressed()), SLOT(hide())); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void NormalTextFieldWidget::keyPressEvent(QKeyEvent* event) |
---|
| 78 | { |
---|
| 79 | if(!hideOnEscape(this, event)) |
---|
| 80 | { |
---|
| 81 | QLineEdit::keyPressEvent(event); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void NormalTextFieldWidget::on_textChanged(const QString& text) |
---|
| 86 | { |
---|
| 87 | LOCK_FORM_FIELD |
---|
| 88 | |
---|
| 89 | m_formField->setText(text); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | MultilineTextFieldWidget::MultilineTextFieldWidget(QMutex* mutex, Poppler::FormFieldText* formField, QWidget* parent) : QPlainTextEdit(parent), |
---|
| 93 | m_mutex(mutex), |
---|
| 94 | m_formField(formField) |
---|
| 95 | { |
---|
| 96 | LOCK_FORM_FIELD |
---|
| 97 | |
---|
| 98 | setPlainText(m_formField->text()); |
---|
| 99 | |
---|
| 100 | connect(this, SIGNAL(textChanged()), SLOT(on_textChanged())); |
---|
| 101 | connect(this, SIGNAL(textChanged()), SIGNAL(wasModified())); |
---|
| 102 | |
---|
| 103 | moveCursor(QTextCursor::End); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void MultilineTextFieldWidget::keyPressEvent(QKeyEvent* event) |
---|
| 107 | { |
---|
| 108 | if(!hideOnEscape(this, event)) |
---|
| 109 | { |
---|
| 110 | QPlainTextEdit::keyPressEvent(event); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void MultilineTextFieldWidget::on_textChanged() |
---|
| 115 | { |
---|
| 116 | LOCK_FORM_FIELD |
---|
| 117 | |
---|
| 118 | m_formField->setText(toPlainText()); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | ComboBoxChoiceFieldWidget::ComboBoxChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldChoice* formField, QWidget* parent) : QComboBox(parent), |
---|
| 122 | m_mutex(mutex), |
---|
| 123 | m_formField(formField) |
---|
| 124 | { |
---|
| 125 | LOCK_FORM_FIELD |
---|
| 126 | |
---|
| 127 | addItems(m_formField->choices()); |
---|
| 128 | |
---|
| 129 | if(!m_formField->currentChoices().isEmpty()) |
---|
| 130 | { |
---|
| 131 | setCurrentIndex(m_formField->currentChoices().first()); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | connect(this, SIGNAL(currentIndexChanged(int)), SLOT(on_currentIndexChanged(int))); |
---|
| 135 | connect(this, SIGNAL(currentIndexChanged(int)), SIGNAL(wasModified())); |
---|
| 136 | |
---|
| 137 | #ifdef HAS_POPPLER_22 |
---|
| 138 | |
---|
| 139 | if(m_formField->isEditable()) |
---|
| 140 | { |
---|
| 141 | setEditable(true); |
---|
| 142 | setInsertPolicy(QComboBox::NoInsert); |
---|
| 143 | |
---|
| 144 | lineEdit()->setText(m_formField->editChoice()); |
---|
| 145 | |
---|
| 146 | connect(lineEdit(), SIGNAL(textChanged(QString)), SLOT(on_currentTextChanged(QString))); |
---|
| 147 | connect(lineEdit(), SIGNAL(textChanged(QString)), SIGNAL(wasModified())); |
---|
| 148 | |
---|
| 149 | connect(lineEdit(), SIGNAL(returnPressed()), SLOT(hide())); |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | connect(this, SIGNAL(activated(int)), SLOT(hide())); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | #else |
---|
| 157 | |
---|
| 158 | connect(this, SIGNAL(activated(int)), SLOT(hide())); |
---|
| 159 | |
---|
| 160 | #endif // HAS_POPPLER_22 |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void ComboBoxChoiceFieldWidget::keyPressEvent(QKeyEvent* event) |
---|
| 164 | { |
---|
| 165 | if(!hideOnEscape(this, event)) |
---|
| 166 | { |
---|
| 167 | QComboBox::keyPressEvent(event); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void ComboBoxChoiceFieldWidget::showPopup() |
---|
| 172 | { |
---|
| 173 | QComboBox::showPopup(); |
---|
| 174 | |
---|
| 175 | graphicsProxyWidget()->setZValue(1.0); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | void ComboBoxChoiceFieldWidget::hidePopup() |
---|
| 179 | { |
---|
| 180 | QComboBox::hidePopup(); |
---|
| 181 | |
---|
| 182 | graphicsProxyWidget()->setZValue(0.0); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void ComboBoxChoiceFieldWidget::on_currentIndexChanged(int index) |
---|
| 186 | { |
---|
| 187 | LOCK_FORM_FIELD |
---|
| 188 | |
---|
| 189 | m_formField->setCurrentChoices(QList< int >() << index); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void ComboBoxChoiceFieldWidget::on_currentTextChanged(const QString& text) |
---|
| 193 | { |
---|
| 194 | LOCK_FORM_FIELD |
---|
| 195 | |
---|
| 196 | #ifdef HAS_POPPLER_22 |
---|
| 197 | |
---|
| 198 | m_formField->setEditChoice(text); |
---|
| 199 | |
---|
| 200 | #endif // HAS_POPPLER_22 |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | ListBoxChoiceFieldWidget::ListBoxChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldChoice* formField, QWidget* parent) : QListWidget(parent), |
---|
| 204 | m_mutex(mutex), |
---|
| 205 | m_formField(formField) |
---|
| 206 | { |
---|
| 207 | LOCK_FORM_FIELD |
---|
| 208 | |
---|
| 209 | addItems(m_formField->choices()); |
---|
| 210 | setSelectionMode(m_formField->multiSelect() ? QAbstractItemView::MultiSelection : QAbstractItemView::SingleSelection); |
---|
| 211 | |
---|
| 212 | foreach(int index, m_formField->currentChoices()) |
---|
| 213 | { |
---|
| 214 | if(index >= 0 && index < count()) |
---|
| 215 | { |
---|
| 216 | item(index)->setSelected(true); |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | connect(this, SIGNAL(itemSelectionChanged()), SLOT(on_itemSelectionChanged())); |
---|
| 221 | connect(this, SIGNAL(itemSelectionChanged()), SIGNAL(wasModified())); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void ListBoxChoiceFieldWidget::keyPressEvent(QKeyEvent* event) |
---|
| 225 | { |
---|
| 226 | if(!hideOnEscape(this, event)) |
---|
| 227 | { |
---|
| 228 | QListWidget::keyPressEvent(event); |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void ListBoxChoiceFieldWidget::on_itemSelectionChanged() |
---|
| 233 | { |
---|
| 234 | LOCK_FORM_FIELD |
---|
| 235 | |
---|
| 236 | QList< int > currentChoices; |
---|
| 237 | |
---|
| 238 | for(int index = 0; index < count(); ++index) |
---|
| 239 | { |
---|
| 240 | if(item(index)->isSelected()) |
---|
| 241 | { |
---|
| 242 | currentChoices.append(index); |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | m_formField->setCurrentChoices(currentChoices); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | CheckBoxChoiceFieldWidget::CheckBoxChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldButton* formField, QWidget* parent) : QCheckBox(parent), |
---|
| 250 | m_mutex(mutex), |
---|
| 251 | m_formField(formField) |
---|
| 252 | { |
---|
| 253 | LOCK_FORM_FIELD |
---|
| 254 | |
---|
| 255 | setChecked(m_formField->state()); |
---|
| 256 | |
---|
| 257 | connect(this, SIGNAL(toggled(bool)), SLOT(on_toggled(bool))); |
---|
| 258 | connect(this, SIGNAL(toggled(bool)), SIGNAL(wasModified())); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | void CheckBoxChoiceFieldWidget::keyPressEvent(QKeyEvent* event) |
---|
| 262 | { |
---|
| 263 | if(!hideOnEscape(this, event)) |
---|
| 264 | { |
---|
| 265 | QCheckBox::keyPressEvent(event); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | void CheckBoxChoiceFieldWidget::on_toggled(bool checked) |
---|
| 270 | { |
---|
| 271 | LOCK_FORM_FIELD |
---|
| 272 | |
---|
| 273 | m_formField->setState(checked); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | RadioChoiceFieldWidget::Siblings RadioChoiceFieldWidget::s_siblings; |
---|
| 277 | |
---|
| 278 | RadioChoiceFieldWidget::RadioChoiceFieldWidget(QMutex* mutex, Poppler::FormFieldButton* formField, QWidget* parent) : QRadioButton(parent), |
---|
| 279 | m_mutex(mutex), |
---|
| 280 | m_formField(formField) |
---|
| 281 | { |
---|
| 282 | LOCK_FORM_FIELD |
---|
| 283 | |
---|
| 284 | s_siblings.insert(qMakePair(m_mutex, m_formField->id()), this); |
---|
| 285 | |
---|
| 286 | setAutoExclusive(false); |
---|
| 287 | setChecked(m_formField->state()); |
---|
| 288 | |
---|
| 289 | connect(this, SIGNAL(toggled(bool)), SLOT(on_toggled(bool))); |
---|
| 290 | connect(this, SIGNAL(toggled(bool)), SIGNAL(wasModified())); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | RadioChoiceFieldWidget::~RadioChoiceFieldWidget() |
---|
| 294 | { |
---|
| 295 | LOCK_FORM_FIELD |
---|
| 296 | |
---|
| 297 | s_siblings.remove(qMakePair(m_mutex, m_formField->id())); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | void RadioChoiceFieldWidget::keyPressEvent(QKeyEvent* event) |
---|
| 301 | { |
---|
| 302 | if(!hideOnEscape(this, event)) |
---|
| 303 | { |
---|
| 304 | QRadioButton::keyPressEvent(event); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | void RadioChoiceFieldWidget::on_toggled(bool checked) |
---|
| 309 | { |
---|
| 310 | LOCK_FORM_FIELD |
---|
| 311 | |
---|
| 312 | m_formField->setState(checked); |
---|
| 313 | |
---|
| 314 | if(checked) |
---|
| 315 | { |
---|
| 316 | const QList< int > siblings = m_formField->siblings(); |
---|
| 317 | |
---|
| 318 | #ifndef HAS_POPPLER_24 |
---|
| 319 | |
---|
| 320 | mutexLocker.unlock(); |
---|
| 321 | |
---|
| 322 | #endif // HAS_POPPLER_24 |
---|
| 323 | |
---|
| 324 | foreach(int id, siblings) |
---|
| 325 | { |
---|
| 326 | const QPair< QMutex*, int > key = qMakePair(m_mutex, id); |
---|
| 327 | |
---|
| 328 | if(s_siblings.contains(key)) |
---|
| 329 | { |
---|
| 330 | s_siblings.value(key)->setChecked(false); |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | } // qpdfview |
---|