kdeprint Library API Documentation

kfilelist.cpp

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