File: pgmargs.cpp

package info (click to toggle)
kdbg 1.2.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,504 kB
  • ctags: 1,945
  • sloc: cpp: 15,961; sh: 8,183; makefile: 481; perl: 71; ansic: 16
file content (261 lines) | stat: -rw-r--r-- 6,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// $Id: pgmargs.cpp,v 1.16 2001/04/16 17:24:44 jsixt Exp $

// Copyright by Johannes Sixt
// This file is under GPL, the GNU General Public Licence

#include "pgmargs.h"
#include <kapp.h>
#include <kfiledialog.h>
#if QT_VERSION >= 200
#include <klocale.h>			/* i18n */
#endif
#include "mydebug.h"

PgmArgs::PgmArgs(QWidget* parent, const QString& pgm, QDict<EnvVar>& envVars) :
	QDialog(parent, "pgmargs", true),
	m_envVars(envVars),
	m_label(this, "label"),
	m_programArgs(this, "args"),
	m_wdLabel(this, "wd_label"),
	m_wd(this, "wd"),
	m_wdBrowse(this, "wd_browse"),
	m_envLabel(this, "env_label"),
	m_envVar(this, "env_var"),
	m_envList(this, "env_list"),
	m_buttonOK(this, "ok"),
	m_buttonCancel(this, "cancel"),
	m_buttonModify(this, "modify"),
	m_buttonDelete(this, "delete"),
	m_layout(this, 8),
	m_edits(8),
	m_buttons(8),
	m_wdEdit(0)
{
    m_envVars.setAutoDelete(false);

    QString title = kapp->getCaption();
    title += i18n(": Program arguments");
    setCaption(title);

    QString fmt = i18n("Run %s with these arguments:");
#if QT_VERSION < 200
    QString lab(fmt.length() + pgm.length());
#else
    QString lab;
#endif
    lab.sprintf(fmt, pgm.data());
    m_label.setText(lab);
    QSize s = m_label.sizeHint();
    /* don't make the label too wide if pgm name is very long */
    if (s.width() > 450)
	s.setWidth(450);
    m_label.setMinimumSize(s);

    s = m_programArgs.sizeHint();
    m_programArgs.setMinimumSize(s);
    m_programArgs.setMaxLength(10000);

    m_wdLabel.setText(i18n("Working directory:"));
    s = m_wdLabel.sizeHint();
    m_wdLabel.setMinimumSize(s);
    int btnSpace = s.height();

    m_wdBrowse.setText("...");
    s = m_wdBrowse.sizeHint();
    m_wdBrowse.setMinimumSize(s);
    connect(&m_wdBrowse, SIGNAL(clicked()), SLOT(browseWd()));

    s = m_wd.sizeHint();
    m_wd.setMinimumSize(s);
    m_wd.setMaxLength(10000);
    btnSpace += s.height();

    m_envLabel.setText(i18n("Environment variables (NAME=value):"));
    s = m_envLabel.sizeHint();
    m_envLabel.setMinimumSize(s);

    s = m_envVar.sizeHint();
    m_envVar.setMinimumSize(s);
    m_envVar.setMaxLength(10000);

    m_envList.setMinimumSize(330, 40);
    m_envList.addColumn(i18n("Name"), 100);
    m_envList.addColumn(i18n("Value"), 260);
    connect(&m_envList, SIGNAL(currentChanged(QListViewItem*)),
	    SLOT(envListCurrentChanged(QListViewItem*)));
    initEnvList();

    m_buttonOK.setText(i18n("OK"));
    m_buttonOK.setDefault(true);
    s = m_buttonOK.sizeHint();
    m_buttonOK.setMinimumSize(s);
    connect(&m_buttonOK, SIGNAL(clicked()), SLOT(accept()));

    m_buttonCancel.setText(i18n("Cancel"));
    s = m_buttonCancel.sizeHint();
    m_buttonCancel.setMinimumSize(s);
    connect(&m_buttonCancel, SIGNAL(clicked()), SLOT(reject()));

    m_buttonModify.setText(i18n("&Modify"));
    s = m_buttonModify.sizeHint();
    m_buttonModify.setMinimumSize(s);
    connect(&m_buttonModify, SIGNAL(clicked()), SLOT(modifyVar()));

    m_buttonDelete.setText(i18n("&Delete"));
    s = m_buttonDelete.sizeHint();
    m_buttonDelete.setMinimumSize(s);
    connect(&m_buttonDelete, SIGNAL(clicked()), SLOT(deleteVar()));

    m_layout.addLayout(&m_edits, 10);
    m_layout.addLayout(&m_buttons);
    m_edits.addWidget(&m_label);
    m_edits.addWidget(&m_programArgs);
    m_edits.addWidget(&m_wdLabel);
    m_edits.addLayout(&m_wdEdit);
    m_edits.addWidget(&m_envLabel);
    m_edits.addWidget(&m_envVar);
    m_edits.addWidget(&m_envList, 10);
    m_buttons.addWidget(&m_buttonOK);
    m_buttons.addWidget(&m_buttonCancel);
    m_buttons.addSpacing(btnSpace + 2*m_edits.defaultBorder());
    m_buttons.addWidget(&m_buttonModify);
    m_buttons.addWidget(&m_buttonDelete);
    m_buttons.addStretch(10);
    m_wdEdit.addWidget(&m_wd, 10);
    m_wdEdit.addWidget(&m_wdBrowse);

    m_layout.activate();

    m_programArgs.setFocus();
    resize(300, 300);
}

PgmArgs::~PgmArgs()
{
}

void PgmArgs::modifyVar()
{
    QString name, value;
    parseEnvInput(name, value);
    if (name.isEmpty() || name.find(' ') >= 0)	/* disallow spaces in names */
	return;

    // lookup the value in the dictionary
    EnvVar* val = m_envVars[name];
    if (val != 0) {
	// see if this is a zombie
	if (val->status == EnvVar::EVdeleted) {
	    // resurrect
	    val->value = value;
	    val->status = EnvVar::EVdirty;
	    val->item = new QListViewItem(&m_envList, name, value);	// inserts itself
	    m_envVars.insert(name, val);
	} else if (value != val->value) {
	    // change the value
	    val->value = value;
	    val->status = EnvVar::EVdirty;
	    val->item->setText(1, value);
	}
    } else {
	// add the value
	val = new EnvVar;
	val->value = value;
	val->status = EnvVar::EVnew;
	val->item = new QListViewItem(&m_envList, name, value);	// inserts itself
	m_envVars.insert(name, val);
    }
}

void PgmArgs::deleteVar()
{
    QString name, value;
    parseEnvInput(name, value);

    // lookup the value in the dictionary
    EnvVar* val = m_envVars[name];
    if (val == 0)
	return;

    // delete from list
    QListViewItem* item = val->item;
    val->item = 0;
    delete item;
    // if this is a new item, delete it completely, otherwise zombie-ize it
    if (val->status == EnvVar::EVnew) {
	m_envVars.remove(name);
	delete val;
    } else {
	// mark value deleted
	val->status = EnvVar::EVdeleted;
    }
    // clear the input
    m_envVar.setText("");
}

void PgmArgs::parseEnvInput(QString& name, QString& value)
{
    // parse input from edit field
    QString input = m_envVar.text();
    int equalSign = input.find('=');
    if (equalSign >= 0) {
	name = input.left(equalSign).stripWhiteSpace();
#if QT_VERSION < 200
	value = input.mid(equalSign+1,input.length());
#else
	value = input.mid(equalSign+1);
#endif
    } else {
	name = input.stripWhiteSpace();
	value = QString();		/* value is empty */
    }
}

void PgmArgs::initEnvList()
{
    QDictIterator<EnvVar> it = m_envVars;
    EnvVar* val;
    QString name;
    for (; (val = it) != 0; ++it) {
	val->status = EnvVar::EVclean;
	name = it.currentKey();
	val->item = new QListViewItem(&m_envList, name, val->value);	// inserts itself
    }
}

void PgmArgs::envListCurrentChanged(QListViewItem* item)
{
    if (item == 0)
	return;

    // must get name from list box
    QString name = item->text(0);
    EnvVar* val = m_envVars[name];
    ASSERT(val != 0);
    if (val != 0) {
	m_envVar.setText(name + "=" + val->value);
    } else {
	m_envVar.setText(name);
    }
}

void PgmArgs::accept()
{
    modifyVar();
    QDialog::accept();
}

void PgmArgs::browseWd()
{
    // browse for the working directory
#if QT_VERSION < 200
    QString newDir = KDirDialog::getDirectory(wd(), this);
#else
    QString newDir = KFileDialog::getExistingDirectory(wd(), this);
#endif
    if (!newDir.isEmpty()) {
	setWd(newDir);
    }
}

#include "pgmargs.moc"