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]
|
---|
47 | Window::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 |
|
---|
69 | void Window::localeChanged(int index)
|
---|
70 | {
|
---|
71 | calendar->setLocale(localeCombo->itemData(index).toLocale());
|
---|
72 | }
|
---|
73 |
|
---|
74 | //! [1]
|
---|
75 | void Window::firstDayChanged(int index)
|
---|
76 | {
|
---|
77 | calendar->setFirstDayOfWeek(Qt::DayOfWeek(
|
---|
78 | firstDayCombo->itemData(index).toInt()));
|
---|
79 | }
|
---|
80 | //! [1]
|
---|
81 |
|
---|
82 | void Window::selectionModeChanged(int index)
|
---|
83 | {
|
---|
84 | calendar->setSelectionMode(QCalendarWidget::SelectionMode(
|
---|
85 | selectionModeCombo->itemData(index).toInt()));
|
---|
86 | }
|
---|
87 |
|
---|
88 | void Window::horizontalHeaderChanged(int index)
|
---|
89 | {
|
---|
90 | calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
|
---|
91 | horizontalHeaderCombo->itemData(index).toInt()));
|
---|
92 | }
|
---|
93 |
|
---|
94 | void Window::verticalHeaderChanged(int index)
|
---|
95 | {
|
---|
96 | calendar->setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
|
---|
97 | verticalHeaderCombo->itemData(index).toInt()));
|
---|
98 | }
|
---|
99 |
|
---|
100 | //! [2]
|
---|
101 | void Window::selectedDateChanged()
|
---|
102 | {
|
---|
103 | currentDateEdit->setDate(calendar->selectedDate());
|
---|
104 | }
|
---|
105 | //! [2]
|
---|
106 |
|
---|
107 | //! [3]
|
---|
108 | void Window::minimumDateChanged(const QDate &date)
|
---|
109 | {
|
---|
110 | calendar->setMinimumDate(date);
|
---|
111 | maximumDateEdit->setDate(calendar->maximumDate());
|
---|
112 | }
|
---|
113 | //! [3]
|
---|
114 |
|
---|
115 | //! [4]
|
---|
116 | void Window::maximumDateChanged(const QDate &date)
|
---|
117 | {
|
---|
118 | calendar->setMaximumDate(date);
|
---|
119 | minimumDateEdit->setDate(calendar->minimumDate());
|
---|
120 | }
|
---|
121 | //! [4]
|
---|
122 |
|
---|
123 | //! [5]
|
---|
124 | void 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]
|
---|
139 | void 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]
|
---|
151 | void 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]
|
---|
168 | void 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]
|
---|
190 | void 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]
|
---|
209 | void Window::createGeneralOptionsGroupBox()
|
---|
210 | {
|
---|
211 | generalOptionsGroupBox = new QGroupBox(tr("General Options"));
|
---|
|
---|