1 /***************************************************************************
2 * Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #include "formwidgets.h"
12 #include <qbuttongroup.h>
13 #include <klineedit.h>
17 #include "core/form.h"
19 FormWidgetsController::FormWidgetsController( QObject
*parent
)
24 FormWidgetsController::~FormWidgetsController()
28 void FormWidgetsController::signalChanged( FormWidgetIface
*w
)
33 void FormWidgetsController::signalAction( Okular::Action
*a
)
38 QButtonGroup
* FormWidgetsController::registerRadioButton( FormWidgetIface
* widget
, const QList
< int >& siblings
)
40 if ( !widget
->button() )
43 QList
< RadioData
>::iterator it
= m_radios
.begin(), itEnd
= m_radios
.end();
44 const int id
= widget
->formField()->id();
45 for ( ; it
!= itEnd
; ++it
)
47 const QList
< int >::const_iterator idsIt
= qFind( (*it
).ids
, id
);
48 if ( idsIt
!= (*it
).ids
.end() )
50 (*it
).group
->addButton( widget
->button() );
55 newdata
.ids
= siblings
;
56 newdata
.ids
.append( id
);
57 newdata
.group
= new QButtonGroup();
58 newdata
.group
->addButton( widget
->button() );
59 connect( newdata
.group
, SIGNAL( buttonClicked( QAbstractButton
* ) ),
60 this, SLOT( slotButtonClicked( QAbstractButton
* ) ) );
61 m_radios
.append( newdata
);
65 void FormWidgetsController::dropRadioButtons()
67 QList
< RadioData
>::iterator it
= m_radios
.begin(), itEnd
= m_radios
.end();
68 for ( ; it
!= itEnd
; ++it
)
75 void FormWidgetsController::slotButtonClicked( QAbstractButton
*button
)
77 if ( CheckBoxEdit
*check
= qobject_cast
< CheckBoxEdit
* >( button
) )
78 emit
changed( check
);
79 else if ( RadioButtonEdit
*radio
= qobject_cast
< RadioButtonEdit
* >( button
) )
80 emit
changed( radio
);
84 FormWidgetIface
* FormWidgetFactory::createWidget( Okular::FormField
* ff
, QWidget
* parent
)
86 FormWidgetIface
* widget
= 0;
89 case Okular::FormField::FormButton
:
91 Okular::FormFieldButton
* ffb
= static_cast< Okular::FormFieldButton
* >( ff
);
92 switch ( ffb
->buttonType() )
94 case Okular::FormFieldButton::Push
:
95 widget
= new PushButtonEdit( ffb
, parent
);
97 case Okular::FormFieldButton::CheckBox
:
98 widget
= new CheckBoxEdit( ffb
, parent
);
100 case Okular::FormFieldButton::Radio
:
101 widget
= new RadioButtonEdit( ffb
, parent
);
107 case Okular::FormField::FormText
:
109 Okular::FormFieldText
* fft
= static_cast< Okular::FormFieldText
* >( ff
);
110 switch ( fft
->textType() )
112 case Okular::FormFieldText::Multiline
:
113 widget
= new TextAreaEdit( fft
, parent
);
115 case Okular::FormFieldText::Normal
:
116 widget
= new FormLineEdit( fft
, parent
);
118 case Okular::FormFieldText::FileSelect
:
119 widget
= new FileEdit( fft
, parent
);
124 case Okular::FormField::FormChoice
:
126 Okular::FormFieldChoice
* ffc
= static_cast< Okular::FormFieldChoice
* >( ff
);
127 switch ( ffc
->choiceType() )
129 case Okular::FormFieldChoice::ListBox
:
130 widget
= new ListEdit( ffc
, parent
);
132 case Okular::FormFieldChoice::ComboBox
:
133 widget
= new ComboEdit( ffc
, parent
);
144 FormWidgetIface::FormWidgetIface( QWidget
* w
, Okular::FormField
* ff
)
145 : m_controller( 0 ), m_widget( w
), m_ff( ff
), m_pageItem( 0 )
149 FormWidgetIface::~FormWidgetIface()
153 Okular::NormalizedRect
FormWidgetIface::rect() const
158 void FormWidgetIface::setWidthHeight( int w
, int h
)
160 m_widget
->resize( w
, h
);
163 void FormWidgetIface::moveTo( int x
, int y
)
165 m_widget
->move( x
, y
);
168 bool FormWidgetIface::setVisibility( bool visible
)
170 if ( !m_ff
->isVisible() )
173 bool hadfocus
= m_widget
->hasFocus();
175 m_widget
->clearFocus();
176 m_widget
->setVisible( visible
);
180 void FormWidgetIface::setCanBeFilled( bool fill
)
182 if ( m_widget
->isEnabled() )
184 m_widget
->setEnabled( fill
);
188 void FormWidgetIface::setPageItem( PageViewItem
*pageItem
)
190 m_pageItem
= pageItem
;
193 Okular::FormField
* FormWidgetIface::formField() const
198 PageViewItem
* FormWidgetIface::pageItem() const
203 void FormWidgetIface::setFormWidgetsController( FormWidgetsController
*controller
)
205 m_controller
= controller
;
208 QAbstractButton
* FormWidgetIface::button()
214 PushButtonEdit::PushButtonEdit( Okular::FormFieldButton
* button
, QWidget
* parent
)
215 : QPushButton( parent
), FormWidgetIface( this, button
), m_form( button
)
217 setText( m_form
->caption() );
218 setEnabled( !m_form
->isReadOnly() );
219 setVisible( m_form
->isVisible() );
221 connect( this, SIGNAL( clicked() ), this, SLOT( slotClicked() ) );
224 void PushButtonEdit::slotClicked()
226 if ( m_form
->activationAction() )
227 m_controller
->signalAction( m_form
->activationAction() );
231 CheckBoxEdit::CheckBoxEdit( Okular::FormFieldButton
* button
, QWidget
* parent
)
232 : QCheckBox( parent
), FormWidgetIface( this, button
), m_form( button
)
234 setText( m_form
->caption() );
235 setEnabled( !m_form
->isReadOnly() );
237 setVisible( m_form
->isVisible() );
240 void CheckBoxEdit::setFormWidgetsController( FormWidgetsController
*controller
)
242 FormWidgetIface::setFormWidgetsController( controller
);
244 const QList
< int > siblings
= m_form
->siblings();
245 if ( !siblings
.isEmpty() )
246 m_controller
->registerRadioButton( this, siblings
);
248 setCheckState( m_form
->state() ? Qt::Checked
: Qt::Unchecked
);
250 connect( this, SIGNAL( stateChanged( int ) ), this, SLOT( slotStateChanged( int ) ) );
253 QAbstractButton
* CheckBoxEdit::button()
258 void CheckBoxEdit::slotStateChanged( int state
)
260 m_form
->setState( state
== Qt::Checked
);
263 m_controller
->signalChanged( this );
267 RadioButtonEdit::RadioButtonEdit( Okular::FormFieldButton
* button
, QWidget
* parent
)
268 : QRadioButton( parent
), FormWidgetIface( this, button
), m_form( button
)
270 setText( m_form
->caption() );
271 setEnabled( !m_form
->isReadOnly() );
273 setVisible( m_form
->isVisible() );
276 void RadioButtonEdit::setFormWidgetsController( FormWidgetsController
*controller
)
278 FormWidgetIface::setFormWidgetsController( controller
);
280 m_controller
->registerRadioButton( this, m_form
->siblings() );
282 setChecked( m_form
->state() );
284 connect( this, SIGNAL( toggled( bool ) ), this, SLOT( slotToggled( bool ) ) );
287 QAbstractButton
* RadioButtonEdit::button()
292 void RadioButtonEdit::slotToggled( bool checked
)
294 m_form
->setState( checked
);
297 m_controller
->signalChanged( this );
301 FormLineEdit::FormLineEdit( Okular::FormFieldText
* text
, QWidget
* parent
)
302 : QLineEdit( parent
), FormWidgetIface( this, text
), m_form( text
)
304 int maxlen
= m_form
->maximumLength();
306 setMaxLength( maxlen
);
307 setAlignment( m_form
->textAlignment() );
308 setText( m_form
->text() );
309 if ( m_form
->isPassword() )
310 setEchoMode( QLineEdit::Password
);
311 setReadOnly( m_form
->isReadOnly() );
313 connect( this, SIGNAL( textEdited( const QString
& ) ), this, SLOT( textEdited( const QString
& ) ) );
314 setVisible( m_form
->isVisible() );
317 void FormLineEdit::textEdited( const QString
& )
319 m_form
->setText( text() );
321 m_controller
->signalChanged( this );
325 TextAreaEdit::TextAreaEdit( Okular::FormFieldText
* text
, QWidget
* parent
)
326 : KTextEdit( parent
), FormWidgetIface( this, text
), m_form( text
)
328 setAcceptRichText( m_form
->isRichText() );
329 setCheckSpellingEnabled( m_form
->canBeSpellChecked() );
330 setAlignment( m_form
->textAlignment() );
331 setPlainText( m_form
->text() );
332 setReadOnly( m_form
->isReadOnly() );
334 connect( this, SIGNAL( textChanged() ), this, SLOT( slotChanged() ) );
335 setVisible( m_form
->isVisible() );
338 void TextAreaEdit::slotChanged()
340 m_form
->setText( toPlainText() );
342 m_controller
->signalChanged( this );
346 FileEdit::FileEdit( Okular::FormFieldText
* text
, QWidget
* parent
)
347 : KUrlRequester( parent
), FormWidgetIface( this, text
), m_form( text
)
349 setMode( KFile::File
| KFile::ExistingOnly
| KFile::LocalOnly
);
350 setFilter( i18n( "*|All Files" ) );
351 setPath( m_form
->text() );
352 lineEdit()->setAlignment( m_form
->textAlignment() );
353 setEnabled( !m_form
->isReadOnly() );
355 connect( this, SIGNAL( textChanged( const QString
& ) ), this, SLOT( slotChanged( const QString
& ) ) );
356 setVisible( m_form
->isVisible() );
359 void FileEdit::slotChanged( const QString
& )
361 m_form
->setText( url().path() );
363 m_controller
->signalChanged( this );
367 ListEdit::ListEdit( Okular::FormFieldChoice
* choice
, QWidget
* parent
)
368 : QListWidget( parent
), FormWidgetIface( this, choice
), m_form( choice
)
370 addItems( m_form
->choices() );
371 setSelectionMode( m_form
->multiSelect() ? QAbstractItemView::ExtendedSelection
: QAbstractItemView::SingleSelection
);
372 setVerticalScrollMode( QAbstractItemView::ScrollPerPixel
);
373 QList
< int > selectedItems
= m_form
->currentChoices();
374 if ( m_form
->multiSelect() )
376 foreach ( int index
, selectedItems
)
377 if ( index
>= 0 && index
< count() )
378 item( index
)->setSelected( true );
382 if ( selectedItems
.count() == 1 && selectedItems
.at(0) >= 0 && selectedItems
.at(0) < count() )
384 setCurrentRow( selectedItems
.at(0) );
385 scrollToItem( item( selectedItems
.at(0) ) );
388 setEnabled( !m_form
->isReadOnly() );
390 connect( this, SIGNAL( itemSelectionChanged() ), this, SLOT( slotSelectionChanged() ) );
391 setVisible( m_form
->isVisible() );
394 void ListEdit::slotSelectionChanged()
396 QList
< QListWidgetItem
* > selection
= selectedItems();
398 foreach( const QListWidgetItem
* item
, selection
)
399 rows
.append( row( item
) );
400 m_form
->setCurrentChoices( rows
);
402 m_controller
->signalChanged( this );
406 ComboEdit::ComboEdit( Okular::FormFieldChoice
* choice
, QWidget
* parent
)
407 : QComboBox( parent
), FormWidgetIface( this, choice
), m_form( choice
)
409 addItems( m_form
->choices() );
411 lineEdit()->setReadOnly( !m_form
->isEditable() );
412 QList
< int > selectedItems
= m_form
->currentChoices();
413 if ( selectedItems
.count() == 1 && selectedItems
.at(0) >= 0 && selectedItems
.at(0) < count() )
414 setCurrentIndex( selectedItems
.at(0) );
415 setEnabled( !m_form
->isReadOnly() );
417 connect( this, SIGNAL( currentIndexChanged( int ) ), this, SLOT( indexChanged( int ) ) );
418 setVisible( m_form
->isVisible() );
421 void ComboEdit::indexChanged( int index
)
423 m_form
->setCurrentChoices( QList
< int >() << index
);
425 m_controller
->signalChanged( this );
429 #include "formwidgets.moc"