3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
8 #include <kfiledialog.h>
9 #include <klocale.h> /* i18n */
13 PgmArgs::PgmArgs(QWidget
* parent
, const QString
& pgm
, QDict
<EnvVar
>& envVars
,
14 const QStringList
& allOptions
) :
15 QDialog(parent
, "pgmargs", true),
17 m_label(this, "label"),
18 m_programArgs(this, "args"),
19 m_fileBrowse(this, "args_browse"),
20 m_optionsLabel(this, "options_label"),
21 m_options(this, "options"),
22 m_wdLabel(this, "wd_label"),
24 m_wdBrowse(this, "wd_browse"),
25 m_envLabel(this, "env_label"),
26 m_envVar(this, "env_var"),
27 m_envList(this, "env_list"),
28 m_buttonOK(this, "ok"),
29 m_buttonCancel(this, "cancel"),
30 m_buttonModify(this, "modify"),
31 m_buttonDelete(this, "delete"),
38 m_envVars
.setAutoDelete(false);
40 QString title
= kapp
->caption();
41 title
+= i18n(": Program arguments");
44 QString lab
= i18n("Run %1 with these arguments:");
47 m_label
.setText(lab
.arg(fi
.fileName()));
49 QSize s
= m_label
.sizeHint();
50 /* don't make the label too wide if pgm name is very long */
53 m_label
.setMinimumSize(s
);
55 s
= m_programArgs
.sizeHint();
56 m_programArgs
.setMinimumSize(s
);
57 m_programArgs
.setMaxLength(10000);
59 m_fileBrowse
.setText("...");
60 s
= m_fileBrowse
.sizeHint();
61 m_fileBrowse
.setMinimumSize(s
);
62 connect(&m_fileBrowse
, SIGNAL(clicked()), SLOT(browseArgs()));
64 int btnSpace
= 0, numSpace
= 0; /* used to shift env buttons down */
66 // add options only if the option list is non-empty
67 if (!allOptions
.isEmpty())
69 m_optionsLabel
.setText(i18n("Options:"));
70 s
= m_optionsLabel
.sizeHint();
71 m_optionsLabel
.setMinimumSize(s
);
72 btnSpace
+= s
.height();
74 m_options
.setSelectionMode(QListBox::Multi
);
76 // add 5 entries, then get size hint, then add the rest
77 QStringList rest
= allOptions
;
78 for (int i
= 0; i
< 5 && !rest
.isEmpty(); i
++) {
79 m_options
.insertItem(rest
.first());
80 rest
.remove(rest
.begin());
82 s
= m_options
.sizeHint();
83 m_options
.insertStringList(rest
);
85 m_options
.setMinimumSize(s
);
86 m_options
.setMaximumHeight(s
.height());
87 btnSpace
+= s
.height();
91 m_wdLabel
.setText(i18n("Working directory:"));
92 s
= m_wdLabel
.sizeHint();
93 m_wdLabel
.setMinimumSize(s
);
94 btnSpace
+= s
.height();
96 m_wdBrowse
.setText("...");
97 s
= m_wdBrowse
.sizeHint();
98 m_wdBrowse
.setMinimumSize(s
);
99 connect(&m_wdBrowse
, SIGNAL(clicked()), SLOT(browseWd()));
102 m_wd
.setMinimumSize(s
);
103 m_wd
.setMaxLength(10000);
104 btnSpace
+= s
.height();
106 m_envLabel
.setText(i18n("Environment variables (NAME=value):"));
107 s
= m_envLabel
.sizeHint();
108 m_envLabel
.setMinimumSize(s
);
110 s
= m_envVar
.sizeHint();
111 m_envVar
.setMinimumSize(s
);
112 m_envVar
.setMaxLength(10000);
114 m_envList
.setMinimumSize(330, 40);
115 m_envList
.addColumn(i18n("Name"), 100);
116 m_envList
.addColumn(i18n("Value"), 260);
117 connect(&m_envList
, SIGNAL(selectionChanged(QListViewItem
*)),
118 SLOT(envListCurrentChanged(QListViewItem
*)));
121 m_buttonOK
.setText(i18n("OK"));
122 m_buttonOK
.setDefault(true);
123 s
= m_buttonOK
.sizeHint();
124 m_buttonOK
.setMinimumSize(s
);
125 connect(&m_buttonOK
, SIGNAL(clicked()), SLOT(accept()));
127 m_buttonCancel
.setText(i18n("Cancel"));
128 s
= m_buttonCancel
.sizeHint();
129 m_buttonCancel
.setMinimumSize(s
);
130 connect(&m_buttonCancel
, SIGNAL(clicked()), SLOT(reject()));
132 m_buttonModify
.setText(i18n("&Modify"));
133 s
= m_buttonModify
.sizeHint();
134 m_buttonModify
.setMinimumSize(s
);
135 connect(&m_buttonModify
, SIGNAL(clicked()), SLOT(modifyVar()));
137 m_buttonDelete
.setText(i18n("&Delete"));
138 s
= m_buttonDelete
.sizeHint();
139 m_buttonDelete
.setMinimumSize(s
);
140 connect(&m_buttonDelete
, SIGNAL(clicked()), SLOT(deleteVar()));
142 m_layout
.addLayout(&m_edits
, 10);
143 m_layout
.addLayout(&m_buttons
);
144 m_edits
.addWidget(&m_label
);
145 m_edits
.addLayout(&m_pgmArgsEdit
);
146 if (!allOptions
.isEmpty()) {
147 m_edits
.addWidget(&m_optionsLabel
);
148 m_edits
.addWidget(&m_options
);
150 m_optionsLabel
.hide();
152 m_options
.setEnabled(false);
154 m_edits
.addWidget(&m_wdLabel
);
155 m_edits
.addLayout(&m_wdEdit
);
156 m_edits
.addWidget(&m_envLabel
);
157 m_edits
.addWidget(&m_envVar
);
158 m_edits
.addWidget(&m_envList
, 10);
159 m_buttons
.addWidget(&m_buttonOK
);
160 m_buttons
.addWidget(&m_buttonCancel
);
161 m_buttons
.addSpacing(btnSpace
+ numSpace
*m_edits
.defaultBorder());
162 m_buttons
.addWidget(&m_buttonModify
);
163 m_buttons
.addWidget(&m_buttonDelete
);
164 m_buttons
.addStretch(10);
165 m_pgmArgsEdit
.addWidget(&m_programArgs
, 10);
166 m_pgmArgsEdit
.addWidget(&m_fileBrowse
);
167 m_wdEdit
.addWidget(&m_wd
, 10);
168 m_wdEdit
.addWidget(&m_wdBrowse
);
170 m_programArgs
.setFocus();
177 // initializes the selected options
178 void PgmArgs::setOptions(const QStringList
& selectedOptions
)
180 QStringList::ConstIterator it
;
181 for (it
= selectedOptions
.begin(); it
!= selectedOptions
.end(); ++it
) {
182 for (uint i
= 0; i
< m_options
.count(); i
++) {
183 if (m_options
.text(i
) == *it
) {
184 m_options
.setSelected(i
, true);
191 // returns the selected options
192 QStringList
PgmArgs::options() const
195 for (uint i
= 0; i
< m_options
.count(); i
++) {
196 if (m_options
.isSelected(i
))
197 sel
.append(m_options
.text(i
));
202 void PgmArgs::modifyVar()
205 parseEnvInput(name
, value
);
206 if (name
.isEmpty() || name
.find(' ') >= 0) /* disallow spaces in names */
209 // lookup the value in the dictionary
210 EnvVar
* val
= m_envVars
[name
];
212 // see if this is a zombie
213 if (val
->status
== EnvVar::EVdeleted
) {
216 val
->status
= EnvVar::EVdirty
;
217 val
->item
= new QListViewItem(&m_envList
, name
, value
); // inserts itself
218 m_envVars
.insert(name
, val
);
219 } else if (value
!= val
->value
) {
222 val
->status
= EnvVar::EVdirty
;
223 val
->item
->setText(1, value
);
229 val
->status
= EnvVar::EVnew
;
230 val
->item
= new QListViewItem(&m_envList
, name
, value
); // inserts itself
231 m_envVars
.insert(name
, val
);
233 m_envList
.setSelected(val
->item
, true);
234 m_buttonDelete
.setEnabled(true);
237 // delete the selected item
238 void PgmArgs::deleteVar()
240 QListViewItem
* item
= m_envList
.selectedItem();
243 QString name
= item
->text(0);
245 // lookup the value in the dictionary
246 EnvVar
* val
= m_envVars
[name
];
251 // if this is a new item, delete it completely, otherwise zombie-ize it
252 if (val
->status
== EnvVar::EVnew
) {
253 m_envVars
.remove(name
);
256 // mark value deleted
257 val
->status
= EnvVar::EVdeleted
;
261 // there is no selected item anymore
262 m_buttonDelete
.setEnabled(false);
265 void PgmArgs::parseEnvInput(QString
& name
, QString
& value
)
267 // parse input from edit field
268 QString input
= m_envVar
.text();
269 int equalSign
= input
.find('=');
270 if (equalSign
>= 0) {
271 name
= input
.left(equalSign
).stripWhiteSpace();
272 value
= input
.mid(equalSign
+1);
274 name
= input
.stripWhiteSpace();
275 value
= QString(); /* value is empty */
279 void PgmArgs::initEnvList()
281 QDictIterator
<EnvVar
> it
= m_envVars
;
284 for (; (val
= it
) != 0; ++it
) {
285 val
->status
= EnvVar::EVclean
;
286 name
= it
.currentKey();
287 val
->item
= new QListViewItem(&m_envList
, name
, val
->value
); // inserts itself
290 m_envList
.setAllColumnsShowFocus(true);
291 m_buttonDelete
.setEnabled(m_envList
.selectedItem() != 0);
294 void PgmArgs::envListCurrentChanged(QListViewItem
* item
)
296 m_buttonDelete
.setEnabled(item
!= 0);
300 // must get name from list box
301 QString name
= item
->text(0);
302 EnvVar
* val
= m_envVars
[name
];
305 m_envVar
.setText(name
+ "=" + val
->value
);
307 m_envVar
.setText(name
);
311 void PgmArgs::accept()
317 void PgmArgs::browseWd()
319 // browse for the working directory
320 QString newDir
= KFileDialog::getExistingDirectory(wd(), this);
321 if (!newDir
.isEmpty()) {
326 void PgmArgs::browseArgs()
328 QString caption
= i18n("Select a file name to insert as program argument");
330 // use the selection as default
331 QString f
= m_programArgs
.markedText();
332 f
= KFileDialog::getSaveFileName(f
, QString::null
,
334 // don't clear the selection of no file was selected
336 m_programArgs
.insert(f
);
340 #include "pgmargs.moc"