3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
9 #include <klocale.h> /* i18n */
13 PgmArgs::PgmArgs(QWidget
* parent
, const QString
& pgm
, QDict
<EnvVar
>& envVars
) :
14 QDialog(parent
, "pgmargs", true),
16 m_label(this, "label"),
17 m_programArgs(this, "args"),
18 m_wdLabel(this, "wd_label"),
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"),
31 m_envVars
.setAutoDelete(false);
33 QString title
= kapp
->getCaption();
34 title
+= i18n(": Program arguments");
37 QString fmt
= i18n("Run %s with these arguments:");
39 QString
lab(fmt
.length() + pgm
.length());
43 lab
.sprintf(fmt
, pgm
.data());
45 QSize s
= m_label
.sizeHint();
46 /* don't make the label too wide if pgm name is very long */
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();
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
*)));
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);
121 m_programArgs
.setFocus();
129 void PgmArgs::modifyVar()
132 parseEnvInput(name
, value
);
133 if (name
.isEmpty() || name
.find(' ') >= 0) /* disallow spaces in names */
136 // lookup the value in the dictionary
137 EnvVar
* val
= m_envVars
[name
];
139 // see if this is a zombie
140 if (val
->status
== EnvVar::EVdeleted
) {
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
) {
149 val
->status
= EnvVar::EVdirty
;
150 val
->item
->setText(1, 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()
165 parseEnvInput(name
, value
);
167 // lookup the value in the dictionary
168 EnvVar
* val
= m_envVars
[name
];
173 QListViewItem
* item
= val
->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
);
181 // mark value deleted
182 val
->status
= EnvVar::EVdeleted
;
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();
196 value
= input
.mid(equalSign
+1,input
.length());
198 value
= input
.mid(equalSign
+1);
201 name
= input
.stripWhiteSpace();
202 value
= QString(); /* value is empty */
206 void PgmArgs::initEnvList()
208 QDictIterator
<EnvVar
> it
= m_envVars
;
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
)
223 // must get name from list box
224 QString name
= item
->text(0);
225 EnvVar
* val
= m_envVars
[name
];
228 m_envVar
.setText(name
+ "=" + val
->value
);
230 m_envVar
.setText(name
);
234 void PgmArgs::accept()
240 #include "pgmargs.moc"