source: tests/widget/widget.cpp@ 256

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

tests: Added generic widget test for quick testing and hacking.

File size: 11.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
64class MyButton : public QPushButton
65{
66public:
67
68 MyButton(QWidget *aParent) : QPushButton(aParent) {}
69
70 void focusInEvent(QFocusEvent *aE)
71 {
72#if 0
73 qDebug() << __FUNCTION__ << ":" << text() << "reason" << aE->reason()
74 << "focus" << (qApp->focusWidget() ?
75 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
76#endif
77 }
78
79 void focusOutEvent(QFocusEvent *aE)
80 {
81#if 0
82 qDebug() << __FUNCTION__ << ":" << text() << "reason" << aE->reason()
83 << "focus" << (qApp->focusWidget() ?
84 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
85#endif
86 }
87};
88
89class MyCombo : public QComboBox
90{
91public:
92
93 MyCombo(QWidget *aParent) : QComboBox(aParent) {}
94
95 void focusInEvent(QFocusEvent *aE)
96 {
97 update();
98#if 0
99 qDebug() << __FUNCTION__ << ":" << currentText() << "reason" << aE->reason()
100 << "focus" << (qApp->focusWidget() ?
101 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
102#endif
103 }
104
105 void focusOutEvent(QFocusEvent *aE)
106 {
107 update();
108#if 0
109 qDebug() << __FUNCTION__ << ":" << currentText() << "reason" << aE->reason()
110 << "focus" << (qApp->focusWidget() ?
111 qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
112#endif
113 }
114
115#if 0
116 void resizeEvent(QResizeEvent *aE)
117 {
118 qDebug() << __FUNCTION__ << ":" << currentText() << "g" << geometry() << "fg" << frameGeometry()
119 << "sz" << aE->size() << "old" << aE->oldSize();
120 }
121
122 void moveEvent(QMoveEvent *aE)
123 {
124 qDebug() << __FUNCTION__ << ":" << currentText() << " g" << geometry() << "fg" << frameGeometry()
125 << "pos" << aE->pos() << "old" << aE->oldPos();
126 }
127#endif
128
129#if 0
130 virtual void enterEvent(QEvent *event)
131 {
132 qDebug() << __FUNCTION__ << ":" << currentText();
133 }
134
135 virtual void leaveEvent(QEvent *event)
136 {
137 qDebug() << __FUNCTION__ << ":" << currentText();
138 }
139#endif
140
141#if 0
142 void mousePressEvent(QMouseEvent *aE)
143 {
144 qDebug() << __FUNCTION__ << ": btn" << aE->button()
145 << QDbgStr(QString().sprintf("btns %08X mods %08X",
146 (int) aE->buttons(), (int) aE->modifiers()))
147 << "gpos" << aE->globalPos() << "pos" << aE->pos();
148 QComboBox::mousePressEvent(aE);
149 }
150
151 void mouseReleaseEvent(QMouseEvent *aE)
152 {
153 qDebug() << __FUNCTION__ << ": btn" << aE->button()
154 << QDbgStr(QString().sprintf("btns %08X mods %08X",
155 (int) aE->buttons(), (int) aE->modifiers()))
156 << "gpos" << aE->globalPos() << "pos" << aE->pos();
157 QComboBox::mouseReleaseEvent(aE);
158 }
159
160 void contextMenuEvent(QContextMenuEvent *aE)
161 {
162 QMenu menu;
163 menu.addAction(QLatin1String("Action &1"));
164 menu.addAction(QLatin1String("Action &2"));
165 menu.exec(aE->globalPos());
166 winId();
167 }
168#endif
169};
170
171class MyWidget : public QWidget
172{
173public:
174
175 MyWidget()
176 {
177 //setFocusPolicy(Qt::StrongFocus);
178
179// MyButton *btn1 = new MyButton(this);
180// btn1->setText(QLatin1String("Hello 1"));
181// btn1->setObjectName("Hello 1");
182// btn1->move(20, 20);
183// MyButton *btn2 = new MyButton(this);
184// btn2->setText(QLatin1String("Hello 2"));
185// btn2->setObjectName("Hello 2");
186// btn2->move(20, 60);
187
188 QComboBox *cb1 = new MyCombo(this);
189 cb1->addItem(QLatin1String("Test 1"));
190 cb1->addItem(QLatin1String("Test 2"));
191
192// QComboBox *cb2 = new MyCombo(this);
193// cb2->addItem(QLatin1String("Test 3"));
194// cb2->addItem(QLatin1String("Test 4"));
195
196 QVBoxLayout *mainLayout = new QVBoxLayout();
197// mainLayout->addWidget(btn1);
198// mainLayout->addWidget(btn2);
199 mainLayout->addWidget(cb1);
200// mainLayout->addWidget(cb2);
201
202 setLayout(mainLayout);
203 };
204
205#if 0
206 void paintEvent(QPaintEvent *aE)
207 {
208 qDebug() << __FUNCTION__ <<": " << aE->rect();
209
210 QPainter p(this);
211 p.setRenderHint(QPainter::Antialiasing);
212
213 p.fillRect(0, 0, 20, 20, Qt::green);
214
215 p.setPen(Qt::black);
216 p.drawEllipse(10, 10, 10, 10);
217
218 //p.setFont(QFont("Arial", 12));
219 p.drawText(10, 30, QLatin1String("ABC"));
220 }
221#endif
222
223#if 0
224 void resizeEvent(QResizeEvent *aE)
225 {
226 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
227 << "sz" << aE->size() << "old" << aE->oldSize();
228 }
229
230 void moveEvent(QMoveEvent *aE)
231 {
232 qDebug() << __FUNCTION__ << ": g" << geometry() << "fg" << frameGeometry()
233 << "pos" << aE->pos() << "old" << aE->oldPos();
234 }
235#endif
236
237#if 0
238 void focusInEvent(QFocusEvent *aE)
239 {
240 qDebug() << __FUNCTION__ << ": reason" << aE->reason();
241 }
242
243 void focusOutEvent(QFocusEvent *aE)
244 {
245 qDebug() << __FUNCTION__ << ": reason" << aE->reason();
246 }
247#endif
248
249#if 0
250 void changeEvent(QEvent *aE)
251 {
252 switch (aE->type()) {
253 case QEvent::WindowStateChange: {
254 QWindowStateChangeEvent *e2 = (QWindowStateChangeEvent *)aE;
255 qDebug().nospace() << __FUNCTION__ << ": QWindowStateChangeEvent(" <<
256 e2->oldState() << "->" << windowState() << ")";
257 break;
258 }
259 default:
260 break;
261 }
262 }
263#endif
264
265#if 0
266 void keyPressEvent(QKeyEvent *aE)
267 {
268 qDebug() << __FUNCTION__ << " : cnt" << aE->count()
269 << "rep" << aE->isAutoRepeat()
270 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
271 << "text" << aE->text()
272 << QDbgStr(aE->text().isEmpty() ? QString() :
273 QString().sprintf("(%04X)", aE->text()[0].unicode()));
274 }
275
276 void keyReleaseEvent(QKeyEvent *aE)
277 {
278 qDebug() << __FUNCTION__ << ": cnt" << aE->count()
279 << "rep" << aE->isAutoRepeat()
280 << QDbgStr(QString().sprintf("key %08X mods %08X", aE->key(), (int) aE->modifiers()))
281 << "text" << aE->text()
282 << QDbgStr(aE->text().isEmpty() ? QString() :
283 QString().sprintf("(%04X)", aE->text()[0].unicode()));
284 }
285#endif
286
287#if 0
288 void mouseMoveEvent(QMouseEvent *aE)
289 {
290 qDebug() << __FUNCTION__ << ": btn" << aE->button()
291 << QDbgStr(QString().sprintf("btns %08X mods %08X",
292 (int) aE->buttons(), (int) aE->modifiers()))
293 << "gpos" << aE->globalPos() << "pos" << aE->pos();
294 }
295
296 void mousePressEvent(QMouseEvent *aE)
297 {
298 qDebug() << __FUNCTION__ << ": btn" << aE->button()
299 << QDbgStr(QString().sprintf("btns %08X mods %08X",
300 (int) aE->buttons(), (int) aE->modifiers()))
301 << "gpos" << aE->globalPos() << "pos" << aE->pos();
302 }
303
304 void mouseReleaseEvent(QMouseEvent *aE)
305 {
306 qDebug() << __FUNCTION__ << ": btn" << aE->button()
307 << QDbgStr(QString().sprintf("btns %08X mods %08X",
308 (int) aE->buttons(), (int) aE->modifiers()))
309 << "gpos" << aE->globalPos() << "pos" << aE->pos();
310 }
311#endif
312
313#if 0
314 virtual void enterEvent(QEvent *event)
315 {
316 qDebug() << __FUNCTION__ << ":";
317 }
318
319 virtual void leaveEvent(QEvent *event)
320 {
321 qDebug() << __FUNCTION__ << ":";
322 }
323#endif
324
325#if 0
326 void contextMenuEvent(QContextMenuEvent *aE)
327 {
328 QMenu menu;
329 menu.addAction(QLatin1String("Action &1"));
330 menu.addAction(QLatin1String("Action &2"));
331 menu.exec(aE->globalPos());
332 }
333#endif
334
335#if 0
336 void timerEvent(QTimerEvent *aE)
337 {
338 qDebug() << __FUNCTION__ << ": id" << aE->timerId();
339 }
340#endif
341};
342
343int main(int argc, char **argv)
344{
345 QApplication app(argc, argv);
346
347 MyWidget widget;
348 widget.resize(100, 100);
349 widget.move(500, 500);
350
351 widget.show();
352
353#if 1
354 {
355 QFontDialog fntDlg;
356 fntDlg.exec();
357
358 {
359 QFont font("MyCoolFont");
360 QFontInfo info(font);
361 qDebug() << info.family();
362 }
363 {
364 QFont font("MyCoolFont2");
365 QFontInfo info(font);
366 qDebug() << info.family();
367 }
368 }
369#endif
370
371#if 0
372 widget.startTimer(1000);
373 widget.startTimer(2000);
374 widget.startTimer(4000);
375#endif
376
377#if 0
378 qDebug() << "------------- before min" << widget.geometry() << widget.frameGeometry();
379 widget.showMinimized();
380 qDebug() << "------------- after min" << widget.geometry() << widget.frameGeometry();
381 widget.move(100, 100);
382 qDebug() << "------------- after move" << widget.geometry() << widget.frameGeometry();
383 widget.showNormal();
384 qDebug() << "------------- after norm" << widget.geometry() << widget.frameGeometry();
385 widget.showFullScreen();
386 qDebug() << "------------- after full" << widget.geometry() << widget.frameGeometry();
387#endif
388
389 return app.exec();
390}
Note: See TracBrowser for help on using the repository browser.