not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kdm / kdm-dlg.cpp
blob52656e8a0d772eb6e78f8011980f0dead1faf439
1 /*
2 Copyright (C) 1997-1998 Thomas Tanghus (tanghus@earthling.net)
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "kdm-dlg.h"
22 #include "positioner.h"
24 #include <KApplication>
25 #include <KDialog>
26 #include <KFileDialog>
27 #include <KIconLoader>
28 #include <KImageFilePreview>
29 #include <KImageIO>
30 #include <KIO/NetAccess>
31 #include <KLineEdit>
32 #include <KLocale>
33 #include <KMessageBox>
34 #include <KConfig>
35 #include <KConfigGroup>
36 #include <KStandardDirs>
38 #include <QButtonGroup>
39 #include <QDragEnterEvent>
40 #include <QEvent>
41 #include <QGroupBox>
42 #include <QLabel>
43 #include <QPushButton>
44 #include <QRadioButton>
45 #include <QStyle>
46 #include <QGridLayout>
47 #include <QHBoxLayout>
48 #include <QVBoxLayout>
50 extern KConfig *config;
52 KDMDialogWidget::KDMDialogWidget( QWidget *parent )
53 : QWidget( parent )
55 QString wtstr;
57 QGridLayout *grid = new QGridLayout( this );
58 grid->setMargin( KDialog::marginHint() );
59 grid->setSpacing( KDialog::spacingHint() );
60 grid->setColumnStretch( 1, 1 );
62 QHBoxLayout *hlay = new QHBoxLayout();
63 hlay->setSpacing( KDialog::spacingHint() );
64 grid->addLayout( hlay, 0, 0, 1, 2 );
65 greetstr_lined = new KLineEdit( this );
66 QLabel *label = new QLabel( i18n("&Greeting:"), this );
67 label->setBuddy( greetstr_lined );
68 hlay->addWidget( label );
69 connect( greetstr_lined, SIGNAL(textChanged( const QString& )),
70 SIGNAL(changed()) );
71 hlay->addWidget( greetstr_lined );
72 wtstr = i18n(
73 "<p>This is the \"headline\" for KDM's login window. You may want to "
74 "put some nice greeting or information about the operating system here.</p>"
75 "<p>KDM will substitute the following character pairs with the "
76 "respective contents:</p>"
77 "<ul>"
78 "<li>%d -> current display</li>"
79 "<li>%h -> host name, possibly with domain name</li>"
80 "<li>%n -> node name, most probably the host name without domain name</li>"
81 "<li>%s -> the operating system</li>"
82 "<li>%r -> the operating system's version</li>"
83 "<li>%m -> the machine (hardware) type</li>"
84 "<li>%% -> a single %</li>"
85 "</ul>");
86 label->setWhatsThis( wtstr );
87 greetstr_lined->setWhatsThis( wtstr );
90 QGridLayout *hglay = new QGridLayout();
91 hglay->setSpacing( KDialog::spacingHint() );
92 grid->addLayout( hglay, 1, 0 );
94 label = new QLabel( i18n("Logo area:"), this );
95 hglay->addWidget( label, 0, 0 );
96 QVBoxLayout *vlay = new QVBoxLayout();
97 vlay->setSpacing( KDialog::spacingHint() );
98 hglay->addLayout( vlay, 0, 1, 1, 2 );
99 noneRadio = new QRadioButton( i18nc("logo area", "&None"), this );
100 clockRadio = new QRadioButton( i18n("Show cloc&k"), this );
101 logoRadio = new QRadioButton( i18n("Sho&w logo"), this );
102 QButtonGroup *buttonGroup = new QButtonGroup( this );
103 connect( buttonGroup, SIGNAL(buttonClicked( int )),
104 SLOT(slotAreaRadioClicked( int )) );
105 connect( buttonGroup, SIGNAL(buttonClicked( int )), SIGNAL(changed()) );
106 buttonGroup->addButton( noneRadio, KdmNone );
107 buttonGroup->addButton( clockRadio, KdmClock );
108 buttonGroup->addButton( logoRadio, KdmLogo );
109 vlay->addWidget( noneRadio );
110 vlay->addWidget( clockRadio );
111 vlay->addWidget( logoRadio );
112 wtstr = i18n("You can choose to display a custom logo (see below), a clock or no logo at all.");
113 label->setWhatsThis( wtstr );
114 noneRadio->setWhatsThis( wtstr );
115 logoRadio->setWhatsThis( wtstr );
116 clockRadio->setWhatsThis( wtstr );
118 logoLabel = new QLabel( i18n("&Logo:"), this );
119 logobutton = new QPushButton( this );
120 logoLabel->setBuddy( logobutton );
121 logobutton->setAutoDefault( false );
122 logobutton->setAcceptDrops( true );
123 logobutton->installEventFilter( this ); // for drag and drop
124 connect( logobutton, SIGNAL(clicked()), SLOT(slotLogoButtonClicked()) );
125 hglay->addWidget( logoLabel, 1, 0, Qt::AlignVCenter );
126 hglay->addWidget( logobutton, 1, 1, Qt::AlignCenter );
127 hglay->setRowMinimumHeight( 1, 110 );
128 wtstr = i18n(
129 "Click here to choose an image that KDM will display. "
130 "You can also drag and drop an image onto this button "
131 "(e.g. from Konqueror).");
132 logoLabel->setWhatsThis( wtstr );
133 logobutton->setWhatsThis( wtstr );
136 vlay = new QVBoxLayout();
137 grid->addLayout( vlay, 1, 1, 2, 1 );
138 vlay->setParent( grid );
140 label = new QLabel( i18n("Dialog &position:"), this );
141 vlay->addWidget( label );
142 positioner = new Positioner( this );
143 label->setBuddy( positioner );
144 connect( positioner, SIGNAL(positionChanged()), SIGNAL(changed()) );
145 vlay->addWidget( positioner );
147 grid->setRowStretch( 3, 1 );
151 void KDMDialogWidget::makeReadOnly()
153 disconnect( logobutton, SIGNAL(clicked()),
154 this, SLOT(slotLogoButtonClicked()) );
155 logobutton->setAcceptDrops( false );
156 greetstr_lined->setReadOnly( true );
157 noneRadio->setEnabled( false );
158 clockRadio->setEnabled( false );
159 logoRadio->setEnabled( false );
160 positioner->makeReadOnly();
163 bool KDMDialogWidget::setLogo( const QString &logo )
165 QString flogo = logo.isEmpty() ?
166 KStandardDirs::locate( "data", QLatin1String("kdm/pics/kdelogo.png") ) :
167 logo;
168 QImage p( flogo );
169 if (p.isNull())
170 return false;
171 if (p.width() > 100 || p.height() > 100)
172 p = p.scaled( 100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation );
173 logobutton->setIcon( QPixmap::fromImage( p ) );
174 uint bd = style()->pixelMetric( QStyle::PM_ButtonMargin ) * 2;
175 logobutton->setFixedSize( p.width() + bd, p.height() + bd );
176 logopath = logo;
177 return true;
181 void KDMDialogWidget::slotLogoButtonClicked()
183 KFileDialog dialog( KStandardDirs::locate( "data", QLatin1String("kdm/pics/") ),
184 KImageIO::pattern( KImageIO::Reading ),
185 this );
186 dialog.setOperationMode( KFileDialog::Opening );
187 dialog.setMode( KFile::File | KFile::LocalOnly );
189 KImageFilePreview *imagePreview = new KImageFilePreview( &dialog );
190 dialog.setPreviewWidget( imagePreview );
191 if (dialog.exec() == QDialog::Accepted &&
192 setLogo( dialog.selectedFile() ))
193 changed();
197 void KDMDialogWidget::slotAreaRadioClicked( int id )
199 logobutton->setEnabled( id == KdmLogo );
200 logoLabel->setEnabled( id == KdmLogo );
204 bool KDMDialogWidget::eventFilter( QObject *, QEvent *e )
206 if (e->type() == QEvent::DragEnter) {
207 iconLoaderDragEnterEvent( (QDragEnterEvent *)e );
208 return true;
211 if (e->type() == QEvent::Drop) {
212 iconLoaderDropEvent( (QDropEvent *)e );
213 return true;
216 return false;
219 void KDMDialogWidget::iconLoaderDragEnterEvent( QDragEnterEvent *e )
221 e->setAccepted( KUrl::List::canDecode( e->mimeData() ) );
225 KUrl *decodeImgDrop( QDropEvent *e, QWidget *wdg );
227 void KDMDialogWidget::iconLoaderDropEvent( QDropEvent *e )
229 KUrl pixurl;
230 bool istmp;
232 KUrl *url = decodeImgDrop( e, this );
233 if (url) {
235 // we gotta check if it is a non-local file and make a tmp copy at the hd.
236 if (!url->isLocalFile()) {
237 pixurl.setPath( KStandardDirs::installPath( "data" ) +
238 "kdm/pics/" + url->fileName() );
239 KIO::NetAccess::file_copy( *url, pixurl, parentWidget() );
240 istmp = true;
241 } else {
242 pixurl = *url;
243 istmp = false;
246 // By now url should be "file:/..."
247 if (!setLogo( pixurl.path() )) {
248 KIO::NetAccess::del( pixurl, parentWidget() );
249 QString msg = i18n("There was an error loading the image:\n"
250 "%1\n"
251 "It will not be saved.",
252 pixurl.path());
253 KMessageBox::sorry( this, msg );
256 delete url;
261 void KDMDialogWidget::save()
263 KConfigGroup configGrp = config->group( "X-*-Greeter" );
265 configGrp.writeEntry( "GreetString", greetstr_lined->text() );
267 configGrp.writeEntry( "LogoArea", noneRadio->isChecked() ? "None" :
268 logoRadio->isChecked() ? "Logo" : "Clock" );
270 configGrp.writeEntry( "LogoPixmap",
271 KIconLoader::global()->iconPath( logopath, KIconLoader::Desktop, true ) );
273 configGrp.writeEntry( "GreeterPos",
274 QString("%1,%2").arg( positioner->x() ).arg( positioner->y() ) );
278 void KDMDialogWidget::load()
280 KConfigGroup configGrp = config->group( "X-*-Greeter" );
282 // Read the greeting string
283 greetstr_lined->setText( configGrp.readEntry( "GreetString",
284 i18n("Welcome to %s at %n") ) );
286 // Regular logo or clock
287 QString logoArea = configGrp.readEntry( "LogoArea", "Logo" );
288 if (logoArea == "Clock") {
289 clockRadio->setChecked( true );
290 slotAreaRadioClicked( KdmClock );
291 } else if (logoArea == "Logo") {
292 logoRadio->setChecked( true );
293 slotAreaRadioClicked( KdmLogo );
294 } else {
295 noneRadio->setChecked( true );
296 slotAreaRadioClicked( KdmNone );
299 // See if we use alternate logo
300 setLogo( configGrp.readEntry( "LogoPixmap" ) );
302 QStringList sl = configGrp.readEntry( "GreeterPos", QStringList() );
303 if (sl.count() != 2)
304 positioner->setPosition( 50, 50 );
305 else
306 positioner->setPosition( sl.first().toInt(), sl.last().toInt() );
310 void KDMDialogWidget::defaults()
312 greetstr_lined->setText( i18n("Welcome to %s at %n") );
313 logoRadio->setChecked( true );
314 slotAreaRadioClicked( KdmLogo );
315 setLogo( "" );
316 positioner->setPosition( 50, 50 );
319 QString KDMDialogWidget::quickHelp() const
321 return i18n(
322 "<h1>KDM - Dialog</h1> Here you can configure the basic appearance of "
323 "the KDM login manager in dialog mode, i.e. a greeting string, an icon etc.");
326 #include "kdm-dlg.moc"