qdirmultilineedit.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001-2002 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "qdirmultilineedit.h"
00021 
00022 #include <qlayout.h>
00023 #include <qheader.h>
00024 #include <qpushbutton.h>
00025 #include <klistview.h>
00026 #include <klocale.h>
00027 #include <kfiledialog.h>
00028 #include <kiconloader.h>
00029 
00030 QDirMultiLineEdit::QDirMultiLineEdit(QWidget *parent, const char *name)
00031 : QWidget(parent, name)
00032 {
00033     m_view = new KListView(this);
00034     m_view->header()->hide();
00035     m_view->addColumn("");
00036     m_view->setFullWidth(true);
00037     connect(m_view, SIGNAL(selectionChanged(QListViewItem*)), SLOT(slotSelected(QListViewItem*)));
00038 
00039     m_add = new QPushButton(this);
00040     m_add->setPixmap(SmallIcon("folder_new"));
00041     connect(m_add, SIGNAL(clicked()), SLOT(slotAddClicked()));
00042     m_remove = new QPushButton(this);
00043     m_remove->setPixmap(SmallIcon("editdelete"));
00044     connect(m_remove, SIGNAL(clicked()), SLOT(slotRemoveClicked()));
00045     m_remove->setEnabled(false);
00046 
00047     m_view->setFixedHeight(QMAX(m_view->fontMetrics().lineSpacing()*3+m_view->lineWidth()*2, m_add->sizeHint().height()*2));
00048 
00049     QHBoxLayout *l0 = new QHBoxLayout(this, 0, 3);
00050     QVBoxLayout *l1 = new QVBoxLayout(0, 0, 0);
00051     l0->addWidget(m_view);
00052     l0->addLayout(l1);
00053     l1->addWidget(m_add);
00054     l1->addWidget(m_remove);
00055     l1->addStretch(1);
00056 }
00057 
00058 QDirMultiLineEdit::~QDirMultiLineEdit()
00059 {
00060 }
00061 
00062 void QDirMultiLineEdit::setURLs(const QStringList& urls)
00063 {
00064     m_view->clear();
00065     for (QStringList::ConstIterator it=urls.begin(); it!=urls.end(); ++it)
00066         addURL(*it);
00067 }
00068 
00069 QStringList QDirMultiLineEdit::urls()
00070 {
00071     QListViewItem   *item = m_view->firstChild();
00072     QStringList l;
00073     while (item)
00074     {
00075         l << item->text(0);
00076         item = item->nextSibling();
00077     }
00078     return l;
00079 }
00080 
00081 void QDirMultiLineEdit::addURL(const QString& url)
00082 {
00083     QListViewItem   *item = new QListViewItem(m_view, url);
00084     item->setRenameEnabled(0, true);
00085 }
00086 
00087 void QDirMultiLineEdit::slotAddClicked()
00088 {
00089     QString dirname = KFileDialog::getExistingDirectory(QString::null, this);
00090     if (!dirname.isEmpty())
00091         addURL(dirname);
00092 }
00093 
00094 void QDirMultiLineEdit::slotRemoveClicked()
00095 {
00096     QListViewItem   *item = m_view->currentItem();
00097     if (item)
00098     {
00099         delete item;
00100         slotSelected(m_view->currentItem());
00101     }
00102 }
00103 
00104 void QDirMultiLineEdit::slotSelected(QListViewItem *item)
00105 {
00106     m_remove->setEnabled((item != NULL));
00107 }
00108 
00109 #include "qdirmultilineedit.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys