Merge branch 'master' of git://labs.trolltech.com/qtscriptgenerator
[qtscriptgenerator/amarok.git] / examples / CalendarWidget.qs
blob9f9f876d057cf1bd7869b36db9c1f45e9ab9c4ab
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file.  Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function tr(s) { return s; }
26 function CalendarWidget(parent) { 
27     QWidget.call(this, parent);
29     this.createPreviewGroupBox();
30     this.createGeneralOptionsGroupBox();
31     this.createDatesGroupBox();
32     this.createTextFormatsGroupBox();
33     
34     var layout = new QGridLayout();
35     layout.addWidget(this.previewGroupBox, 0, 0);
36     layout.addWidget(this.generalOptionsGroupBox, 0, 1);
37     layout.addWidget(this.datesGroupBox, 1, 0);
38     layout.addWidget(this.textFormatsGroupBox, 1, 1);
39     layout.sizeConstraint = QLayout.SetFixedSize;
40     this.setLayout(layout);
41     
42     this.previewLayout.setRowMinimumHeight(0, this.calendar.sizeHint.height());
43     this.previewLayout.setColumnMinimumWidth(0, this.calendar.sizeHint.width());
44     
45     this.setWindowTitle(tr("Calendar Widget"));
48 CalendarWidget.prototype = new QWidget();
50 CalendarWidget.prototype.localeChanged = function(index) {
51     this.calendar.setLocale(this.localeCombo.itemData(index));
54 CalendarWidget.prototype.firstDayChanged = function(index) {
55     this.calendar.firstDayOfWeek = this.firstDayCombo.itemData(index);
58 CalendarWidget.prototype.selectionModeChanged = function(index) {
59     this.calendar.selectionMode = this.selectionModeCombo.itemData(index);
62 CalendarWidget.prototype.horizontalHeaderChanged = function(index) {
63     this.calendar.horizontalHeaderFormat = this.horizontalHeaderCombo.itemData(index);
66 CalendarWidget.prototype.verticalHeaderChanged = function(index) {
67     this.calendar.verticalHeaderFormat = this.verticalHeaderCombo.itemData(index);
70 CalendarWidget.prototype.selectedDateChanged = function() {
71     this.currentDateEdit.setDate(this.calendar.selectedDate);
74 CalendarWidget.prototype.minimumDateChanged = function(date) {
75     this.calendar.minimumDate = date;
76     this.maximumDateEdit.setDate(this.calendar.maximumDate);
79 CalendarWidget.prototype.maximumDateChanged = function(date) {
80     this.calendar.setMaximumDate(date);
81     this.minimumDateEdit.setDate(this.calendar.minimumDate);
84 CalendarWidget.prototype.weekdayFormatChanged = function() {
85     var format = new QTextCharFormat();
86     format.setForeground(new QBrush(this.weekdayColorCombo.itemData(this.weekdayColorCombo.currentIndex)));
88     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Monday, format);
89     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Tuesday, format);
90     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Wednesday, format);
91     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Thursday, format);
92     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Friday, format);
95 CalendarWidget.prototype.weekendFormatChanged = function() {
96     var format = new QTextCharFormat();
97     format.setForeground(new QBrush(this.weekendColorCombo.itemData(this.weekendColorCombo.currentIndex)));
98     
99     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Saturday, format);
100     this.calendar.setWeekdayTextFormat(Qt.DayOfWeek.Sunday, format);
103 CalendarWidget.prototype.reformatHeaders = function() {
104     var text = this.headerTextFormatCombo.currentText;
105     var format = new QTextCharFormat();
107     if (text == tr("Bold")) {
108         format.setFontWeight(QFont.Bold);
109     } else if (text == tr("Italic")) {
110         format.setFontItalic(true);
111     } else if (text == tr("Green")) {
112         format.setForeground(new QBrush(new QColor(Qt.green)));
113     }
114     
115     this.calendar.setHeaderTextFormat(format);
118 CalendarWidget.prototype.reformatCalendarPage = function() {
119     var mayFirstFormat = new QTextCharFormat();
120     if (this.mayFirstCheckBox.isChecked)
121         mayFirstFormat.setForeground(new QBrush(new QColor(Qt.red)));
123     var firstFridayFormat = new QTextCharFormat();
124     if (this.firstFridayCheckBox.isChecked)
125         firstFridayFormat.setForeground(new QBrush(new QColor(Qt.blue)));
126     
127     var date = new Date(this.calendar.yearShown(), this.calendar.monthShown(), 1); //fixme
129     this.calendar.setDateTextFormat(new Date(date.year, 5, 1), mayFirstFormat);
131     date.setDate(date.year, date.month, 1);
132  //fixme
133  //   while (date.dayOfWeek != Qt.Friday)
134  //       date = date.addDays(1);
135     this.calendar.setDateTextFormat(date, firstFridayFormat);
138 CalendarWidget.prototype.createPreviewGroupBox = function() {
139     this.previewGroupBox = new QGroupBox(tr("Preview"));
141     this.calendar = new QCalendarWidget();
142     this.calendar.minimumDate = new Date(1900, 1, 1);
143     this.calendar.maximumDate = new Date(3000, 1, 1);
144     this.calendar.setGridVisible(true);
146     this.calendar.currentPageChanged.connect(this, this.reformatCalendarPage);
148     this.previewLayout = new QGridLayout();
149     this.previewLayout.addWidget(this.calendar, 0, 0, Qt.AlignmentFlag.AlignCenter);
150     this.previewGroupBox.setLayout(this.previewLayout);
153 CalendarWidget.prototype.createGeneralOptionsGroupBox = function() {
154     this.generalOptionsGroupBox = new QGroupBox(tr("General Options"));
155     
156     this.localeCombo = new QComboBox();
157     var curLocaleIndex = -1;
158     var index = 0;
160     
162     for (var lang in QLocale.Language) {
163         var countries = QLocale.countriesForLanguage(lang);
164             
165         for (var country in countries) {
166             var label = QLocale.languageToString(lang);
167             label += "/";
168             label += QLocale.countryToString(country);
169             var locale = new QLocale(lang, country);
170             if (this.locale().language() == lang && this.locale().country() == country)
171                 curLocaleIndex = index;
173             localeCombo.addItem(label, locale);
174             ++index;
175         }
176     }
178     if (curLocaleIndex != -1)
179         localeCombo.setCurrentIndex(curLocaleIndex);
180     this.localeLabel = new QLabel(tr("&Locale"));
181     this.localeLabel.setBuddy(this.localeCombo);
182     
183     this.firstDayCombo = new QComboBox();
184     this.firstDayCombo.addItem(null, tr("Sunday"), Qt.DayOfWeek.Sunday);
185     this.firstDayCombo.addItem(null, tr("Monday"), Qt.DayOfWeek.Monday);
186     this.firstDayCombo.addItem(null, tr("Tuesday"), Qt.DayOfWeek.Tuesday);
187     this.firstDayCombo.addItem(null, tr("Wednesday"), Qt.DayOfWeek.Wednesday);
188     this.firstDayCombo.addItem(null, tr("Thursday"), Qt.DayOfWeek.Thursday);
189     this.firstDayCombo.addItem(null, tr("Friday"), Qt.DayOfWeek.Friday);
190     this.firstDayCombo.addItem(null, tr("Saturday"), Qt.DayOfWeek.Saturday);
191     
192     this.firstDayLabel = new QLabel(tr("Wee&k starts on:"));
193     this.firstDayLabel.setBuddy(this.firstDayCombo);
194     
195     this.selectionModeCombo = new QComboBox();
196     this.selectionModeCombo.addItem(null, tr("Single selection"),
197                                QCalendarWidget.SelectionMode.SingleSelection);
198     this.selectionModeCombo.addItem(null, tr("None"), QCalendarWidget.SelectionMode.NoSelection);
199     
200     this.selectionModeLabel = new QLabel(tr("&Selection mode:"));
201     this.selectionModeLabel.setBuddy(this.selectionModeCombo);
202     
203     this.gridCheckBox = new QCheckBox(tr("&Grid"));
204     this.gridCheckBox.setChecked(this.calendar.gridVisible);
205     
206     this.navigationCheckBox = new QCheckBox(tr("&Navigation bar"));
207     this.navigationCheckBox.setChecked(true);
208     
209     this.horizontalHeaderCombo = new QComboBox();
210     this.horizontalHeaderCombo.addItem(null, tr("Single letter day names"),
211                                   QCalendarWidget.HorizontalHeaderFormat.SingleLetterDayNames);
212     this.horizontalHeaderCombo.addItem(null, tr("Short day names"),
213                                   QCalendarWidget.HorizontalHeaderFormat.ShortDayNames);
214     this.horizontalHeaderCombo.addItem(null, tr("None"),
215                                   QCalendarWidget.HorizontalHeaderFormat.NoHorizontalHeader);
216     this.horizontalHeaderCombo.setCurrentIndex(1);
217     
218     this.horizontalHeaderLabel = new QLabel(tr("&Horizontal header:"));
219     this.horizontalHeaderLabel.setBuddy(this.horizontalHeaderCombo);
220     
221     this.verticalHeaderCombo = new QComboBox();
222     this.verticalHeaderCombo.addItem(null, tr("ISO week numbers"),
223                                 QCalendarWidget.VerticalHeaderFormat.ISOWeekNumbers);
224     this.verticalHeaderCombo.addItem(null, tr("None"), QCalendarWidget.VerticalHeaderFormat.NoVerticalHeader);
225     
226     this.verticalHeaderLabel = new QLabel(tr("&Vertical header:"));
227     this.verticalHeaderLabel.setBuddy(this.verticalHeaderCombo);
228     
229     this.localeCombo.currentIndexChanged.connect(this, this.localeChanged);
230     this.firstDayCombo.currentIndexChanged.connect(this, this.firstDayChanged);
231     this.selectionModeCombo.currentIndexChanged.connect(this, this.selectionModeChanged);
232     this.gridCheckBox.toggled.connect(this, this.calendar.setGridVisible);        
233     this.navigationCheckBox.toggled.connect(this, this.calendar.setNavigationBarVisible);
234     this.horizontalHeaderCombo.currentIndexChanged.connect(this, this.horizontalHeaderChanged);
235     this.verticalHeaderCombo.currentIndexChanged.connect(this, "verticalHeaderChanged");
236     
237     var checkBoxLayout = new QHBoxLayout();
238     checkBoxLayout.addWidget(this.gridCheckBox, 0, Qt.AlignmentFlag(1));  //FIXME
239     checkBoxLayout.addStretch();
240     checkBoxLayout.addWidget(this.navigationCheckBox, 0, Qt.AlignmentFlag(1));  //FIXME
241     var outerLayout = new QGridLayout();
242     outerLayout.addWidget(this.localeLabel, 0, 0);
243     outerLayout.addWidget(this.localeCombo, 0, 1);
244     outerLayout.addWidget(this.firstDayLabel, 1, 0);
245     outerLayout.addWidget(this.firstDayCombo, 1, 1);
246     outerLayout.addWidget(this.selectionModeLabel, 2, 0);
247     outerLayout.addWidget(this.selectionModeCombo, 2, 1);
248     outerLayout.addLayout(checkBoxLayout, 3, 0, 1, 2);
249     outerLayout.addWidget(this.horizontalHeaderLabel, 4, 0);
250     outerLayout.addWidget(this.horizontalHeaderCombo, 4, 1);
251     outerLayout.addWidget(this.verticalHeaderLabel, 5, 0);
252     outerLayout.addWidget(this.verticalHeaderCombo, 5, 1);
254     this.generalOptionsGroupBox.setLayout(outerLayout);
255      
256     this.firstDayChanged(this.firstDayCombo.currentIndex);
257     this.selectionModeChanged(this.selectionModeCombo.currentIndex);
258     this.horizontalHeaderChanged(this.horizontalHeaderCombo.currentIndex);
259     this.verticalHeaderChanged(this.verticalHeaderCombo.currentIndex);
262 CalendarWidget.prototype.createDatesGroupBox = function() {
263     this.datesGroupBox = new QGroupBox(tr("Dates"));
264     
265     this.minimumDateEdit = new QDateEdit();
266     this.minimumDateEdit.displayFormat = "MMM d, yyyy";
267     this.minimumDateEdit.setDateRange(this.calendar.minimumDate,
268                                  this.calendar.maximumDate);
269     this.minimumDateEdit.setDate(this.calendar.minimumDate);
270     
272     this.minimumDateLabel = new QLabel(tr("&Minimum Date:"));
273     this.minimumDateLabel.setBuddy(this.minimumDateEdit);
274     
275     this.currentDateEdit = new QDateEdit();
276     this.currentDateEdit.displayFormat = "MMM d, yyyy";
277     this.currentDateEdit.setDate(this.calendar.selectedDate);
278     this.currentDateEdit.setDateRange(this.calendar.minimumDate,
279                                  this.calendar.maximumDate);
280     
281     this.currentDateLabel = new QLabel(tr("&Current Date:"));
282     this.currentDateLabel.setBuddy(this.currentDateEdit);
283     
284     this.maximumDateEdit = new QDateEdit();
285     this.maximumDateEdit.displayFormat = "MMM d, yyyy";
286     this.maximumDateEdit.setDateRange(this.calendar.minimumDate,
287                                  this.calendar.maximumDate);
288     this.maximumDateEdit.setDate(this.calendar.maximumDate);
290     this.maximumDateLabel = new QLabel(tr("Ma&ximum Date:"));
291     this.maximumDateLabel.setBuddy(this.maximumDateEdit);
292     
293     this.currentDateEdit.dateChanged.connect(this, this.calendar.setSelectedDate);
294     this.calendar.selectionChanged.connect(this, this.selectedDateChanged);
295     this.minimumDateEdit.dateChanged.connect(this, this.minimumDateChanged);
296     this.maximumDateEdit.dateChanged.connect(this, this.maximumDateChanged);
297     
298     var dateBoxLayout = new QGridLayout();
299     dateBoxLayout.addWidget(this.currentDateLabel, 1, 0);
300     dateBoxLayout.addWidget(this.currentDateEdit, 1, 1);
301     dateBoxLayout.addWidget(this.minimumDateLabel, 0, 0);
302     dateBoxLayout.addWidget(this.minimumDateEdit, 0, 1);
303     dateBoxLayout.addWidget(this.maximumDateLabel, 2, 0);
304     dateBoxLayout.addWidget(this.maximumDateEdit, 2, 1);
305     dateBoxLayout.setRowStretch(3, 1);
306     
307     this.datesGroupBox.setLayout(dateBoxLayout);
310 CalendarWidget.prototype.createTextFormatsGroupBox = function() {
311     this.textFormatsGroupBox = new QGroupBox(tr("Text Formats"));
312     
313     this.weekdayColorCombo = this.createColorComboBox();
314     this.weekdayColorCombo.setCurrentIndex(this.weekdayColorCombo.findText(tr("Black")));
315     
316     this.weekdayColorLabel = new QLabel(tr("&Weekday color:"));
317     this.weekdayColorLabel.setBuddy(this.weekdayColorCombo);
318     
319     this.weekendColorCombo = this.createColorComboBox();
320     this.weekendColorCombo.setCurrentIndex(this.weekendColorCombo.findText(tr("Red")));
321     
322     this.weekendColorLabel = new QLabel(tr("Week&end color:"));
323     this.weekendColorLabel.setBuddy(this.weekendColorCombo);
324     
325     this.headerTextFormatCombo = new QComboBox();
326     this.headerTextFormatCombo.addItem(tr("Bold"));
327     this.headerTextFormatCombo.addItem(tr("Italic"));
328     this.headerTextFormatCombo.addItem(tr("Plain"));
329    
330     this.headerTextFormatLabel = new QLabel(tr("&Header text:"));
331     this.headerTextFormatLabel.setBuddy(this.headerTextFormatCombo);
332     
333     this.firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue"));
334     
335     this.mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
336     
337     this.weekdayColorCombo.currentIndexChanged.connect(this, this.weekdayFormatChanged);
338     this.weekendColorCombo.currentIndexChanged.connect(this, this.weekendFormatChanged);
339     this.headerTextFormatCombo.currentIndexChanged.connect(this, this.reformatHeaders);
340     this.firstFridayCheckBox.toggled.connect(this, this.reformatCalendarPage);
341     this.mayFirstCheckBox.toggled.connect(this, this.reformatCalendarPage);
342     
343     var checkBoxLayout = new QHBoxLayout();
344     checkBoxLayout.addWidget(this.firstFridayCheckBox, 0, Qt.AlignmentFlag(1));
345     checkBoxLayout.addStretch();
346     checkBoxLayout.addWidget(this.mayFirstCheckBox, 0, Qt.AlignmentFlag(1));
347      
348     var outerLayout = new QGridLayout();
349     outerLayout.addWidget(this.weekdayColorLabel, 0, 0);
350     outerLayout.addWidget(this.weekdayColorCombo, 0, 1);
351     outerLayout.addWidget(this.weekendColorLabel, 1, 0);
352     outerLayout.addWidget(this.weekendColorCombo, 1, 1);
353     outerLayout.addWidget(this.headerTextFormatLabel, 2, 0);
354     outerLayout.addWidget(this.headerTextFormatCombo, 2, 1);
355     outerLayout.addLayout(checkBoxLayout, 3, 0, 1, 2);
356     this.textFormatsGroupBox.setLayout(outerLayout);
357   
358     this.weekdayFormatChanged();
359     this.weekendFormatChanged();
360     this.reformatHeaders();
361     this.reformatCalendarPage();
364 CalendarWidget.prototype.createColorComboBox = function() {
365     var comboBox = new QComboBox();
366     comboBox.addItem(null, tr("Red"), new QColor(Qt.red));
367     comboBox.addItem(null, tr("Blue"), new QColor(Qt.blue));
368     comboBox.addItem(null, tr("Black"), new QColor(Qt.black));
369     comboBox.addItem(null, tr("Magenta"), new QColor(Qt.magenta));
370     return comboBox;
374 var calendarWidget = new CalendarWidget(null);
375 calendarWidget.show();
377 QCoreApplication.exec();