summaryrefslogtreecommitdiff
path: root/noncore/tools/opie-sh/inputdialog.cpp
blob: 8046795ca86b6633164ed462109bb509bce76856 (plain)
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
/*
Opie-sh.  convinience app to allow you to use qdialogs in scripts (mainly shell scripts)
Copyright (C) 2002 Thomas Stephens

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "inputdialog.h"

InputDialog::InputDialog(int w, int h, int newtype, QString labelString, QString title, QString filename, bool edit, QWidget *parent, const char *name, bool modal, WFlags f):QDialog(parent, name, modal, f)
{
	type = newtype;
	QHBoxLayout *layout = new QHBoxLayout(this);
	layout->addStrut(32);
	QLabel *label = new QLabel(labelString, this, "label");
	setCaption(title);
	int x, y;

	layout->addSpacing(5);
	layout->addWidget(label);
	layout->addSpacing(5);

	switch(type)
	{
	case 0:
		lineEdit = new QLineEdit(this, "line edit");
		layout->addWidget(lineEdit);
		break;
	case 1:
		comboBox = new QComboBox(edit, this, "combo box");
		layout->addWidget(comboBox);
		if(!filename.isNull())
		{
			QFile file(filename);
			file.open(IO_ReadOnly);
			QTextStream stream(&file);
			QString string = stream.read();

			comboBox->insertStringList(QStringList::split('\n', string));
		}
		else
		{
			QFile file;
			file.open(IO_ReadOnly, 0);
			QTextStream stream(&file);
			QString string = stream.read();

			comboBox->insertStringList(QStringList::split('\n', string));
		}
		break;
	case 2:
		listBox = new QListBox(this, "list box");
		listBox->setSelectionMode(QListBox::Multi);
		layout->addWidget(listBox);
		if(!filename.isNull())
		{
			QFile file(filename);
			file.open(IO_ReadOnly);
			QTextStream stream(&file);
			QString string = stream.read();

			listBox->insertStringList(QStringList::split('\n', string));
		}
		else
		{
			QFile file;
			file.open(IO_ReadOnly, 0);
			QTextStream stream(&file);
			QString string = stream.read();

			listBox->insertStringList(QStringList::split('\n', string));
		}
		break;
	case 3:
		lineEdit = new QLineEdit(this, "line edit");
		lineEdit->setEchoMode(QLineEdit::Password);
		layout->addWidget(lineEdit);
		break;
	}
	layout->addSpacing(5);

	x=(w/2)-(width()/2);
	y=(h/2)-(height()/2);

	move(x,y);
}

QString InputDialog::getString()
{
	switch (type)
	{
	case 0:
	case 3:
		return ((QLineEdit *)child("line edit"))->text();
		break;
	case 1:
		return ((QComboBox *)child("combo box"))->currentText();
		break;
	case 2:
		QString string;
		int i;
		for(i = 0; i < listBox->count(); i++)
		{
			if(listBox->isSelected(i))
			{
				string+=listBox->text(i)+'\n';
			}
		}
		if(string[string.length()-1] == '\n')
		{
			string.truncate(string.length()-1);
		}
		return string;
	}
	return QString::null;
}