source: tests/widget/widget.cpp@ 351

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

tests/widget: Added a testcase for the text codec.

File size: 16.7 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 0
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 1
309 void paintEvent(QPaintEvent *aE)
310 {
311 qDebug() << __FUNCTION__ <<": " << aE->rect();
312
313 QPainter p(this);
314#if 0
315 // simple QPainter test
316
317 p.setRenderHint(QPainter::Antialiasing);
318
319 p.fillRect(0, 0, 20, 20, mPressed % 2 ? Qt::red : Qt::green);
320
321 p.setPen(Qt::black);
322 p.drawEllipse(10, 10, 10, 10);
323
324 //p.setFont(QFont("Arial", 12));
325 p.drawText(10, 30, QLatin1String("ABC"));
326#endif
327#if 1
328 // simple QClipboard image test
329
330 const QMimeData *data = QApplication::clipboard()->mimeData();
331 if (data && data->hasImage()){
332 QImage img = qvariant_cast<QImage>(data->imageData());
333 p.drawImage(0, 0, img);
334 }
335#endif
336 }
337#endif
338
339#if 0
340 void resizeEvent(QResizeEvent *aE)
341 {
342 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
343 << "sz" << aE->size() << "old" << aE->oldSize();
344 }
345
346 void moveEvent(QMoveEvent *aE)
347 {
348 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
349 << "pos" << aE->pos() << "old" << aE->oldPos();
350 }
351#endif
352
353#if 0
354 void focusInEvent(QFocusEvent *aE)
355 {
356 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
357 QWidget::focusInEvent(aE);
358 }
359
360 void focusOutEvent(QFocusEvent *aE)
361 {
362 qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
363 QWidget::focusOutEvent(aE);
364 }
365#endif
366
367#if 0
368 void changeEvent(QEvent *aE)
369 {
370 switch (aE->type()) {
371 case QEvent::WindowStateChange: {
372 QWindowStateChangeEvent *e2 = (QWindowStateChangeEvent *)aE;
373 qDebug().nospace() << __FUNCTION__ << ": QWindowStateChangeEvent(" <<
374 e2->oldState() << "->" << windowState() << ")";
375 break;
376 }
377 default:
378 break;
379 }
380 }
381#endif
382
383#if 0
384 void keyPressEvent(QKeyEvent *aE)
385 {
386 qDebug() << __FUNCTION__ << " : cnt" << aE->count()
387 << "rep" << aE->isAutoRepeat()
388 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
389 << "text" << aE->text()
390 << QDbgStr(aE->text().isEmpty() ? QString() :
391 QString().sprintf("(%04X)", aE->text()[0].unicode()));
392 }
393
394 void keyReleaseEvent(QKeyEvent *aE)
395 {
396 qDebug() << __FUNCTION__ << ": cnt" << aE->count()
397 << "rep" << aE->isAutoRepeat()
398 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
399 << "text" << aE->text()
400 << QDbgStr(aE->text().isEmpty() ? QString() :
401 QString().sprintf("(%04X)", aE->text()[0].unicode()));
402 }
403#endif
404
405#if 0
406 void mousePressEvent(QMouseEvent *aE)
407 {
408 qDebug() << __FUNCTION__ << ": btn" << aE->button()
409 << QDbgStr(QString().sprintf("btns %08X mods %08X",
410 (int) aE->buttons(), (int) aE->modifiers()))
411 << "gpos" << aE->globalPos() << "pos" << aE->pos();
412
413 ++mPressed;
414 update();
415 }
416
417 void mouseReleaseEvent(QMouseEvent *aE)
418 {
419 qDebug() << __FUNCTION__ << ": btn" << aE->button()
420 << QDbgStr(QString().sprintf("btns %08X mods %08X",
421 (int) aE->buttons(), (int) aE->modifiers()))
422 << "gpos" << aE->globalPos() << "pos" << aE->pos();
423 }
424
425 void mouseMoveEvent(QMouseEvent *aE)
426 {
427 qDebug() << __FUNCTION__ << ": btn" << aE->button()
428 << QDbgStr(QString().sprintf("btns %08X mods %08X",
429 (int) aE->buttons(), (int) aE->modifiers()))
430 << "gpos" << aE->globalPos() << "pos" << aE->pos();
431 }
432#endif
433
434#if 0
435 virtual void enterEvent(QEvent *event)
436 {
437 qDebug() << __FUNCTION__ << ":";
438 }
439
440 virtual void leaveEvent(QEvent *event)
441 {
442 qDebug() << __FUNCTION__ << ":";
443 }
444#endif
445
446#if 0
447 void contextMenuEvent(QContextMenuEvent *aE)
448 {
449 QMenu menu;
450 menu.addAction(QLatin1String("Action &1"));
451 menu.addAction(QLatin1String("Action &2"));
452 menu.exec(aE->globalPos());
453 }
454#endif
455
456#if 0
457 void timerEvent(QTimerEvent *aE)
458 {
459 qDebug() << __FUNCTION__ << ": id" << aE->timerId();
460 }
461#endif
462
463private:
464
465 int mPressed;
466};
467
468void testCodec(const char *title, const QString &sample, QTextCodec *codec)
469{
470 // test console output
471
472 printf("%s : Unicode (source) : ", title);
473 foreach (QChar ch, sample)
474 printf("%hX ", ch.unicode());
475 printf("\n");
476
477 QByteArray eightbit = codec->fromUnicode(sample);
478 printf("%s : 8-bit (as is) : %s\n", title, eightbit.data());
479
480 QString sample2 = codec->toUnicode(eightbit);
481 printf("%s : Unicode (result) : ", title);
482 foreach (QChar ch, sample2)
483 printf("%hX ", ch.unicode());
484 printf("\n");
485
486 if (sample != sample2)
487 qWarning() << "Source and resulting Unicode differ!";
488
489 qWarning() << "";
490
491 // test GUI output (both window title and window text)
492
493 QString combined = QString("%1 : %2").arg(QLatin1String(title), sample);
494 QMessageBox mbox;
495 mbox.setWindowTitle(combined);
496 mbox.setText(combined);
497 mbox.exec();
498}
499
500int main(int argc, char **argv)
501{
502#if 0
503 ////////////////////////////////////////////////////////////////////////////
504 // Text mode
505
506 QCoreApplication app(argc, argv);
507
508#else
509 ////////////////////////////////////////////////////////////////////////////
510 // GUI
511
512 QApplication app(argc, argv);
513
514#if 1
515 //--------------------------------------------------------------------------
516 // locale test
517
518 QLocale locale = QLocale::system();
519
520 qWarning() << "Library paths :" << QApplication::libraryPaths();
521 qWarning() << "";
522 qWarning() << "Available codecs :\n" << QTextCodec::availableCodecs();
523 qWarning() << "";
524 qWarning() << "Codec for locale :" << QTextCodec::codecForLocale()->name();
525 qWarning() << " Aliases :" << QTextCodec::codecForLocale()->aliases();
526 qWarning() << "";
527 qWarning() << "Country for locale :" << QLocale::countryToString(locale.country())
528 << "[" << locale.country() << "]";
529 qWarning() << "Language for locale :" << QLocale::languageToString(locale.language())
530 << "[" << locale.language() << "]";
531 qWarning() << "";
532
533 testCodec("First 3 months",
534 QString("%1, %2, %3").arg(locale.standaloneMonthName(1),
535 locale.standaloneMonthName(2),
536 locale.standaloneMonthName(3)),
537 QTextCodec::codecForLocale());
538 return 0;
539#endif
540
541#if 0
542 QRect r = QApplication::desktop()->availableGeometry();
543 MyWidget widget;
544 widget.resize(100, 100);
545 widget.move(r.width() - 200, r.height() - 200);
546
547 widget.show();
548#endif
549
550#if 0
551 {
552 QFontDialog fntDlg;
553 fntDlg.exec();
554
555 {
556 QFont font("MyCoolFont");
557 QFontInfo info(font);
558 qDebug() << info.family();
559 }
560 {
561 QFont font("MyCoolFont2");
562 QFontInfo info(font);
563 qDebug() << info.family();
564 }
565 }
566#endif
567
568#if 0
569 QSound snd2("C:/MMOS2/SOUNDS/DESKTOP/dsk_shut.wav");
570 snd2.setLoops(2);
571 snd2.play();
572
573 QSound snd1("C:/MMOS2/SOUNDS/DESKTOP/dsk_strt.wav");
574 snd1.setLoops(3);
575 snd1.play();
576#endif
577
578#if 0
579 widget.startTimer(1000);
580 widget.startTimer(2000);
581 widget.startTimer(4000);
582#endif
583
584#if 0
585 qDebug() << "------------- before min" << widget.geometry() << widget.frameGeometry();
586 widget.showMinimized();
587 qDebug() << "------------- after min" << widget.geometry() << widget.frameGeometry();
588 widget.move(100, 100);
589 qDebug() << "------------- after move" << widget.geometry() << widget.frameGeometry();
590 widget.showNormal();
591 qDebug() << "------------- after norm" << widget.geometry() << widget.frameGeometry();
592 widget.showFullScreen();
593 qDebug() << "------------- after full" << widget.geometry() << widget.frameGeometry();
594#endif
595
596 return app.exec();
597#endif
598}
599
600#include "widget.moc"
Note: See TracBrowser for help on using the repository browser.