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