File: dialogs.C

package info (click to toggle)
qps 1.4.4-1
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 580 kB
  • ctags: 1,049
  • sloc: cpp: 6,545; makefile: 129; sh: 1
file content (200 lines) | stat: -rw-r--r-- 5,779 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
// dialogs.C
//
// This program is free software. See the file COPYING for details.
// Author: Mattias Engdegrd, 1997, 1998

#include "dialogs.h"
#include "warn.xpm"
#include <qaccel.h>
#include <qapp.h>

// center window a with respect to main application window
void center_window(QWidget *a)
{
    QWidget *b = qApp->mainWidget();
    a->move(b->x() + (b->width() - a->width()) / 2,
	    b->y() + (b->height() - a->height()) / 2);
}

// ValueDialog: modal dialog to input a single string value

ValueDialog::ValueDialog(const char *caption, const char *msg,
			 const char *ed_txt)
            : QDialog(0, caption, TRUE)
{
    setCaption(caption);
    ok = new QPushButton("OK", this);
    ok->setGeometry(20, 50, 64, 24);
    connect(ok, SIGNAL(clicked()), SLOT(done_dialog()));
    ok->setDefault(TRUE);
    cancel = new QPushButton("Cancel", this);
    cancel->setGeometry(130, 50, 64, 24);
    connect(cancel, SIGNAL(clicked()), SLOT(reject()));
    label = new QLabel(msg, this);
    label->setGeometry(10, 10, 120, 30);
    lined = new QLineEdit(this);
    QFont f = font();
    f.setBold(FALSE);
    lined->setFont(f);
    lined->resize(50, lined->sizeHint().height());
    lined->move(140, label->y() + (label->height() - lined->height()) / 2);
    lined->setMaxLength(7);
    lined->setText(ed_txt);
    lined->selectAll();
    lined->setFocus();
    connect(lined, SIGNAL(returnPressed()), SLOT(done_dialog()));
    QAccel *acc = new QAccel(this);
    acc->connectItem(acc->insertItem(Key_Escape), this, SLOT(reject()));
    adjustSize();
    center_window(this);
}

void ValueDialog::done_dialog()
{
    ed_result = lined->text();
    ed_result = ed_result.stripWhiteSpace();
    accept();
}

// MessageDialog: modal dialog that just gives a message

MessageDialog::MessageDialog(QWidget *parent)
             : QDialog(parent, 0, TRUE)
{
    label = new QLabel(this);
    label->setAlignment(AlignCenter);
    icon = 0;			// no icon by default
    button = new QPushButton("OK", this);
    connect(button, SIGNAL(clicked()), SLOT(accept()));
}

void MessageDialog::setText(const char *text)
{
    label->setText(text);
}

void MessageDialog::setButtonText(const char *buttonText)
{
    button->setText(buttonText);
}

void MessageDialog::setIcon(QPixmap *pixmap)
{
    if(pixmap) {
	if(!icon) {
	    icon = new QLabel(this);
	    label->setAlignment(AlignLeft | AlignVCenter);
	}
	icon->setPixmap(*pixmap);
    }
}

void MessageDialog::adjustSize()
{
    button->adjustSize();
    // don't let the button be too narrow
    if(button->width() < button_minwidth)
	button->resize(button_minwidth, button->height());
    button->setDefault(TRUE);	// do this _after_ adjusting button size

    label->adjustSize();
    if(icon)
	icon->adjustSize();

    int label_x;
    // give generous margins in all directions
    if(icon) {
	icon->move(margin_side, margin_top);
	label_x = margin_side + icon->width() + margin_side;
    } else {
	label_x = margin_side;
    }
    label->move(label_x, margin_top);
    button->move(label_x + (label->width() - button->width()) / 2,
		 margin_top + label->height() + margin_middle);
    resize(label_x + label->width() + margin_side,
	   button->y() + button->height() + margin_bottom);
}

// static function: pop up a simple modal message box
void MessageDialog::message(const char *caption, const char *text,
			    const char *buttonText,
			    QPixmap *icon)
{
    MessageDialog *md = new MessageDialog();
    md->setIcon(icon);
    md->setCaption(caption);
    md->setText(text);
    if(buttonText)
	md->setButtonText(buttonText);
    md->adjustSize();
    center_window(md);
    md->exec();
    delete md;
}

QPixmap *MessageDialog::warn_icon = 0;

// static function: return a warning icon, for use in alert boxes
QPixmap *MessageDialog::warningIcon()
{
    if(!warn_icon)
	warn_icon = new QPixmap((const char**)warn_xpm);
    return warn_icon;
}

SliderDialog::SliderDialog(const char *caption, const char *msg,
			   int defaultval, int minval, int maxval,
			   const char *leftlbl, const char *rightlbl)
            : ValueDialog(caption, msg, "")
{
    QString s;
    s.setNum(defaultval);
    lined->setText(s);
    lined->selectAll();

    slider = new QSlider(minval, maxval, 1, defaultval, QSlider::Horizontal,
			 this);
    slider->setTickInterval(10);
    slider->setTickmarks(QSlider::Below);
    slider->setFixedHeight(slider->sizeHint().height());
    slider->setGeometry(10, 48, 200, slider->height());
    connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slider_change(int)));
    // decorate slider
    QLabel *left = new QLabel(this);
    QLabel *mid = new QLabel(this);
    QLabel *right = new QLabel(this);
    left->setNum(minval);
    mid->setNum((minval + maxval) / 2);
    right->setNum(maxval);
    
    left->adjustSize();
    left->move(slider->x(), slider->y() + slider->height());
    mid->adjustSize();
    mid->move(slider->x() + (slider->width() - mid->width()) / 2, left->y());
    right->adjustSize();
    right->move(slider->x() + slider->width() - right->width(), left->y());
    QLabel *leftl = new QLabel(leftlbl, this);
    leftl->adjustSize();
    leftl->move(slider->x(), left->y()+ left->height());
    QLabel *rightl = new QLabel(rightlbl, this);
    rightl->adjustSize();
    rightl->move(slider->x() + slider->width() - rightl->width(),
		 right->y()+ right->height());

    // rearrange geometry from ValueDialog
    ok->setDefault(FALSE);
    ok->move(30, leftl->y() + leftl->height() + 15);
    cancel->move(130, ok->y());
    ok->setDefault(TRUE);	// because Motif buttons are bigger as default
    adjustSize();
    center_window(this);
}

void SliderDialog::slider_change(int val)
{
    QString s;
    s.setNum(val);
    lined->setText(s);
    lined->selectAll();
}