20 #include "kxmlcommandselector.h"
21 #include "kxmlcommand.h"
22 #include "kxmlcommanddlg.h"
23 #include "tdeprintcheck.h"
25 #include <tqcombobox.h>
26 #include <kpushbutton.h>
28 #include <tqcheckbox.h>
30 #include <tqtooltip.h>
31 #include <tqlineedit.h>
32 #include <kinputdialog.h>
33 #include <tdelocale.h>
34 #include <kiconloader.h>
35 #include <tdemessagebox.h>
36 #include <tdefiledialog.h>
37 #include <kseparator.h>
39 #include <kactivelabel.h>
41 #include <kdialogbase.h>
43 KXmlCommandSelector::KXmlCommandSelector(
bool canBeNull, TQWidget *parent,
const char *name, KDialogBase *dlg)
44 : TQWidget(parent, name)
46 m_cmd =
new TQComboBox(
this);
47 connect(m_cmd, TQ_SIGNAL(activated(
int)), TQ_SLOT(slotCommandSelected(
int)));
48 TQPushButton *m_add =
new KPushButton(
this);
49 TQPushButton *m_edit =
new KPushButton(
this);
50 m_add->setPixmap(SmallIcon(
"document-new"));
51 m_edit->setPixmap(SmallIcon(
"configure"));
52 connect(m_add, TQ_SIGNAL(clicked()), TQ_SLOT(slotAddCommand()));
53 connect(m_edit, TQ_SIGNAL(clicked()), TQ_SLOT(slotEditCommand()));
54 TQToolTip::add(m_add, i18n(
"New command"));
55 TQToolTip::add(m_edit, i18n(
"Edit command"));
56 m_shortinfo =
new TQLabel(
this);
57 m_helpbtn =
new KPushButton(
this );
58 m_helpbtn->setIconSet( SmallIconSet(
"help" ) );
59 connect( m_helpbtn, TQ_SIGNAL( clicked() ), TQ_SLOT( slotHelpCommand() ) );
60 TQToolTip::add( m_helpbtn, i18n(
"Information" ) );
61 m_helpbtn->setEnabled(
false );
65 TQPushButton *m_browse = 0;
67 TQVBoxLayout *l0 =
new TQVBoxLayout(
this, 0, 10);
71 m_line =
new TQLineEdit(
this);
72 m_browse =
new KPushButton(KGuiItem(i18n(
"&Browse..."),
"document-open"),
this);
73 m_usefilter =
new TQCheckBox(i18n(
"Use co&mmand:"),
this);
74 connect(m_browse, TQ_SIGNAL(clicked()), TQ_SLOT(slotBrowse()));
75 connect(m_usefilter, TQ_SIGNAL(toggled(
bool)), m_line, TQ_SLOT(setDisabled(
bool)));
76 connect(m_usefilter, TQ_SIGNAL(toggled(
bool)), m_browse, TQ_SLOT(setDisabled(
bool)));
77 connect(m_usefilter, TQ_SIGNAL(toggled(
bool)), m_cmd, TQ_SLOT(setEnabled(
bool)));
78 connect(m_usefilter, TQ_SIGNAL(toggled(
bool)), m_add, TQ_SLOT(setEnabled(
bool)));
79 connect(m_usefilter, TQ_SIGNAL(toggled(
bool)), m_edit, TQ_SLOT(setEnabled(
bool)));
80 connect(m_usefilter, TQ_SIGNAL(toggled(
bool)), m_shortinfo, TQ_SLOT(setEnabled(
bool)));
81 connect( m_usefilter, TQ_SIGNAL( toggled(
bool ) ), TQ_SLOT( slotXmlCommandToggled(
bool ) ) );
82 m_usefilter->setChecked(
true);
83 m_usefilter->setChecked(
false);
85 setTabOrder(m_usefilter, m_cmd);
86 setTabOrder(m_cmd, m_add);
87 setTabOrder(m_add, m_edit);
89 TQHBoxLayout *l1 =
new TQHBoxLayout(0, 0, 10);
91 l1->addWidget(m_line);
92 l1->addWidget(m_browse);
94 KSeparator *sep =
new KSeparator(TQt::Horizontal,
this);
100 TQGridLayout *l2 =
new TQGridLayout(0, 2, (m_usefilter?3:2), 0, 5);
105 l2->addWidget(m_usefilter, 0, c++);
107 l2->addWidget(m_cmd, 0, c);
108 TQHBoxLayout *l4 =
new TQHBoxLayout( 0, 0, 5 );
109 l2->addLayout( l4, 1, c );
110 l4->addWidget( m_helpbtn, 0 );
111 l4->addWidget( m_shortinfo, 1 );
112 TQHBoxLayout *l3 =
new TQHBoxLayout(0, 0, 0);
113 l2->addLayout(l3, 0, c+1);
114 l3->addWidget(m_add);
115 l3->addWidget(m_edit);
118 connect(
this, TQ_SIGNAL( commandValid(
bool ) ), dlg, TQ_SLOT( enableButtonOK(
bool ) ) );
123 void KXmlCommandSelector::loadCommands()
125 TQString thisCmd = (m_cmd->currentItem() != -1 ? m_cmdlist[m_cmd->currentItem()] : TQString::null);
130 TQStringList list = KXmlCommandManager::self()->commandListWithDescription();
131 TQStringList desclist;
132 for (TQStringList::Iterator it=list.begin(); it!=list.end(); ++it)
138 m_cmd->insertStringList(desclist);
140 int index = m_cmdlist.findIndex(thisCmd);
142 m_cmd->setCurrentItem(index);
143 if (m_cmd->currentItem() != -1 && m_cmd->isEnabled())
144 slotCommandSelected(m_cmd->currentItem());
147 TQString KXmlCommandSelector::command()
const
150 if (m_line && !m_usefilter->isChecked())
151 cmd = m_line->text();
153 cmd = m_cmdlist[m_cmd->currentItem()];
157 void KXmlCommandSelector::setCommand(
const TQString& cmd)
159 int index = m_cmdlist.findIndex(cmd);
162 m_usefilter->setChecked(index != -1);
164 m_line->setText((index == -1 ? cmd : TQString::null));
166 m_cmd->setCurrentItem(index);
167 if (m_cmd->currentItem() != -1 && m_cmd->isEnabled())
168 slotCommandSelected(m_cmd->currentItem());
171 void KXmlCommandSelector::slotAddCommand()
174 TQString cmdId = KInputDialog::getText(i18n(
"Command Name"), i18n(
"Enter an identification name for the new command:"), TQString::null, &ok,
this);
179 if (m_cmdlist.findIndex(cmdId) != -1)
181 if (KMessageBox::warningContinueCancel(
183 i18n(
"A command named %1 already exists. Do you want "
184 "to continue and edit the existing one?").arg(cmdId),
186 KStdGuiItem::cont()) == KMessageBox::Cancel)
194 KXmlCommand *xmlCmd = KXmlCommandManager::self()->loadCommand(cmdId);
195 if (KXmlCommandDlg::editCommand(xmlCmd,
this))
196 KXmlCommandManager::self()->saveCommand(xmlCmd);
203 void KXmlCommandSelector::slotEditCommand()
205 TQString xmlId = m_cmdlist[m_cmd->currentItem()];
206 KXmlCommand *xmlCmd = KXmlCommandManager::self()->loadCommand(xmlId);
209 if (KXmlCommandDlg::editCommand(xmlCmd,
this))
213 KXmlCommandManager::self()->saveCommand(xmlCmd);
215 m_cmd->changeItem(xmlCmd->description(), m_cmd->currentItem());
217 slotCommandSelected(m_cmd->currentItem());
220 KMessageBox::error(
this, i18n(
"Internal error. The XML driver for the command %1 could not be found.").arg(xmlId));
223 void KXmlCommandSelector::slotBrowse()
225 TQString filename = KFileDialog::getOpenFileName(TQString::null, TQString::null,
this);
226 if (!filename.isEmpty() && m_line)
227 m_line->setText(filename);
230 void KXmlCommandSelector::slotCommandSelected(
int ID)
232 KXmlCommand *xmlCmd = KXmlCommandManager::self()->loadCommand(m_cmdlist[ID],
true);
236 if ( xmlCmd->isValid() && KdeprintChecker::check( xmlCmd->requirements() ) )
238 msg = TQString::fromLocal8Bit(
"(ID = %1, %2 = ").arg(xmlCmd->name()).arg(i18n(
"output"));
239 if (KXmlCommandManager::self()->checkCommand(xmlCmd->name(), KXmlCommandManager::None, KXmlCommandManager::Basic))
241 if (xmlCmd->mimeType() ==
"all/all")
242 msg.append(i18n(
"undefined"));
244 msg.append(xmlCmd->mimeType());
247 msg.append(i18n(
"not allowed"));
249 emit commandValid(
true );
253 msg =
"<font color=\"red\">" + i18n(
"(Unavailable: requirements not satisfied)" ) +
"</font>";
254 emit commandValid(
false );
256 m_shortinfo->setText(msg);
257 m_help = xmlCmd->comment();
258 m_helpbtn->setEnabled( !m_help.isEmpty() );
263 void KXmlCommandSelector::slotXmlCommandToggled(
bool on )
266 slotCommandSelected( m_cmd->currentItem() );
269 emit commandValid(
true );
270 m_shortinfo->setText( TQString::null );
274 void KXmlCommandSelector::slotHelpCommand()
276 TDEPopupFrame *pop =
new TDEPopupFrame( m_helpbtn );
277 KActiveLabel *lab =
new KActiveLabel( m_help, pop );
278 lab->resize( lab->sizeHint() );
279 pop->setMainWidget( lab );
280 pop->exec( m_helpbtn->mapToGlobal( TQPoint( m_helpbtn->width(), 0 ) ) );
285 #include "kxmlcommandselector.moc"