~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kdm/kcm/background/bgadvanced.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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 <QCheckBox>
 
24
#include <QHeaderView>
 
25
#include <QLabel>
 
26
#include <QLineEdit>
 
27
#include <QPushButton>
 
28
#include <QSpinBox>
 
29
#include <QFrame>
 
30
#include <QGridLayout>
 
31
 
 
32
#include <kconfig.h>
 
33
#include <kcolorbutton.h>
 
34
#include <klocale.h>
 
35
#include <kmessagebox.h>
 
36
#include <kstandarddirs.h>
 
37
 
 
38
#include "bgrender.h"
 
39
#include "bgadvanced.h"
 
40
 
 
41
#include <X11/Xlib.h>
 
42
#include <fixx11h.h>
 
43
 
 
44
/**** BGAdvancedDialog ****/
 
45
 
 
46
BGAdvancedDialog::BGAdvancedDialog(KBackgroundRenderer *_r,
 
47
                                   QWidget *parent)
 
48
    : KDialog(parent),
 
49
      r(_r)
 
50
{
 
51
    setObjectName("BGAdvancedDialog");
 
52
    setModal(true);
 
53
    setCaption(i18n("Advanced Background Settings"));
 
54
    setButtons(Ok | Cancel);
 
55
 
 
56
    dlg = new BGAdvancedBase(this);
 
57
    setMainWidget(dlg);
 
58
 
 
59
    dlg->m_listPrograms->header()->setResizeMode(1, QHeaderView::Stretch);
 
60
    dlg->m_listPrograms->setRootIsDecorated(false);
 
61
    dlg->m_listPrograms->setAllColumnsShowFocus(true);
 
62
 
 
63
    connect(dlg->m_listPrograms, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
 
64
            SLOT(slotProgramItemClicked(QTreeWidgetItem *)));
 
65
 
 
66
    // Load programs
 
67
    const QStringList lst = KBackgroundProgram::list();
 
68
    QStringList::const_iterator it;
 
69
    for (it = lst.constBegin(); it != lst.constEnd(); ++it)
 
70
        addProgram(*it);
 
71
 
 
72
    // FIXME: support it proper
 
73
    dlg->m_buttonAdd->hide();
 
74
    dlg->m_buttonRemove->hide();
 
75
    dlg->m_buttonModify->hide();
 
76
    dlg->m_groupCache->hide();
 
77
 
 
78
    connect(dlg->m_cbProgram, SIGNAL(toggled(bool)),
 
79
            SLOT(slotEnableProgram(bool)));
 
80
 
 
81
    m_oldBackgroundMode = r->backgroundMode();
 
82
    if (m_oldBackgroundMode == KBackgroundSettings::Program)
 
83
        m_oldBackgroundMode = KBackgroundSettings::Flat;
 
84
 
 
85
    dlg->adjustSize();
 
86
    updateUI();
 
87
}
 
88
 
 
89
void BGAdvancedDialog::makeReadOnly()
 
90
{
 
91
    dlg->m_cbProgram->setEnabled(false);
 
92
    dlg->m_listPrograms->setEnabled(false);
 
93
}
 
94
 
 
95
#if 0
 
96
void BGAdvancedDialog::setCacheSize(int s)
 
97
{
 
98
    dlg->m_spinCache->setValue(s);
 
99
}
 
100
 
 
101
int BGAdvancedDialog::cacheSize()
 
102
{
 
103
    return dlg->m_spinCache->value();
 
104
}
 
105
 
 
106
QColor BGAdvancedDialog::textColor()
 
107
{
 
108
    return dlg->m_colorText->color();
 
109
}
 
110
 
 
111
void BGAdvancedDialog::setTextColor(const QColor &color)
 
112
{
 
113
    dlg->m_colorText->setColor(color);
 
114
}
 
115
 
 
116
QColor BGAdvancedDialog::textBackgroundColor()
 
117
{
 
118
    return dlg->m_cbSolidTextBackground->isChecked() ?
 
119
           dlg->m_colorTextBackground->color() : QColor();
 
120
}
 
121
 
 
122
void BGAdvancedDialog::setTextBackgroundColor(const QColor &color)
 
123
{
 
124
    dlg->m_colorTextBackground->blockSignals(true);
 
125
    dlg->m_cbSolidTextBackground->blockSignals(true);
 
126
    if (color.isValid()) {
 
127
        dlg->m_cbSolidTextBackground->setChecked(true);
 
128
        dlg->m_colorTextBackground->setColor(color);
 
129
        dlg->m_colorTextBackground->setEnabled(true);
 
130
    } else {
 
131
        dlg->m_cbSolidTextBackground->setChecked(false);
 
132
        dlg->m_colorTextBackground->setColor(Qt::white);
 
133
        dlg->m_colorTextBackground->setEnabled(false);
 
134
    }
 
135
    dlg->m_colorTextBackground->blockSignals(false);
 
136
    dlg->m_cbSolidTextBackground->blockSignals(false);
 
137
}
 
138
 
 
139
bool BGAdvancedDialog::shadowEnabled()
 
140
{
 
141
    return dlg->m_cbShadow->isChecked();
 
142
}
 
143
 
 
144
void BGAdvancedDialog::setShadowEnabled(bool enabled)
 
145
{
 
146
    dlg->m_cbShadow->setChecked(enabled);
 
147
}
 
148
 
 
149
void BGAdvancedDialog::setTextLines(int lines)
 
150
{
 
151
    dlg->m_spinTextLines->setValue(lines);
 
152
}
 
153
 
 
154
int BGAdvancedDialog::textLines() const
 
155
{
 
156
    return dlg->m_spinTextLines->value();
 
157
}
 
158
 
 
159
void BGAdvancedDialog::setTextWidth(int width)
 
160
{
 
161
    dlg->m_spinTextWidth->setValue(width);
 
162
}
 
163
 
 
164
int BGAdvancedDialog::textWidth() const
 
165
{
 
166
    return dlg->m_spinTextWidth->value();
 
167
}
 
168
#endif
 
169
 
 
170
void BGAdvancedDialog::updateUI()
 
171
{
 
172
    QString prog = r->KBackgroundProgram::name();
 
173
 
 
174
    dlg->m_cbProgram->blockSignals(true);
 
175
    if ((r->backgroundMode() == KBackgroundSettings::Program)
 
176
            && !prog.isEmpty()) {
 
177
        dlg->m_cbProgram->setChecked(true);
 
178
        dlg->m_listPrograms->setEnabled(true);
 
179
        dlg->m_buttonAdd->setEnabled(true);
 
180
        dlg->m_buttonRemove->setEnabled(true);
 
181
        dlg->m_buttonModify->setEnabled(true);
 
182
        selectProgram(prog);
 
183
    } else {
 
184
        dlg->m_cbProgram->setChecked(false);
 
185
        dlg->m_listPrograms->setEnabled(false);
 
186
        dlg->m_buttonAdd->setEnabled(false);
 
187
        dlg->m_buttonRemove->setEnabled(false);
 
188
        dlg->m_buttonModify->setEnabled(false);
 
189
    }
 
190
    dlg->m_cbProgram->blockSignals(false);
 
191
}
 
192
 
 
193
#if 0
 
194
void BGAdvancedDialog::removeProgram(const QString &name)
 
195
{
 
196
    delete m_programItems.take(name);
 
197
}
 
198
#endif
 
199
 
 
200
void BGAdvancedDialog::addProgram(const QString &name)
 
201
{
 
202
#if 0
 
203
    removeProgram(name);
 
204
#endif
 
205
 
 
206
    KBackgroundProgram prog(name);
 
207
    if (prog.command().isEmpty() || (prog.isGlobal() && !prog.isAvailable()))
 
208
        return;
 
209
 
 
210
    QTreeWidgetItem *item = new QTreeWidgetItem(dlg->m_listPrograms);
 
211
    item->setText(0, prog.name());
 
212
    item->setText(1, prog.comment());
 
213
    item->setText(2, i18n("%1 min.", prog.refresh()));
 
214
 
 
215
    m_programItems.insert(name, item);
 
216
}
 
217
 
 
218
void BGAdvancedDialog::selectProgram(const QString &name)
 
219
{
 
220
    if (QTreeWidgetItem *item = m_programItems[name]) {
 
221
        dlg->m_listPrograms->scrollToItem(item);
 
222
        item->setSelected(true);
 
223
        m_selectedProgram = name;
 
224
    }
 
225
}
 
226
 
 
227
#if 0
 
228
void BGAdvancedDialog::slotAdd()
 
229
{
 
230
    KProgramEditDialog dlg(QString(), this);
 
231
    dlg.exec();
 
232
    if (dlg.result() == QDialog::Accepted) {
 
233
        QString program = dlg.program();
 
234
        addProgram(program);
 
235
        selectProgram(program);
 
236
    }
 
237
}
 
238
 
 
239
void BGAdvancedDialog::slotRemove()
 
240
{
 
241
    if (m_selectedProgram.isEmpty())
 
242
        return;
 
243
 
 
244
    KBackgroundProgram prog(m_selectedProgram);
 
245
    if (prog.isGlobal()) {
 
246
        KMessageBox::sorry(this,
 
247
           i18n("Unable to remove the program: the program is global "
 
248
                "and can only be removed by the system administrator."),
 
249
           i18n("Cannot Remove Program"));
 
250
        return;
 
251
    }
 
252
    if (KMessageBox::warningContinueCancel(this,
 
253
           i18n("Are you sure you want to remove the program `%1'?",
 
254
                prog.name()),
 
255
           i18n("Remove Background Program"),
 
256
           KGuiItem(i18n("&Remove"))) != KMessageBox::Continue)
 
257
        return;
 
258
 
 
259
    prog.remove();
 
260
    removeProgram(m_selectedProgram);
 
261
    m_selectedProgram.clear();
 
262
}
 
263
 
 
264
/*
 
265
 * Modify a program.
 
266
 */
 
267
void BGAdvancedDialog::slotModify()
 
268
{
 
269
    if (m_selectedProgram.isEmpty())
 
270
        return;
 
271
 
 
272
    KProgramEditDialog dlg(m_selectedProgram, this);
 
273
    dlg.exec();
 
274
    if (dlg.result() == QDialog::Accepted) {
 
275
        QString program = dlg.program();
 
276
        if (program != m_selectedProgram) {
 
277
            KBackgroundProgram prog(m_selectedProgram);
 
278
            prog.remove();
 
279
            removeProgram(m_selectedProgram);
 
280
        }
 
281
        addProgram(dlg.program());
 
282
        selectProgram(dlg.program());
 
283
    }
 
284
}
 
285
#endif
 
286
 
 
287
void BGAdvancedDialog::slotProgramItemClicked(QTreeWidgetItem *item)
 
288
{
 
289
    if (item)
 
290
        m_selectedProgram = item->text(0);
 
291
    slotProgramChanged();
 
292
}
 
293
 
 
294
#if 0
 
295
void BGAdvancedDialog::slotProgramItemDoubleClicked(QTreeWidgetItem *item)
 
296
{
 
297
    slotProgramItemClicked(item);
 
298
    slotModify();
 
299
}
 
300
#endif
 
301
 
 
302
void BGAdvancedDialog::slotProgramChanged()
 
303
{
 
304
//   r->stop();
 
305
 
 
306
    r->setProgram(m_selectedProgram);
 
307
    if (dlg->m_cbProgram->isChecked() && !m_selectedProgram.isEmpty())
 
308
        r->setBackgroundMode(KBackgroundSettings::Program);
 
309
    else
 
310
        r->setBackgroundMode(m_oldBackgroundMode);
 
311
 
 
312
// We have no preview, no need to start the renderer
 
313
//   r->start();
 
314
}
 
315
 
 
316
void BGAdvancedDialog::slotEnableProgram(bool b)
 
317
{
 
318
    dlg->m_listPrograms->setEnabled(b);
 
319
    if (b) {
 
320
        if (QTreeWidgetItem *cur = dlg->m_listPrograms->currentItem()) {
 
321
            dlg->m_listPrograms->blockSignals(true);
 
322
            cur->setSelected(true);
 
323
            dlg->m_listPrograms->scrollToItem(cur);
 
324
            dlg->m_listPrograms->blockSignals(false);
 
325
            slotProgramItemClicked(cur);
 
326
        }
 
327
    } else {
 
328
        slotProgramChanged();
 
329
    }
 
330
}
 
331
 
 
332
/**** KProgramEditDialog ****/
 
333
#if 0
 
334
KProgramEditDialog::KProgramEditDialog(const QString &program, QWidget *parent, char *name)
 
335
    : KDialog(parent)
 
336
{
 
337
    setObjectName(name);
 
338
    setModal(true);
 
339
    setCaption(i18n("Configure Background Program"));
 
340
    setButtons(Ok | Cancel);
 
341
 
 
342
    QFrame *frame = new QFrame(this);
 
343
    setMainWidget(frame);
 
344
 
 
345
    QGridLayout *grid = new QGridLayout(frame);
 
346
    grid->setSpacing(spacingHint());
 
347
    grid->setMargin(0);
 
348
    grid->setColumnMinimumWidth(1, 300);
 
349
 
 
350
    QLabel *lbl = new QLabel(i18n("&Name:"), frame);
 
351
    grid->addWidget(lbl, 0, 0);
 
352
    m_NameEdit = new QLineEdit(frame);
 
353
    lbl->setBuddy(m_NameEdit);
 
354
    grid->addWidget(m_NameEdit, 0, 1);
 
355
 
 
356
    lbl = new QLabel(i18n("Co&mment:"), frame);
 
357
    grid->addWidget(lbl, 1, 0);
 
358
    m_CommentEdit = new QLineEdit(frame);
 
359
    lbl->setBuddy(m_CommentEdit);
 
360
    grid->addWidget(m_CommentEdit, 1, 1);
 
361
 
 
362
    lbl = new QLabel(i18n("Comman&d:"), frame);
 
363
    grid->addWidget(lbl, 2, 0);
 
364
    m_CommandEdit = new QLineEdit(frame);
 
365
    lbl->setBuddy(m_CommandEdit);
 
366
    grid->addWidget(m_CommandEdit, 2, 1);
 
367
 
 
368
    lbl = new QLabel(i18n("&Preview cmd:"), frame);
 
369
    grid->addWidget(lbl, 3, 0);
 
370
    m_PreviewEdit = new QLineEdit(frame);
 
371
    lbl->setBuddy(m_PreviewEdit);
 
372
    grid->addWidget(m_PreviewEdit, 3, 1);
 
373
 
 
374
    lbl = new QLabel(i18n("&Executable:"), frame);
 
375
    grid->addWidget(lbl, 4, 0);
 
376
    m_ExecEdit = new QLineEdit(frame);
 
377
    lbl->setBuddy(m_ExecEdit);
 
378
    grid->addWidget(m_ExecEdit, 4, 1);
 
379
 
 
380
    lbl = new QLabel(i18n("&Refresh time:"), frame);
 
381
    grid->addWidget(lbl, 5, 0);
 
382
    m_RefreshEdit = new QSpinBox(frame);
 
383
    m_RefreshEdit->setRange(5, 60);
 
384
    m_RefreshEdit->setSingleStep(5);
 
385
    m_RefreshEdit->setSuffix(i18n(" min"));
 
386
    m_RefreshEdit->setFixedSize(m_RefreshEdit->sizeHint());
 
387
    lbl->setBuddy(m_RefreshEdit);
 
388
    grid->addWidget(m_RefreshEdit, 5, 1, Qt::AlignLeft);
 
389
 
 
390
    m_Program = program;
 
391
    if (m_Program.isEmpty()) {
 
392
        KBackgroundProgram prog(i18n("New Command"));
 
393
        int i = 1;
 
394
        while (!prog.command().isEmpty())
 
395
            prog.load(i18n("New Command <%1>", i++));
 
396
        m_NameEdit->setText(prog.name());
 
397
        m_NameEdit->setSelection(0, 100);
 
398
        m_RefreshEdit->setValue(15);
 
399
        return;
 
400
    }
 
401
 
 
402
    // Fill in the fields
 
403
    m_NameEdit->setText(m_Program);
 
404
    KBackgroundProgram prog(m_Program);
 
405
    m_CommentEdit->setText(prog.comment());
 
406
    m_ExecEdit->setText(prog.executable());
 
407
    m_CommandEdit->setText(prog.command());
 
408
    m_PreviewEdit->setText(prog.previewCommand());
 
409
    m_RefreshEdit->setValue(prog.refresh());
 
410
}
 
411
 
 
412
 
 
413
QString KProgramEditDialog::program()const
 
414
{
 
415
    return m_NameEdit->text();
 
416
}
 
417
 
 
418
void KProgramEditDialog::accept()
 
419
{
 
420
    QString s = m_NameEdit->text();
 
421
    if (s.isEmpty()) {
 
422
        KMessageBox::sorry(this, i18n("You did not fill in the `Name' field.\n"
 
423
                                      "This is a required field."));
 
424
        return;
 
425
    }
 
426
 
 
427
    KBackgroundProgram prog(s);
 
428
    if ((s != m_Program) && !prog.command().isEmpty()) {
 
429
        int ret = KMessageBox::warningContinueCancel(this,
 
430
                  i18n("There is already a program with the name `%1'.\n"
 
431
                       "Do you want to overwrite it?", s), QString(), KGuiItem(i18n("Overwrite")));
 
432
        if (ret != KMessageBox::Continue)
 
433
            return;
 
434
    }
 
435
 
 
436
    if (m_ExecEdit->text().isEmpty()) {
 
437
        KMessageBox::sorry(this, i18n("You did not fill in the `Executable' field.\n"
 
438
                                      "This is a required field."));
 
439
        return;
 
440
    }
 
441
    if (m_CommandEdit->text().isEmpty()) {
 
442
        KMessageBox::sorry(this, i18n("You did not fill in the `Command' field.\n"
 
443
                                      "This is a required field."));
 
444
        return;
 
445
    }
 
446
 
 
447
    prog.setComment(m_CommentEdit->text());
 
448
    prog.setExecutable(m_ExecEdit->text());
 
449
    prog.setCommand(m_CommandEdit->text());
 
450
    prog.setPreviewCommand(m_PreviewEdit->text());
 
451
    prog.setRefresh(m_RefreshEdit->value());
 
452
 
 
453
    prog.writeSettings();
 
454
 
 
455
    KDialog::accept();
 
456
}
 
457
#endif
 
458
 
 
459
#include "bgadvanced.moc"