source: tests/widget/widget.cpp@ 334

Last change on this file since 334 was 311, checked in by Dmitry A. Kuminov, 16 years ago

tests: widget: Added the QMenuBar test case.

File size: 14.0 KB
Line 
1#include <QDebug>
2
3#include <QtGui>
4
5#ifndef Q_WS_PM
6
7// For printing non-quoted QString's with QDebug)
8struct QDbgStr: public QString
9{
10 inline QDbgStr(const QString &str) : QString(str) {}
11};
12
13// Prints a non-quoted QString
14inline QDebug operator<<(QDebug dbg, const QDbgStr &str)
15{ dbg << str.toUtf8().constData(); return dbg; }
16
17QDbgStr qWidgetName(QWidget *w)
18{
19 if (w)
20 return QString()
21 .sprintf("%s.%s", w->metaObject()->className(),
22 w->objectName().isEmpty() ? "<noname>" :
23 w->objectName().toUtf8().constData());
24 return QString(QLatin1String("<no-widget>"));
25}
26
27#endif
28
29#define myDefFlagEx(var,fl,varstr,flstr) if (var & fl) { \
30 if (!varstr.isEmpty()) varstr += QLatin1String("|"); varstr += QLatin1String(flstr); \
31} else do {} while(0)
32
33#define myDefFlag(var,fl,varstr) myDefFlagEx(var,fl,varstr,#fl)
34#define myDefFlagCut(var,fl,varstr,pos) myDefFlagEx(var,fl,varstr,#fl + pos)
35
36
37QDebug operator<<(QDebug dbg, Qt::WindowStates st)
38{
39 QString name;
40 myDefFlagEx(st, Qt::WindowMinimized, name, "Min");
41 myDefFlagEx(st, Qt::WindowMaximized, name, "Max");
42 myDefFlagEx(st, Qt::WindowFullScreen, name, "Full");
43 myDefFlagEx(st, Qt::WindowActive, name, "Act");
44 if (name.isEmpty()) name = QLatin1String("None");
45 dbg.nospace() << "WindowState(" << QDbgStr(name) << ")";
46 return dbg.space();
47}
48
49QDebug operator<<(QDebug dbg, Qt::FocusReason r)
50{
51 QString name;
52 if (r == Qt::MouseFocusReason) name = "Mouse";
53 if (r == Qt::TabFocusReason) name = "Tab";
54 if (r == Qt::BacktabFocusReason) name = "Backtab";
55 if (r == Qt::ActiveWindowFocusReason) name = "ActWin";
56 if (r == Qt::PopupFocusReason) name = "Popup";
57 if (r == Qt::ShortcutFocusReason) name = "Shortcut";
58 if (r == Qt::MenuBarFocusReason) name = "MenuBar";
59 if (r == Qt::OtherFocusReason) name = "Other";
60 dbg.nospace() << "FocusReason(" << QDbgStr(name) << ")";
61 return dbg.space();
62}
63
64////////////////////////////////////////////////////////////////////////////////
65
66class MyChild : public QWidget
67{
68 Q_OBJECT
69
70public:
71
72 MyChild(QWidget *aParent) : QWidget(aParent), mPressed(0)
73 {
74 setFocusPolicy(Qt::StrongFocus);
75
76 resize(64, 32);
77 }
78
79 void paintEvent(QPaintEvent *aE)
80 {
81 qDebug() << qWidgetName(this) << __FUNCTION__
82 << ": " << aE->rect() << "focus" << hasFocus();
83
84 QPainter p(this);
85 p.setRenderHint(QPainter::Antialiasing);
86
87 if (hasFocus())
88 p.fillRect(aE->rect(), mPressed % 2 ? Qt::red : Qt::green);
89 else
90 p.fillRect(aE->rect(), Qt::gray);
91 }
92
93 void mousePressEvent(QMouseEvent *aE)
94 {
95 ++mPressed;
96 update();
97 }
98
99 void mouseReleaseEvent(QMouseEvent *aE)
100 {
101 ++mPressed;
102 update();
103 }
104
105 void focusInEvent(QFocusEvent *aE)
106 {
107 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
108 QWidget::focusInEvent(aE);
109 }
110
111 void focusOutEvent(QFocusEvent *aE)
112 {
113 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
114 QWidget::focusOutEvent(aE);
115 }
116
117private:
118
119 int mPressed;
120};
121
122////////////////////////////////////////////////////////////////////////////////
123
124class MyButton : public QPushButton
125{
126public:
127
128 MyButton(QWidget *aParent) : QPushButton(aParent)
129 {
130 QMenu *menu = new QMenu(aParent);
131 menu->addAction(QLatin1String("Action &1"));
132 menu->addAction(QLatin1String("Action &2"));
133 setMenu(menu);
134 }
135
136#if 0
137 void focusInEvent(QFocusEvent *aE)
138 {
139 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << text()
140 << "reason" << aE->reason()
141 << "focus" << (qApp->focusWidget() ?
142 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
143
144 QPushButton::focusInEvent(aE);
145 }
146
147 void focusOutEvent(QFocusEvent *aE)
148 {
149 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << text()
150 << "reason" << aE->reason()
151 << "focus" << (qApp->focusWidget() ?
152 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
153 QPushButton::focusOutEvent(aE);
154 }
155#endif
156};
157
158
159////////////////////////////////////////////////////////////////////////////////
160
161class MyCombo : public QComboBox
162{
163public:
164
165 MyCombo(QWidget *aParent) : QComboBox(aParent) {}
166
167#if 0
168 void focusInEvent(QFocusEvent *aE)
169 {
170 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << currentText()
171 << "reason" << aE->reason()
172 << "focus" << (qApp->focusWidget() ?
173 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
174 QComboBox::focusInEvent(aE);
175 }
176
177 void focusOutEvent(QFocusEvent *aE)
178 {
179 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << currentText()
180 << "reason" << aE->reason()
181 << "focus" << (qApp->focusWidget() ?
182 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
183 QComboBox::focusOutEvent(aE);
184 }
185#endif
186
187#if 0
188 void resizeEvent(QResizeEvent *aE)
189 {
190 qDebug() << __FUNCTION__ << ":" << currentText() << "g" << geometry() << "fg" << frameGeometry()
191 << "sz" << aE->size() << "old" << aE->oldSize();
192 }
193
194 void moveEvent(QMoveEvent *aE)
195 {
196 qDebug() << __FUNCTION__ << ":" << currentText() << " g" << geometry() << "fg" << frameGeometry()
197 << "pos" << aE->pos() << "old" << aE->oldPos();
198 }
199#endif
200
201#if 0
202 virtual void enterEvent(QEvent *event)
203 {
204 qDebug() << __FUNCTION__ << ":" << currentText();
205 }
206
207 virtual void leaveEvent(QEvent *event)
208 {
209 qDebug() << __FUNCTION__ << ":" << currentText();
210 }
211#endif
212
213#if 0
214 void mousePressEvent(QMouseEvent *aE)
215 {
216 qDebug() << __FUNCTION__ << ": btn" << aE->button()
217 << QDbgStr(QString().sprintf("btns %08X mods %08X",
218 (int) aE->buttons(), (int) aE->modifiers()))
219 << "gpos" << aE->globalPos() << "pos" << aE->pos();
220 QComboBox::mousePressEvent(aE);
221 }
222
223 void mouseReleaseEvent(QMouseEvent *aE)
224 {
225 qDebug() << __FUNCTION__ << ": btn" << aE->button()
226 << QDbgStr(QString().sprintf("btns %08X mods %08X",
227 (int) aE->buttons(), (int) aE->modifiers()))
228 << "gpos" << aE->globalPos() << "pos" << aE->pos();
229 QComboBox::mouseReleaseEvent(aE);
230 }
231
232 void contextMenuEvent(QContextMenuEvent *aE)
233 {
234 QMenu menu;
235 menu.addAction(QLatin1String("Action &1"));
236 menu.addAction(QLatin1String("Action &2"));
237 menu.exec(aE->globalPos());
238 winId();
239 }
240#endif
241};
242
243////////////////////////////////////////////////////////////////////////////////
244
245class MyWidget : public QWidget
246{
247public:
248
249 MyWidget() : mPressed(0)
250 {
251// setFocusPolicy(Qt::StrongFocus);
252
253#if 0
254 MyButton *btn1 = new MyButton(this);
255 btn1->setText(QLatin1String("Hello 1"));
256 btn1->setObjectName("Hello 1");
257 btn1->move(20, 20);
258 MyButton *btn2 = new MyButton(this);
259 btn2->setText(QLatin1String("Hello 2"));
260 btn2->setObjectName("Hello 2");
261 btn2->move(20, 60);
262
263// QComboBox *cb1 = new MyCombo(this);
264// cb1->addItem(QLatin1String("Test 1"));
265// cb1->addItem(QLatin1String("Test 2"));
266
267// QComboBox *cb2 = new MyCombo(this);
268// cb2->addItem(QLatin1String("Test 3"));
269// cb2->addItem(QLatin1String("Test 4"));
270
271// QVBoxLayout *mainLayout = new QVBoxLayout();
272// mainLayout->addWidget(btn1);
273// mainLayout->addWidget(btn2);
274// mainLayout->addWidget(cb1);
275// mainLayout->addWidget(cb2);
276
277// setLayout(mainLayout);
278#endif
279
280#if 1
281 QMenuBar *mb = new QMenuBar(this);
282
283 QMenu *menu = new QMenu(mb);
284 menu->setTitle ("Menu &1");
285 menu->addAction(QLatin1String("Action &1"));
286 menu->addAction(QLatin1String("Action &2"));
287 mb->addMenu(menu);
288
289 menu = new QMenu(mb);
290 menu->setTitle ("Menu &2");
291 menu->addAction(QLatin1String("Action &1"));
292 menu->addAction(QLatin1String("Action &2"));
293 mb->addMenu(menu);
294
295 menu = new QMenu(mb);
296 menu->setTitle ("Menu &3");
297 menu->addAction(QLatin1String("Action &1"));
298 menu->addAction(QLatin1String("Action &2"));
299 mb->addMenu(menu);
300#endif
301
302#if 0
303 new MyChild(this);
304#endif
305
306 };
307
308#if 0
309 void paintEvent(QPaintEvent *aE)
310 {
311 qDebug() << __FUNCTION__ <<": " << aE->rect();
312
313 QPainter p(this);
314 p.setRenderHint(QPainter::Antialiasing);
315
316 p.fillRect(0, 0, 20, 20, mPressed % 2 ? Qt::red : Qt::green);
317
318 p.setPen(Qt::black);
319 p.drawEllipse(10, 10, 10, 10);
320
321 //p.setFont(QFont("Arial", 12));
322 p.drawText(10, 30, QLatin1String("ABC"));
323 }
324#endif
325
326#if 0
327 void resizeEvent(QResizeEvent *aE)
328 {
329 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
330 << "sz" << aE->size() << "old" << aE->oldSize();
331 }
332
333 void moveEvent(QMoveEvent *aE)
334 {
335 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
336 << "pos" << aE->pos() << "old" << aE->oldPos();
337 }
338#endif
339
340#if 0
341 void focusInEvent(QFocusEvent *aE)
342 {
343 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
344 QWidget::focusInEvent(aE);
345 }
346
347 void focusOutEvent(QFocusEvent *aE)
348 {
349 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
350 QWidget::focusOutEvent(aE);
351 }
352#endif
353
354#if 0
355 void changeEvent(QEvent *aE)
356 {
357 switch (aE->type()) {
358 case QEvent::WindowStateChange: {
359 QWindowStateChangeEvent *e2 = (QWindowStateChangeEvent *)aE;
360 qDebug().nospace() << __FUNCTION__ << ": QWindowStateChangeEvent(" <<
361 e2->oldState() << "->" << windowState() << ")";
362 break;
363 }
364 default:
365 break;
366 }
367 }
368#endif
369
370#if 0
371 void keyPressEvent(QKeyEvent *aE)
372 {
373 qDebug() << __FUNCTION__ << " : cnt" << aE->count()
374 << "rep" << aE->isAutoRepeat()
375 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
376 << "text" << aE->text()
377 << QDbgStr(aE->text().isEmpty() ? QString() :
378 QString().sprintf("(%04X)", aE->text()[0].unicode()));
379 }
380
381 void keyReleaseEvent(QKeyEvent *aE)
382 {
383 qDebug() << __FUNCTION__ << ": cnt" << aE->count()
384 << "rep" << aE->isAutoRepeat()
385 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
386 << "text" << aE->text()
387 << QDbgStr(aE->text().isEmpty() ? QString() :
388 QString().sprintf("(%04X)", aE->text()[0].unicode()));
389 }
390#endif
391
392#if 0
393 void mousePressEvent(QMouseEvent *aE)
394 {
395 qDebug() << __FUNCTION__ << ": btn" << aE->button()
396 << QDbgStr(QString().sprintf("btns %08X mods %08X",
397 (int) aE->buttons(), (int) aE->modifiers()))
398 << "gpos" << aE->globalPos() << "pos" << aE->pos();
399
400 ++mPressed;
401 update();
402 }
403
404 void mouseReleaseEvent(QMouseEvent *aE)
405 {
406 qDebug() << __FUNCTION__ << ": btn" << aE->button()
407 << QDbgStr(QString().sprintf("btns %08X mods %08X",
408 (int) aE->buttons(), (int) aE->modifiers()))
409 << "gpos" << aE->globalPos() << "pos" << aE->pos();
410 }
411
412 void mouseMoveEvent(QMouseEvent *aE)
413 {
414 qDebug() << __FUNCTION__ << ": btn" << aE->button()
415 << QDbgStr(QString().sprintf("btns %08X mods %08X",
416 (int) aE->buttons(), (int) aE->modifiers()))
417 << "gpos" << aE->globalPos() << "pos" << aE->pos();
418 }
419#endif
420
421#if 0
422 virtual void enterEvent(QEvent *event)
423 {
424 qDebug() << __FUNCTION__ << ":";
425 }
426
427 virtual void leaveEvent(QEvent *event)
428 {
429 qDebug() << __FUNCTION__ << ":";
430 }
431#endif
432
433#if 0
434 void contextMenuEvent(QContextMenuEvent *aE)
435 {
436 QMenu menu;