TDE base libraries and programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.0 KiB

  1. /* vi: ts=8 sts=4 sw=4
  2. This file is part of the KDE project, module kcmbackground.
  3. Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
  4. Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. version 2 as published by the Free Software Foundation.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <config.h>
  18. #include <tqcheckbox.h>
  19. #include <tqevent.h>
  20. #include <tqpushbutton.h>
  21. #include <tqspinbox.h>
  22. #include <tdefiledialog.h>
  23. #include <kimageio.h>
  24. #include <tdelocale.h>
  25. #include <kstandarddirs.h>
  26. #include <kurldrag.h>
  27. #include "bgsettings.h"
  28. #include "bgwallpaper.h"
  29. #include "bgwallpaper_ui.h"
  30. /**** BGMultiWallpaperList ****/
  31. BGMultiWallpaperList::BGMultiWallpaperList(TQWidget *parent, const char *name)
  32. : TQListBox(parent, name)
  33. {
  34. setAcceptDrops(true);
  35. setSelectionMode(TQListBox::Extended);
  36. }
  37. void BGMultiWallpaperList::dragEnterEvent(TQDragEnterEvent *ev)
  38. {
  39. ev->accept(KURLDrag::canDecode(ev));
  40. }
  41. void BGMultiWallpaperList::dropEvent(TQDropEvent *ev)
  42. {
  43. TQStringList files;
  44. KURL::List urls;
  45. KURLDrag::decode(ev, urls);
  46. for(KURL::List::ConstIterator it = urls.begin();
  47. it != urls.end(); ++it)
  48. {
  49. // TODO: Download remote files
  50. if ((*it).isLocalFile())
  51. files.append((*it).path());
  52. }
  53. insertStringList(files);
  54. }
  55. bool BGMultiWallpaperList::hasSelection()
  56. {
  57. for ( unsigned i = 0; i < count(); i++)
  58. {
  59. if ( item( i ) && item( i )->isSelected() )
  60. return true;
  61. }
  62. return false;
  63. }
  64. void BGMultiWallpaperList::ensureSelectionVisible()
  65. {
  66. for ( int i = topItem(); i < topItem() + numItemsVisible() - 1; i++)
  67. if ( item( i ) && item( i )->isSelected() )
  68. return;
  69. for ( unsigned i = 0; i < count(); i++)
  70. if ( item( i ) && item( i )->isSelected() )
  71. {
  72. setTopItem( i );
  73. return;
  74. }
  75. }
  76. /**** BGMultiWallpaperDialog ****/
  77. BGMultiWallpaperDialog::BGMultiWallpaperDialog(KBackgroundSettings *settings,
  78. TQWidget *parent, const char *name)
  79. : KDialogBase(parent, name, true, i18n("Setup Slide Show"),
  80. Ok | Cancel, Ok, true), m_pSettings(settings)
  81. {
  82. dlg = new BGMultiWallpaperBase(this);
  83. setMainWidget(dlg);
  84. dlg->m_spinInterval->setRange(1, 99999);
  85. dlg->m_spinInterval->setSteps(1, 15);
  86. dlg->m_spinInterval->setSuffix(i18n(" min"));
  87. // Load
  88. dlg->m_spinInterval->setValue(TQMAX(1,m_pSettings->wallpaperChangeInterval()));
  89. dlg->m_listImages->insertStringList(m_pSettings->wallpaperList());
  90. if (m_pSettings->multiWallpaperMode() == KBackgroundSettings::Random)
  91. dlg->m_cbRandom->setChecked(true);
  92. connect(dlg->m_buttonAdd, TQT_SIGNAL(clicked()), TQT_SLOT(slotAdd()));
  93. connect(dlg->m_buttonRemove, TQT_SIGNAL(clicked()), TQT_SLOT(slotRemove()));
  94. connect(dlg->m_buttonMoveUp, TQT_SIGNAL(clicked()), TQT_SLOT(slotMoveUp()));
  95. connect(dlg->m_buttonMoveDown, TQT_SIGNAL(clicked()), TQT_SLOT(slotMoveDown()));
  96. connect(dlg->m_listImages, TQT_SIGNAL(clicked ( TQListBoxItem * )), TQT_SLOT(slotItemSelected( TQListBoxItem *)));
  97. dlg->m_buttonRemove->setEnabled( false );
  98. dlg->m_buttonMoveUp->setEnabled( false );
  99. dlg->m_buttonMoveDown->setEnabled( false );
  100. }
  101. void BGMultiWallpaperDialog::slotItemSelected( TQListBoxItem * )
  102. {
  103. dlg->m_buttonRemove->setEnabled( dlg->m_listImages->hasSelection() );
  104. setEnabledMoveButtons();
  105. }
  106. void BGMultiWallpaperDialog::setEnabledMoveButtons()
  107. {
  108. bool hasSelection = dlg->m_listImages->hasSelection();
  109. TQListBoxItem * item;
  110. item = dlg->m_listImages->firstItem();
  111. dlg->m_buttonMoveUp->setEnabled( hasSelection && item && !item->isSelected() );
  112. item = dlg->m_listImages->item( dlg->m_listImages->count() - 1 );
  113. dlg->m_buttonMoveDown->setEnabled( hasSelection && item && !item->isSelected() );
  114. }
  115. void BGMultiWallpaperDialog::slotAdd()
  116. {
  117. TQStringList mimeTypes = KImageIO::mimeTypes( KImageIO::Reading );
  118. #ifdef HAVE_LIBART
  119. mimeTypes += "image/svg+xml";
  120. #endif
  121. KFileDialog fileDialog(TDEGlobal::dirs()->findDirs("wallpaper", "").first(),
  122. mimeTypes.join( " " ), this,
  123. 0L, true);
  124. fileDialog.setCaption(i18n("Select Image"));
  125. KFile::Mode mode = static_cast<KFile::Mode> (KFile::Files |
  126. KFile::Directory |
  127. KFile::ExistingOnly |
  128. KFile::LocalOnly);
  129. fileDialog.setMode(mode);
  130. fileDialog.exec();
  131. TQStringList files = fileDialog.selectedFiles();
  132. if (files.isEmpty())
  133. return;
  134. dlg->m_listImages->insertStringList(files);
  135. }
  136. void BGMultiWallpaperDialog::slotRemove()
  137. {
  138. int current = -1;
  139. for ( unsigned i = 0; i < dlg->m_listImages->count();)
  140. {
  141. TQListBoxItem * item = dlg->m_listImages->item( i );
  142. if ( item && item->isSelected())
  143. {
  144. dlg->m_listImages->removeItem(i);
  145. if (current == -1)
  146. current = i;
  147. }
  148. else
  149. i++;
  150. }
  151. if ((current != -1) && (current < (signed)dlg->m_listImages->count()))
  152. dlg->m_listImages->setSelected(current, true);
  153. dlg->m_buttonRemove->setEnabled(dlg->m_listImages->hasSelection());
  154. setEnabledMoveButtons();
  155. }
  156. void BGMultiWallpaperDialog::slotMoveUp()
  157. {
  158. for ( unsigned i = 1; i < dlg->m_listImages->count(); i++)
  159. {
  160. TQListBoxItem * item = dlg->m_listImages->item( i );
  161. if ( item && item->isSelected() )
  162. {
  163. dlg->m_listImages->takeItem( item );
  164. dlg->m_listImages->insertItem( item, i - 1 );
  165. }
  166. }
  167. dlg->m_listImages->ensureSelectionVisible();
  168. setEnabledMoveButtons();
  169. }
  170. void BGMultiWallpaperDialog::slotMoveDown()
  171. {
  172. for ( unsigned i = dlg->m_listImages->count() - 1; i > 0; i--)
  173. {
  174. TQListBoxItem * item = dlg->m_listImages->item( i - 1 );
  175. if ( item && item->isSelected())
  176. {
  177. dlg->m_listImages->takeItem( item );
  178. dlg->m_listImages->insertItem( item, i );
  179. }
  180. }
  181. dlg->m_listImages->ensureSelectionVisible();
  182. setEnabledMoveButtons();
  183. }
  184. void BGMultiWallpaperDialog::slotOk()
  185. {
  186. TQStringList lst;
  187. for (unsigned i=0; i < dlg->m_listImages->count(); i++)
  188. lst.append(dlg->m_listImages->text(i));
  189. m_pSettings->setWallpaperList(lst);
  190. m_pSettings->setWallpaperChangeInterval(dlg->m_spinInterval->value());
  191. if (dlg->m_cbRandom->isChecked())
  192. m_pSettings->setMultiWallpaperMode(KBackgroundSettings::Random);
  193. else
  194. m_pSettings->setMultiWallpaperMode(KBackgroundSettings::InOrder);
  195. accept();
  196. }
  197. #include "bgwallpaper.moc"