# kdeprint Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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., 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     //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 each file, download it (if necessary) and add it
    00193         QString downloaded;
    00194         for (KURL::List::ConstIterator it=files.begin(); it!=files.end(); ++it)
    00195             if (KIO::NetAccess::download(*it, downloaded, this))
    00196             {
    00197                 KURL    url;
    00198                 url.setPath(downloaded);
    00199                 KMimeType::Ptr  mime = KMimeType::findByURL(url, 0, true, false);
    00200                 item = new QListViewItem(m_files, item, url.fileName(), mime->comment(), downloaded);
    00201                 item->setPixmap(0, mime->pixmap(url, KIcon::Small));
    00202             }
    00203
    00204         slotSelectionChanged();
    00205         /*
    00206         if (m_files->childCount() > 0)
    00207         {
    00208             m_remove->setEnabled(true);
    00209             m_open->setEnabled(true);
    00210             if (m_files->currentItem() == 0)
    00211                 m_files->setSelected(m_files->firstChild(), true);
    00212         }
    00213         */
    00214     }
    00215 }
    00216
    00217 void KFileList::setFileList(const QStringList& files)
    00218 {
    00219     m_files->clear();
    00220     QListViewItem *item = 0;
    00221     for (QStringList::ConstIterator it=files.begin(); it!=files.end(); ++it)
    00222     {
    00223         KURL    url;
    00224         url.setPath(*it);
    00225         KMimeType::Ptr  mime = KMimeType::findByURL(url, 0, true, false);
    00226         item = new QListViewItem(m_files, item, url.fileName(), mime->comment(), *it);
    00227         item->setPixmap(0, mime->pixmap(url, KIcon::Small));
    00228     }
    00229     slotSelectionChanged();
    00230 }
    00231
    00232 QStringList KFileList::fileList() const
    00233 {
    00234     QStringList l;
    00235     QListViewItem   *item = m_files->firstChild();
    00236     while (item)
    00237     {
    00238         l << item->text(2);
    00239         item = item->nextSibling();
    00240     }
    00241     return l;
    00242 }
    00243
    00244 void KFileList::slotAddFile()
    00245 {
    00246     KURL    fname = KFileDialog::getOpenURL(QString::null, QString::null, this);
    00247     if (!fname.isEmpty())
    00248         addFiles(KURL::List(fname));
    00249 }
    00250
    00251 void KFileList::slotRemoveFile()
    00252 {
    00253     QPtrList<QListViewItem> l;
    00254     selection(l);
    00255     l.setAutoDelete(true);
    00256     m_block = true;
    00257     l.clear();
    00258     m_block = false;
    00259     slotSelectionChanged();
    00260 }
    00261
    00262 void KFileList::slotOpenFile()
    00263 {
    00264     QListViewItem   *item = m_files->currentItem();
    00265     if (item)
    00266     {
    00267         KURL url( item->text( 2 ) );
    00268         new KRun(url);
    00269     }
    00270 }
    00271
    00272 QSize KFileList::sizeHint() const
    00273 {
    00274     return QSize(100, 100);
    00275 }
    00276
    00277 void KFileList::selection(QPtrList<QListViewItem>& l)
    00278 {
    00279     l.setAutoDelete(false);
    00280     QListViewItem   *item = m_files->firstChild();
    00281     while (item)
    00282     {
    00283         if (item->isSelected())
    00284             l.append(item);
    00285         item = item->nextSibling();
    00286     }
    00287 }
    00288
    00289 void KFileList::slotSelectionChanged()
    00290 {
    00291     if (m_block)
    00292         return;
    00293
    00294     QPtrList<QListViewItem> l;
    00295     selection(l);
    00296     m_remove->setEnabled(l.count() > 0);
    00297     m_open->setEnabled(l.count() == 1);
    00298     m_up->setEnabled(l.count() == 1 && l.first()->itemAbove());
    00299     m_down->setEnabled(l.count() == 1 && l.first()->itemBelow());
    00300 }
    00301
    00302 void KFileList::slotUp()
    00303 {
    00304     QPtrList<QListViewItem> l;
    00305     selection(l);
    00306     if (l.count() == 1 && l.first()->itemAbove())
    00307     {
    00308         QListViewItem   *item(l.first()), *clone;
    00309         clone = new QListViewItem(m_files, item->itemAbove()->itemAbove(), item->text(0), item->text(1), item->text(2));
    00310         clone->setPixmap(0, *(item->pixmap(0)));
    00311         delete item;
    00312         m_files->setCurrentItem(clone);
    00313         m_files->setSelected(clone, true);
    00314     }
    00315 }
    00316
    00317 void KFileList::slotDown()
    00318 {
    00319     QPtrList<QListViewItem> l;
    00320     selection(l);
    00321     if (l.count() == 1 && l.first()->itemBelow())
    00322     {
    00323         QListViewItem   *item(l.first()), *clone;
    00324         clone = new QListViewItem(m_files, item->itemBelow(), item->text(0), item->text(1), item->text(2));
    00325         clone->setPixmap(0, *(item->pixmap(0)));
    00326         delete item;
    00327         m_files->setCurrentItem(clone);
    00328         m_files->setSelected(clone, true);
    00329     }
    00330 }
    00331
    00332 #include "kfilelist.moc"

This file is part of the documentation for kdeprint Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:31:01 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003