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

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

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 16.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "window.h"
45
46//! [0]
47Window::Window()
48{
49 createPreviewGroupBox();
50 createGeneralOptionsGroupBox();
51 createDatesGroupBox();
52 createTextFormatsGroupBox();
53
54 QGridLayout *layout = new QGridLayout;
55 layout->addWidget(previewGroupBox, 0, 0);
56 layout->addWidget(generalOptionsGroupBox, 0, 1);
57 layout->addWidget(datesGroupBox, 1, 0);
58 layout->addWidget(textFormatsGroupBox, 1, 1);
59 layout->setSizeConstraint(QLayout::SetFixedSize);
60 setLayout(layout);
61
62 previewLayout->setRowMinimumHeight(0, calendar->sizeHint().height());
63 previewLayout->setColumnMinimumWidth(0, calendar->sizeHint().width());
64
65 setWindowTitle(tr("Calendar Widget"));
66}
67//! [0]
68
69void Window::localeChanged(int index)
70{
71 calendar->setLocale(localeCombo->itemData(index).toLocale());
72}
73
74//! [1]
75void Window::firstDayChanged(int index)
76{
77 calendar->setFirstDayOfWeek(Qt::DayOfWeek(
78 firstDayCombo->itemData(index).toInt()));
79}
80//! [1]
81
82void Window::selectionModeChanged(int index)
83{
84 calendar->setSelectionMode(QCalendarWidget::SelectionMode(
85 selectionModeCombo->itemData(index).toInt()));
86}
87
88void Window::horizontalHeaderChanged(int index)
89{
90 calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
91 horizontalHeaderCombo->itemData(index).toInt()));
92}
93
94void Window::verticalHeaderChanged(int index)
95{
96 calendar->setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
97 verticalHeaderCombo->itemData(index).toInt()));
98}
99
100//! [2]
101void Window::selectedDateChanged()
102{
103 currentDateEdit->setDate(calendar->selectedDate());
104}
105//! [2]
106
107//! [3]
108void Window::minimumDateChanged(const QDate &date)
109{
110 calendar->setMinimumDate(date);
111 maximumDateEdit->setDate(calendar->maximumDate());
112}
113//! [3]
114
115//! [4]
116void Window::maximumDateChanged(const QDate &date)
117{
118 calendar->setMaximumDate(date);
119 minimumDateEdit->setDate(calendar->minimumDate());
120}
121//! [4]
122
123//! [5]
124void Window::weekdayFormatChanged()
125{
126 QTextCharFormat format;
127
128 format.setForeground(qvariant_cast<QColor>(
129 weekdayColorCombo->itemData(weekdayColorCombo->currentIndex())));
130 calendar->setWeekdayTextFormat(Qt::Monday, format);
131 calendar->setWeekdayTextFormat(Qt::Tuesday, format);
132 calendar->setWeekdayTextFormat(Qt::Wednesday, format);
133 calendar->setWeekdayTextFormat(Qt::Thursday, format);
134 calendar->setWeekdayTextFormat(Qt::Friday, format);
135}
136//! [5]
137
138//! [6]
139void Window::weekendFormatChanged()
140{
141 QTextCharFormat format;
142
143 format.setForeground(qvariant_cast<QColor>(
144 weekendColorCombo->itemData(weekendColorCombo->currentIndex())));
145 calendar->setWeekdayTextFormat(Qt::Saturday, format);
146 calendar->setWeekdayTextFormat(Qt::Sunday, format);
147}
148//! [6]
149
150//! [7]
151void Window::reformatHeaders()
152{
153 QString text = headerTextFormatCombo->currentText();
154 QTextCharFormat format;
155
156 if (text == tr("Bold")) {
157 format.setFontWeight(QFont::Bold);
158 } else if (text == tr("Italic")) {
159 format.setFontItalic(true);
160 } else if (text == tr("Green")) {
161 format.setForeground(Qt::green);
162 }
163 calendar->setHeaderTextFormat(format);
164}
165//! [7]
166
167//! [8]
168void Window::reformatCalendarPage()
169{
170 QTextCharFormat mayFirstFormat;
171 if (mayFirstCheckBox->isChecked())
172 mayFirstFormat.setForeground(Qt::red);
173
174 QTextCharFormat firstFridayFormat;
175 if (firstFridayCheckBox->isChecked())
176 firstFridayFormat.setForeground(Qt::blue);
177
178 QDate date(calendar->yearShown(), calendar->monthShown(), 1);
179
180 calendar->setDateTextFormat(QDate(date.year(), 5, 1), mayFirstFormat);
181
182 date.setDate(date.year(), date.month(), 1);
183 while (date.dayOfWeek() != Qt::Friday)
184 date = date.addDays(1);
185 calendar->setDateTextFormat(date, firstFridayFormat);
186}
187//! [8]
188
189//! [9]
190void Window::createPreviewGroupBox()
191{
192 previewGroupBox = new QGroupBox(tr("Preview"));
193
194 calendar = new QCalendarWidget;
195 calendar->setMinimumDate(QDate(1900, 1, 1));
196 calendar->setMaximumDate(QDate(3000, 1, 1));
197 calendar->setGridVisible(true);
198
199 connect(calendar, SIGNAL(currentPageChanged(int, int)),
200 this, SLOT(reformatCalendarPage()));
201
202 previewLayout = new QGridLayout;
203 previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
204 previewGroupBox->setLayout(previewLayout);
205}
206//! [9]
207
208//! [10]
209void Window::createGeneralOptionsGroupBox()
210{
211 generalOptionsGroupBox = new QGroupBox(tr("General Options"));
212
213 localeCombo = new QComboBox;
214 int curLocaleIndex = -1;
215 int index = 0;
216 for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) {
217 QLocale::Language lang = static_cast<QLocale::Language>(_lang);
218 QList<QLocale::Country> countries = QLocale::countriesForLanguage(lang);
219 for (int i = 0; i < countries.count(); ++i) {
220 QLocale::Country country = countries.at(i);
221 QString label = QLocale::languageToString(lang);
222 label += QLatin1Char('/');
223 label += QLocale::countryToString(country);
224 QLocale locale(lang, country);
225 if (this->locale().language() == lang && this->locale().country() == country)
226 curLocaleIndex = index;
227 localeCombo->addItem(label, locale);
228 ++index;
229 }
230 }
231 if (curLocaleIndex != -1)
232 localeCombo->setCurrentIndex(curLocaleIndex);
233 localeLabel = new QLabel(tr("&Locale"));
234 localeLabel->setBuddy(localeCombo);
235
236 firstDayCombo = new QComboBox;
237 firstDayCombo->addItem(tr("Sunday"), Qt::Sunday);
238 firstDayCombo->addItem(tr("Monday"), Qt::Monday);
239 firstDayCombo->addItem(tr("Tuesday"), Qt::Tuesday);
240 firstDayCombo->addItem(tr("Wednesday"), Qt::Wednesday);
241 firstDayCombo->addItem(tr("Thursday"), Qt::Thursday);
242 firstDayCombo->addItem(tr("Friday"), Qt::Friday);
243 firstDayCombo->addItem(tr("Saturday"), Qt::Saturday);
244
245 firstDayLabel = new QLabel(tr("Wee&k starts on:"));
246 firstDayLabel->setBuddy(firstDayCombo);
247//! [10]
248
249 selectionModeCombo = new QComboBox;
250 selectionModeCombo->addItem(tr("Single selection"),
251 QCalendarWidget::SingleSelection);
252 selectionModeCombo->addItem(tr("None"), QCalendarWidget::NoSelection);
253
254 selectionModeLabel = new QLabel(tr("&Selection mode:"));
255 selectionModeLabel->setBuddy(selectionModeCombo);
256
257 gridCheckBox = new QCheckBox(tr("&Grid"));
258 gridCheckBox->setChecked(calendar->isGridVisible());
259
260 navigationCheckBox = new QCheckBox(tr("&Navigation bar"));
261 navigationCheckBox->setChecked(true);
262
263 horizontalHeaderCombo = new QComboBox;
264 horizontalHeaderCombo->addItem(tr("Single letter day names"),
265 QCalendarWidget::SingleLetterDayNames);
266 horizontalHeaderCombo->addItem(tr("Short day names"),
267 QCalendarWidget::ShortDayNames);
268 horizontalHeaderCombo->addItem(tr("None"),
269 QCalendarWidget::NoHorizontalHeader);
270 horizontalHeaderCombo->setCurrentIndex(1);
271
272 horizontalHeaderLabel = new QLabel(tr("&Horizontal header:"));
273 horizontalHeaderLabel->setBuddy(horizontalHeaderCombo);
274
275 verticalHeaderCombo = new QComboBox;
276 verticalHeaderCombo->addItem(tr("ISO week numbers"),
277 QCalendarWidget::ISOWeekNumbers);
278 verticalHeaderCombo->addItem(tr("None"), QCalendarWidget::NoVerticalHeader);
279
280 verticalHeaderLabel = new QLabel(tr("&Vertical header:"));
281 verticalHeaderLabel->setBuddy(verticalHeaderCombo);
282
283//! [11]
284 connect(localeCombo, SIGNAL(currentIndexChanged(int)),
285 this, SLOT(localeChanged(int)));
286 connect(firstDayCombo, SIGNAL(currentIndexChanged(int)),
287 this, SLOT(firstDayChanged(int)));
288 connect(selectionModeCombo, SIGNAL(currentIndexChanged(int)),
289 this, SLOT(selectionModeChanged(int)));
290 connect(gridCheckBox, SIGNAL(toggled(bool)),
291 calendar, SLOT(setGridVisible(bool)));
292 connect(navigationCheckBox, SIGNAL(toggled(bool)),
293 calendar, SLOT(setNavigationBarVisible(bool)));
294 connect(horizontalHeaderCombo, SIGNAL(currentIndexChanged(int)),
295 this, SLOT(horizontalHeaderChanged(int)));
296 connect(verticalHeaderCombo, SIGNAL(currentIndexChanged(int)),
297 this, SLOT(verticalHeaderChanged(int)));
298//! [11]
299
300 QHBoxLayout *checkBoxLayout = new QHBoxLayout;
301 checkBoxLayout->addWidget(gridCheckBox);