00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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><STDIN></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
00180 QListViewItem *item = m_files->firstChild();
00181 while (item && item->nextSibling())
00182 item = item->nextSibling();
00183
00184
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
00199
00200
00201
00202
00203
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( item->text( 2 ) );
00260 new KRun(url);
00261 }
00262 }
00263
00264 QSize KFileList::sizeHint() const
00265 {
00266 return QSize(100, 100);
00267 }
00268
00269 void KFileList::selection(QPtrList<QListViewItem>& l)
00270 {
00271 l.setAutoDelete(false);
00272 QListViewItem *item = m_files->firstChild();
00273 while (item)
00274 {
00275 if (item->isSelected())
00276 l.append(item);
00277 item = item->nextSibling();
00278 }
00279 }
00280
00281 void KFileList::slotSelectionChanged()
00282 {
00283 if (m_block)
00284 return;
00285
00286 QPtrList<QListViewItem> l;
00287 selection(l);
00288 m_remove->setEnabled(l.count() > 0);
00289 m_open->setEnabled(l.count() == 1);
00290 m_up->setEnabled(l.count() == 1 && l.first()->itemAbove());
00291 m_down->setEnabled(l.count() == 1 && l.first()->itemBelow());
00292 }
00293
00294 void KFileList::slotUp()
00295 {
00296 QPtrList<QListViewItem> l;
00297 selection(l);
00298 if (l.count() == 1 && l.first()->itemAbove())
00299 {
00300 QListViewItem *item(l.first()), *clone;
00301 clone = new QListViewItem(m_files, item->itemAbove()->itemAbove(), item->text(0), item->text(1), item->text(2));
00302 clone->setPixmap(0, *(item->pixmap(0)));
00303 delete item;
00304 m_files->setCurrentItem(clone);
00305 m_files->setSelected(clone, true);
00306 }
00307 }
00308
00309 void KFileList::slotDown()
00310 {
00311 QPtrList<QListViewItem> l;
00312 selection(l);
00313 if (l.count() == 1 && l.first()->itemBelow())
00314 {
00315 QListViewItem *item(l.first()), *clone;
00316 clone = new QListViewItem(m_files, item->itemBelow(), item->text(0), item->text(1), item->text(2));
00317 clone->setPixmap(0, *(item->pixmap(0)));
00318 delete item;
00319 m_files->setCurrentItem(clone);
00320 m_files->setSelected(clone, true);
00321 }
00322 }
00323
00324 #include "kfilelist.moc"