1 /*
2     SPDX-FileCopyrightText: 2009 Michal Malek <michalm@jabster.pl>
3     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "k3bvolumenamewidget.h"
9 #include "k3bdatadoc.h"
10 #include "k3bisooptions.h"
11 #include "k3bvalidators.h"
12 
13 #include <KLineEdit>
14 #include <KLocalizedString>
15 
16 #include <QEvent>
17 #include <QHBoxLayout>
18 #include <QLabel>
19 
20 namespace K3b {
21 
22 class VolumeNameWidget::Private
23 {
24 public:
25     DataDoc* doc;
26     KLineEdit* volumeNameEdit;
27 
28     void fontChanged( const QFontMetrics& fontMetrics );
29 };
30 
31 
fontChanged(const QFontMetrics & fontMetrics)32 void VolumeNameWidget::Private::fontChanged( const QFontMetrics& fontMetrics )
33 {
34     volumeNameEdit->setMaximumWidth( fontMetrics.boundingRect('A').width()*50 );
35 }
36 
37 
VolumeNameWidget(DataDoc * doc,QWidget * parent)38 VolumeNameWidget::VolumeNameWidget( DataDoc* doc, QWidget* parent )
39     : QWidget( parent ),
40       d( new Private )
41 {
42     d->doc = doc;
43 
44     d->volumeNameEdit = new KLineEdit( doc->isoOptions().volumeID(), this );
45     d->volumeNameEdit->setValidator( new Latin1Validator( d->volumeNameEdit ) );
46     d->volumeNameEdit->setClearButtonEnabled( true );
47     d->fontChanged( fontMetrics() );
48 
49     QHBoxLayout* layout = new QHBoxLayout( this );
50     layout->addWidget( new QLabel( i18n("Volume Name:"), this ), 1, Qt::AlignRight );
51     layout->addWidget( d->volumeNameEdit, 2 );
52     layout->setContentsMargins( 0, 0, 0, 0 );
53 
54     connect( d->volumeNameEdit, SIGNAL(textChanged(QString)),
55              d->doc, SLOT(setVolumeID(QString)) );
56     connect( d->doc, SIGNAL(changed()),
57              this, SLOT(slotDocChanged()) );
58 }
59 
60 
~VolumeNameWidget()61 VolumeNameWidget::~VolumeNameWidget()
62 {
63     delete d;
64 }
65 
66 
changeEvent(QEvent * event)67 void VolumeNameWidget::changeEvent( QEvent* event )
68 {
69     if( event->type() == QEvent::FontChange ) {
70         d->fontChanged( fontMetrics() );
71     }
72     QWidget::changeEvent( event );
73 }
74 
75 
slotDocChanged()76 void VolumeNameWidget::slotDocChanged()
77 {
78     // do not update the editor in case it changed the volume id itself
79     if( d->doc->isoOptions().volumeID() != d->volumeNameEdit->text() )
80         d->volumeNameEdit->setText( d->doc->isoOptions().volumeID() );
81 }
82 
83 } // namespace K3b
84