3 This file is part of the KDE project, module kcmbackground.
5 Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
6 Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 This library 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 GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
24 #include <QHeaderView>
29 #include <QPushButton>
33 #include <QGridLayout>
38 #include <kcolorbutton.h>
40 #include <kmessagebox.h>
41 #include <kstandarddirs.h>
44 #include "bgadvanced.h"
49 /**** BGAdvancedDialog ****/
51 BGAdvancedDialog::BGAdvancedDialog(KBackgroundRenderer
*_r
,
56 setObjectName( "BGAdvancedDialog" );
58 setCaption( i18n("Advanced Background Settings") );
59 setButtons( Ok
| Cancel
);
60 showButtonSeparator( true );
62 dlg
= new BGAdvancedBase(this);
65 dlg
->m_listPrograms
->header()->setResizeMode(1, QHeaderView::Stretch
);
66 dlg
->m_listPrograms
->setRootIsDecorated(false);
67 dlg
->m_listPrograms
->setAllColumnsShowFocus(true);
69 connect(dlg
->m_listPrograms
, SIGNAL(itemClicked(QTreeWidgetItem
*, int)),
70 SLOT(slotProgramItemClicked(QTreeWidgetItem
*)));
73 const QStringList lst
= KBackgroundProgram::list();
74 QStringList::const_iterator it
;
75 for (it
=lst
.constBegin(); it
!= lst
.constEnd(); ++it
)
78 // FIXME: support it proper
79 dlg
->m_buttonAdd
->hide();
80 dlg
->m_buttonRemove
->hide();
81 dlg
->m_buttonModify
->hide();
82 dlg
->m_groupCache
->hide();
84 connect( dlg
->m_cbProgram
, SIGNAL(toggled(bool)),
85 SLOT(slotEnableProgram(bool)));
87 m_oldBackgroundMode
= r
->backgroundMode();
88 if (m_oldBackgroundMode
== KBackgroundSettings::Program
)
89 m_oldBackgroundMode
= KBackgroundSettings::Flat
;
95 void BGAdvancedDialog::makeReadOnly()
97 dlg
->m_cbProgram
->setEnabled(false);
98 dlg
->m_listPrograms
->setEnabled(false);
102 void BGAdvancedDialog::setCacheSize(int s
)
104 dlg
->m_spinCache
->setValue(s
);
107 int BGAdvancedDialog::cacheSize()
109 return dlg
->m_spinCache
->value();
112 QColor
BGAdvancedDialog::textColor()
114 return dlg
->m_colorText
->color();
117 void BGAdvancedDialog::setTextColor(const QColor
&color
)
119 dlg
->m_colorText
->setColor(color
);
122 QColor
BGAdvancedDialog::textBackgroundColor()
124 return dlg
->m_cbSolidTextBackground
->isChecked() ?
125 dlg
->m_colorTextBackground
->color() : QColor();
128 void BGAdvancedDialog::setTextBackgroundColor(const QColor
&color
)
130 dlg
->m_colorTextBackground
->blockSignals(true);
131 dlg
->m_cbSolidTextBackground
->blockSignals(true);
134 dlg
->m_cbSolidTextBackground
->setChecked(true);
135 dlg
->m_colorTextBackground
->setColor(color
);
136 dlg
->m_colorTextBackground
->setEnabled(true);
140 dlg
->m_cbSolidTextBackground
->setChecked(false);
141 dlg
->m_colorTextBackground
->setColor(Qt::white
);
142 dlg
->m_colorTextBackground
->setEnabled(false);
144 dlg
->m_colorTextBackground
->blockSignals(false);
145 dlg
->m_cbSolidTextBackground
->blockSignals(false);
148 bool BGAdvancedDialog::shadowEnabled()
150 return dlg
->m_cbShadow
->isChecked();
153 void BGAdvancedDialog::setShadowEnabled(bool enabled
)
155 dlg
->m_cbShadow
->setChecked(enabled
);
158 void BGAdvancedDialog::setTextLines(int lines
)
160 dlg
->m_spinTextLines
->setValue(lines
);
163 int BGAdvancedDialog::textLines() const
165 return dlg
->m_spinTextLines
->value();
168 void BGAdvancedDialog::setTextWidth(int width
)
170 dlg
->m_spinTextWidth
->setValue(width
);
173 int BGAdvancedDialog::textWidth() const
175 return dlg
->m_spinTextWidth
->value();
179 void BGAdvancedDialog::updateUI()
181 QString prog
= r
->KBackgroundProgram::name();
183 dlg
->m_cbProgram
->blockSignals(true);
184 if ((r
->backgroundMode() == KBackgroundSettings::Program
)
187 dlg
->m_cbProgram
->setChecked(true);
188 dlg
->m_listPrograms
->setEnabled(true);
189 dlg
->m_buttonAdd
->setEnabled(true);
190 dlg
->m_buttonRemove
->setEnabled(true);
191 dlg
->m_buttonModify
->setEnabled(true);
196 dlg
->m_cbProgram
->setChecked(false);
197 dlg
->m_listPrograms
->setEnabled(false);
198 dlg
->m_buttonAdd
->setEnabled(false);
199 dlg
->m_buttonRemove
->setEnabled(false);
200 dlg
->m_buttonModify
->setEnabled(false);
202 dlg
->m_cbProgram
->blockSignals(false);
206 void BGAdvancedDialog::removeProgram(const QString
&name
)
208 delete m_programItems
.take(name
);
212 void BGAdvancedDialog::addProgram(const QString
&name
)
218 KBackgroundProgram
prog(name
);
219 if (prog
.command().isEmpty() || (prog
.isGlobal() && !prog
.isAvailable()))
222 QTreeWidgetItem
*item
= new QTreeWidgetItem(dlg
->m_listPrograms
);
223 item
->setText(0, prog
.name());
224 item
->setText(1, prog
.comment());
225 item
->setText(2, i18n("%1 min.", prog
.refresh()));
227 m_programItems
.insert(name
, item
);
230 void BGAdvancedDialog::selectProgram(const QString
&name
)
232 if (QTreeWidgetItem
*item
= m_programItems
[name
])
234 dlg
->m_listPrograms
->scrollToItem(item
);
235 item
->setSelected(true);
236 m_selectedProgram
= name
;
241 void BGAdvancedDialog::slotAdd()
243 KProgramEditDialog
dlg(QString(), this);
245 if (dlg
.result() == QDialog::Accepted
)
247 QString program
= dlg
.program();
249 selectProgram(program
);
253 void BGAdvancedDialog::slotRemove()
255 if (m_selectedProgram
.isEmpty())
258 KBackgroundProgram
prog(m_selectedProgram
);
261 KMessageBox::sorry(this,
262 i18n("Unable to remove the program: the program is global "
263 "and can only be removed by the system administrator."),
264 i18n("Cannot Remove Program"));
267 if (KMessageBox::warningContinueCancel(this,
268 i18n("Are you sure you want to remove the program `%1'?",
270 i18n("Remove Background Program"),
271 KGuiItem(i18n("&Remove"))) != KMessageBox::Continue
)
275 removeProgram(m_selectedProgram
);
276 m_selectedProgram
.clear();
282 void BGAdvancedDialog::slotModify()
284 if (m_selectedProgram
.isEmpty())
287 KProgramEditDialog
dlg(m_selectedProgram
, this);
289 if (dlg
.result() == QDialog::Accepted
)
291 QString program
= dlg
.program();
292 if (program
!= m_selectedProgram
)
294 KBackgroundProgram
prog(m_selectedProgram
);
296 removeProgram(m_selectedProgram
);
298 addProgram(dlg
.program());
299 selectProgram(dlg
.program());
304 void BGAdvancedDialog::slotProgramItemClicked(QTreeWidgetItem
*item
)
307 m_selectedProgram
= item
->text(0);
308 slotProgramChanged();
312 void BGAdvancedDialog::slotProgramItemDoubleClicked(QTreeWidgetItem
*item
)
314 slotProgramItemClicked(item
);
319 void BGAdvancedDialog::slotProgramChanged()
323 r
->setProgram(m_selectedProgram
);
324 if (dlg
->m_cbProgram
->isChecked() && !m_selectedProgram
.isEmpty())
325 r
->setBackgroundMode(KBackgroundSettings::Program
);
327 r
->setBackgroundMode(m_oldBackgroundMode
);
329 // We have no preview, no need to start the renderer
333 void BGAdvancedDialog::slotEnableProgram(bool b
)
335 dlg
->m_listPrograms
->setEnabled(b
);
338 if (QTreeWidgetItem
*cur
= dlg
->m_listPrograms
->currentItem()) {
339 dlg
->m_listPrograms
->blockSignals(true);
340 cur
->setSelected(true);
341 dlg
->m_listPrograms
->scrollToItem(cur
);
342 dlg
->m_listPrograms
->blockSignals(false);
343 slotProgramItemClicked(cur
);
348 slotProgramChanged();
352 /**** KProgramEditDialog ****/
354 KProgramEditDialog::KProgramEditDialog(const QString
&program
, QWidget
*parent
, char *name
)
357 setObjectName( name
);
359 setCaption( i18n("Configure Background Program") );
360 setButtons( Ok
| Cancel
);
361 showButtonSeparator( true );
363 QFrame
*frame
= new QFrame( this );
364 setMainWidget( frame
);
366 QGridLayout
*grid
= new QGridLayout(frame
);
367 grid
->setSpacing(spacingHint());
369 grid
->setColumnMinimumWidth(1, 300);
371 QLabel
*lbl
= new QLabel(i18n("&Name:"), frame
);
372 grid
->addWidget(lbl
, 0, 0);
373 m_NameEdit
= new QLineEdit(frame
);
374 lbl
->setBuddy(m_NameEdit
);
375 grid
->addWidget(m_NameEdit
, 0, 1);
377 lbl
= new QLabel(i18n("Co&mment:"), frame
);
378 grid
->addWidget(lbl
, 1, 0);
379 m_CommentEdit
= new QLineEdit(frame
);
380 lbl
->setBuddy(m_CommentEdit
);
381 grid
->addWidget(m_CommentEdit
, 1, 1);
383 lbl
= new QLabel(i18n("Comman&d:"), frame
);
384 grid
->addWidget(lbl
, 2, 0);
385 m_CommandEdit
= new QLineEdit(frame
);
386 lbl
->setBuddy(m_CommandEdit
);
387 grid
->addWidget(m_CommandEdit
, 2, 1);
389 lbl
= new QLabel(i18n("&Preview cmd:"), frame
);
390 grid
->addWidget(lbl
, 3, 0);
391 m_PreviewEdit
= new QLineEdit(frame
);
392 lbl
->setBuddy(m_PreviewEdit
);
393 grid
->addWidget(m_PreviewEdit
, 3, 1);
395 lbl
= new QLabel(i18n("&Executable:"), frame
);
396 grid
->addWidget(lbl
, 4, 0);
397 m_ExecEdit
= new QLineEdit(frame
);
398 lbl
->setBuddy(m_ExecEdit
);
399 grid
->addWidget(m_ExecEdit
, 4, 1);
401 lbl
= new QLabel(i18n("&Refresh time:"), frame
);
402 grid
->addWidget(lbl
, 5, 0);
403 m_RefreshEdit
= new QSpinBox(frame
);
404 m_RefreshEdit
->setRange(5, 60);
405 m_RefreshEdit
->setSingleStep(5);
406 m_RefreshEdit
->setSuffix(i18n(" min"));
407 m_RefreshEdit
->setFixedSize(m_RefreshEdit
->sizeHint());
408 lbl
->setBuddy(m_RefreshEdit
);
409 grid
->addWidget(m_RefreshEdit
, 5, 1, Qt::AlignLeft
);
412 if (m_Program
.isEmpty()) {
413 KBackgroundProgram
prog(i18n("New Command"));
415 while (!prog
.command().isEmpty())
416 prog
.load(i18n("New Command <%1>", i
++));
417 m_NameEdit
->setText(prog
.name());
418 m_NameEdit
->setSelection(0, 100);
419 m_RefreshEdit
->setValue(15);
423 // Fill in the fields
424 m_NameEdit
->setText(m_Program
);
425 KBackgroundProgram
prog(m_Program
);
426 m_CommentEdit
->setText(prog
.comment());
427 m_ExecEdit
->setText(prog
.executable());
428 m_CommandEdit
->setText(prog
.command());
429 m_PreviewEdit
->setText(prog
.previewCommand());
430 m_RefreshEdit
->setValue(prog
.refresh());
434 QString
KProgramEditDialog::program()const
436 return m_NameEdit
->text();
439 void KProgramEditDialog::accept()
441 QString s
= m_NameEdit
->text();
443 KMessageBox::sorry(this, i18n("You did not fill in the `Name' field.\n"
444 "This is a required field."));
448 KBackgroundProgram
prog(s
);
449 if ((s
!= m_Program
) && !prog
.command().isEmpty()) {
450 int ret
= KMessageBox::warningContinueCancel(this,
451 i18n("There is already a program with the name `%1'.\n"
452 "Do you want to overwrite it?", s
),QString(),KGuiItem(i18n("Overwrite")));
453 if (ret
!= KMessageBox::Continue
)
457 if (m_ExecEdit
->text().isEmpty()) {
458 KMessageBox::sorry(this, i18n("You did not fill in the `Executable' field.\n"
459 "This is a required field."));
462 if (m_CommandEdit
->text().isEmpty()) {
463 KMessageBox::sorry(this, i18n("You did not fill in the `Command' field.\n"
464 "This is a required field."));
468 prog
.setComment(m_CommentEdit
->text());
469 prog
.setExecutable(m_ExecEdit
->text());
470 prog
.setCommand(m_CommandEdit
->text());
471 prog
.setPreviewCommand(m_PreviewEdit
->text());
472 prog
.setRefresh(m_RefreshEdit
->value());
474 prog
.writeSettings();
480 #include "bgadvanced.moc"