kpfilterpage.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 "kpfilterpage.h"
00021 #include "kmfactory.h"
00022 #include "kxmlcommand.h"
00023 
00024 #include <qtoolbutton.h>
00025 #include <qheader.h>
00026 #include <qtooltip.h>
00027 #include <qlayout.h>
00028 #include <qwhatsthis.h>
00029 #include <klistview.h>
00030 #include <klocale.h>
00031 #include <kiconloader.h>
00032 #include <kmessagebox.h>
00033 #include <kactivelabel.h>
00034 #include <kdebug.h>
00035 #include <kapplication.h>
00036 #include <kdialog.h>
00037 
00038 KPFilterPage::KPFilterPage(QWidget *parent, const char *name)
00039 : KPrintDialogPage(parent,name)
00040 {
00041     //WhatsThis strings.... (added by pfeifle@kde.org)
00042     QString whatsThisAddFilterButton = i18n(  " <qt> <b>Add Filter button</b>"
00043                         " <p>This button calls a little dialog to let you"
00044                         " select a filter here. </p>"
00045                         " <p><b>Note 1:</b> You can chain different filters as long as you make "
00046                         " sure that the output of one fits as input of the next. (KDEPrint "
00047                         " checks your filtering chain and will warn you if you fail to do so.</p> "
00048                         " <p><b>Note 2:</b> The filters you define here are applied to your jobfile "
00049                         " <em><b>before</b></em> it is handed downstream to your spooler and print "
00050                         " subsystem (e.g. CUPS, LPRng, LPD).</p>"
00051                         " </ul>"
00052                         " </qt>" );
00053 
00054     QString whatsThisRemoveFilterButton = i18n(" <qt> <b>Remove Filter button</b>"
00055                         " <p>This button removes the highlighted filter from the"
00056                         " list of filters."
00057                         " </qt>" );
00058 
00059     QString whatsThisMoveFilterUpButton = i18n(" <qt> <b>Move Filter Up button</b>"
00060                         " <p>This button moves the highlighted filter up in the list"
00061                         " of filters, towards the front of the filtering chain. </p>"
00062                         " </qt>" );
00063 
00064     QString whatsThisMoveFilterDownButton = i18n(" <qt> <b>Move Filter Down button</b>"
00065                         " <p>This button moves the highlighted filter down in the list"
00066                         " of filters, towards the end of the filtering chain..</p>"
00067                         " </qt>" );
00068 
00069     QString whatsThisConfigureFilterButton = i18n( " <qt> <b>Configure Filter button</b>"
00070                         " <p>This button lets you configure the currently highlighted filter."
00071                         " It opens a separate dialog. "
00072                         " </p>"
00073                         " </qt>" );
00074 
00075     QString whatsThisFilterInfoPane = i18n( " <qt> <b>Filter Info Pane</b>"
00076                         " <p>This field shows some general info about the selected filter. "
00077                         " Amongst them are: "
00078                         " <ul> "
00079                         " <li>the <em>filter name</em> (as displayed in the KDEPrint user interface); </li> "
00080                         " <li>the <em>filter requirements</em> (that is the external program that needs "
00081                         " to present and executable on this system); </li> "
00082                         " <li>the <em>filter input format</em> (in the form of one or several <em>MIME types</em>"
00083                         " accepted by the filter); </li> "
00084                         " <li>the <em>filter output format</em> (in the form of a <em>MIME type</em> "
00085                         " generated by the filter); </li> "
00086                         " <li>a more or less verbose text describing the filter's operation.</li> "
00087                         " </ul> "
00088                         " </p>"
00089                         " </qt>" );
00090 
00091     QString whatsThisFilterchainListView = i18n( " <qt> <b>Filtering Chain</b> (if enabled, is run <em>before</em> actual "
00092                         " job submission to print system)"
00093                         " <p>This field shows which filters are currently selected to act as 'pre-filters' "
00094                         " for KDEPrint. Pre-filters are processing the print files <em>before</em> they are "
00095                         " send downstream to your real print subsystem. </p> "
00096                         " <p>The list shown in this field may be empty (default). </p> "
00097                         " <p>The pre-filters act on the printjob in the order they are listed (from top to bottom). "
00098                         " This is done by acting as a <em>filtering chain</em> where the output of one filter "
00099                         " acts as input to the next. By putting the filters into the wrong order, you can make " 
00100                         " the filtering chain fail. For example: if your file is ASCII text, and you want the "
00101                         " output being processed by the 'Multipage per Sheet' filter, the first filter must be "
00102                         " one that processes ASCII into PostScript. </p> "
00103                         " <p>KDEPrint can utilize <em>any</em> external filtering program which you may find useful "
00104                         " through this interface. </p> "
00105                         " <p>KDEPrint ships preconfigured with support for a selection of common filters. These "
00106                         " filters however need to be "
00107                         " installed independently from KDEPrint. These pre-filters work <em>for all</em> print subsystems"
00108                         " supported by KDEPrint (such as CUPS, LPRng and LPD), because they are not depending on these.</p> "
00109                         ".<p> Amongst the pre-configured filters shipping with KDEPrint are: </p> "
00110                         " <ul> "
00111                         " <li>the <em>Enscript text filter</em>  </li> "
00112                         " <li>a <em>Multiple Pages per Sheet filter</em </li> "
00113                         " <li>a <em>PostScript to PDF converter</em>.</li> "
00114                         " <li>a <em>Page Selection/Ordering filter</em>.</li> "
00115                         " <li>a <em>Poster Printing filter</em>.</li> "
00116                         " <li>and some more..</li> "
00117                         " </ul> "
00118                         " To insert a filter into this list, simply click on the <em>funnel</em> icon (topmost on "
00119                         " the right icon column group) and proceed. </p>"
00120                         " <p>Please click on the other elements of this dialog to learn more about the KDEPrint "
00121                         " pre-filters. "
00122                         " </p>"
00123                         " </qt>" );
00124 
00125     setTitle(i18n("Filters"));
00126     m_activefilters.setAutoDelete(true);
00127     m_valid = true;
00128 
00129     m_view = new KListView(this);
00130       QWhatsThis::add(m_view, whatsThisFilterchainListView);
00131     m_view->addColumn("");
00132     m_view->setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
00133     m_view->setLineWidth(1);
00134     m_view->setSorting(-1);
00135     m_view->header()->hide();
00136     connect(m_view,SIGNAL(selectionChanged(QListViewItem*)),SLOT(slotItemSelected(QListViewItem*)));
00137 
00138     m_add = new QToolButton(this);
00139       QWhatsThis::add(m_add, whatsThisAddFilterButton);
00140     m_add->setIconSet(BarIconSet("filter"));
00141     QToolTip::add(m_add, i18n("Add filter"));
00142 
00143     m_remove = new QToolButton(this);
00144       QWhatsThis::add(m_remove, whatsThisRemoveFilterButton);
00145     m_remove->setIconSet(BarIconSet("remove"));
00146     QToolTip::add(m_remove, i18n("Remove filter"));
00147 
00148     m_up = new QToolButton(this);
00149       QWhatsThis::add(m_up, whatsThisMoveFilterUpButton);
00150     m_up->setIconSet(BarIconSet("up"));
00151     QToolTip::add(m_up, i18n("Move filter up"));
00152 
00153     m_down = new QToolButton(this);
00154       QWhatsThis::add(m_down, whatsThisMoveFilterDownButton);
00155     m_down->setIconSet(BarIconSet("down"));
00156     QToolTip::add(m_down, i18n("Move filter down"));
00157 
00158     m_configure = new QToolButton(this);
00159       QWhatsThis::add(m_configure, whatsThisConfigureFilterButton);
00160     m_configure->setIconSet(BarIconSet("configure"));
00161     QToolTip::add(m_configure, i18n("Configure filter"));
00162 
00163     connect(m_add,SIGNAL(clicked()),SLOT(slotAddClicked()));
00164     connect(m_remove,SIGNAL(clicked()),SLOT(slotRemoveClicked()));
00165     connect(m_up,SIGNAL(clicked()),SLOT(slotUpClicked()));
00166     connect(m_down,SIGNAL(clicked()),SLOT(slotDownClicked()));
00167     connect(m_configure,SIGNAL(clicked()),SLOT(slotConfigureClicked()));
00168     connect(m_view,SIGNAL(doubleClicked(QListViewItem*)),SLOT(slotConfigureClicked()));
00169 
00170     m_info = new KActiveLabel(this);
00171       QWhatsThis::add(m_info, whatsThisFilterInfoPane);
00172     m_info->setVScrollBarMode( QScrollView::Auto );
00173     m_info->setHScrollBarMode( QScrollView::Auto );
00174     m_info->setFrameStyle( QFrame::Panel|QFrame::Sunken );
00175     m_info->setMinimumSize( QSize( 240, 100 ) );
00176 
00177     QGridLayout *l1 = new QGridLayout(this, 2, 2, 0, KDialog::spacingHint());
00178     l1->setColStretch(0, 1);
00179     QVBoxLayout *l2 = new QVBoxLayout(0, 0, 1);
00180     l1->addWidget(m_view, 0, 0);
00181     l1->addLayout(l2, 0, 1);
00182     l2->addWidget(m_add);
00183     l2->addWidget(m_remove);
00184     l2->addSpacing(5);
00185     l2->addWidget(m_up);
00186     l2->addWidget(m_down);
00187     l2->addSpacing(5);
00188     l2->addWidget(m_configure);
00189     l2->addStretch(1);
00190     l1->addMultiCellWidget(m_info, 1, 1, 0, 1);
00191     slotItemSelected(0);
00192 
00193     resize(100,50);
00194 }
00195 
00196 KPFilterPage::~KPFilterPage()
00197 {
00198 }
00199 
00200 void KPFilterPage::updateButton()
00201 {
00202 /*  QListViewItem   *item = m_view->currentItem();
00203   bool state=(item!=0);
00204   m_remove->setEnabled(state);
00205   m_up->setEnabled((state && item->itemAbove() != 0));
00206   m_down->setEnabled((state && item->itemBelow() != 0));
00207   m_configure->setEnabled(state);*/
00208 }
00209 
00210 void KPFilterPage::slotAddClicked()
00211 {
00212     bool    ok;
00213     QString choice = KXmlCommandManager::self()->selectCommand( this );
00214     ok = !choice.isEmpty();
00215     if (ok)
00216     {
00217         KXmlCommand *cmd = KXmlCommandManager::self()->loadCommand(choice);
00218         if (!cmd) return; // Error
00219         QStringList filters = activeList();
00220         int     pos = KXmlCommandManager::self()->insertCommand(filters, cmd->name());
00221         QListViewItem   *prev(0);
00222         if (pos > 0)
00223         {
00224             prev = m_view->firstChild();
00225             for (int i=1;prev && i<pos;i++)
00226                 prev = prev->nextSibling();
00227         }
00228         m_activefilters.insert(cmd->name(), cmd);
00229         QListViewItem   *item = new QListViewItem(m_view, prev, cmd->description(), cmd->name());
00230         item->setPixmap(0, SmallIcon("filter"));
00231         checkFilterChain();
00232     }
00233 }
00234 
00235 void KPFilterPage::slotRemoveClicked()
00236 {
00237     if (m_view->selectedItem())
00238     {
00239         QString idname = m_view->selectedItem()->text(1);
00240         delete m_view->selectedItem();
00241         m_activefilters.remove(idname);
00242         checkFilterChain();
00243         if (m_view->currentItem())
00244             m_view->setSelected(m_view->currentItem(), true);
00245         slotItemSelected(m_view->currentItem());
00246     }
00247 }
00248 
00249 void KPFilterPage::slotUpClicked()
00250 {
00251     QListViewItem   *item = m_view->selectedItem();
00252     if (item && item->itemAbove())
00253     {
00254         QListViewItem   *clone = new QListViewItem(m_view,item->itemAbove()->itemAbove(),item->text(0),item->text(1));
00255         clone->setPixmap(0, SmallIcon("filter"));
00256         delete item;
00257         m_view->setSelected(clone, true);
00258         checkFilterChain();
00259     }
00260 }
00261 
00262 void KPFilterPage::slotDownClicked()
00263 {
00264     QListViewItem   *item = m_view->selectedItem();
00265     if (item && item->itemBelow())
00266     {
00267         QListViewItem   *clone = new QListViewItem(m_view,item->itemBelow(),item->text(0),item->text(1));
00268         clone->setPixmap(0, SmallIcon("filter"));
00269         delete item;
00270         m_view->setSelected(clone, true);
00271         checkFilterChain();
00272     }
00273 }
00274 
00275 void KPFilterPage::slotConfigureClicked()
00276 {
00277     KXmlCommand *filter = currentFilter();
00278     if (!filter || !KXmlCommandManager::self()->configure(filter, this))
00279         KMessageBox::error(this,i18n("Internal error: unable to load filter."));
00280 }
00281 
00282 void KPFilterPage::slotItemSelected(QListViewItem *item)
00283 {
00284     m_remove->setEnabled((item != 0));
00285     m_up->setEnabled((item != 0 && item->itemAbove() != 0));
00286     m_down->setEnabled((item != 0 && item->itemBelow() != 0));
00287     m_configure->setEnabled((item != 0));
00288     updateInfo();
00289 }
00290 
00291 void KPFilterPage::setOptions(const QMap<QString,QString>& opts)
00292 {
00293     QStringList filters = QStringList::split(',',opts["_kde-filters"],false);
00294     // remove unneeded filters
00295     QDictIterator<KXmlCommand>  dit(m_activefilters);
00296     for (;dit.current();)
00297     {
00298         if (filters.find(dit.currentKey()) == filters.end())
00299             m_activefilters.remove(dit.currentKey());
00300         else
00301         {
00302             dit.current()->setOptions(opts);
00303             ++dit;
00304         }
00305     }
00306     // add needed filters
00307     m_view->clear();
00308     QListViewItem   *item(0);
00309     for (QStringList::ConstIterator sit=filters.begin(); sit!=filters.end(); ++sit)
00310     {
00311         KXmlCommand *f(0);
00312         if ((f=m_activefilters.find(*sit)) == 0)
00313         {
00314             f = KXmlCommandManager::self()->loadCommand(*sit);
00315             if (f)
00316             {
00317                 m_activefilters.insert(*sit,f);
00318                 f->setOptions(opts);
00319             }
00320         }
00321         if (f)
00322             item = new QListViewItem(m_view,item,f->description(),f->name());
00323     }
00324     checkFilterChain();
00325 }
00326 
00327 void KPFilterPage::getOptions(QMap<QString,QString>& opts, bool incldef)
00328 {
00329     QStringList filters = activeList();
00330     for (QStringList::ConstIterator it=filters.begin(); it!=filters.end(); ++it)
00331     {
00332         KXmlCommand *f = m_activefilters.find(*it);
00333         if (f)
00334             f->getOptions(opts, incldef);
00335     }
00336     if (filters.count() > 0 || incldef)
00337     {
00338         opts["_kde-filters"] = filters.join(",");
00339     }
00340 }
00341 
00342 QStringList KPFilterPage::activeList()
00343 {
00344     QStringList list;
00345     QListViewItem   *item = m_view->firstChild();
00346     while (item)
00347     {
00348         list.append(item->text(1));
00349         item = item->nextSibling();
00350     }
00351     return list;
00352 }
00353 
00354 KXmlCommand* KPFilterPage::currentFilter()
00355 {
00356     KXmlCommand *filter(0);
00357     if (m_view->selectedItem())
00358         filter = m_activefilters.find(m_view->selectedItem()->text(1));
00359     return filter;
00360 }
00361 
00362 void KPFilterPage::checkFilterChain()
00363 {
00364     QListViewItem   *item = m_view->firstChild();
00365     bool        ok(true);
00366     m_valid = true;
00367     while (item)
00368     {
00369         item->setPixmap(0, (ok ? SmallIcon("filter") : SmallIcon("filterstop")));
00370         KXmlCommand *f1 = m_activefilters.find(item->text(1));
00371         if (f1 && item->nextSibling())
00372         {
00373             KXmlCommand *f2 = m_activefilters.find(item->nextSibling()->text(1));
00374             if (f2)
00375             {
00376                 if (!f2->acceptMimeType(f1->mimeType()))
00377                 {
00378                     item->setPixmap(0, SmallIcon("filterstop"));
00379                     ok = false;
00380                     m_valid = false;
00381                 }
00382                 else
00383                     ok = true;
00384             }
00385         }
00386         item = item->nextSibling();
00387     }
00388 }
00389 
00390 bool KPFilterPage::isValid(QString& msg)
00391 {
00392     if (!m_valid)
00393     {
00394         msg = i18n("<p>The filter chain is wrong. The output format of at least one filter is not supported by its follower. See <b>Filters</b> tab for more information.</p>");
00395     }
00396     return m_valid;
00397 }
00398 
00399 void KPFilterPage::updateInfo()
00400 {
00401     QString txt;
00402     KXmlCommand *f = currentFilter();
00403     if (f)
00404     {
00405         QString templ("<b>%1:</b> %2<br>");
00406         txt.append(templ.arg(i18n("Name")).arg(f->name()));
00407         txt.append(templ.arg(i18n("Requirements")).arg(f->requirements().join(", ")));
00408         txt.append(templ.arg(i18n("Input")).arg(f->inputMimeTypes().join(", ")));
00409         txt.append(templ.arg(i18n("Output")).arg(f->mimeType()));
00410         if ( !f->comment().isEmpty() )
00411             txt.append( "<br>" ).append( f->comment() );
00412     }
00413     m_info->setText(txt);
00414 }
00415 
00416 #include "kpfilterpage.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys