kdeprint Library API Documentation

escpwidget.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 "escpwidget.h"
00021 
00022 #include <qpushbutton.h>
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qcheckbox.h>
00026 #include <qaccel.h>
00027 #include <klocale.h>
00028 #include <kmessagebox.h>
00029 #include <kstandarddirs.h>
00030 #include <kiconloader.h>
00031 #include <kdialogbase.h>
00032 #include <klibloader.h>
00033 #include <kseparator.h>
00034 #include <kdebug.h>
00035 
00036 class EscpFactory : public KLibFactory
00037 {
00038 public:
00039     EscpFactory(QObject *parent = 0, const char *name = 0) : KLibFactory(parent, name) {}
00040 protected:
00041     QObject* createObject(QObject *parent = 0, const char *name = 0, const char * /*classname*/ = "QObject", const QStringList& args = QStringList())
00042     {
00043         KDialogBase *dlg = new KDialogBase(static_cast<QWidget*>(parent), name, true, i18n("EPSON InkJet Printer Utilities"), KDialogBase::Close);
00044         EscpWidget  *w = new EscpWidget(dlg);
00045         if (args.count() > 0)
00046             w->setDevice(args[0]);
00047         if (args.count() > 1)
00048             w->setPrinterName(args[1]);
00049         dlg->setMainWidget(w);
00050         return dlg;
00051     }
00052 };
00053 
00054 extern "C"
00055 {
00056     void* init_kdeprint_tool_escputil()
00057     {
00058         return new EscpFactory;
00059     }
00060 }
00061 
00062 EscpWidget::EscpWidget(QWidget *parent, const char *name)
00063 : QWidget(parent, name)
00064 {
00065     m_hasoutput = false;
00066 
00067     connect(&m_proc, SIGNAL(processExited(KProcess*)), SLOT(slotProcessExited(KProcess*)));
00068     connect(&m_proc, SIGNAL(receivedStdout(KProcess*,char*,int)), SLOT(slotReceivedStdout(KProcess*,char*,int)));
00069     connect(&m_proc, SIGNAL(receivedStderr(KProcess*,char*,int)), SLOT(slotReceivedStderr(KProcess*,char*,int)));
00070 
00071     QPushButton *cleanbtn = new QPushButton(this, "-c");
00072     cleanbtn->setPixmap(DesktopIcon("exec"));
00073     QPushButton *nozzlebtn = new QPushButton(this, "-n");
00074     nozzlebtn->setPixmap(DesktopIcon("exec"));
00075     QPushButton *alignbtn = new QPushButton(this, "-a");
00076     alignbtn->setPixmap(DesktopIcon("exec"));
00077     QPushButton *inkbtn = new QPushButton(this, "-i");
00078     inkbtn->setPixmap(DesktopIcon("kdeprint_inklevel"));
00079     QPushButton *identbtn = new QPushButton(this, "-d");
00080     identbtn->setPixmap(DesktopIcon("exec"));
00081 
00082     QFont   f(font());
00083     f.setBold(true);
00084     m_printer = new QLabel(this);
00085     m_printer->setFont(f);
00086     m_device = new QLabel(this);
00087     m_device->setFont(f);
00088     m_useraw = new QCheckBox(i18n("&Use direct connection (might need root permissions)"), this);
00089 
00090     connect(cleanbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00091     connect(nozzlebtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00092     connect(alignbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00093     connect(inkbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00094     connect(identbtn, SIGNAL(clicked()), SLOT(slotButtonClicked()));
00095 
00096     QLabel  *printerlab = new QLabel(i18n("Printer:"), this);
00097     printerlab->setAlignment(AlignRight|AlignVCenter);
00098     QLabel  *devicelab = new QLabel(i18n("Device:"), this);
00099     devicelab->setAlignment(AlignRight|AlignVCenter);
00100     QLabel  *cleanlab = new QLabel(i18n("Clea&n print head"), this);
00101     QLabel  *nozzlelab = new QLabel(i18n("&Print a nozzle test pattern"), this);
00102     QLabel  *alignlab = new QLabel(i18n("&Align print head"), this);
00103     QLabel  *inklab = new QLabel(i18n("&Ink level"), this);
00104     QLabel  *identlab = new QLabel(i18n("P&rinter identification"), this);
00105 
00106     cleanlab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00107     nozzlelab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00108     alignlab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00109     inklab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00110     identlab->setAlignment(AlignLeft|AlignVCenter|ShowPrefix);
00111 
00112     cleanbtn->setAccel(QAccel::shortcutKey(cleanlab->text()));
00113     nozzlebtn->setAccel(QAccel::shortcutKey(nozzlelab->text()));
00114     alignbtn->setAccel(QAccel::shortcutKey(alignlab->text()));
00115     inkbtn->setAccel(QAccel::shortcutKey(inklab->text()));
00116     identbtn->setAccel(QAccel::shortcutKey(identlab->text()));
00117 
00118     KSeparator  *sep = new KSeparator(this);
00119     sep->setFixedHeight(10);
00120 
00121     QGridLayout *l0 = new QGridLayout(this, 8, 2, 10, 10);
00122     QGridLayout *l1 = new QGridLayout(0, 2, 2, 0, 5);
00123     l0->addMultiCellLayout(l1, 0, 0, 0, 1);
00124     l1->addWidget(printerlab, 0, 0);
00125     l1->addWidget(devicelab, 1, 0);
00126     l1->addWidget(m_printer, 0, 1);
00127     l1->addWidget(m_device, 1, 1);
00128     l1->setColStretch(1, 1);
00129     l0->addMultiCellWidget(sep, 1, 1, 0, 1);
00130     l0->addWidget(cleanbtn, 2, 0);
00131     l0->addWidget(nozzlebtn, 3, 0);
00132     l0->addWidget(alignbtn, 4, 0);
00133     l0->addWidget(inkbtn, 5, 0);
00134     l0->addWidget(identbtn, 6, 0);
00135     l0->addWidget(cleanlab, 2, 1);
00136     l0->addWidget(nozzlelab, 3, 1);
00137     l0->addWidget(alignlab, 4, 1);
00138     l0->addWidget(inklab, 5, 1);
00139     l0->addWidget(identlab, 6, 1);
00140     l0->addMultiCellWidget(m_useraw, 7, 7, 0, 1);
00141     l0->setColStretch(1, 1);
00142 }
00143 
00144 void EscpWidget::startCommand(const QString& arg)
00145 {
00146     bool    useUSB(false);
00147 
00148     if (m_deviceURL.isEmpty())
00149     {
00150         KMessageBox::error(this, i18n("Internal error: no device set."));
00151         return;
00152     }
00153     else
00154     {
00155         QString protocol = m_deviceURL.protocol();
00156         if (protocol == "usb")
00157             useUSB = true;
00158         else if (protocol != "file" && protocol != "parallel" && protocol != "serial" && !protocol.isEmpty())
00159         {
00160             KMessageBox::error(this,
00161                 i18n("Unsupported connection type: %1").arg(protocol));
00162             return;
00163         }
00164     }
00165 
00166     if (m_proc.isRunning())
00167     {
00168         KMessageBox::error(this, i18n("An escputil process is still running. "
00169                                       "You must wait until its completion before continuing."));
00170         return;
00171     }
00172 
00173     QString exestr = KStandardDirs::findExe("escputil");
00174     if (exestr.isEmpty())
00175     {
00176         KMessageBox::error(this, i18n("The executable escputil cannot be found in your "
00177                                       "PATH environment variable. Make sure gimp-print is "
00178                                       "installed and that escputil is in your PATH."));
00179         return;
00180     }
00181 
00182     m_proc.clearArguments();
00183     m_proc << exestr;
00184     if (m_useraw->isChecked() || arg == "-i")
00185         m_proc << "-r" << m_deviceURL.path();
00186     else
00187         m_proc << "-P" << m_printer->text();
00188     if (useUSB)
00189         m_proc << "-u";
00190 
00191     m_proc << arg << "-q";
00192     m_errorbuffer = m_outbuffer = QString::null;
00193     m_hasoutput = ( arg == "-i" || arg == "-d" );
00194     for ( QValueList<QCString>::ConstIterator it=m_proc.args().begin(); it!=m_proc.args().end(); ++it )
00195         kdDebug() << "ARG: " << *it << endl;
00196     if (m_proc.start(KProcess::NotifyOnExit, KProcess::AllOutput))
00197         setEnabled(false);
00198     else
00199     {
00200         KMessageBox::error(this,
00201             i18n("Internal error: unable to start escputil process."));
00202         return;
00203     }
00204 }
00205 
00206 void EscpWidget::slotProcessExited(KProcess*)
00207 {
00208     setEnabled(true);
00209     if (!m_proc.normalExit() || m_proc.exitStatus() != 0)
00210     {
00211         QString msg1 = "<qt>"+i18n("Operation terminated with errors.")+"</qt>";
00212         QString msg2;
00213         if (!m_outbuffer.isEmpty())
00214             msg2 += "<p><b><u>"+i18n("Output")+"</u></b></p><p>"+m_outbuffer+"</p>";
00215         if (!m_errorbuffer.isEmpty())
00216             msg2 += "<p><b><u>"+i18n("Error")+"</u></b></p><p>"+m_errorbuffer+"</p>";
00217         if (!msg2.isEmpty())
00218             KMessageBox::detailedError(this, msg1, msg2);
00219         else
00220             KMessageBox::error(this, msg1);
00221     }
00222     else if ( !m_outbuffer.isEmpty() && m_hasoutput )
00223     {
00224         KMessageBox::information( this, m_outbuffer );
00225     }
00226     m_hasoutput = false;
00227 }
00228 
00229 void EscpWidget::slotReceivedStdout(KProcess*, char *buf, int len)
00230 {
00231     QString bufstr = QCString(buf, len);
00232     m_outbuffer.append(bufstr);
00233 }
00234 
00235 void EscpWidget::slotReceivedStderr(KProcess*, char *buf, int len)
00236 {
00237     QString bufstr = QCString(buf, len);
00238     m_errorbuffer.append(bufstr);
00239 }
00240 
00241 void EscpWidget::slotButtonClicked()
00242 {
00243     QString arg = sender()->name();
00244     startCommand(arg);
00245 }
00246 
00247 void EscpWidget::setPrinterName(const QString& p)
00248 {
00249     m_printer->setText(p);
00250 }
00251 
00252 void EscpWidget::setDevice(const QString& dev)
00253 {
00254     m_deviceURL = dev;
00255     m_device->setText(dev);
00256 }
00257 
00258 #include "escpwidget.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 28 01:39:56 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003