File indexing completed on 2024-04-28 04:02:30

0001 /***************************************************************************
0002                           cdirlist.cpp  -  pick a list of directories
0003                              -------------------
0004     begin                : So apr 12 2003
0005     copyright            : (C) 2003 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "cdirlist.h"
0019 
0020 #include <QFileDialog>
0021 #include <QGridLayout>
0022 #include <QIcon>
0023 #include <QListWidget>
0024 #include <QPushButton>
0025 
0026 #include <KLocalizedString>
0027 
0028 cDirList::cDirList (QWidget *parent) : QWidget(parent)
0029 {
0030   //prepare layout
0031   QGridLayout *layout = new QGridLayout (this);
0032 
0033   //create all widgets
0034   listbox = new QListWidget (this);
0035   btadd = new QPushButton (i18n ("&Add..."), this);
0036   btdel = new QPushButton (i18n ("Delete"), this);
0037   btup = new QPushButton (QIcon::fromTheme ("go-up"), i18n ("Move &up"), this);
0038   btdown = new QPushButton (QIcon::fromTheme ("go-down"), i18n ("Move &down"), this);
0039 
0040   //add widgets to layout
0041   layout->setSpacing (10);
0042   layout->setRowStretch (4, 10);
0043   layout->addWidget (listbox, 0, 0, 5, 1);
0044   layout->addWidget (btadd, 0, 1);
0045   layout->addWidget (btdel, 1, 1);
0046   layout->addWidget (btup, 2, 1);
0047   layout->addWidget (btdown, 3, 1);
0048   
0049   //establish connections
0050   connect (btadd, &QPushButton::clicked, this, &cDirList::addEntry);
0051   connect (btdel, &QPushButton::clicked, this, &cDirList::removeEntry);
0052   connect (btup, &QPushButton::clicked, this, &cDirList::moveEntryUp);
0053   connect (btdown, &QPushButton::clicked, this, &cDirList::moveEntryDown);
0054   
0055   //update our contents
0056   update ();
0057 }
0058 
0059 cDirList::~cDirList ()
0060 {
0061 
0062 }
0063 
0064 void cDirList::setDirList (const QStringList &dlist)
0065 {
0066   dirlist = dlist;
0067   update ();
0068 }
0069 
0070 void cDirList::addEntry ()
0071 {
0072   QString dirName = QFileDialog::getExistingDirectory (this, i18n ("Choose sound directory"));
0073   if (dirName.isEmpty()) return;
0074 
0075   dirlist.append (dirName);
0076   update();
0077 }
0078 
0079 void cDirList::removeEntry ()
0080 {
0081   int item = listbox->currentRow ();
0082   if (item == -1)  //nothing selected
0083     return;
0084   //find it
0085   QStringList::iterator it = dirlist.begin() + item;
0086   //kill it
0087   dirlist.erase (it);
0088   update ();
0089 }
0090 
0091 void cDirList::moveEntryUp ()
0092 {
0093   int item = listbox->currentRow ();
0094   if (item == -1)  //nothing selected
0095     return;
0096   if (item == 0)  //first item
0097     return;
0098   QStringList::iterator it = dirlist.begin() + item;
0099   QString text = *it;
0100   //it2 will point at previous item
0101   QStringList::iterator it2 = it;
0102   it2--;
0103   //erase that item
0104   dirlist.erase (it);
0105   //and insert it before previous item
0106   dirlist.insert (it2, text);
0107   update ();
0108   listbox->setCurrentRow (item - 1);
0109 }
0110 
0111 void cDirList::moveEntryDown ()
0112 {
0113   int item = listbox->currentRow ();
0114   if (item == -1)  //nothing selected
0115     return;
0116   if (item == dirlist.count() - 1) //last item
0117     return;
0118   QStringList::iterator it = dirlist.begin() + item;
0119   QString text = *it;
0120   //erase that item, make it point to next item
0121   it = dirlist.erase (it);
0122   //and insert it after that item
0123   dirlist.insert (++it, text);
0124   update ();
0125   listbox->setCurrentRow (item + 1);
0126 }
0127 
0128 void cDirList::update ()
0129 {
0130   int item = listbox->currentRow ();
0131   listbox->clear ();
0132   listbox->addItems (dirlist);
0133   if (item != -1)
0134     listbox->setCurrentRow (item);
0135 }
0136 
0137 #include "moc_cdirlist.cpp"