kdeprint Library API Documentation

kxmlcommandselector.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <goffioul@imec.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 "kxmlcommandselector.h"
00021 #include "kxmlcommand.h"
00022 #include "kxmlcommanddlg.h"
00023 #include "kdeprintcheck.h"
00024 
00025 #include <qcombobox.h>
00026 #include <kpushbutton.h>
00027 #include <qlabel.h>
00028 #include <qcheckbox.h>
00029 #include <qlayout.h>
00030 #include <qtooltip.h>
00031 #include <qlineedit.h>
00032 #include <kinputdialog.h>
00033 #include <klocale.h>
00034 #include <kiconloader.h>
00035 #include <kmessagebox.h>
00036 #include <kfiledialog.h>
00037 #include <kseparator.h>
00038 #include <kguiitem.h>
00039 #include <kactivelabel.h>
00040 #include <kdatetbl.h>
00041 #include <kdialogbase.h>
00042 
00043 extern "C"
00044 {
00045     QString select_command( QWidget* parent )
00046     {
00047         KDialogBase dlg( parent, 0, true, i18n( "Select Command" ), KDialogBase::Ok|KDialogBase::Cancel );
00048         KXmlCommandSelector *xmlSel = new KXmlCommandSelector( false, &dlg, "CommandSelector", &dlg );
00049         dlg.setMainWidget( xmlSel );
00050         if ( dlg.exec() )
00051             return xmlSel->command();
00052         return QString::null;
00053     }
00054 }
00055 
00056 KXmlCommandSelector::KXmlCommandSelector(bool canBeNull, QWidget *parent, const char *name, KDialogBase *dlg)
00057 : QWidget(parent, name)
00058 {
00059     m_cmd = new QComboBox(this);
00060     connect(m_cmd, SIGNAL(activated(int)), SLOT(slotCommandSelected(int)));
00061     QPushButton *m_add = new KPushButton(this);
00062     QPushButton *m_edit = new KPushButton(this);
00063     m_add->setPixmap(SmallIcon("filenew"));
00064     m_edit->setPixmap(SmallIcon("configure"));
00065     connect(m_add, SIGNAL(clicked()), SLOT(slotAddCommand()));
00066     connect(m_edit, SIGNAL(clicked()), SLOT(slotEditCommand()));
00067     QToolTip::add(m_add, i18n("New command"));
00068     QToolTip::add(m_edit, i18n("Edit command"));
00069     m_shortinfo = new QLabel(this);
00070     m_helpbtn = new KPushButton( this );
00071     m_helpbtn->setPixmap( SmallIcon( "help" ) );
00072     connect( m_helpbtn, SIGNAL( clicked() ), SLOT( slotHelpCommand() ) );
00073     QToolTip::add( m_helpbtn, i18n( "Information" ) );
00074         m_helpbtn->setEnabled( false );
00075 
00076     m_line = 0;
00077     m_usefilter = 0;
00078     QPushButton *m_browse = 0;
00079 
00080     QVBoxLayout *l0 = new QVBoxLayout(this, 0, 10);
00081 
00082     if (canBeNull)
00083     {
00084         m_line = new QLineEdit(this);
00085         m_browse = new KPushButton(KGuiItem(i18n("&Browse..."), "fileopen"), this);
00086         m_usefilter = new QCheckBox(i18n("Use co&mmand:"), this);
00087         connect(m_browse, SIGNAL(clicked()), SLOT(slotBrowse()));
00088         connect(m_usefilter, SIGNAL(toggled(bool)), m_line, SLOT(setDisabled(bool)));
00089         connect(m_usefilter, SIGNAL(toggled(bool)), m_browse, SLOT(setDisabled(bool)));
00090         connect(m_usefilter, SIGNAL(toggled(bool)), m_cmd, SLOT(setEnabled(bool)));
00091         connect(m_usefilter, SIGNAL(toggled(bool)), m_add, SLOT(setEnabled(bool)));
00092         connect(m_usefilter, SIGNAL(toggled(bool)), m_edit, SLOT(setEnabled(bool)));
00093         connect(m_usefilter, SIGNAL(toggled(bool)), m_shortinfo, SLOT(setEnabled(bool)));
00094         connect( m_usefilter, SIGNAL( toggled( bool ) ), SLOT( slotXmlCommandToggled( bool ) ) );
00095         m_usefilter->setChecked(true);
00096         m_usefilter->setChecked(false);
00097         //setFocusProxy(m_line);
00098         setTabOrder(m_usefilter, m_cmd);
00099         setTabOrder(m_cmd, m_add);
00100         setTabOrder(m_add, m_edit);
00101 
00102         QHBoxLayout *l1 = new QHBoxLayout(0, 0, 10);
00103         l0->addLayout(l1);
00104         l1->addWidget(m_line);
00105         l1->addWidget(m_browse);
00106 
00107         KSeparator  *sep = new KSeparator(Qt::Horizontal, this);
00108         l0->addWidget(sep);
00109     }
00110     else
00111         setFocusProxy(m_cmd);
00112 
00113     QGridLayout *l2 = new QGridLayout(0, 2, (m_usefilter?3:2), 0, 5);
00114     int c(0);
00115     l0->addLayout(l2);
00116     if (m_usefilter)
00117     {
00118         l2->addWidget(m_usefilter, 0, c++);
00119     }
00120     l2->addWidget(m_cmd, 0, c);
00121     QHBoxLayout *l4 = new QHBoxLayout( 0, 0, 5 );
00122     l2->addLayout( l4, 1, c );
00123     l4->addWidget( m_helpbtn, 0 );
00124     l4->addWidget( m_shortinfo, 1 );
00125     QHBoxLayout *l3 = new QHBoxLayout(0, 0, 0);
00126     l2->addLayout(l3, 0, c+1);
00127     l3->addWidget(m_add);
00128     l3->addWidget(m_edit);
00129 
00130     if ( dlg )
00131         connect( this, SIGNAL( commandValid( bool ) ), dlg, SLOT( enableButtonOK( bool ) ) );
00132 
00133     loadCommands();
00134 }
00135 
00136 void KXmlCommandSelector::loadCommands()
00137 {
00138     QString thisCmd = (m_cmd->currentItem() != -1 ? m_cmdlist[m_cmd->currentItem()] : QString::null);
00139 
00140     m_cmd->clear();
00141     m_cmdlist.clear();
00142 
00143     QStringList list = KXmlCommandManager::self()->commandListWithDescription();
00144     QStringList desclist;
00145     for (QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
00146     {
00147         m_cmdlist << (*it);
00148         ++it;
00149         desclist << (*it);
00150     }
00151     m_cmd->insertStringList(desclist);
00152 
00153     int index = m_cmdlist.findIndex(thisCmd);
00154     if (index != -1)
00155         m_cmd->setCurrentItem(index);
00156     if (m_cmd->currentItem() != -1 && m_cmd->isEnabled())
00157         slotCommandSelected(m_cmd->currentItem());
00158 }
00159 
00160 QString KXmlCommandSelector::command() const
00161 {
00162     QString cmd;
00163     if (m_line && !m_usefilter->isChecked())
00164         cmd = m_line->text();
00165     else
00166         cmd = m_cmdlist[m_cmd->currentItem()];
00167     return cmd;
00168 }
00169 
00170 void KXmlCommandSelector::setCommand(const QString& cmd)
00171 {
00172     int index = m_cmdlist.findIndex(cmd);
00173 
00174     if (m_usefilter)
00175         m_usefilter->setChecked(index != -1);
00176     if (m_line)
00177         m_line->setText((index == -1 ? cmd : QString::null));
00178     if (index != -1)
00179         m_cmd->setCurrentItem(index);
00180     if (m_cmd->currentItem() != -1 && m_cmd->isEnabled())
00181         slotCommandSelected(m_cmd->currentItem());
00182 }
00183 
00184 void KXmlCommandSelector::slotAddCommand()
00185 {
00186     bool    ok(false);
00187     QString cmdId = KInputDialog::getText(i18n("Command Name"), i18n("Enter an identification name for the new command:"), QString::null, &ok, this);
00188     if (ok)
00189     {
00190         bool    added(true);
00191 
00192         if (m_cmdlist.findIndex(cmdId) != -1)
00193         {
00194             if (KMessageBox::warningContinueCancel(
00195                 this,
00196                 i18n("A command named %1 already exists. Do you want "
00197                      "to continue and edit the existing one?").arg(cmdId),
00198                 QString::null,
00199                 i18n("Continue")) == KMessageBox::Cancel)
00200             {
00201                 return;
00202             }
00203             else
00204                 added = false;
00205     }
00206 
00207         KXmlCommand *xmlCmd = KXmlCommandManager::self()->loadCommand(cmdId);
00208         if (KXmlCommandDlg::editCommand(xmlCmd, this))
00209             KXmlCommandManager::self()->saveCommand(xmlCmd);
00210 
00211         if (added)
00212             loadCommands();
00213     }
00214 }
00215 
00216 void KXmlCommandSelector::slotEditCommand()
00217 {
00218     QString xmlId = m_cmdlist[m_cmd->currentItem()];
00219     KXmlCommand *xmlCmd = KXmlCommandManager::self()->loadCommand(xmlId);
00220     if (xmlCmd)
00221     {
00222         if (KXmlCommandDlg::editCommand(xmlCmd, this))
00223         {
00224             // force to load the driver if not already done
00225             xmlCmd->driver();
00226             KXmlCommandManager::self()->saveCommand(xmlCmd);
00227         }
00228         m_cmd->changeItem(xmlCmd->description(), m_cmd->currentItem());
00229         delete xmlCmd;
00230         slotCommandSelected(m_cmd->currentItem());
00231     }
00232     else
00233         KMessageBox::error(this, i18n("Internal Error. The XML driver for the command %1 could not be found.").arg(xmlId));
00234 }
00235 
00236 void KXmlCommandSelector::slotBrowse()
00237 {
00238     QString filename = KFileDialog::getOpenFileName(QString::null, QString::null, this);
00239     if (!filename.isEmpty() && m_line)
00240         m_line->setText(filename);
00241 }
00242 
00243 void KXmlCommandSelector::slotCommandSelected(int ID)
00244 {
00245     KXmlCommand *xmlCmd = KXmlCommandManager::self()->loadCommand(m_cmdlist[ID], true);
00246     if (xmlCmd)
00247     {
00248         QString msg;
00249         if ( xmlCmd->isValid() && KdeprintChecker::check( xmlCmd->requirements() ) )
00250         {
00251             msg = QString::fromLocal8Bit("(ID = %1, %2 = ").arg(xmlCmd->name()).arg(i18n("output"));
00252             if (KXmlCommandManager::self()->checkCommand(xmlCmd->name(), KXmlCommandManager::None, KXmlCommandManager::Basic))
00253             {
00254                 if (xmlCmd->mimeType() == "all/all")
00255                     msg.append(i18n("undefined"));
00256                 else
00257                     msg.append(xmlCmd->mimeType());
00258             }
00259             else
00260                 msg.append(i18n("not allowed"));
00261             msg.append(")");
00262             emit commandValid( true );
00263         }
00264         else
00265         {
00266             msg = "<font color=\"red\">" + i18n( "(Unavailable: requirements not satisfied)" ) + "</font>";
00267             emit commandValid( false );
00268         }
00269         m_shortinfo->setText(msg);
00270         m_help = xmlCmd->comment();
00271         m_helpbtn->setEnabled( !m_help.isEmpty() );
00272     }
00273     delete xmlCmd;
00274 }
00275 
00276 void KXmlCommandSelector::slotXmlCommandToggled( bool on )
00277 {
00278     if ( on )
00279         slotCommandSelected( m_cmd->currentItem() );
00280     else
00281     {
00282         emit commandValid( true );
00283         m_shortinfo->setText( QString::null );
00284     }
00285 }
00286 
00287 void KXmlCommandSelector::slotHelpCommand()
00288 {
00289     KPopupFrame *pop = new KPopupFrame( m_helpbtn );
00290     KActiveLabel *lab = new KActiveLabel( m_help, pop );
00291     lab->resize( lab->sizeHint() );
00292     pop->setMainWidget( lab );
00293     pop->exec( m_helpbtn->mapToGlobal( QPoint( m_helpbtn->width(), 0 ) ) );
00294     pop->close( 0 );
00295     delete pop;
00296 }
00297 
00298 #include "kxmlcommandselector.moc"
KDE Logo
This file is part of the documentation for kdeprint Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:36:25 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003