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.
23 #include <config-workspace.h>
27 #include <QPushButton>
30 #include <QDragEnterEvent>
33 #include <kfiledialog.h>
36 #include <kstandarddirs.h>
37 #include <k3urldrag.h>
39 #include "bgsettings.h"
40 #include "bgwallpaper.h"
41 #include "ui_bgwallpaper_ui.h"
44 /**** BGMultiWallpaperList ****/
46 class BGMultiWallpaperBase
: public QWidget
, public Ui::BGMultiWallpaperBase
49 BGMultiWallpaperBase( QWidget
*parent
) : QWidget( parent
) {
54 BGMultiWallpaperList::BGMultiWallpaperList(QWidget
*parent
, const char *name
)
59 setSelectionMode(QListWidget::ExtendedSelection
);
63 void BGMultiWallpaperList::dragEnterEvent(QDragEnterEvent
*ev
)
65 ev
->setAccepted(K3URLDrag::canDecode(ev
));
69 void BGMultiWallpaperList::dropEvent(QDropEvent
*ev
)
73 K3URLDrag::decode(ev
, urls
);
74 for(KUrl::List::ConstIterator it
= urls
.constBegin();
75 it
!= urls
.constEnd(); ++it
)
77 // TODO: Download remote files
78 if ((*it
).isLocalFile())
79 files
.append((*it
).path());
84 bool BGMultiWallpaperList::hasSelection()
86 for ( int i
= 0; i
< count(); i
++)
88 if ( item( i
) && item( i
)->isSelected() )
94 void BGMultiWallpaperList::ensureSelectionVisible()
96 // KDE 4 - I believe the three lines below are redundant
98 //for ( int i = topItem(); i < topItem() + numItemsVisible() - 1; i++)
99 // if ( item( i ) && item( i )->isSelected() )
102 for ( int i
= 0; i
< count(); i
++)
103 if ( item( i
) && item( i
)->isSelected() )
105 scrollToItem( item(i
) , QAbstractItemView::PositionAtTop
);
110 /**** BGMultiWallpaperDialog ****/
112 BGMultiWallpaperDialog::BGMultiWallpaperDialog(KBackgroundSettings
*settings
,
113 QWidget
*parent
, const char *name
)
114 : KDialog( parent
), m_pSettings(settings
)
116 setObjectName( name
);
118 setCaption( i18n("Setup Slide Show") );
119 setButtons( Ok
| Cancel
);
120 showButtonSeparator( true );
122 dlg
= new BGMultiWallpaperBase(this);
125 dlg
->m_spinInterval
->setRange(1, 99999);
126 dlg
->m_spinInterval
->setSingleStep(15);
127 dlg
->m_spinInterval
->setSuffix(i18n(" min"));
130 dlg
->m_spinInterval
->setValue(qMax(1,m_pSettings
->wallpaperChangeInterval()));
132 dlg
->m_listImages
->addItems(m_pSettings
->wallpaperList());
134 if (m_pSettings
->multiWallpaperMode() == KBackgroundSettings::Random
)
135 dlg
->m_cbRandom
->setChecked(true);
137 connect(dlg
->m_buttonAdd
, SIGNAL(clicked()), SLOT(slotAdd()));
138 connect(dlg
->m_buttonRemove
, SIGNAL(clicked()), SLOT(slotRemove()));
139 connect(dlg
->m_buttonMoveUp
, SIGNAL(clicked()), SLOT(slotMoveUp()));
140 connect(dlg
->m_buttonMoveDown
, SIGNAL(clicked()), SLOT(slotMoveDown()));
141 connect(dlg
->m_listImages
, SIGNAL(itemClicked ( QListWidgetItem
* )), SLOT(slotItemSelected( QListWidgetItem
*)));
142 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
143 dlg
->m_buttonRemove
->setEnabled( false );
144 dlg
->m_buttonMoveUp
->setEnabled( false );
145 dlg
->m_buttonMoveDown
->setEnabled( false );
149 void BGMultiWallpaperDialog::slotItemSelected( QListWidgetItem
* )
151 dlg
->m_buttonRemove
->setEnabled( dlg
->m_listImages
->hasSelection() );
152 setEnabledMoveButtons();
155 void BGMultiWallpaperDialog::setEnabledMoveButtons()
157 bool hasSelection
= dlg
->m_listImages
->hasSelection();
158 QListWidgetItem
* item
;
160 item
= dlg
->m_listImages
->item(0);
161 dlg
->m_buttonMoveUp
->setEnabled( hasSelection
&& item
&& !item
->isSelected() );
162 item
= dlg
->m_listImages
->item( dlg
->m_listImages
->count() - 1 );
163 dlg
->m_buttonMoveDown
->setEnabled( hasSelection
&& item
&& !item
->isSelected() );
166 void BGMultiWallpaperDialog::slotAdd()
168 QStringList mimeTypes
= KImageIO::mimeTypes( KImageIO::Reading
);
169 mimeTypes
+= "image/svg+xml";
171 QStringList lstWallpaper
= KGlobal::dirs()->findDirs("wallpaper", "");
172 KFileDialog
fileDialog((lstWallpaper
.count()>0) ? lstWallpaper
.first() : QString(),
173 mimeTypes
.join( " " ), this);
175 fileDialog
.setCaption(i18n("Select Image"));
176 KFile::Modes mode
= KFile::Files
|
178 KFile::ExistingOnly
|
180 fileDialog
.setMode(mode
);
182 QStringList files
= fileDialog
.selectedFiles();
186 dlg
->m_listImages
->addItems(files
);
189 void BGMultiWallpaperDialog::slotRemove()
192 for ( int i
= 0; i
< dlg
->m_listImages
->count();)
194 QListWidgetItem
* item
= dlg
->m_listImages
->item( i
);
195 if ( item
&& item
->isSelected())
197 delete dlg
->m_listImages
->takeItem(i
);
204 if ((current
!= -1) && (current
< (signed)dlg
->m_listImages
->count()))
205 dlg
->m_listImages
->item(current
)->setSelected(true);
207 dlg
->m_buttonRemove
->setEnabled(dlg
->m_listImages
->hasSelection());
209 setEnabledMoveButtons();
212 void BGMultiWallpaperDialog::slotMoveUp()
214 for ( int i
= 1; i
< dlg
->m_listImages
->count(); i
++)
216 QListWidgetItem
* item
= dlg
->m_listImages
->item( i
);
217 if ( item
&& item
->isSelected() )
219 dlg
->m_listImages
->takeItem( i
);
220 dlg
->m_listImages
->insertItem( i
- 1 , item
);
223 dlg
->m_listImages
->ensureSelectionVisible();
224 setEnabledMoveButtons();
227 void BGMultiWallpaperDialog::slotMoveDown()
229 for ( int i
= dlg
->m_listImages
->count() - 1; i
> 0; i
--)
231 QListWidgetItem
* item
= dlg
->m_listImages
->item( i
- 1 );
232 if ( item
&& item
->isSelected())
234 dlg
->m_listImages
->takeItem( i
- 1 );
235 dlg
->m_listImages
->insertItem( i
, item
);
238 dlg
->m_listImages
->ensureSelectionVisible();
239 setEnabledMoveButtons();
242 void BGMultiWallpaperDialog::slotOk()
245 for ( int i
=0; i
< dlg
->m_listImages
->count(); i
++)
246 lst
.append(dlg
->m_listImages
->item(i
)->text());
247 m_pSettings
->setWallpaperList(lst
);
248 m_pSettings
->setWallpaperChangeInterval(dlg
->m_spinInterval
->value());
249 if (dlg
->m_cbRandom
->isChecked())
250 m_pSettings
->setMultiWallpaperMode(KBackgroundSettings::Random
);
252 m_pSettings
->setMultiWallpaperMode(KBackgroundSettings::InOrder
);
258 #include "bgwallpaper.moc"