~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to kcontrol/background/bgadvanced.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vi: ts=8 sts=4 sw=4
2
 
 
3
 
   This file is part of the KDE project, module kcmbackground.
4
 
 
5
 
   Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
6
 
   Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
7
 
 
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.
11
 
 
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.
16
 
 
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.
21
 
 */
22
 
 
23
 
#include <qbuttongroup.h>
24
 
#include <qcheckbox.h>
25
 
#include <qheader.h>
26
 
#include <qlabel.h>
27
 
#include <qlayout.h>
28
 
#include <qlineedit.h>
29
 
#include <qcombobox.h>
30
 
#include <qpushbutton.h>
31
 
#include <qslider.h>
32
 
#include <qspinbox.h>
33
 
#include <qwhatsthis.h>
34
 
 
35
 
#include <kconfig.h>
36
 
#include <kcolorbutton.h>
37
 
#include <klocale.h>
38
 
#include <kmessagebox.h>
39
 
#include <kpixmap.h>
40
 
#include <kstandarddirs.h>
41
 
#include <kwin.h>
42
 
 
43
 
#include "bgrender.h"
44
 
#include "bgadvanced.h"
45
 
#include "bgadvanced_ui.h"
46
 
 
47
 
#include <X11/Xlib.h>
48
 
 
49
 
/**** BGAdvancedDialog ****/
50
 
 
51
 
static QCString desktopConfigname()
52
 
{
53
 
    int desktop=0;
54
 
    if (qt_xdisplay())
55
 
        desktop = DefaultScreen(qt_xdisplay());
56
 
    QCString name;
57
 
    if (desktop == 0)
58
 
        name = "kdesktoprc";
59
 
    else
60
 
        name.sprintf("kdesktop-screen-%drc", desktop);
61
 
 
62
 
    return name;
63
 
}
64
 
 
65
 
 
66
 
BGAdvancedDialog::BGAdvancedDialog(KBackgroundRenderer *_r,
67
 
                                   QWidget *parent,
68
 
                                   bool m_multidesktop)
69
 
    : KDialogBase(parent, "BGAdvancedDialog",
70
 
                  true, i18n("Advanced Background Settings"),
71
 
                  Ok | Cancel, Ok, true),
72
 
      r(_r)
73
 
{
74
 
   dlg = new BGAdvancedBase(this);
75
 
   setMainWidget(dlg);
76
 
 
77
 
   dlg->m_listPrograms->header()->setStretchEnabled ( true, 1 );
78
 
   dlg->m_listPrograms->setAllColumnsShowFocus(true);
79
 
 
80
 
   connect(dlg->m_listPrograms, SIGNAL(clicked(QListViewItem *)),
81
 
         SLOT(slotProgramItemClicked(QListViewItem *)));
82
 
 
83
 
   // Load programs
84
 
   QStringList lst = KBackgroundProgram::list();
85
 
   QStringList::Iterator it;
86
 
   for (it=lst.begin(); it != lst.end(); ++it)
87
 
      addProgram(*it);
88
 
 
89
 
   if (m_multidesktop)
90
 
   {
91
 
      KConfig cfg(desktopConfigname(), false, false);
92
 
      cfg.setGroup( "General" );
93
 
      if (!cfg.readBoolEntry( "Enabled", true ))
94
 
      {
95
 
         dlg->m_groupIconText->hide();
96
 
      }
97
 
 
98
 
      dlg->m_spinCache->setSteps(512, 1024);
99
 
      dlg->m_spinCache->setRange(0, 40960);
100
 
      dlg->m_spinCache->setSpecialValueText(i18n("Unlimited"));
101
 
      dlg->m_spinCache->setSuffix(i18n(" KB"));
102
 
 
103
 
      connect(dlg->m_buttonAdd, SIGNAL(clicked()),
104
 
         SLOT(slotAdd()));
105
 
      connect(dlg->m_buttonRemove, SIGNAL(clicked()),
106
 
         SLOT(slotRemove()));
107
 
      connect(dlg->m_buttonModify, SIGNAL(clicked()),
108
 
         SLOT(slotModify()));
109
 
 
110
 
      connect(dlg->m_listPrograms, SIGNAL(doubleClicked(QListViewItem *)),
111
 
         SLOT(slotProgramItemDoubleClicked(QListViewItem *)));
112
 
   }
113
 
   else
114
 
   {
115
 
      dlg->m_buttonAdd->hide();
116
 
      dlg->m_buttonRemove->hide();
117
 
      dlg->m_buttonModify->hide();
118
 
      dlg->m_groupIconText->hide();
119
 
      dlg->m_groupCache->hide();
120
 
   }
121
 
 
122
 
   connect( dlg->m_cbProgram, SIGNAL(toggled(bool)),
123
 
            SLOT(slotEnableProgram(bool)));
124
 
 
125
 
   m_backgroundMode = m_oldBackgroundMode = r->backgroundMode();
126
 
   if (m_oldBackgroundMode == KBackgroundSettings::Program)
127
 
      m_oldBackgroundMode = KBackgroundSettings::Flat;
128
 
 
129
 
   dlg->adjustSize();
130
 
   updateUI();
131
 
}
132
 
 
133
 
void BGAdvancedDialog::makeReadOnly()
134
 
{
135
 
   dlg->m_cbProgram->setEnabled(false);
136
 
   dlg->m_listPrograms->setEnabled(false);
137
 
}
138
 
 
139
 
void BGAdvancedDialog::setCacheSize(int s)
140
 
{
141
 
   dlg->m_spinCache->setValue(s);
142
 
}
143
 
 
144
 
int BGAdvancedDialog::cacheSize()
145
 
{
146
 
   return dlg->m_spinCache->value();
147
 
}
148
 
 
149
 
QColor BGAdvancedDialog::textColor()
150
 
{
151
 
    return dlg->m_colorText->color();
152
 
}
153
 
 
154
 
void BGAdvancedDialog::setTextColor(const QColor &color)
155
 
{
156
 
    dlg->m_colorText->setColor(color);
157
 
}
158
 
 
159
 
QColor BGAdvancedDialog::textBackgroundColor()
160
 
{
161
 
    return dlg->m_cbSolidTextBackground->isChecked() ?
162
 
           dlg->m_colorTextBackground->color() : QColor();
163
 
}
164
 
 
165
 
void BGAdvancedDialog::setTextBackgroundColor(const QColor &color)
166
 
{
167
 
    dlg->m_colorTextBackground->blockSignals(true);
168
 
    dlg->m_cbSolidTextBackground->blockSignals(true);
169
 
    if (color.isValid())
170
 
    {
171
 
        dlg->m_cbSolidTextBackground->setChecked(true);
172
 
        dlg->m_colorTextBackground->setColor(color);
173
 
        dlg->m_colorTextBackground->setEnabled(true);
174
 
    }
175
 
    else
176
 
    {
177
 
        dlg->m_cbSolidTextBackground->setChecked(false);
178
 
        dlg->m_colorTextBackground->setColor(Qt::white);
179
 
        dlg->m_colorTextBackground->setEnabled(false);
180
 
    }
181
 
    dlg->m_colorTextBackground->blockSignals(false);
182
 
    dlg->m_cbSolidTextBackground->blockSignals(false);
183
 
}
184
 
 
185
 
bool BGAdvancedDialog::shadowEnabled()
186
 
{
187
 
    return dlg->m_cbShadow->isChecked();
188
 
}
189
 
 
190
 
void BGAdvancedDialog::setShadowEnabled(bool enabled)
191
 
{
192
 
    dlg->m_cbShadow->setChecked(enabled);
193
 
}
194
 
 
195
 
void BGAdvancedDialog::setTextLines(int lines)
196
 
{
197
 
    dlg->m_spinTextLines->setValue(lines);
198
 
}
199
 
 
200
 
int BGAdvancedDialog::textLines() const
201
 
{
202
 
    return dlg->m_spinTextLines->value();
203
 
}
204
 
 
205
 
void BGAdvancedDialog::setTextWidth(int width)
206
 
{
207
 
    dlg->m_spinTextWidth->setValue(width);
208
 
}
209
 
 
210
 
int BGAdvancedDialog::textWidth() const
211
 
{
212
 
    return dlg->m_spinTextWidth->value();
213
 
}
214
 
 
215
 
void BGAdvancedDialog::updateUI()
216
 
{
217
 
    QString prog = r->KBackgroundProgram::name();
218
 
 
219
 
    dlg->m_cbProgram->blockSignals(true);
220
 
    if ((r->backgroundMode() == KBackgroundSettings::Program)
221
 
        && !prog.isEmpty())
222
 
    {
223
 
        dlg->m_cbProgram->setChecked(true);
224
 
        dlg->m_listPrograms->setEnabled(true);
225
 
        dlg->m_buttonAdd->setEnabled(true);
226
 
        dlg->m_buttonRemove->setEnabled(true);
227
 
        dlg->m_buttonModify->setEnabled(true);
228
 
        selectProgram(prog);
229
 
    }
230
 
    else
231
 
    {
232
 
        dlg->m_cbProgram->setChecked(false);
233
 
        dlg->m_listPrograms->setEnabled(false);
234
 
        dlg->m_buttonAdd->setEnabled(false);
235
 
        dlg->m_buttonRemove->setEnabled(false);
236
 
        dlg->m_buttonModify->setEnabled(false);
237
 
    }
238
 
    dlg->m_cbProgram->blockSignals(false);
239
 
}
240
 
 
241
 
void BGAdvancedDialog::removeProgram(const QString &name)
242
 
{
243
 
   if (m_programItems.find(name))
244
 
   {
245
 
      delete m_programItems[name];
246
 
      m_programItems.remove(name);
247
 
   }
248
 
}
249
 
 
250
 
void BGAdvancedDialog::addProgram(const QString &name)
251
 
{
252
 
   removeProgram(name);
253
 
 
254
 
   KBackgroundProgram prog(name);
255
 
   if (prog.command().isEmpty() || (prog.isGlobal() && !prog.isAvailable()))
256
 
      return;
257
 
 
258
 
   QListViewItem *item = new QListViewItem(dlg->m_listPrograms);
259
 
   item->setText(0, prog.name());
260
 
   item->setText(1, prog.comment());
261
 
   item->setText(2, i18n("%1 min.").arg(prog.refresh()));
262
 
 
263
 
   m_programItems.insert(name, item);
264
 
}
265
 
 
266
 
void BGAdvancedDialog::selectProgram(const QString &name)
267
 
{
268
 
   if (m_programItems.find(name))
269
 
   {
270
 
      QListViewItem *item = m_programItems[name];
271
 
      dlg->m_listPrograms->ensureItemVisible(item);
272
 
      dlg->m_listPrograms->setSelected(item, true);
273
 
      m_selectedProgram = name;
274
 
   }
275
 
}
276
 
 
277
 
void BGAdvancedDialog::slotAdd()
278
 
{
279
 
   KProgramEditDialog dlg;
280
 
   dlg.exec();
281
 
   if (dlg.result() == QDialog::Accepted)
282
 
   {
283
 
      QString program = dlg.program();
284
 
      addProgram(program);
285
 
      selectProgram(program);
286
 
   }
287
 
}
288
 
 
289
 
void BGAdvancedDialog::slotRemove()
290
 
{
291
 
   if (m_selectedProgram.isEmpty())
292
 
      return;
293
 
 
294
 
   KBackgroundProgram prog(m_selectedProgram);
295
 
   if (prog.isGlobal())
296
 
   {
297
 
      KMessageBox::sorry(this,
298
 
                i18n("Unable to remove the program: the program is global "
299
 
                "and can only be removed by the system administrator."),
300
 
                i18n("Cannot Remove Program"));
301
 
      return;
302
 
   }
303
 
   if (KMessageBox::warningContinueCancel(this,
304
 
            i18n("Are you sure you want to remove the program `%1'?")
305
 
            .arg(prog.name()),
306
 
            i18n("Remove Background Program"),
307
 
            i18n("&Remove")) != KMessageBox::Continue)
308
 
      return;
309
 
 
310
 
   prog.remove();
311
 
   removeProgram(m_selectedProgram);
312
 
   m_selectedProgram = QString::null;
313
 
}
314
 
 
315
 
/*
316
 
 * Modify a program.
317
 
 */
318
 
void BGAdvancedDialog::slotModify()
319
 
{
320
 
   if (m_selectedProgram.isEmpty())
321
 
      return;
322
 
 
323
 
   KProgramEditDialog dlg(m_selectedProgram);
324
 
   dlg.exec();
325
 
   if (dlg.result() == QDialog::Accepted)
326
 
   {
327
 
      QString program = dlg.program();
328
 
      if (program != m_selectedProgram)
329
 
      {
330
 
         KBackgroundProgram prog(m_selectedProgram);
331
 
         prog.remove();
332
 
         removeProgram(m_selectedProgram);
333
 
      }
334
 
      addProgram(dlg.program());
335
 
      selectProgram(dlg.program());
336
 
   }
337
 
}
338
 
 
339
 
void BGAdvancedDialog::slotProgramItemClicked(QListViewItem *item)
340
 
{
341
 
   if (item)
342
 
      m_selectedProgram = item->text(0);
343
 
   slotProgramChanged();
344
 
}
345
 
 
346
 
void BGAdvancedDialog::slotProgramItemDoubleClicked(QListViewItem *item)
347
 
{
348
 
   slotProgramItemClicked(item);
349
 
   slotModify();
350
 
}
351
 
 
352
 
void BGAdvancedDialog::slotProgramChanged()
353
 
{
354
 
   if (dlg->m_cbProgram->isChecked() && !m_selectedProgram.isEmpty())
355
 
      m_backgroundMode = KBackgroundSettings::Program;
356
 
   else
357
 
      m_backgroundMode = m_oldBackgroundMode;
358
 
}
359
 
 
360
 
void BGAdvancedDialog::slotEnableProgram(bool b)
361
 
{
362
 
   dlg->m_listPrograms->setEnabled(b);
363
 
   if (b)
364
 
   {
365
 
      dlg->m_listPrograms->blockSignals(true);
366
 
      QListViewItem *cur = dlg->m_listPrograms->currentItem();
367
 
      dlg->m_listPrograms->setSelected(cur, true);
368
 
      dlg->m_listPrograms->ensureItemVisible(cur);
369
 
      dlg->m_listPrograms->blockSignals(false);
370
 
      slotProgramItemClicked(cur);
371
 
   }
372
 
   else
373
 
   {
374
 
      slotProgramChanged();
375
 
   }
376
 
}
377
 
 
378
 
QString BGAdvancedDialog::backgroundProgram() const
379
 
{
380
 
   return m_selectedProgram;
381
 
}
382
 
 
383
 
int BGAdvancedDialog::backgroundMode() const
384
 
{
385
 
   return m_backgroundMode;
386
 
}
387
 
 
388
 
/**** KProgramEditDialog ****/
389
 
 
390
 
KProgramEditDialog::KProgramEditDialog(const QString &program, QWidget *parent, char *name)
391
 
    : KDialogBase(parent, name, true, i18n("Configure Background Program"),
392
 
        Ok | Cancel, Ok, true)
393
 
{
394
 
    QFrame *frame = makeMainWidget();
395
 
 
396
 
    QGridLayout *grid = new QGridLayout(frame, 6, 2, 0, spacingHint());
397
 
    grid->addColSpacing(1, 300);
398
 
 
399
 
    QLabel *lbl = new QLabel(i18n("&Name:"), frame);
400
 
    grid->addWidget(lbl, 0, 0);
401
 
    m_NameEdit = new QLineEdit(frame);
402
 
    lbl->setBuddy(m_NameEdit);
403
 
    grid->addWidget(m_NameEdit, 0, 1);
404
 
 
405
 
    lbl = new QLabel(i18n("Co&mment:"), frame);
406
 
    grid->addWidget(lbl, 1, 0);
407
 
    m_CommentEdit = new QLineEdit(frame);
408
 
    lbl->setBuddy(m_CommentEdit);
409
 
    grid->addWidget(m_CommentEdit, 1, 1);
410
 
 
411
 
    lbl = new QLabel(i18n("Comman&d:"), frame);
412
 
    grid->addWidget(lbl, 2, 0);
413
 
    m_CommandEdit = new QLineEdit(frame);
414
 
    lbl->setBuddy(m_CommandEdit);
415
 
    grid->addWidget(m_CommandEdit, 2, 1);
416
 
 
417
 
    lbl = new QLabel(i18n("&Preview cmd:"), frame);
418
 
    grid->addWidget(lbl, 3, 0);
419
 
    m_PreviewEdit = new QLineEdit(frame);
420
 
    lbl->setBuddy(m_PreviewEdit);
421
 
    grid->addWidget(m_PreviewEdit, 3, 1);
422
 
 
423
 
    lbl = new QLabel(i18n("&Executable:"), frame);
424
 
    grid->addWidget(lbl, 4, 0);
425
 
    m_ExecEdit = new QLineEdit(frame);
426
 
    lbl->setBuddy(m_ExecEdit);
427
 
    grid->addWidget(m_ExecEdit, 4, 1);
428
 
 
429
 
    lbl = new QLabel(i18n("&Refresh time:"), frame);
430
 
    grid->addWidget(lbl, 5, 0);
431
 
    m_RefreshEdit = new QSpinBox(frame);
432
 
    m_RefreshEdit->setRange(5, 60);
433
 
    m_RefreshEdit->setSteps(5, 10);
434
 
    m_RefreshEdit->setSuffix(i18n(" min"));
435
 
    m_RefreshEdit->setFixedSize(m_RefreshEdit->sizeHint());
436
 
    lbl->setBuddy(m_RefreshEdit);
437
 
    grid->addWidget(m_RefreshEdit, 5, 1, AlignLeft);
438
 
 
439
 
    m_Program = program;
440
 
    if (m_Program.isEmpty()) {
441
 
        KBackgroundProgram prog(i18n("New Command"));
442
 
        int i = 1;
443
 
        while (!prog.command().isEmpty())
444
 
            prog.load(i18n("New Command <%1>").arg(i++));
445
 
        m_NameEdit->setText(prog.name());
446
 
        m_NameEdit->setSelection(0, 100);
447
 
        m_RefreshEdit->setValue(15);
448
 
        return;
449
 
    }
450
 
 
451
 
    // Fill in the fields
452
 
    m_NameEdit->setText(m_Program);
453
 
    KBackgroundProgram prog(m_Program);
454
 
    m_CommentEdit->setText(prog.comment());
455
 
    m_ExecEdit->setText(prog.executable());
456
 
    m_CommandEdit->setText(prog.command());
457
 
    m_PreviewEdit->setText(prog.previewCommand());
458
 
    m_RefreshEdit->setValue(prog.refresh());
459
 
}
460
 
 
461
 
 
462
 
QString KProgramEditDialog::program()const
463
 
{
464
 
    return m_NameEdit->text();
465
 
}
466
 
 
467
 
void KProgramEditDialog::slotOk()
468
 
{
469
 
    QString s = m_NameEdit->text();
470
 
    if (s.isEmpty()) {
471
 
        KMessageBox::sorry(this, i18n("You did not fill in the `Name' field.\n"
472
 
                "This is a required field."));
473
 
        return;
474
 
    }
475
 
 
476
 
    KBackgroundProgram prog(s);
477
 
    if ((s != m_Program) && !prog.command().isEmpty()) {
478
 
        int ret = KMessageBox::warningContinueCancel(this,
479
 
            i18n("There is already a program with the name `%1'.\n"
480
 
            "Do you want to overwrite it?").arg(s),QString::null,i18n("Overwrite"));
481
 
        if (ret != KMessageBox::Continue)
482
 
            return;
483
 
    }
484
 
 
485
 
    if (m_ExecEdit->text().isEmpty()) {
486
 
        KMessageBox::sorry(this, i18n("You did not fill in the `Executable' field.\n"
487
 
                "This is a required field."));
488
 
        return;
489
 
    }
490
 
    if (m_CommandEdit->text().isEmpty()) {
491
 
        KMessageBox::sorry(this, i18n("You did not fill in the `Command' field.\n"
492
 
                "This is a required field."));
493
 
        return;
494
 
    }
495
 
 
496
 
    prog.setComment(m_CommentEdit->text());
497
 
    prog.setExecutable(m_ExecEdit->text());
498
 
    prog.setCommand(m_CommandEdit->text());
499
 
    prog.setPreviewCommand(m_PreviewEdit->text());
500
 
    prog.setRefresh(m_RefreshEdit->value());
501
 
 
502
 
    prog.writeSettings();
503
 
    accept();
504
 
}
505
 
 
506
 
 
507
 
#include "bgadvanced.moc"