~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kcontrol/background/bgwallpaper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

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 <config-workspace.h>
 
24
 
 
25
#include <QCheckBox>
 
26
#include <QEvent>
 
27
#include <QPushButton>
 
28
#include <QSpinBox>
 
29
//Added by qt3to4:
 
30
#include <QDragEnterEvent>
 
31
#include <QDropEvent>
 
32
 
 
33
#include <kfiledialog.h>
 
34
#include <kimageio.h>
 
35
#include <klocale.h>
 
36
#include <kstandarddirs.h>
 
37
#include <k3urldrag.h>
 
38
 
 
39
#include "bgsettings.h"
 
40
#include "bgwallpaper.h"
 
41
#include "bgwallpaper_ui.h"
 
42
 
 
43
/**** BGMultiWallpaperList ****/
 
44
 
 
45
BGMultiWallpaperList::BGMultiWallpaperList(QWidget *parent, const char *name)
 
46
        : QListWidget(parent)
 
47
{
 
48
   setObjectName(name);
 
49
   setAcceptDrops(true);
 
50
   setSelectionMode(QListWidget::ExtendedSelection);
 
51
}
 
52
 
 
53
 
 
54
void BGMultiWallpaperList::dragEnterEvent(QDragEnterEvent *ev)
 
55
{
 
56
   ev->setAccepted(K3URLDrag::canDecode(ev));
 
57
}
 
58
 
 
59
 
 
60
void BGMultiWallpaperList::dropEvent(QDropEvent *ev)
 
61
{
 
62
   QStringList files;
 
63
   KUrl::List urls;
 
64
   K3URLDrag::decode(ev, urls);
 
65
   for(KUrl::List::ConstIterator it = urls.begin();
 
66
       it != urls.end(); ++it)
 
67
   {
 
68
      // TODO: Download remote files
 
69
      if ((*it).isLocalFile())
 
70
          files.append((*it).path());
 
71
   }
 
72
   addItems(files);
 
73
}
 
74
 
 
75
bool BGMultiWallpaperList::hasSelection()
 
76
{
 
77
    for ( int i = 0; i < count(); i++)
 
78
    {
 
79
        if ( item( i ) && item( i )->isSelected() )
 
80
            return true;
 
81
    }
 
82
    return false;
 
83
}
 
84
 
 
85
void BGMultiWallpaperList::ensureSelectionVisible()
 
86
{
 
87
    // KDE 4 - I believe the three lines below are redundant
 
88
    //
 
89
    //for ( int i = topItem(); i < topItem() + numItemsVisible() - 1; i++)
 
90
    //    if ( item( i ) && item( i )->isSelected() )
 
91
    //        return;
 
92
 
 
93
    for ( int i = 0; i < count(); i++)
 
94
        if ( item( i ) && item( i )->isSelected() )
 
95
        {
 
96
            scrollToItem( item(i) , QAbstractItemView::PositionAtTop );
 
97
            return;
 
98
        }
 
99
}
 
100
 
 
101
/**** BGMultiWallpaperDialog ****/
 
102
 
 
103
BGMultiWallpaperDialog::BGMultiWallpaperDialog(KBackgroundSettings *settings,
 
104
        QWidget *parent, const char *name)
 
105
        : KDialog( parent ), m_pSettings(settings)
 
106
{
 
107
   setObjectName( name );
 
108
   setModal( true );
 
109
   setCaption( i18n("Setup Slide Show") );
 
110
   setButtons( Ok | Cancel );
 
111
   showButtonSeparator( true );
 
112
 
 
113
   dlg = new BGMultiWallpaperBase(this);
 
114
   setMainWidget(dlg);
 
115
 
 
116
   dlg->m_spinInterval->setRange(1, 99999);
 
117
   dlg->m_spinInterval->setSingleStep(15);
 
118
   dlg->m_spinInterval->setSuffix(i18n(" min"));
 
119
 
 
120
   // Load
 
121
   dlg->m_spinInterval->setValue(qMax(1,m_pSettings->wallpaperChangeInterval()));
 
122
 
 
123
   dlg->m_listImages->addItems(m_pSettings->wallpaperList());
 
124
 
 
125
   if (m_pSettings->multiWallpaperMode() == KBackgroundSettings::Random)
 
126
      dlg->m_cbRandom->setChecked(true);
 
127
 
 
128
   connect(dlg->m_buttonAdd, SIGNAL(clicked()), SLOT(slotAdd()));
 
129
   connect(dlg->m_buttonRemove, SIGNAL(clicked()), SLOT(slotRemove()));
 
130
   connect(dlg->m_buttonMoveUp, SIGNAL(clicked()), SLOT(slotMoveUp()));
 
131
   connect(dlg->m_buttonMoveDown, SIGNAL(clicked()), SLOT(slotMoveDown()));
 
132
   connect(dlg->m_listImages, SIGNAL(itemClicked ( QListWidgetItem * )), SLOT(slotItemSelected( QListWidgetItem *)));
 
133
   connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
 
134
   dlg->m_buttonRemove->setEnabled( false );
 
135
   dlg->m_buttonMoveUp->setEnabled( false );
 
136
   dlg->m_buttonMoveDown->setEnabled( false );
 
137
 
 
138
}
 
139
 
 
140
void BGMultiWallpaperDialog::slotItemSelected( QListWidgetItem * )
 
141
{
 
142
    dlg->m_buttonRemove->setEnabled( dlg->m_listImages->hasSelection() );
 
143
    setEnabledMoveButtons();
 
144
}
 
145
 
 
146
void BGMultiWallpaperDialog::setEnabledMoveButtons()
 
147
{
 
148
    bool hasSelection = dlg->m_listImages->hasSelection();
 
149
    QListWidgetItem * item;
 
150
 
 
151
    item = dlg->m_listImages->item(0);
 
152
    dlg->m_buttonMoveUp->setEnabled( hasSelection && item && !item->isSelected() );
 
153
    item  = dlg->m_listImages->item( dlg->m_listImages->count() - 1 );
 
154
    dlg->m_buttonMoveDown->setEnabled( hasSelection && item && !item->isSelected() );
 
155
}
 
156
 
 
157
void BGMultiWallpaperDialog::slotAdd()
 
158
{
 
159
    QStringList mimeTypes = KImageIO::mimeTypes( KImageIO::Reading );
 
160
    mimeTypes += "image/svg+xml";
 
161
 
 
162
    QStringList lstWallpaper = KGlobal::dirs()->findDirs("wallpaper", "");
 
163
    KFileDialog fileDialog((lstWallpaper.count()>0) ? lstWallpaper.first() : QString(),
 
164
                           mimeTypes.join( " " ), this);
 
165
 
 
166
    fileDialog.setCaption(i18n("Select Image"));
 
167
    KFile::Modes mode = KFile::Files |
 
168
                        KFile::Directory |
 
169
                        KFile::ExistingOnly |
 
170
                        KFile::LocalOnly;
 
171
    fileDialog.setMode(mode);
 
172
    fileDialog.exec();
 
173
    QStringList files = fileDialog.selectedFiles();
 
174
    if (files.isEmpty())
 
175
        return;
 
176
 
 
177
    dlg->m_listImages->addItems(files);
 
178
}
 
179
 
 
180
void BGMultiWallpaperDialog::slotRemove()
 
181
{
 
182
    int current = -1;
 
183
    for ( int i = 0; i < dlg->m_listImages->count();)
 
184
    {
 
185
        QListWidgetItem * item = dlg->m_listImages->item( i );
 
186
        if ( item && item->isSelected())
 
187
        {
 
188
            delete dlg->m_listImages->takeItem(i);
 
189
            if (current == -1)
 
190
               current = i;
 
191
        }
 
192
        else
 
193
            i++;
 
194
    }
 
195
    if ((current != -1) && (current < (signed)dlg->m_listImages->count()))
 
196
       dlg->m_listImages->item(current)->setSelected(true);
 
197
 
 
198
    dlg->m_buttonRemove->setEnabled(dlg->m_listImages->hasSelection());
 
199
 
 
200
    setEnabledMoveButtons();
 
201
}
 
202
 
 
203
void BGMultiWallpaperDialog::slotMoveUp()
 
204
{
 
205
    for ( int i = 1; i < dlg->m_listImages->count(); i++)
 
206
    {
 
207
        QListWidgetItem * item = dlg->m_listImages->item( i );
 
208
        if ( item && item->isSelected() )
 
209
        {
 
210
            dlg->m_listImages->takeItem( i );
 
211
            dlg->m_listImages->insertItem( i - 1 , item );
 
212
        }
 
213
    }
 
214
    dlg->m_listImages->ensureSelectionVisible();
 
215
    setEnabledMoveButtons();
 
216
}
 
217
 
 
218
void BGMultiWallpaperDialog::slotMoveDown()
 
219
{
 
220
    for ( int i = dlg->m_listImages->count() - 1; i > 0; i--)
 
221
    {
 
222
        QListWidgetItem * item = dlg->m_listImages->item( i - 1 );
 
223
        if ( item && item->isSelected())
 
224
        {
 
225
            dlg->m_listImages->takeItem( i - 1 );
 
226
            dlg->m_listImages->insertItem( i , item );
 
227
        }
 
228
    }
 
229
    dlg->m_listImages->ensureSelectionVisible();
 
230
    setEnabledMoveButtons();
 
231
}
 
232
 
 
233
void BGMultiWallpaperDialog::slotOk()
 
234
{
 
235
    QStringList lst;
 
236
    for ( int i=0; i < dlg->m_listImages->count(); i++)
 
237
        lst.append(dlg->m_listImages->item(i)->text());
 
238
    m_pSettings->setWallpaperList(lst);
 
239
    m_pSettings->setWallpaperChangeInterval(dlg->m_spinInterval->value());
 
240
    if (dlg->m_cbRandom->isChecked())
 
241
       m_pSettings->setMultiWallpaperMode(KBackgroundSettings::Random);
 
242
    else
 
243
       m_pSettings->setMultiWallpaperMode(KBackgroundSettings::InOrder);
 
244
 
 
245
    KDialog::accept();
 
246
}
 
247
 
 
248
 
 
249
#include "bgwallpaper.moc"