1 | /* |
2 | This file is part of libkresources. |
3 | |
4 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> |
5 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> |
6 | Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> |
7 | |
8 | This library is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Library General Public |
10 | License as published by the Free Software Foundation; either |
11 | version 2 of the License, or (at your option) any later version. |
12 | |
13 | This library is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Library General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Library General Public License |
19 | along with this library; see the file COPYING.LIB. If not, write to |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. |
22 | */ |
23 | /** |
24 | @file |
25 | This file is part of the KDE resource framework and defines the |
26 | ConfigDialog class. |
27 | |
28 | @brief |
29 | Provides a resource configuration dialog. |
30 | |
31 | @author Tobias Koenig |
32 | @author Jan-Pascal van Best |
33 | */ |
34 | |
35 | #include "configdialog.h" |
36 | #include <klocalizedstring.h> |
37 | #include <klineedit.h> |
38 | #include <kmessagebox.h> |
39 | |
40 | #include <QGroupBox> |
41 | #include <QLabel> |
42 | #include <QLayout> |
43 | #include <QCheckBox> |
44 | |
45 | #include "factory.h" |
46 | |
47 | using namespace KRES; |
48 | |
49 | class ConfigDialog::Private |
50 | { |
51 | public: |
52 | ConfigWidget *mConfigWidget; |
53 | Resource *mResource; |
54 | KLineEdit *mName; |
55 | QCheckBox *mReadOnly; |
56 | }; |
57 | |
58 | ConfigDialog::ConfigDialog( QWidget *parent, const QString &resourceFamily, |
59 | Resource *resource ) |
60 | : KDialog( parent ), d( new Private ) |
61 | { |
62 | setModal( true ); |
63 | setCaption( i18nc( "@title:window" , "Resource Configuration" ) ); |
64 | setButtons( Ok | Cancel ); |
65 | setDefaultButton( Ok ); |
66 | showButtonSeparator( false ); |
67 | |
68 | d->mResource = resource; |
69 | Factory *factory = Factory::self( resourceFamily ); |
70 | |
71 | QFrame *main = new QFrame( this ); |
72 | setMainWidget( main ); |
73 | |
74 | QVBoxLayout *mainLayout = new QVBoxLayout( main ); |
75 | mainLayout->setMargin( 0 ); |
76 | |
77 | QGroupBox *generalGroupBox = new QGroupBox( main ); |
78 | QGridLayout *gbLayout = new QGridLayout; |
79 | generalGroupBox->setLayout( gbLayout ); |
80 | |
81 | generalGroupBox->setTitle( i18nc( "@title:group" , "General Settings" ) ); |
82 | |
83 | gbLayout->addWidget( new QLabel( i18nc( "@label resource name" , "Name:" ), |
84 | generalGroupBox ), 0, 0 ); |
85 | |
86 | d->mName = new KLineEdit(); |
87 | gbLayout->addWidget( d->mName, 0, 1 ); |
88 | |
89 | d->mReadOnly = |
90 | new QCheckBox( i18nc( "@option:check if resource is read-only" , "Read-only" ), |
91 | generalGroupBox ); |
92 | gbLayout->addWidget( d->mReadOnly, 1, 0, 1, 2 ); |
93 | |
94 | d->mName->setText( d->mResource->resourceName() ); |
95 | d->mReadOnly->setChecked( d->mResource->readOnly() ); |
96 | |
97 | mainLayout->addWidget( generalGroupBox ); |
98 | |
99 | QGroupBox *resourceGroupBox = new QGroupBox( main ); |
100 | QGridLayout *resourceLayout = new QGridLayout; |
101 | resourceGroupBox->setLayout( resourceLayout ); |
102 | |
103 | resourceGroupBox->setTitle( i18nc( "@title:group" , "%1 Resource Settings" , |
104 | factory->typeName( resource->type() ) ) ); |
105 | mainLayout->addWidget( resourceGroupBox ); |
106 | |
107 | mainLayout->addStretch(); |
108 | |
109 | d->mConfigWidget = factory->configWidget( resource->type(), resourceGroupBox ); |
110 | if ( d->mConfigWidget ) { |
111 | resourceLayout->addWidget( d->mConfigWidget ); |
112 | d->mConfigWidget->setInEditMode( false ); |
113 | d->mConfigWidget->loadSettings( d->mResource ); |
114 | d->mConfigWidget->show(); |
115 | connect( d->mConfigWidget, SIGNAL(setReadOnly(bool)), |
116 | SLOT(setReadOnly(bool)) ); |
117 | } |
118 | |
119 | connect( d->mName, SIGNAL(textChanged(QString)), |
120 | SLOT(slotNameChanged(QString)) ); |
121 | |
122 | slotNameChanged( d->mName->text() ); |
123 | setMinimumSize( sizeHint() ); |
124 | } |
125 | |
126 | ConfigDialog::~ConfigDialog() |
127 | { |
128 | delete d; |
129 | } |
130 | |
131 | void ConfigDialog::setInEditMode( bool value ) |
132 | { |
133 | if ( d->mConfigWidget ) { |
134 | d->mConfigWidget->setInEditMode( value ); |
135 | } |
136 | } |
137 | |
138 | void ConfigDialog::slotNameChanged( const QString &text ) |
139 | { |
140 | enableButtonOk( !text.isEmpty() ); |
141 | } |
142 | |
143 | void ConfigDialog::setReadOnly( bool value ) |
144 | { |
145 | d->mReadOnly->setChecked( value ); |
146 | } |
147 | |
148 | void ConfigDialog::accept() |
149 | { |
150 | if ( d->mName->text().isEmpty() ) { |
151 | KMessageBox::sorry( this, i18nc( "@info" , "Please enter a resource name." ) ); |
152 | return; |
153 | } |
154 | |
155 | d->mResource->setResourceName( d->mName->text() ); |
156 | d->mResource->setReadOnly( d->mReadOnly->isChecked() ); |
157 | |
158 | if ( d->mConfigWidget ) { |
159 | // First save generic information |
160 | // Also save setting of specific resource type |
161 | d->mConfigWidget->saveSettings( d->mResource ); |
162 | } |
163 | |
164 | KDialog::accept(); |
165 | } |
166 | |
167 | |