Clean up #includes.
[kdbg.git] / kdbg / pgmargs.cpp
blob2155d3f56e7694d7b73f8dd645792e7da7b97454
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include "pgmargs.h"
8 #include <Q3ListView>
9 #include <kfiledialog.h>
10 #include <klocale.h> /* i18n */
11 #include "mydebug.h"
13 PgmArgs::PgmArgs(QWidget* parent, const QString& pgm, Q3Dict<EnvVar>& envVars,
14 const QStringList& allOptions) :
15 KDialog(parent),
16 m_envVars(envVars)
18 setupUi(this);
20 m_envVars.setAutoDelete(false);
23 QFileInfo fi = pgm;
24 QString newText = labelArgs->text().arg(fi.fileName());
25 labelArgs->setText(newText);
28 // add options only if the option list is non-empty
29 if (!allOptions.isEmpty())
31 xsldbgOptions->insertStringList(allOptions);
33 else
35 delete xsldbgOptionsPage;
36 xsldbgOptionsPage = 0;
39 initEnvList();
42 PgmArgs::~PgmArgs()
46 // initializes the selected options
47 void PgmArgs::setOptions(const QStringList& selectedOptions)
49 QStringList::ConstIterator it;
50 for (it = selectedOptions.begin(); it != selectedOptions.end(); ++it) {
51 for (uint i = 0; i < xsldbgOptions->count(); i++) {
52 if (xsldbgOptions->text(i) == *it) {
53 xsldbgOptions->setSelected(i, true);
54 break;
60 // returns the selected options
61 QStringList PgmArgs::options() const
63 QStringList sel;
64 if (xsldbgOptionsPage != 0)
66 for (uint i = 0; i < xsldbgOptions->count(); i++) {
67 if (xsldbgOptions->isSelected(i))
68 sel.append(xsldbgOptions->text(i));
71 return sel;
74 // this is a slot
75 void PgmArgs::on_buttonModify_clicked()
77 modifyVar(true); // re-add deleted entries
80 void PgmArgs::modifyVar(bool resurrect)
82 QString name, value;
83 parseEnvInput(name, value);
84 if (name.isEmpty() || name.find(' ') >= 0) /* disallow spaces in names */
85 return;
87 // lookup the value in the dictionary
88 EnvVar* val = m_envVars[name];
89 if (val != 0) {
90 // see if this is a zombie
91 if (val->status == EnvVar::EVdeleted) {
92 // resurrect
93 if (resurrect)
95 val->value = value;
96 val->status = EnvVar::EVdirty;
97 val->item = new Q3ListViewItem(envList, name, value); // inserts itself
98 m_envVars.insert(name, val);
100 } else if (value != val->value) {
101 // change the value
102 val->value = value;
103 val->status = EnvVar::EVdirty;
104 val->item->setText(1, value);
106 } else {
107 // add the value
108 val = new EnvVar;
109 val->value = value;
110 val->status = EnvVar::EVnew;
111 val->item = new Q3ListViewItem(envList, name, value); // inserts itself
112 m_envVars.insert(name, val);
114 envList->setSelected(val->item, true);
115 buttonDelete->setEnabled(true);
118 // delete the selected item
119 void PgmArgs::on_buttonDelete_clicked()
121 Q3ListViewItem* item = envList->selectedItem();
122 if (item == 0)
123 return;
124 QString name = item->text(0);
126 // lookup the value in the dictionary
127 EnvVar* val = m_envVars[name];
128 if (val != 0)
130 // delete from list
131 val->item = 0;
132 // if this is a new item, delete it completely, otherwise zombie-ize it
133 if (val->status == EnvVar::EVnew) {
134 m_envVars.remove(name);
135 delete val;
136 } else {
137 // mark value deleted
138 val->status = EnvVar::EVdeleted;
141 delete item;
142 // there is no selected item anymore
143 buttonDelete->setEnabled(false);
146 void PgmArgs::parseEnvInput(QString& name, QString& value)
148 // parse input from edit field
149 QString input = envVar->text();
150 int equalSign = input.find('=');
151 if (equalSign >= 0) {
152 name = input.left(equalSign).stripWhiteSpace();
153 value = input.mid(equalSign+1);
154 } else {
155 name = input.stripWhiteSpace();
156 value = QString(); /* value is empty */
160 void PgmArgs::initEnvList()
162 Q3DictIterator<EnvVar> it = m_envVars;
163 EnvVar* val;
164 QString name;
165 for (; (val = it) != 0; ++it) {
166 val->status = EnvVar::EVclean;
167 name = it.currentKey();
168 val->item = new Q3ListViewItem(envList, name, val->value); // inserts itself
171 envList->setAllColumnsShowFocus(true);
172 buttonDelete->setEnabled(envList->selectedItem() != 0);
175 void PgmArgs::on_envList_selectionChanged()
177 Q3ListViewItem* item = envList->selectedItem();
178 buttonDelete->setEnabled(item != 0);
179 if (item == 0)
180 return;
182 // must get name from list box
183 QString name = item->text(0);
184 EnvVar* val = m_envVars[name];
185 ASSERT(val != 0);
186 if (val != 0) {
187 envVar->setText(name + "=" + val->value);
188 } else {
189 envVar->setText(name);
193 void PgmArgs::accept()
195 // simulate that the Modify button was pressed, but don't revive
196 // dead entries even if the user changed the edit box
197 modifyVar(false);
198 QDialog::accept();
201 void PgmArgs::on_wdBrowse_clicked()
203 // browse for the working directory
204 QString newDir = KFileDialog::getExistingDirectory(wd(), this);
205 if (!newDir.isEmpty()) {
206 setWd(newDir);
210 void PgmArgs::on_insertFile_clicked()
212 QString caption = i18n("Select a file name to insert as program argument");
214 // use the selection as default
215 QString f = programArgs->markedText();
216 f = KFileDialog::getSaveFileName(f, QString::null,
217 this, caption);
218 // don't clear the selection if no file was selected
219 if (!f.isEmpty()) {
220 programArgs->insert(f);
224 void PgmArgs::on_insertDir_clicked()
226 QString caption = i18n("Select a directory to insert as program argument");
228 // use the selection as default
229 QString f = programArgs->markedText();
230 f = KFileDialog::getExistingDirectory(f, this, caption);
231 // don't clear the selection if no file was selected
232 if (!f.isEmpty()) {
233 programArgs->insert(f);
237 #include "pgmargs.moc"