kmspecialprinterdlg.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 "kmspecialprinterdlg.h"
00021 #include "kmprinter.h"
00022 #include "kdeprintcheck.h"
00023 #include "kmfactory.h"
00024 #include "kmspecialmanager.h"
00025 #include "kxmlcommandselector.h"
00026 #include "kxmlcommand.h"
00027 #include "driver.h"
00028 
00029 #include <qpushbutton.h>
00030 #include <qlineedit.h>
00031 #include <qcheckbox.h>
00032 #include <qcombobox.h>
00033 #include <qlabel.h>
00034 #include <qlayout.h>
00035 #include <qwhatsthis.h>
00036 #include <qgroupbox.h>
00037 #include <klocale.h>
00038 #include <kmessagebox.h>
00039 #include <kicondialog.h>
00040 #include <kfiledialog.h>
00041 #include <kseparator.h>
00042 
00043 KMSpecialPrinterDlg::KMSpecialPrinterDlg(QWidget *parent, const char *name)
00044 : KDialogBase(parent, name, true, QString::null, Ok|Cancel, Ok)
00045 {
00046     setCaption(i18n("Add Special Printer"));
00047 
00048     QWidget *dummy = new QWidget(this);
00049     setMainWidget(dummy);
00050 
00051     // widget creation
00052     m_name = new QLineEdit(dummy);
00053     connect(m_name, SIGNAL(textChanged ( const QString & )),this,SLOT(slotTextChanged(const QString & )));
00054     m_description = new QLineEdit(dummy);
00055     m_location = new QLineEdit(dummy);
00056     QLabel  *m_namelabel = new QLabel(i18n("&Name:"), dummy);
00057     QLabel  *m_desclabel = new QLabel(i18n("&Description:"), dummy);
00058     QLabel  *m_loclabel = new QLabel(i18n("&Location:"), dummy);
00059     m_namelabel->setBuddy(m_name);
00060     m_desclabel->setBuddy(m_description);
00061     m_loclabel->setBuddy(m_location);
00062 
00063     KSeparator* sep = new KSeparator( KSeparator::HLine, dummy);
00064 
00065     sep->setFixedHeight(10);
00066     QGroupBox   *m_gb = new QGroupBox(1, Qt::Horizontal, i18n("Command &Settings"), dummy);
00067     m_command = new KXmlCommandSelector(true, m_gb, "CommandSelector", this);
00068 
00069     QGroupBox *m_outfile_gb = new QGroupBox( 0, Qt::Horizontal, i18n( "Outp&ut File" ), dummy );
00070 
00071     m_usefile = new QCheckBox( i18n("&Enable output file"), m_outfile_gb);
00072 
00073     m_mimetype = new QComboBox(m_outfile_gb);
00074     KMimeType::List list = KMimeType::allMimeTypes();
00075     for (QValueList<KMimeType::Ptr>::ConstIterator it=list.begin(); it!=list.end(); ++it)
00076     {
00077         QString mimetype = (*it)->name();
00078         m_mimelist << mimetype;
00079     }
00080     m_mimelist.sort();
00081     m_mimetype->insertStringList(m_mimelist);
00082 
00083     QLabel  *m_mimetypelabel = new QLabel(i18n("&Format:"), m_outfile_gb);
00084     m_mimetypelabel->setBuddy (m_mimetype);
00085 
00086     m_extension = new QLineEdit(m_outfile_gb);
00087 
00088     QLabel  *m_extensionlabel = new QLabel(i18n("Filename e&xtension:"), m_outfile_gb);
00089     m_extensionlabel->setBuddy(m_extension);
00090 
00091     m_icon = new KIconButton(dummy);
00092     m_icon->setIcon("fileprint");
00093     m_icon->setFixedSize(QSize(48,48));
00094 
00095     connect( m_usefile, SIGNAL( toggled( bool ) ), m_mimetype, SLOT( setEnabled( bool ) ) );
00096     connect( m_usefile, SIGNAL( toggled( bool ) ), m_extension, SLOT( setEnabled( bool ) ) );
00097     connect( m_usefile, SIGNAL( toggled( bool ) ), m_mimetypelabel, SLOT( setEnabled( bool ) ) );
00098     connect( m_usefile, SIGNAL( toggled( bool ) ), m_extensionlabel, SLOT( setEnabled( bool ) ) );
00099     m_mimetypelabel->setEnabled( false );
00100     m_mimetype->setEnabled( false );
00101     m_extensionlabel->setEnabled( false );
00102     m_extension->setEnabled( false );
00103 
00104     QWhatsThis::add(m_usefile,
00105         i18n("<p>The command will use an output file. If checked, make sure the "
00106              "command contains an output tag.</p>"));
00107     QWhatsThis::add(m_command,
00108         i18n("<p>The command to execute when printing on this special printer. Either enter "
00109              "the command to execute directly, or associate/create a command object with/for "
00110              "this special printer. The command object is the preferred method as it provides "
00111              "support for advanced settings like mime type checking, configurable options and "
00112              "requirement list (the plain command is only provided for backward compatibility). "
00113              "When using a plain command, the following tags are recognized:</p>"
00114              "<ul><li><b>%in</b>: the input file (required).</li>"
00115              "<li><b>%out</b>: the output file (required if using an output file).</li>"
00116              "<li><b>%psl</b>: the paper size in lower case.</li>"
00117              "<li><b>%psu</b>: the paper size with the first letter in upper case.</li></ul>"));
00118     QString mimetypeWhatsThis = i18n("<p>The default mimetype for the output file (e.g. application/postscript).</p>");
00119     QWhatsThis::add(m_mimetypelabel, mimetypeWhatsThis);
00120     QWhatsThis::add(m_mimetype, mimetypeWhatsThis);
00121     QString extensionWhatsThis = i18n("<p>The default extension for the output file (e.g. ps, pdf, ps.gz).</p>");
00122     QWhatsThis::add(m_extensionlabel, extensionWhatsThis);
00123     QWhatsThis::add(m_extension, extensionWhatsThis);
00124 
00125     // layout creation
00126     QVBoxLayout *l0 = new QVBoxLayout(dummy, 0, 10);
00127     QGridLayout *l1 = new QGridLayout(0, 3, 3, 0, 5);
00128     l0->addLayout(l1);
00129     l1->setColStretch(2,1);
00130     l1->addColSpacing(0,60);
00131     l1->addMultiCellWidget(m_icon, 0, 2, 0, 0, Qt::AlignCenter);
00132     l1->addWidget(m_namelabel, 0, 1);
00133     l1->addWidget(m_desclabel, 1, 1);
00134     l1->addWidget(m_loclabel, 2, 1);
00135     l1->addWidget(m_name, 0, 2);
00136     l1->addWidget(m_description, 1, 2);
00137     l1->addWidget(m_location, 2, 2);
00138     l0->addWidget(sep);
00139     l0->addWidget(m_gb);
00140     l0->addWidget(m_outfile_gb);
00141     QGridLayout *l6 = new QGridLayout(m_outfile_gb->layout(), 3, 2, 10);
00142     l6->addMultiCellWidget( m_usefile, 0, 0, 0, 1 );
00143     l6->addWidget(m_mimetypelabel, 1, 0);
00144     l6->addWidget(m_mimetype, 1, 1);
00145     l6->addWidget(m_extensionlabel, 2, 0);
00146     l6->addWidget(m_extension, 2, 1);
00147 
00148     enableButton(Ok, !m_name->text().isEmpty());
00149 
00150     // resize dialog
00151     resize(400,100);
00152 }
00153 
00154 void KMSpecialPrinterDlg::slotTextChanged(const QString & )
00155 {
00156     enableButton(Ok, !m_name->text().isEmpty());
00157 }
00158 
00159 void KMSpecialPrinterDlg::slotOk()
00160 {
00161     if (!checkSettings())
00162         return;
00163     KDialogBase::slotOk();
00164 }
00165 
00166 bool KMSpecialPrinterDlg::checkSettings()
00167 {
00168     QString msg;
00169     if (m_name->text().isEmpty())
00170         msg = i18n("You must provide a non-empty name.");
00171     else
00172         KXmlCommandManager::self()->checkCommand(m_command->command(),
00173                     KXmlCommandManager::Basic,
00174                     (m_usefile->isChecked() ? KXmlCommandManager::Basic : KXmlCommandManager::None),
00175                     &msg);
00176 
00177     if (!msg.isEmpty())
00178         KMessageBox::error(this, i18n("Invalid settings. %1.").arg(msg));
00179 
00180     return (msg.isEmpty());
00181 }
00182 
00183 void KMSpecialPrinterDlg::setPrinter(KMPrinter *printer)
00184 {
00185     if (printer && printer->isSpecial())
00186     {
00187         m_command->setCommand(printer->option("kde-special-command"));
00188         m_usefile->setChecked(printer->option("kde-special-file") == "1");
00189         int index = m_mimelist.findIndex(printer->option("kde-special-mimetype"));
00190         m_mimetype->setCurrentItem(index == -1 ? 0 : index);
00191         m_extension->setText(printer->option("kde-special-extension"));
00192         m_name->setText(printer->name());
00193         m_description->setText(printer->description());
00194         m_location->setText(printer->location());
00195         m_icon->setIcon(printer->pixmap());
00196 
00197         setCaption(i18n("Configuring %1").arg(printer->name()));
00198     }
00199 }
00200 
00201 KMPrinter* KMSpecialPrinterDlg::printer()
00202 {
00203     KMPrinter   *printer = new KMPrinter();
00204     printer->setName(m_name->text());
00205     printer->setPrinterName(m_name->text());
00206     printer->setPixmap(m_icon->icon());
00207     printer->setDescription(m_description->text());
00208     printer->setLocation(m_location->text());
00209     printer->setOption("kde-special-command",m_command->command());
00210     printer->setOption("kde-special-file",(m_usefile->isChecked() ? "1" : "0"));
00211     if (m_usefile->isChecked ())
00212     {
00213         if (m_mimetype->currentText() != "all/all")
00214             printer->setOption("kde-special-mimetype", m_mimetype->currentText());
00215         printer->setOption("kde-special-extension",m_extension->text());
00216     }
00217     printer->setType(KMPrinter::Special);
00218     printer->setState(KMPrinter::Idle);
00219     return printer;
00220 }
00221 
00222 #include "kmspecialprinterdlg.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys