examples/widgets/calendarwidget/window.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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     if (firstFridayCheckBox->isChecked()) {
       
   171         QDate firstFriday(calendar->yearShown(), calendar->monthShown(), 1);
       
   172         while (firstFriday.dayOfWeek() != Qt::Friday)
       
   173             firstFriday = firstFriday.addDays(1);
       
   174         QTextCharFormat firstFridayFormat;
       
   175         firstFridayFormat.setForeground(Qt::blue);
       
   176         calendar->setDateTextFormat(firstFriday, firstFridayFormat);
       
   177     }
       
   178 
       
   179     //May First in Red takes precedence
       
   180     if (mayFirstCheckBox->isChecked()) {
       
   181         const QDate mayFirst(calendar->yearShown(), 5, 1);
       
   182         QTextCharFormat mayFirstFormat;
       
   183         mayFirstFormat.setForeground(Qt::red);
       
   184         calendar->setDateTextFormat(mayFirst, mayFirstFormat);
       
   185     }
       
   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"));
       
   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);
       
   302     checkBoxLayout->addStretch();
       
   303     checkBoxLayout->addWidget(navigationCheckBox);
       
   304 
       
   305     QGridLayout *outerLayout = new QGridLayout;
       
   306     outerLayout->addWidget(localeLabel, 0, 0);
       
   307     outerLayout->addWidget(localeCombo, 0, 1);
       
   308     outerLayout->addWidget(firstDayLabel, 1, 0);
       
   309     outerLayout->addWidget(firstDayCombo, 1, 1);
       
   310     outerLayout->addWidget(selectionModeLabel, 2, 0);
       
   311     outerLayout->addWidget(selectionModeCombo, 2, 1);
       
   312     outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
       
   313     outerLayout->addWidget(horizontalHeaderLabel, 4, 0);
       
   314     outerLayout->addWidget(horizontalHeaderCombo, 4, 1);
       
   315     outerLayout->addWidget(verticalHeaderLabel, 5, 0);
       
   316     outerLayout->addWidget(verticalHeaderCombo, 5, 1);
       
   317     generalOptionsGroupBox->setLayout(outerLayout);
       
   318 
       
   319 //! [12]
       
   320     firstDayChanged(firstDayCombo->currentIndex());
       
   321     selectionModeChanged(selectionModeCombo->currentIndex());
       
   322     horizontalHeaderChanged(horizontalHeaderCombo->currentIndex());
       
   323     verticalHeaderChanged(verticalHeaderCombo->currentIndex());
       
   324 }
       
   325 //! [12]
       
   326 
       
   327 //! [13]
       
   328 void Window::createDatesGroupBox()
       
   329 {
       
   330     datesGroupBox = new QGroupBox(tr("Dates"));
       
   331 
       
   332     minimumDateEdit = new QDateEdit;
       
   333     minimumDateEdit->setDisplayFormat("MMM d yyyy");
       
   334     minimumDateEdit->setDateRange(calendar->minimumDate(),
       
   335                                   calendar->maximumDate());
       
   336     minimumDateEdit->setDate(calendar->minimumDate());
       
   337 
       
   338     minimumDateLabel = new QLabel(tr("&Minimum Date:"));
       
   339     minimumDateLabel->setBuddy(minimumDateEdit);
       
   340 
       
   341     currentDateEdit = new QDateEdit;
       
   342     currentDateEdit->setDisplayFormat("MMM d yyyy");
       
   343     currentDateEdit->setDate(calendar->selectedDate());
       
   344     currentDateEdit->setDateRange(calendar->minimumDate(),
       
   345                                   calendar->maximumDate());
       
   346 
       
   347     currentDateLabel = new QLabel(tr("&Current Date:"));
       
   348     currentDateLabel->setBuddy(currentDateEdit);
       
   349 
       
   350     maximumDateEdit = new QDateEdit;
       
   351     maximumDateEdit->setDisplayFormat("MMM d yyyy");
       
   352     maximumDateEdit->setDateRange(calendar->minimumDate(),
       
   353                                   calendar->maximumDate());
       
   354     maximumDateEdit->setDate(calendar->maximumDate());
       
   355 
       
   356     maximumDateLabel = new QLabel(tr("Ma&ximum Date:"));
       
   357     maximumDateLabel->setBuddy(maximumDateEdit);
       
   358 
       
   359 //! [13] //! [14]
       
   360     connect(currentDateEdit, SIGNAL(dateChanged(const QDate &)),
       
   361             calendar, SLOT(setSelectedDate(const QDate &)));
       
   362     connect(calendar, SIGNAL(selectionChanged()),
       
   363             this, SLOT(selectedDateChanged()));
       
   364     connect(minimumDateEdit, SIGNAL(dateChanged(const QDate &)),
       
   365             this, SLOT(minimumDateChanged(const QDate &)));
       
   366     connect(maximumDateEdit, SIGNAL(dateChanged(const QDate &)),
       
   367             this, SLOT(maximumDateChanged(const QDate &)));
       
   368 
       
   369 //! [14]
       
   370     QGridLayout *dateBoxLayout = new QGridLayout;
       
   371     dateBoxLayout->addWidget(currentDateLabel, 1, 0);
       
   372     dateBoxLayout->addWidget(currentDateEdit, 1, 1);
       
   373     dateBoxLayout->addWidget(minimumDateLabel, 0, 0);
       
   374     dateBoxLayout->addWidget(minimumDateEdit, 0, 1);
       
   375     dateBoxLayout->addWidget(maximumDateLabel, 2, 0);
       
   376     dateBoxLayout->addWidget(maximumDateEdit, 2, 1);
       
   377     dateBoxLayout->setRowStretch(3, 1);
       
   378 
       
   379     datesGroupBox->setLayout(dateBoxLayout);
       
   380 //! [15]
       
   381 }
       
   382 //! [15]
       
   383 
       
   384 //! [16]
       
   385 void Window::createTextFormatsGroupBox()
       
   386 {
       
   387     textFormatsGroupBox = new QGroupBox(tr("Text Formats"));
       
   388 
       
   389     weekdayColorCombo = createColorComboBox();
       
   390     weekdayColorCombo->setCurrentIndex(
       
   391             weekdayColorCombo->findText(tr("Black")));
       
   392 
       
   393     weekdayColorLabel = new QLabel(tr("&Weekday color:"));
       
   394     weekdayColorLabel->setBuddy(weekdayColorCombo);
       
   395 
       
   396     weekendColorCombo = createColorComboBox();
       
   397     weekendColorCombo->setCurrentIndex(
       
   398             weekendColorCombo->findText(tr("Red")));
       
   399 
       
   400     weekendColorLabel = new QLabel(tr("Week&end color:"));
       
   401     weekendColorLabel->setBuddy(weekendColorCombo);
       
   402 
       
   403 //! [16] //! [17]
       
   404     headerTextFormatCombo = new QComboBox;
       
   405     headerTextFormatCombo->addItem(tr("Bold"));
       
   406     headerTextFormatCombo->addItem(tr("Italic"));
       
   407     headerTextFormatCombo->addItem(tr("Plain"));
       
   408 
       
   409     headerTextFormatLabel = new QLabel(tr("&Header text:"));
       
   410     headerTextFormatLabel->setBuddy(headerTextFormatCombo);
       
   411 
       
   412     firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue"));
       
   413 
       
   414     mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
       
   415 
       
   416 //! [17] //! [18]
       
   417     connect(weekdayColorCombo, SIGNAL(currentIndexChanged(int)),
       
   418             this, SLOT(weekdayFormatChanged()));
       
   419     connect(weekendColorCombo, SIGNAL(currentIndexChanged(int)),
       
   420             this, SLOT(weekendFormatChanged()));
       
   421     connect(headerTextFormatCombo, SIGNAL(currentIndexChanged(const QString &)),
       
   422             this, SLOT(reformatHeaders()));
       
   423     connect(firstFridayCheckBox, SIGNAL(toggled(bool)),
       
   424             this, SLOT(reformatCalendarPage()));
       
   425     connect(mayFirstCheckBox, SIGNAL(toggled(bool)),
       
   426             this, SLOT(reformatCalendarPage()));
       
   427 
       
   428 //! [18]
       
   429     QHBoxLayout *checkBoxLayout = new QHBoxLayout;
       
   430     checkBoxLayout->addWidget(firstFridayCheckBox);
       
   431     checkBoxLayout->addStretch();
       
   432     checkBoxLayout->addWidget(mayFirstCheckBox);
       
   433 
       
   434     QGridLayout *outerLayout = new QGridLayout;
       
   435     outerLayout->addWidget(weekdayColorLabel, 0, 0);
       
   436     outerLayout->addWidget(weekdayColorCombo, 0, 1);
       
   437     outerLayout->addWidget(weekendColorLabel, 1, 0);
       
   438     outerLayout->addWidget(weekendColorCombo, 1, 1);
       
   439     outerLayout->addWidget(headerTextFormatLabel, 2, 0);
       
   440     outerLayout->addWidget(headerTextFormatCombo, 2, 1);
       
   441     outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
       
   442     textFormatsGroupBox->setLayout(outerLayout);
       
   443 
       
   444     weekdayFormatChanged();
       
   445     weekendFormatChanged();
       
   446 //! [19]
       
   447     reformatHeaders();
       
   448     reformatCalendarPage();
       
   449 }
       
   450 //! [19]
       
   451 
       
   452 //! [20]
       
   453 QComboBox *Window::createColorComboBox()
       
   454 {
       
   455     QComboBox *comboBox = new QComboBox;
       
   456     comboBox->addItem(tr("Red"), Qt::red);
       
   457     comboBox->addItem(tr("Blue"), Qt::blue);
       
   458     comboBox->addItem(tr("Black"), Qt::black);
       
   459     comboBox->addItem(tr("Magenta"), Qt::magenta);
       
   460     return comboBox;
       
   461 }
       
   462 //! [20]