source: tests/widget/widget.cpp@ 304

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

tests: widget: Improved focus traversal test.

File size: 13.1 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 focusInEvent(QFocusEvent *aE)
100 {
101 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
102 QWidget::focusInEvent(aE);
103 }
104
105 void focusOutEvent(QFocusEvent *aE)
106 {
107 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
108 QWidget::focusOutEvent(aE);
109 }
110
111private:
112
113 int mPressed;
114};
115
116////////////////////////////////////////////////////////////////////////////////
117
118class MyButton : public QPushButton
119{
120public:
121
122 MyButton(QWidget *aParent) : QPushButton(aParent) {}
123
124#if 0
125 void focusInEvent(QFocusEvent *aE)
126 {
127 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << text()
128 << "reason" << aE->reason()
129 << "focus" << (qApp->focusWidget() ?
130 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
131
132 QPushButton::focusInEvent(aE);
133 }
134
135 void focusOutEvent(QFocusEvent *aE)
136 {
137 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << text()
138 << "reason" << aE->reason()
139 << "focus" << (qApp->focusWidget() ?
140 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
141 QPushButton::focusOutEvent(aE);
142 }
143#endif
144};
145
146
147////////////////////////////////////////////////////////////////////////////////
148
149class MyCombo : public QComboBox
150{
151public:
152
153 MyCombo(QWidget *aParent) : QComboBox(aParent) {}
154
155#if 0
156 void focusInEvent(QFocusEvent *aE)
157 {
158 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << currentText()
159 << "reason" << aE->reason()
160 << "focus" << (qApp->focusWidget() ?
161 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
162 QComboBox::focusInEvent(aE);
163 }
164
165 void focusOutEvent(QFocusEvent *aE)
166 {
167 qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << currentText()
168 << "reason" << aE->reason()
169 << "focus" << (qApp->focusWidget() ?
170 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
171 QComboBox::focusOutEvent(aE);
172 }
173#endif
174
175#if 0
176 void resizeEvent(QResizeEvent *aE)
177 {
178 qDebug() << __FUNCTION__ << ":" << currentText() << "g" << geometry() << "fg" << frameGeometry()
179 << "sz" << aE->size() << "old" << aE->oldSize();
180 }
181
182 void moveEvent(QMoveEvent *aE)
183 {
184 qDebug() << __FUNCTION__ << ":" << currentText() << " g" << geometry() << "fg" << frameGeometry()
185 << "pos" << aE->pos() << "old" << aE->oldPos();
186 }
187#endif
188
189#if 0
190 virtual void enterEvent(QEvent *event)
191 {
192 qDebug() << __FUNCTION__ << ":" << currentText();
193 }
194
195 virtual void leaveEvent(QEvent *event)
196 {
197 qDebug() << __FUNCTION__ << ":" << currentText();
198 }
199#endif
200
201#if 0
202 void mousePressEvent(QMouseEvent *aE)
203 {
204 qDebug() << __FUNCTION__ << ": btn" << aE->button()
205 << QDbgStr(QString().sprintf("btns %08X mods %08X",
206 (int) aE->buttons(), (int) aE->modifiers()))
207 << "gpos" << aE->globalPos() << "pos" << aE->pos();
208 QComboBox::mousePressEvent(aE);
209 }
210
211 void mouseReleaseEvent(QMouseEvent *aE)
212 {
213 qDebug() << __FUNCTION__ << ": btn" << aE->button()
214 << QDbgStr(QString().sprintf("btns %08X mods %08X",
215 (int) aE->buttons(), (int) aE->modifiers()))
216 << "gpos" << aE->globalPos() << "pos" << aE->pos();
217 QComboBox::mouseReleaseEvent(aE);
218 }
219
220 void contextMenuEvent(QContextMenuEvent *aE)
221 {
222 QMenu menu;
223 menu.addAction(QLatin1String("Action &1"));
224 menu.addAction(QLatin1String("Action &2"));
225 menu.exec(aE->globalPos());
226 winId();
227 }
228#endif
229};
230
231////////////////////////////////////////////////////////////////////////////////
232
233class MyWidget : public QWidget
234{
235public:
236
237 MyWidget() : mPressed(0)
238 {
239// setFocusPolicy(Qt::StrongFocus);
240
241 MyButton *btn1 = new MyButton(this);
242 btn1->setText(QLatin1String("Hello 1"));
243 btn1->setObjectName("Hello 1");
244 btn1->move(20, 20);
245 MyButton *btn2 = new MyButton(this);
246 btn2->setText(QLatin1String("Hello 2"));
247 btn2->setObjectName("Hello 2");
248 btn2->move(20, 60);
249
250// QComboBox *cb1 = new MyCombo(this);
251// cb1->addItem(QLatin1String("Test 1"));
252// cb1->addItem(QLatin1String("Test 2"));
253
254// QComboBox *cb2 = new MyCombo(this);
255// cb2->addItem(QLatin1String("Test 3"));
256// cb2->addItem(QLatin1String("Test 4"));
257
258// QVBoxLayout *mainLayout = new QVBoxLayout();
259// mainLayout->addWidget(btn1);
260// mainLayout->addWidget(btn2);
261// mainLayout->addWidget(cb1);
262// mainLayout->addWidget(cb2);
263
264// setLayout(mainLayout);
265
266#if 0
267 new MyChild(this);
268#endif
269
270 };
271
272#if 0
273 void paintEvent(QPaintEvent *aE)
274 {
275 qDebug() << __FUNCTION__ <<": " << aE->rect();
276
277 QPainter p(this);
278 p.setRenderHint(QPainter::Antialiasing);
279
280 p.fillRect(0, 0, 20, 20, mPressed % 2 ? Qt::red : Qt::green);
281
282 p.setPen(Qt::black);
283 p.drawEllipse(10, 10, 10, 10);
284
285 //p.setFont(QFont("Arial", 12));
286 p.drawText(10, 30, QLatin1String("ABC"));
287 }
288#endif
289
290#if 0
291 void resizeEvent(QResizeEvent *aE)
292 {
293 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
294 << "sz" << aE->size() << "old" << aE->oldSize();
295 }
296
297 void moveEvent(QMoveEvent *aE)
298 {
299 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
300 << "pos" << aE->pos() << "old" << aE->oldPos();
301 }
302#endif
303
304#if 0
305 void focusInEvent(QFocusEvent *aE)
306 {
307 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
308 QWidget::focusInEvent(aE);
309 }
310
311 void focusOutEvent(QFocusEvent *aE)
312 {
313 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
314 QWidget::focusOutEvent(aE);
315 }
316#endif
317
318#if 0
319 void changeEvent(QEvent *aE)
320 {
321 switch (aE->type()) {
322 case QEvent::WindowStateChange: {
323 QWindowStateChangeEvent *e2 = (QWindowStateChangeEvent *)aE;
324 qDebug().nospace() << __FUNCTION__ << ": QWindowStateChangeEvent(" <<
325 e2->oldState() << "->" << windowState() << ")";
326 break;
327 }
328 default:
329 break;
330 }
331 }
332#endif
333
334#if 0
335 void keyPressEvent(QKeyEvent *aE)
336 {
337 qDebug() << __FUNCTION__ << " : cnt" << aE->count()
338 << "rep" << aE->isAutoRepeat()
339 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
340 << "text" << aE->text()
341 << QDbgStr(aE->text().isEmpty() ? QString() :
342 QString().sprintf("(%04X)", aE->text()[0].unicode()));
343 }
344
345 void keyReleaseEvent(QKeyEvent *aE)
346 {
347 qDebug() << __FUNCTION__ << ": cnt" << aE->count()
348 << "rep" << aE->isAutoRepeat()
349 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
350 << "text" << aE->text()
351 << QDbgStr(aE->text().isEmpty() ? QString() :
352 QString().sprintf("(%04X)", aE->text()[0].unicode()));
353 }
354#endif
355
356#if 0
357 void mousePressEvent(QMouseEvent *aE)
358 {
359 qDebug() << __FUNCTION__ << ": btn" << aE->button()
360 << QDbgStr(QString().sprintf("btns %08X mods %08X",
361 (int) aE->buttons(), (int) aE->modifiers()))
362 << "gpos" << aE->globalPos() << "pos" << aE->pos();
363
364 ++mPressed;
365 update();
366 }
367
368 void mouseReleaseEvent(QMouseEvent *aE)
369 {
370 qDebug() << __FUNCTION__ << ": btn" << aE->button()
371 << QDbgStr(QString().sprintf("btns %08X mods %08X",
372 (int) aE->buttons(), (int) aE->modifiers()))
373 << "gpos" << aE->globalPos() << "pos" << aE->pos();
374 }
375
376 void mouseMoveEvent(QMouseEvent *aE)
377 {
378 qDebug() << __FUNCTION__ << ": btn" << aE->button()
379 << QDbgStr(QString().sprintf("btns %08X mods %08X",
380 (int) aE->buttons(), (int) aE->modifiers()))
381 << "gpos" << aE->globalPos() << "pos" << aE->pos();
382 }
383#endif
384
385#if 0
386 virtual void enterEvent(QEvent *event)
387 {
388 qDebug() << __FUNCTION__ << ":";
389 }
390
391 virtual void leaveEvent(QEvent *event)
392 {
393 qDebug() << __FUNCTION__ << ":";
394 }
395#endif
396
397#if 0
398 void contextMenuEvent(QContextMenuEvent *aE)
399 {
400 QMenu menu;
401 menu.addAction(QLatin1String("Action &1"));
402 menu.addAction(QLatin1String("Action &2"));
403 menu.exec(aE->globalPos());
404 }
405#endif
406
407#if 0
408 void timerEvent(QTimerEvent *aE)
409 {
410 qDebug() << __FUNCTION__ << ": id" << aE->timerId();
411 }
412#endif
413
414private:
415
416 int mPressed;
417};
418
419int main(int argc, char **argv)
420{
421 QApplication app(argc, argv);
422
423 MyWidget widget;
424 widget.resize(100, 100);
425 widget.move(1280-168, 1024-200);
426
427 widget.show();
428
429#if 0
430 {
431 QFontDialog fntDlg;
432 fntDlg.exec();
433
434 {
435 QFont font("MyCoolFont");
436 QFontInfo info(font);
437 qDebug() << info.family();
438 }
439 {
440 QFont font("MyCoolFont2");
441 QFontInfo info(font);
442 qDebug() << info.family();
443 }
444 }
445#endif
446
447#if 0
448 QSound snd2("C:/MMOS2/SOUNDS/DESKTOP/dsk_shut.wav");
449 snd2.setLoops(2);
450 snd2.play();
451
452 QSound snd1("C:/MMOS2/SOUNDS/DESKTOP/dsk_strt.wav");
453 snd1.setLoops(3);
454 snd1.play();
455#endif
456
457#if 0
458 widget.startTimer(1000);
459 widget.startTimer(2000);
460 widget.startTimer(4000);
461#endif
462
463#if 0
464 qDebug() << "------------- before min" << widget.geometry() << widget.frameGeometry();
465 widget.showMinimized();
466 qDebug() << "------------- after min" << widget.geometry() << widget.frameGeometry();
467 widget.move(100, 100);
468 qDebug() << "------------- after move" << widget.geometry() << widget.frameGeometry();
469 widget.showNormal();
470 qDebug() << "------------- after norm" << widget.geometry() << widget.frameGeometry();
471 widget.showFullScreen();
472 qDebug() << "------------- after full" << widget.geometry() << widget.frameGeometry();
473#endif
474
475 return app.exec();
476}
477
478#include "widget.moc"
Note: See TracBrowser for help on using the repository browser.