Updated documentation.
[kdbg.git] / kdbg / pgmargs.cpp
blobeb2ec17f6f5dc833c3d8aefd5408c011c9261079
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "pgmargs.h"
7 #include <kapp.h>
8 #if QT_VERSION >= 200
9 #include <klocale.h> /* i18n */
10 #endif
11 #include "mydebug.h"
13 PgmArgs::PgmArgs(QWidget* parent, const QString& pgm, QDict<EnvVar>& envVars) :
14 QDialog(parent, "pgmargs", true),
15 m_envVars(envVars),
16 m_label(this, "label"),
17 m_programArgs(this, "args"),
18 m_wdLabel(this, "wd_label"),
19 m_wd(this, "wd"),
20 m_envLabel(this, "env_label"),
21 m_envVar(this, "env_var"),
22 m_envList(this, "env_list"),
23 m_buttonOK(this, "ok"),
24 m_buttonCancel(this, "cancel"),
25 m_buttonModify(this, "modify"),
26 m_buttonDelete(this, "delete"),
27 m_layout(this, 8),
28 m_edits(8),
29 m_buttons(8)
31 m_envVars.setAutoDelete(false);
33 QString title = kapp->getCaption();
34 title += i18n(": Program arguments");
35 setCaption(title);
37 QString fmt = i18n("Run %s with these arguments:");
38 #if QT_VERSION < 200
39 QString lab(fmt.length() + pgm.length());
40 #else
41 QString lab;
42 #endif
43 lab.sprintf(fmt, pgm.data());
44 m_label.setText(lab);
45 QSize s = m_label.sizeHint();
46 /* don't make the label too wide if pgm name is very long */
47 if (s.width() > 450)
48 s.setWidth(450);
49 m_label.setMinimumSize(s);
51 s = m_programArgs.sizeHint();
52 m_programArgs.setMinimumSize(s);
53 m_programArgs.setMaxLength(10000);
55 m_wdLabel.setText(i18n("Working directory:"));
56 s = m_wdLabel.sizeHint();
57 m_wdLabel.setMinimumSize(s);
58 int btnSpace = s.height();
60 s = m_wd.sizeHint();
61 m_wd.setMinimumSize(s);
62 m_wd.setMaxLength(10000);
63 btnSpace += s.height();
65 m_envLabel.setText(i18n("Environment variables (NAME=value):"));
66 s = m_envLabel.sizeHint();
67 m_envLabel.setMinimumSize(s);
69 s = m_envVar.sizeHint();
70 m_envVar.setMinimumSize(s);
71 m_envVar.setMaxLength(10000);
73 m_envList.setMinimumSize(330, 40);
74 m_envList.addColumn(i18n("Name"), 100);
75 m_envList.addColumn(i18n("Value"), 260);
76 /* work around a non-feature of KTabListBox: */
77 m_envList.setFocusPolicy(QWidget::StrongFocus);
78 connect(&m_envList, SIGNAL(currentChanged(QListViewItem*)),
79 SLOT(envListCurrentChanged(QListViewItem*)));
80 initEnvList();
82 m_buttonOK.setText(i18n("OK"));
83 m_buttonOK.setDefault(true);
84 s = m_buttonOK.sizeHint();
85 m_buttonOK.setMinimumSize(s);
86 connect(&m_buttonOK, SIGNAL(clicked()), SLOT(accept()));
88 m_buttonCancel.setText(i18n("Cancel"));
89 s = m_buttonCancel.sizeHint();
90 m_buttonCancel.setMinimumSize(s);
91 connect(&m_buttonCancel, SIGNAL(clicked()), SLOT(reject()));
93 m_buttonModify.setText(i18n("&Modify"));
94 s = m_buttonModify.sizeHint();
95 m_buttonModify.setMinimumSize(s);
96 connect(&m_buttonModify, SIGNAL(clicked()), SLOT(modifyVar()));
98 m_buttonDelete.setText(i18n("&Delete"));
99 s = m_buttonDelete.sizeHint();
100 m_buttonDelete.setMinimumSize(s);
101 connect(&m_buttonDelete, SIGNAL(clicked()), SLOT(deleteVar()));
103 m_layout.addLayout(&m_edits, 10);
104 m_layout.addLayout(&m_buttons);
105 m_edits.addWidget(&m_label);
106 m_edits.addWidget(&m_programArgs);
107 m_edits.addWidget(&m_wdLabel);
108 m_edits.addWidget(&m_wd);
109 m_edits.addWidget(&m_envLabel);
110 m_edits.addWidget(&m_envVar);
111 m_edits.addWidget(&m_envList, 10);
112 m_buttons.addWidget(&m_buttonOK);
113 m_buttons.addWidget(&m_buttonCancel);
114 m_buttons.addSpacing(btnSpace + 2*m_edits.defaultBorder());
115 m_buttons.addWidget(&m_buttonModify);
116 m_buttons.addWidget(&m_buttonDelete);
117 m_buttons.addStretch(10);
119 m_layout.activate();
121 m_programArgs.setFocus();
122 resize(300, 300);
125 PgmArgs::~PgmArgs()
129 void PgmArgs::modifyVar()
131 QString name, value;
132 parseEnvInput(name, value);
133 if (name.isEmpty() || name.find(' ') >= 0) /* disallow spaces in names */
134 return;
136 // lookup the value in the dictionary
137 EnvVar* val = m_envVars[name];
138 if (val != 0) {
139 // see if this is a zombie
140 if (val->status == EnvVar::EVdeleted) {
141 // resurrect
142 val->value = value;
143 val->status = EnvVar::EVdirty;
144 val->item = new QListViewItem(&m_envList, name, value); // inserts itself
145 m_envVars.insert(name, val);
146 } else if (value != val->value) {
147 // change the value
148 val->value = value;
149 val->status = EnvVar::EVdirty;
150 val->item->setText(1, value);
152 } else {
153 // add the value
154 val = new EnvVar;
155 val->value = value;
156 val->status = EnvVar::EVnew;
157 val->item = new QListViewItem(&m_envList, name, value); // inserts itself
158 m_envVars.insert(name, val);
162 void PgmArgs::deleteVar()
164 QString name, value;
165 parseEnvInput(name, value);
167 // lookup the value in the dictionary
168 EnvVar* val = m_envVars[name];
169 if (val == 0)
170 return;
172 // delete from list
173 QListViewItem* item = val->item;
174 val->item = 0;
175 delete item;
176 // if this is a new item, delete it completely, otherwise zombie-ize it
177 if (val->status == EnvVar::EVnew) {
178 m_envVars.remove(name);
179 delete val;
180 } else {
181 // mark value deleted
182 val->status = EnvVar::EVdeleted;
184 // clear the input
185 m_envVar.setText("");
188 void PgmArgs::parseEnvInput(QString& name, QString& value)
190 // parse input from edit field
191 QString input = m_envVar.text();
192 int equalSign = input.find('=');
193 if (equalSign >= 0) {
194 name = input.left(equalSign).stripWhiteSpace();
195 #if QT_VERSION < 200
196 value = input.mid(equalSign+1,input.length());
197 #else
198 value = input.mid(equalSign+1);
199 #endif
200 } else {
201 name = input.stripWhiteSpace();
202 value = QString(); /* value is empty */
206 void PgmArgs::initEnvList()
208 QDictIterator<EnvVar> it = m_envVars;
209 EnvVar* val;
210 QString name;
211 for (; (val = it) != 0; ++it) {
212 val->status = EnvVar::EVclean;
213 name = it.currentKey();
214 val->item = new QListViewItem(&m_envList, name, val->value); // inserts itself
218 void PgmArgs::envListCurrentChanged(QListViewItem* item)
220 if (item == 0)
221 return;
223 // must get name from list box
224 QString name = item->text(0);
225 EnvVar* val = m_envVars[name];
226 ASSERT(val != 0);
227 if (val != 0) {
228 m_envVar.setText(name + "=" + val->value);
229 } else {
230 m_envVar.setText(name);
234 void PgmArgs::accept()
236 modifyVar();
237 QDialog::accept();
240 #include "pgmargs.moc"