source: trunk/examples/widgets/calendarwidget/window.cpp@ 983

Last change on this file since 983 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 16.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "window.h"
44
45//! [0]
46Window::Window()
47{
48 createPreviewGroupBox();
49 createGeneralOptionsGroupBox();
50 createDatesGroupBox();
51 createTextFormatsGroupBox();
52
53 QGridLayout *layout = new QGridLayout;
54 layout->addWidget(previewGroupBox, 0, 0);
55 layout->addWidget(generalOptionsGroupBox, 0, 1);
56 layout->addWidget(datesGroupBox, 1, 0);
57 layout->addWidget(textFormatsGroupBox, 1, 1);
58 layout->setSizeConstraint(QLayout::SetFixedSize);
59 setLayout(layout);
60
61 previewLayout->setRowMinimumHeight(0, calendar->sizeHint().height());
62 previewLayout->setColumnMinimumWidth(0, calendar->sizeHint().width());
63
64 setWindowTitle(tr("Calendar Widget"));
65}
66//! [0]
67
68void Window::localeChanged(int index)
69{
70 calendar->setLocale(localeCombo->itemData(index).toLocale());
71}
72
73//! [1]
74void Window::firstDayChanged(int index)
75{
76 calendar->setFirstDayOfWeek(Qt::DayOfWeek(
77 firstDayCombo->itemData(index).toInt()));
78}
79//! [1]
80
81void Window::selectionModeChanged(int index)
82{
83 calendar->setSelectionMode(QCalendarWidget::SelectionMode(
84 selectionModeCombo->itemData(index).toInt()));
85}
86
87void Window::horizontalHeaderChanged(int index)
88{
89 calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
90 horizontalHeaderCombo->itemData(index).toInt()));
91}
92
93void Window::verticalHeaderChanged(int index)
94{
95 calendar->setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
96 verticalHeaderCombo->itemData(index).toInt()));
97}
98
99//! [2]
100void Window::selectedDateChanged()
101{
102 currentDateEdit->setDate(calendar->selectedDate());
103}
104//! [2]
105
106//! [3]
107void Window::minimumDateChanged(const QDate &date)
108{
109 calendar->setMinimumDate(date);
110 maximumDateEdit->setDate(calendar->maximumDate());
111}
112//! [3]
113
114//! [4]
115void Window::maximumDateChanged(const QDate &date)
116{
117 calendar->setMaximumDate(date);
118 minimumDateEdit->setDate(calendar->minimumDate());
119}
120//! [4]
121
122//! [5]
123void Window::weekdayFormatChanged()
124{
125 QTextCharFormat format;
126
127 format.setForeground(qvariant_cast<QColor>(
128 weekdayColorCombo->itemData(weekdayColorCombo->currentIndex())));
129 calendar->setWeekdayTextFormat(Qt::Monday, format);
130 calendar->setWeekdayTextFormat(Qt::Tuesday, format);
131 calendar->setWeekdayTextFormat(Qt::Wednesday, format);
132 calendar->setWeekdayTextFormat(Qt::Thursday, format);
133 calendar->setWeekdayTextFormat(Qt::Friday, format);
134}
135//! [5]
136
137//! [6]
138void Window::weekendFormatChanged()
139{
140 QTextCharFormat format;
141
142 format.setForeground(qvariant_cast<QColor>(
143 weekendColorCombo->itemData(weekendColorCombo->currentIndex())));
144 calendar->setWeekdayTextFormat(Qt::Saturday, format);
145 calendar->setWeekdayTextFormat(Qt::Sunday, format);
146}
147//! [6]
148
149//! [7]
150void Window::reformatHeaders()
151{
152 QString text = headerTextFormatCombo->currentText();
153 QTextCharFormat format;
154
155 if (text == tr("Bold")) {
156 format.setFontWeight(QFont::Bold);
157 } else if (text == tr("Italic")) {
158 format.setFontItalic(true);
159 } else if (text == tr("Green")) {
160 format.setForeground(Qt::green);
161 }
162 calendar->setHeaderTextFormat(format);
163}
164//! [7]
165
166//! [8]
167void Window::reformatCalendarPage()
168{
169 if (firstFridayCheckBox->isChecked()) {
170 QDate firstFriday(calendar->yearShown(), calendar->monthShown(), 1);
171 while (firstFriday.dayOfWeek() != Qt::Friday)
172 firstFriday = firstFriday.addDays(1);
173 QTextCharFormat firstFridayFormat;
174 firstFridayFormat.setForeground(Qt::blue);
175 calendar->setDateTextFormat(firstFriday, firstFridayFormat);
176 }
177
178 //May First in Red takes precedence
179 if (mayFirstCheckBox->isChecked()) {
180 const QDate mayFirst(calendar->yearShown(), 5, 1);
181 QTextCharFormat mayFirstFormat;
182 mayFirstFormat.setForeground(Qt::red);
183 calendar->setDateTextFormat(mayFirst, mayFirstFormat);
184 }
185}
186//! [8]
187
188//! [9]
189void Window::createPreviewGroupBox()
190{
191 previewGroupBox = new QGroupBox(tr("Preview"));
192
193 calendar = new QCalendarWidget;
194 calendar->setMinimumDate(QDate(1900, 1, 1));
195 calendar->setMaximumDate(QDate(3000, 1, 1));
196 calendar->setGridVisible(true);
197
198 connect(calendar, SIGNAL(currentPageChanged(int,int)),
199 this, SLOT(reformatCalendarPage()));
200
201 previewLayout = new QGridLayout;
202 previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
203 previewGroupBox->setLayout(previewLayout);
204}
205//! [9]
206
207//! [10]
208void Window::createGeneralOptionsGroupBox()
209{
210 generalOptionsGroupBox = new QGroupBox(tr("General Options"));
211
212 localeCombo = new QComboBox;
213 int curLocaleIndex = -1;
214 int index = 0;
215 for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) {
216 QLocale::Language lang = static_cast<QLocale::Language>(_lang);
217 QList<QLocale::Country> countries = QLocale::countriesForLanguage(lang);
218 for (int i = 0; i < countries.count(); ++i) {
219 QLocale::Country country = countries.at(i);
220 QString label = QLocale::languageToString(lang);
221 label += QLatin1Char('/');
222 label += QLocale::countryToString(country);
223 QLocale locale(lang, country);
224 if (this->locale().language() == lang && this->locale().country() == country)
225 curLocaleIndex = index;
226 localeCombo->addItem(label, locale);
227 ++index;
228 }
229 }
230 if (curLocaleIndex != -1)
231 localeCombo->setCurrentIndex(curLocaleIndex);
232 localeLabel = new QLabel(tr("&Locale"));
233 localeLabel->setBuddy(localeCombo);
234
235 firstDayCombo = new QComboBox;
236 firstDayCombo->addItem(tr("Sunday"), Qt::Sunday);
237 firstDayCombo->addItem(tr("Monday"), Qt::Monday);
238 firstDayCombo->addItem(tr("Tuesday"), Qt::Tuesday);
239 firstDayCombo->addItem(tr("Wednesday"), Qt::Wednesday);
240 firstDayCombo->addItem(tr("Thursday"), Qt::Thursday);
241 firstDayCombo->addItem(tr("Friday"), Qt::Friday);
242 firstDayCombo->addItem(tr("Saturday"), Qt::Saturday);
243
244 firstDayLabel = new QLabel(tr("Wee&k starts on:"));
245 firstDayLabel->setBuddy(firstDayCombo);
246//! [10]
247
248 selectionModeCombo = new QComboBox;
249 selectionModeCombo->addItem(tr("Single selection"),
250 QCalendarWidget::SingleSelection);
251 selectionModeCombo->addItem(tr("None"), QCalendarWidget::NoSelection);
252
253 selectionModeLabel = new QLabel(tr("&Selection mode:"));
254 selectionModeLabel->setBuddy(selectionModeCombo);
255
256 gridCheckBox = new QCheckBox(tr("&Grid"));
257 gridCheckBox->setChecked(calendar->isGridVisible());
258
259 navigationCheckBox = new QCheckBox(tr("&Navigation bar"));
260 navigationCheckBox->setChecked(true);
261
262 horizontalHeaderCombo = new QComboBox;
263 horizontalHeaderCombo->addItem(tr("Single letter day names"),
264 QCalendarWidget::SingleLetterDayNames);
265 horizontalHeaderCombo->addItem(tr("Short day names"),
266 QCalendarWidget::ShortDayNames);
267 horizontalHeaderCombo->addItem(tr("None"),
268 QCalendarWidget::NoHorizontalHeader);
269 horizontalHeaderCombo->setCurrentIndex(1);
270
271 horizontalHeaderLabel = new QLabel(tr("&Horizontal header:"));
272 horizontalHeaderLabel->setBuddy(horizontalHeaderCombo);
273
274 verticalHeaderCombo = new QComboBox;
275 verticalHeaderCombo->addItem(tr("ISO week numbers"),
276 QCalendarWidget::ISOWeekNumbers);
277 verticalHeaderCombo->addItem(tr("None"), QCalendarWidget::NoVerticalHeader);
278
279 verticalHeaderLabel = new QLabel(tr("&Vertical header:"));
280 verticalHeaderLabel->setBuddy(verticalHeaderCombo);
281
282//! [11]
283 connect(localeCombo, SIGNAL(currentIndexChanged(int)),
284 this, SLOT(localeChanged(int)));
285 connect(firstDayCombo, SIGNAL(currentIndexChanged(int)),
286 this, SLOT(firstDayChanged(int)));
287 connect(selectionModeCombo, SIGNAL(currentIndexChanged(int)),
288 this, SLOT(selectionModeChanged(int)));
289 connect(gridCheckBox, SIGNAL(toggled(bool)),
290 calendar, SLOT(setGridVisible(bool)));
291 connect(navigationCheckBox, SIGNAL(toggled(bool)),
292 calendar, SLOT(setNavigationBarVisible(bool)));
293 connect(horizontalHeaderCombo, SIGNAL(currentIndexChanged(int)),
294 this, SLOT(horizontalHeaderChanged(int)));
295 connect(verticalHeaderCombo, SIGNAL(currentIndexChanged(int)),
296 this, SLOT(verticalHeaderChanged(int)));
297//! [11]
298
299 QHBoxLayout *checkBoxLayout = new QHBoxLayout;
300 checkBoxLayout->addWidget(gridCheckBox);
301 checkBoxLayout->addStretch();
302 checkBoxLayout->addWidget(navigationCheckBox);
303
304 QGridLayout *outerLayout = new QGridLayout;
305 outerLayout->addWidget(localeLabel, 0, 0);
306 outerLayout->addWidget(localeCombo, 0, 1);
307 outerLayout->addWidget(firstDayLabel, 1, 0);
308 outerLayout->addWidget(firstDayCombo, 1, 1);
309 outerLayout->addWidget(selectionModeLabel, 2, 0);
310 outerLayout->addWidget(selectionModeCombo, 2, 1);
311 outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
312 outerLayout->addWidget(horizontalHeaderLabel, 4, 0);
313 outerLayout->addWidget(horizontalHeaderCombo, 4, 1);
314 outerLayout->addWidget(verticalHeaderLabel, 5, 0);
315 outerLayout->addWidget(verticalHeaderCombo, 5, 1);
316 generalOptionsGroupBox->setLayout(outerLayout);
317
318//! [12]
319 firstDayChanged(firstDayCombo->currentIndex());
320 selectionModeChanged(selectionModeCombo->currentIndex());
321 horizontalHeaderChanged(horizontalHeaderCombo->currentIndex());
322 verticalHeaderChanged(verticalHeaderCombo->currentIndex());
323}
324//! [12]
325
326//! [13]
327void Window::createDatesGroupBox()
328{
329 datesGroupBox = new QGroupBox(tr("Dates"));
330
331 minimumDateEdit = new QDateEdit;
332 minimumDateEdit->setDisplayFormat("MMM d yyyy");
333 minimumDateEdit->setDateRange(calendar->minimumDate(),
334 calendar->maximumDate());
335 minimumDateEdit->setDate(calendar->minimumDate());
336
337 minimumDateLabel = new QLabel(tr("&Minimum Date:"));
338 minimumDateLabel->setBuddy(minimumDateEdit);
339
340 currentDateEdit = new QDateEdit;
341 currentDateEdit->setDisplayFormat("MMM d yyyy");
342 currentDateEdit->setDate(calendar->selectedDate());
343 currentDateEdit->setDateRange(calendar->minimumDate(),
344 calendar->maximumDate());
345
346 currentDateLabel = new QLabel(tr("&Current Date:"));
347 currentDateLabel->setBuddy(currentDateEdit);
348
349 maximumDateEdit = new QDateEdit;
350 maximumDateEdit->setDisplayFormat("MMM d yyyy");
351 maximumDateEdit->setDateRange(calendar->minimumDate(),
352 calendar->maximumDate());
353 maximumDateEdit->setDate(calendar->maximumDate());
354
355 maximumDateLabel = new QLabel(tr("Ma&ximum Date:"));
356 maximumDateLabel->setBuddy(maximumDateEdit);
357
358//! [13] //! [14]
359 connect(currentDateEdit, SIGNAL(dateChanged(QDate)),
360 calendar, SLOT(setSelectedDate(QDate)));
361 connect(calendar, SIGNAL(selectionChanged()),
362 this, SLOT(selectedDateChanged()));
363 connect(minimumDateEdit, SIGNAL(dateChanged(QDate)),
364 this, SLOT(minimumDateChanged(QDate)));
365 connect(maximumDateEdit, SIGNAL(dateChanged(QDate)),
366 this, SLOT(maximumDateChanged(QDate)));
367
368//! [14]
369 QGridLayout *dateBoxLayout = new QGridLayout;
370 dateBoxLayout->addWidget(currentDateLabel, 1, 0);
371 dateBoxLayout->addWidget(currentDateEdit, 1, 1);
372 dateBoxLayout->addWidget(minimumDateLabel, 0, 0);
373 dateBoxLayout->addWidget(minimumDateEdit, 0, 1);
374 dateBoxLayout->addWidget(maximumDateLabel, 2, 0);
375 dateBoxLayout->addWidget(maximumDateEdit, 2, 1);
376 dateBoxLayout->setRowStretch(3, 1);
377
378 datesGroupBox->setLayout(dateBoxLayout);
379//! [15]
380}
381//! [15]
382
383//! [16]
384void Window::createTextFormatsGroupBox()
385{
386 textFormatsGroupBox = new QGroupBox(tr("Text Formats"));
387
388 weekdayColorCombo = createColorComboBox();
389 weekdayColorCombo->setCurrentIndex(
390 weekdayColorCombo->findText(tr("Black")));
391
392 weekdayColorLabel = new QLabel(tr("&Weekday color:"));
393 weekdayColorLabel->setBuddy(weekdayColorCombo);
394
395 weekendColorCombo = createColorComboBox();
396 weekendColorCombo->setCurrentIndex(
397 weekendColorCombo->findText(tr("Red")));
398
399 weekendColorLabel = new QLabel(tr("Week&end color:"));
400 weekendColorLabel->setBuddy(weekendColorCombo);
401
402//! [16] //! [17]
403 headerTextFormatCombo = new QComboBox;
404 headerTextFormatCombo->addItem(tr("Bold"));
405 headerTextFormatCombo->addItem(tr("Italic"));
406 headerTextFormatCombo->addItem(tr("Plain"));
407
408 headerTextFormatLabel = new QLabel(tr("&Header text:"));
409 headerTextFormatLabel->setBuddy(headerTextFormatCombo);
410
411 firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue"));
412
413 mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
414
415//! [17] //! [18]
416 connect(weekdayColorCombo, SIGNAL(currentIndexChanged(int)),
417 this, SLOT(weekdayFormatChanged()));
418 connect(weekendColorCombo, SIGNAL(currentIndexChanged(int)),
419 this, SLOT(weekendFormatChanged()));
420 connect(headerTextFormatCombo, SIGNAL(currentIndexChanged(QString)),
421 this, SLOT(reformatHeaders()));
422 connect(firstFridayCheckBox, SIGNAL(toggled(bool)),
423 this, SLOT(reformatCalendarPage()));
424 connect(mayFirstCheckBox, SIGNAL(toggled(bool)),
425 this, SLOT(reformatCalendarPage()));
426
427//! [18]
428 QHBoxLayout *checkBoxLayout = new QHBoxLayout;
429 checkBoxLayout->addWidget(firstFridayCheckBox);
430 checkBoxLayout->addStretch();
431 checkBoxLayout->addWidget(mayFirstCheckBox);
432
433 QGridLayout *outerLayout = new QGridLayout;
434 outerLayout->addWidget(weekdayColorLabel, 0, 0);
435 outerLayout->addWidget(weekdayColorCombo, 0, 1);
436 outerLayout->addWidget(weekendColorLabel, 1, 0);
437 outerLayout->addWidget(weekendColorCombo, 1, 1);
438 outerLayout->addWidget(headerTextFormatLabel, 2, 0);
439 outerLayout->addWidget(headerTextFormatCombo, 2, 1);
440 outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
441 textFormatsGroupBox->setLayout(outerLayout);
442
443 weekdayFormatChanged();
444 weekendFormatChanged();
445//! [19]
446 reformatHeaders();
447 reformatCalendarPage();
448}
449//! [19]
450
451//! [20]
452QComboBox *Window::createColorComboBox()
453{
454 QComboBox *comboBox = new QComboBox;
455 comboBox->addItem(tr("Red"), Qt::red);
456 comboBox->addItem(tr("Blue"), Qt::blue);
457 comboBox->addItem(tr("Black"), Qt::black);
458 comboBox->addItem(tr("Magenta"), Qt::magenta);
459 return comboBox;
460}
461//! [20]
Note: See TracBrowser for help on using the repository browser.