kfilelist.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 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 "kfilelist.h"
00021 
00022 #include <qtoolbutton.h>
00023 #include <qlabel.h>
00024 #include <qlayout.h>
00025 #include <qtooltip.h>
00026 #include <qheader.h>
00027 #include <qwhatsthis.h>
00028 
00029 #include <kio/netaccess.h>
00030 #include <kurldrag.h>
00031 #include <kfiledialog.h>
00032 #include <klocale.h>
00033 #include <kiconloader.h>
00034 #include <klistview.h>
00035 #include <krun.h>
00036 #include <kmimetype.h>
00037 
00038 KFileList::KFileList(QWidget *parent, const char *name)
00039 : QWidget(parent, name)
00040 {
00041     //WhatsThis strings.... (added by pfeifle@kde.org)
00042     QString whatsThisAddFileButton = i18n(  " <qt> <b>Add File button</b>"
00043                         " <p>This button calls the <em>'File Open'</em> dialog to let you"
00044                         " select a file for printing. Note, that "
00045                         " <ul><li>you can select ASCII or International Text, PDF,"
00046                         " PostScript, JPEG, TIFF, PNG, GIF and many other graphic"
00047                         " formats."
00048                         " <li>you can select various files from different paths"
00049                         " and send them as one \"multi-file job\" to the printing"
00050                         " system."
00051                         " </ul>"
00052                             " </qt>" );
00053 
00054     QString whatsThisRemoveFileButton = i18n(" <qt> <b>Remove File button</b>"
00055                                                 " <p>This button removes the highlighted file from the"
00056                         " list of to-be-printed files."
00057                             " </qt>" );
00058 
00059     QString whatsThisMoveFileUpButton = i18n(" <qt> <b>Move File Up button</b>"
00060                                                 " <p>This button moves the highlighted file up in the list"
00061                         " of files to be printed.</p>"
00062                         " <p>In effect, this changes the order"
00063                         " of the files' printout.</p>"
00064                             " </qt>" );
00065 
00066     QString whatsThisMoveFileDownButton = i18n(" <qt> <b>Move File Down button</b>"
00067                                                 " <p>This button moves the highlighted file down in the list"
00068                         " of files to be printed.</p>"
00069                         " <p>In effect, this changes the order"
00070                         " of the files' printout.</p>"
00071                             " </qt>" );
00072 
00073     QString whatsThisOpenFileButton = i18n( " <qt> <b>File Open button</b>"
00074                                                 " <p>This button tries to open the highlighted file, so"
00075                         " you can view or edit it before you send it to the printing"
00076                         " system.</p>"
00077                         " <p>If you open"
00078                         " files, KDEPrint will use the application matching the MIME type of"
00079                         " the file.</p>"
00080                             " </qt>" );
00081 
00082     QString whatsThisFileSelectionListview = i18n( " <qt> <b>File List view</b>"
00083                                                 " <p>This list displays all the files you selected for printing."
00084                         " You can see the file name(s), file path(s) and the file"
00085                         " (MIME) type(s) as determined by KDEPrint. You may re-arrange the "
00086                         " initial order of the list "
00087                         " with the help of the arrow buttons on the right.</p>"
00088                         " <p>The files will be printed as a single job,"
00089                         " in the same order as displayed in the list.</p>"
00090                         " <p><b>Note:</b> You can select multiple files. The files may be in multiple"
00091                         " locations. The files may be of multiple MIME types. The buttons on the right"
00092                         " side let you add more files, remove already selected files from the list, "
00093                         " re-order the list (by moving files up or down), and open files. If you open"
00094                         " files, KDEPrint will use the application matching the MIME type of"
00095                         " the file.</p>"
00096                             " </qt>" );
00097 
00098     m_block = false;
00099 
00100     m_files = new KListView(this);
00101     m_files->addColumn(i18n("Name"));
00102     m_files->addColumn(i18n("Type"));
00103     m_files->addColumn(i18n("Path"));
00104     m_files->setAllColumnsShowFocus(true);
00105     m_files->setSorting(-1);
00106     m_files->setAcceptDrops(false);
00107     m_files->setSelectionMode(QListView::Extended);
00108     m_files->header()->setStretchEnabled(true, 2);
00109     QWhatsThis::add(m_files, whatsThisFileSelectionListview);
00110     connect(m_files, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()));
00111 
00112     m_add = new QToolButton(this);
00113     m_add->setIconSet(SmallIconSet("fileopen"));
00114     connect(m_add, SIGNAL(clicked()), SLOT(slotAddFile()));
00115     QToolTip::add(m_add, i18n("Add file"));
00116     QWhatsThis::add(m_add, whatsThisAddFileButton);
00117 
00118     m_remove = new QToolButton(this);
00119     m_remove->setIconSet(SmallIconSet("remove"));
00120     connect(m_remove, SIGNAL(clicked()), SLOT(slotRemoveFile()));
00121     QToolTip::add(m_remove, i18n("Remove file"));
00122     QWhatsThis::add(m_remove, whatsThisRemoveFileButton);
00123     m_remove->setEnabled(false);
00124 
00125     m_open = new QToolButton(this);
00126     m_open->setIconSet(SmallIconSet("filefind"));
00127     connect(m_open, SIGNAL(clicked()), SLOT(slotOpenFile()));
00128     QToolTip::add(m_open, i18n("Open file"));
00129     QWhatsThis::add(m_open, whatsThisOpenFileButton);
00130     m_open->setEnabled(false);
00131 
00132     m_up = new QToolButton(this);
00133     m_up->setIconSet(SmallIconSet("up"));
00134     connect(m_up, SIGNAL(clicked()), SLOT(slotUp()));
00135     QToolTip::add(m_up, i18n("Move up"));
00136     QWhatsThis::add(m_up, whatsThisMoveFileUpButton);
00137     m_up->setEnabled(false);
00138 
00139     m_down = new QToolButton(this);
00140     m_down->setIconSet(SmallIconSet("down"));
00141     connect(m_down, SIGNAL(clicked()), SLOT(slotDown()));
00142     QToolTip::add(m_down, i18n("Move down"));
00143     QWhatsThis::add(m_down, whatsThisMoveFileDownButton);
00144     m_down->setEnabled(false);
00145 
00146     setAcceptDrops(true);
00147 
00148     QToolTip::add(m_files, i18n(
00149         "Drag file(s) here or use the button to open a file dialog. "
00150         "Leave empty for <b>&lt;STDIN&gt;</b>."));
00151 
00152     QHBoxLayout *l0 = new QHBoxLayout(this, 0, KDialog::spacingHint());
00153     QVBoxLayout *l1 = new QVBoxLayout(0, 0, 1);
00154     l0->addWidget(m_files);
00155     l0->addLayout(l1);
00156     l1->addWidget(m_add);
00157     l1->addWidget(m_remove);
00158     l1->addWidget(m_open);
00159     l1->addSpacing(10);
00160     l1->addWidget(m_up);
00161     l1->addWidget(m_down);
00162     l1->addStretch(1);
00163 }
00164 
00165 KFileList::~KFileList()
00166 {
00167 }
00168 
00169 void KFileList::dragEnterEvent(QDragEnterEvent *e)
00170 {
00171     e->accept(KURLDrag::canDecode(e));
00172 }
00173 
00174 void KFileList::dropEvent(QDropEvent *e)
00175 {
00176     KURL::List  files;
00177     if (KURLDrag::decode(e, files))
00178     {
00179         addFiles(files);
00180     }
00181 }
00182 
00183 void KFileList::addFiles(const KURL::List& files)
00184 {
00185     if (files.count() > 0)
00186     {
00187         // search last item in current list, to add new ones at the end
00188         QListViewItem   *item = m_files->firstChild();
00189         while (item && item->nextSibling())
00190             item = item->nextSibling();
00191 
00192         for (KURL::List::ConstIterator it=files.begin(); it!=files.end(); ++it)
00193         {
00194             KMimeType::Ptr  mime = KMimeType::findByURL( *it, 0, true, false);
00195             item = new QListViewItem(m_files, item, (*it).fileName(), mime->comment(), (*it).url());
00196             item->setPixmap(0, mime->pixmap(*it, KIcon::Small));
00197         }
00198 
00199         slotSelectionChanged();
00200         /*
00201         if (m_files->childCount() > 0)
00202         {
00203             m_remove->setEnabled(true);
00204             m_open->setEnabled(true);
00205             if (m_files->currentItem() == 0)
00206                 m_files->setSelected(m_files->firstChild(), true);
00207         }
00208         */
00209     }
00210 }
00211 
00212 void KFileList::setFileList(const QStringList& files)
00213 {
00214     m_files->clear();
00215     QListViewItem *item = 0;
00216     for (QStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
00217     {
00218         KURL    url = KURL::fromPathOrURL( *it );
00219         KMimeType::Ptr  mime = KMimeType::findByURL(url, 0, true, false);
00220         item = new QListViewItem(m_files, item, url.fileName(), mime->comment(), url.url());
00221         item->setPixmap(0, mime->pixmap(url, KIcon::Small));
00222     }
00223     slotSelectionChanged();
00224 }
00225 
00226 QStringList KFileList::fileList() const
00227 {
00228     QStringList l;
00229     QListViewItem   *item = m_files->firstChild();
00230     while (item)
00231     {
00232         l << item->text(2);
00233         item = item->nextSibling();
00234     }
00235     return l;
00236 }
00237 
00238 void KFileList::slotAddFile()
00239 {
00240     KURL::List fnames = KFileDialog::getOpenURLs(QString::null, QString::null, this);
00241     if (!fnames.empty())
00242         addFiles(fnames);
00243 }
00244 
00245 void KFileList::slotRemoveFile()
00246 {
00247     QPtrList<QListViewItem> l;
00248     selection(l);
00249     l.setAutoDelete(true);
00250     m_block = true;
00251     l.clear();
00252     m_block = false;
00253     slotSelectionChanged();
00254 }
00255 
00256 void KFileList::slotOpenFile()
00257 {
00258     QListViewItem   *item = m_files->currentItem();
00259     if (item)
00260     {
00261         KURL url( item->text( 2 ) );
00262         new KRun(url);
00263     }
00264 }
00265 
00266 QSize KFileList::sizeHint() const
00267 {
00268     return QSize(100, 100);
00269 }
00270 
00271 void KFileList::selection(QPtrList<QListViewItem>& l)
00272 {
00273     l.setAutoDelete(false);
00274     QListViewItem   *item = m_files->firstChild();
00275     while (item)
00276     {
00277         if (item->isSelected())
00278             l.append(item);
00279         item = item->nextSibling();
00280     }
00281 }
00282 
00283 void KFileList::slotSelectionChanged()
00284 {
00285     if (m_block)
00286         return;
00287 
00288     QPtrList<QListViewItem> l;
00289     selection(l);
00290     m_remove->setEnabled(l.count() > 0);
00291     m_open->setEnabled(l.count() == 1);
00292     m_up->setEnabled(l.count() == 1 && l.first()->itemAbove());
00293     m_down->setEnabled(l.count() == 1 && l.first()->itemBelow());
00294 }
00295 
00296 void KFileList::slotUp()
00297 {
00298     QPtrList<QListViewItem> l;
00299     selection(l);
00300     if (l.count() == 1 && l.first()->itemAbove())
00301     {
00302         QListViewItem   *item(l.first()), *clone;
00303         clone = new QListViewItem(m_files, item->itemAbove()->itemAbove(), item->text(0), item->text(1), item->text(2));
00304         clone->setPixmap(0, *(item->pixmap(0)));
00305         delete item;
00306         m_files->setCurrentItem(clone);
00307         m_files->setSelected(clone, true);
00308     }
00309 }
00310 
00311 void KFileList::slotDown()
00312 {
00313     QPtrList<QListViewItem> l;
00314     selection(l);
00315     if (l.count() == 1 && l.first()->itemBelow())
00316     {
00317         QListViewItem   *item(l.first()), *clone;
00318         clone = new QListViewItem(m_files, item->itemBelow(), item->text(0), item->text(1), item->text(2));
00319         clone->setPixmap(0, *(item->pixmap(0)));
00320         delete item;
00321         m_files->setCurrentItem(clone);
00322         m_files->setSelected(clone, true);
00323     }
00324 }
00325 
00326 #include "kfilelist.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys