Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 6242

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: window.cpp Example File (widgets/calendarwidget/window.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">window.cpp Example File<br /><span class="small-subtitle">widgets/calendarwidget/window.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &lt;QtGui&gt;

 #include &quot;window.h&quot;

 Window::Window()
 {
     createPreviewGroupBox();
     createGeneralOptionsGroupBox();
     createDatesGroupBox();
     createTextFormatsGroupBox();

     QGridLayout *layout = new QGridLayout;
     layout-&gt;addWidget(previewGroupBox, 0, 0);
     layout-&gt;addWidget(generalOptionsGroupBox, 0, 1);
     layout-&gt;addWidget(datesGroupBox, 1, 0);
     layout-&gt;addWidget(textFormatsGroupBox, 1, 1);
     layout-&gt;setSizeConstraint(QLayout::SetFixedSize);
     setLayout(layout);

     previewLayout-&gt;setRowMinimumHeight(0, calendar-&gt;sizeHint().height());
     previewLayout-&gt;setColumnMinimumWidth(0, calendar-&gt;sizeHint().width());

     setWindowTitle(tr(&quot;Calendar Widget&quot;));
 }

 void Window::localeChanged(int index)
 {
     calendar-&gt;setLocale(localeCombo-&gt;itemData(index).toLocale());
 }

 void Window::firstDayChanged(int index)
 {
     calendar-&gt;setFirstDayOfWeek(Qt::DayOfWeek(
                                 firstDayCombo-&gt;itemData(index).toInt()));
 }

 void Window::selectionModeChanged(int index)
 {
     calendar-&gt;setSelectionMode(QCalendarWidget::SelectionMode(
                                selectionModeCombo-&gt;itemData(index).toInt()));
 }

 void Window::horizontalHeaderChanged(int index)
 {
     calendar-&gt;setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
         horizontalHeaderCombo-&gt;itemData(index).toInt()));
 }

 void Window::verticalHeaderChanged(int index)
 {
     calendar-&gt;setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
         verticalHeaderCombo-&gt;itemData(index).toInt()));
 }

 void Window::selectedDateChanged()
 {
     currentDateEdit-&gt;setDate(calendar-&gt;selectedDate());
 }

 void Window::minimumDateChanged(const QDate &amp;date)
 {
     calendar-&gt;setMinimumDate(date);
     maximumDateEdit-&gt;setDate(calendar-&gt;maximumDate());
 }

 void Window::maximumDateChanged(const QDate &amp;date)
 {
     calendar-&gt;setMaximumDate(date);
     minimumDateEdit-&gt;setDate(calendar-&gt;minimumDate());
 }

 void Window::weekdayFormatChanged()
 {
     QTextCharFormat format;

     format.setForeground(qvariant_cast&lt;QColor&gt;(
         weekdayColorCombo-&gt;itemData(weekdayColorCombo-&gt;currentIndex())));
     calendar-&gt;setWeekdayTextFormat(Qt::Monday, format);
     calendar-&gt;setWeekdayTextFormat(Qt::Tuesday, format);
     calendar-&gt;setWeekdayTextFormat(Qt::Wednesday, format);
     calendar-&gt;setWeekdayTextFormat(Qt::Thursday, format);
     calendar-&gt;setWeekdayTextFormat(Qt::Friday, format);
 }

 void Window::weekendFormatChanged()
 {
     QTextCharFormat format;

     format.setForeground(qvariant_cast&lt;QColor&gt;(
         weekendColorCombo-&gt;itemData(weekendColorCombo-&gt;currentIndex())));
     calendar-&gt;setWeekdayTextFormat(Qt::Saturday, format);
     calendar-&gt;setWeekdayTextFormat(Qt::Sunday, format);
 }

 void Window::reformatHeaders()
 {
     QString text = headerTextFormatCombo-&gt;currentText();
     QTextCharFormat format;

     if (text == tr(&quot;Bold&quot;)) {
         format.setFontWeight(QFont::Bold);
     } else if (text == tr(&quot;Italic&quot;)) {
         format.setFontItalic(true);
     } else if (text == tr(&quot;Green&quot;)) {
         format.setForeground(Qt::green);
     }
     calendar-&gt;setHeaderTextFormat(format);
 }

 void Window::reformatCalendarPage()
 {
     if (firstFridayCheckBox-&gt;isChecked()) {
         QDate firstFriday(calendar-&gt;yearShown(), calendar-&gt;monthShown(), 1);
         while (firstFriday.dayOfWeek() != Qt::Friday)
             firstFriday = firstFriday.addDays(1);
         QTextCharFormat firstFridayFormat;
         firstFridayFormat.setForeground(Qt::blue);
         calendar-&gt;setDateTextFormat(firstFriday, firstFridayFormat);
     }

     <span class="comment">//May First in Red takes precedence</span>
     if (mayFirstCheckBox-&gt;isChecked()) {
         const QDate mayFirst(calendar-&gt;yearShown(), 5, 1);
         QTextCharFormat mayFirstFormat;
         mayFirstFormat.setForeground(Qt::red);
         calendar-&gt;setDateTextFormat(mayFirst, mayFirstFormat);
     }
 }

 void Window::createPreviewGroupBox()
 {
     previewGroupBox = new QGroupBox(tr(&quot;Preview&quot;));

     calendar = new QCalendarWidget;
     calendar-&gt;setMinimumDate(QDate(1900, 1, 1));
     calendar-&gt;setMaximumDate(QDate(3000, 1, 1));
     calendar-&gt;setGridVisible(true);

     connect(calendar, SIGNAL(currentPageChanged(int,int)),
             this, SLOT(reformatCalendarPage()));

     previewLayout = new QGridLayout;
     previewLayout-&gt;addWidget(calendar, 0, 0, Qt::AlignCenter);
     previewGroupBox-&gt;setLayout(previewLayout);
 }

 void Window::createGeneralOptionsGroupBox()
 {
     generalOptionsGroupBox = new QGroupBox(tr(&quot;General Options&quot;));

     localeCombo = new QComboBox;
     int curLocaleIndex = -1;
     int index = 0;
     for (int _lang = QLocale::C; _lang &lt;= QLocale::LastLanguage; ++_lang) {
         QLocale::Language lang = static_cast&lt;QLocale::Language&gt;(_lang);
         QList&lt;QLocale::Country&gt; countries = QLocale::countriesForLanguage(lang);
         for (int i = 0; i &lt; countries.count(); ++i) {
             QLocale::Country country = countries.at(i);
             QString label = QLocale::languageToString(lang);
             label += QLatin1Char('/');
             label += QLocale::countryToString(country);
             QLocale locale(lang, country);
             if (this-&gt;locale().language() == lang &amp;&amp; this-&gt;locale().country() == country)
                 curLocaleIndex = index;
             localeCombo-&gt;addItem(label, locale);
             ++index;
         }
     }
     if (curLocaleIndex != -1)
         localeCombo-&gt;setCurrentIndex(curLocaleIndex);
     localeLabel = new QLabel(tr(&quot;&amp;Locale&quot;));
     localeLabel-&gt;setBuddy(localeCombo);

     firstDayCombo = new QComboBox;
     firstDayCombo-&gt;addItem(tr(&quot;Sunday&quot;), Qt::Sunday);
     firstDayCombo-&gt;addItem(tr(&quot;Monday&quot;), Qt::Monday);
     firstDayCombo-&gt;addItem(tr(&quot;Tuesday&quot;), Qt::Tuesday);
     firstDayCombo-&gt;addItem(tr(&quot;Wednesday&quot;), Qt::Wednesday);
     firstDayCombo-&gt;addItem(tr(&quot;Thursday&quot;), Qt::Thursday);
     firstDayCombo-&gt;addItem(tr(&quot;Friday&quot;), Qt::Friday);
     firstDayCombo-&gt;addItem(tr(&quot;Saturday&quot;), Qt::Saturday);

     firstDayLabel = new QLabel(tr(&quot;Wee&amp;k starts on:&quot;));
     firstDayLabel-&gt;setBuddy(firstDayCombo);

     selectionModeCombo = new QComboBox;
     selectionModeCombo-&gt;addItem(tr(&quot;Single selection&quot;),
                                 QCalendarWidget::SingleSelection);
     selectionModeCombo-&gt;addItem(tr(&quot;None&quot;), QCalendarWidget::NoSelection);

     selectionModeLabel = new QLabel(tr(&quot;&amp;Selection mode:&quot;));
     selectionModeLabel-&gt;setBuddy(selectionModeCombo);

     gridCheckBox = new QCheckBox(tr(&quot;&amp;Grid&quot;));
     gridCheckBox-&gt;setChecked(calendar-&gt;isGridVisible());

     navigationCheckBox = new QCheckBox(tr(&quot;&amp;Navigation bar&quot;));
     navigationCheckBox-&gt;setChecked(true);

     horizontalHeaderCombo = new QComboBox;
     horizontalHeaderCombo-&gt;addItem(tr(&quot;Single letter day names&quot;),
                                    QCalendarWidget::SingleLetterDayNames);
     horizontalHeaderCombo-&gt;addItem(tr(&quot;Short day names&quot;),
                                    QCalendarWidget::ShortDayNames);
     horizontalHeaderCombo-&gt;addItem(tr(&quot;None&quot;),
                                    QCalendarWidget::NoHorizontalHeader);
     horizontalHeaderCombo-&gt;setCurrentIndex(1);

     horizontalHeaderLabel = new QLabel(tr(&quot;&amp;Horizontal header:&quot;));
     horizontalHeaderLabel-&gt;setBuddy(horizontalHeaderCombo);

     verticalHeaderCombo = new QComboBox;
     verticalHeaderCombo-&gt;addItem(tr(&quot;ISO week numbers&quot;),
                                  QCalendarWidget::ISOWeekNumbers);
     verticalHeaderCombo-&gt;addItem(tr(&quot;None&quot;), QCalendarWidget::NoVerticalHeader);

     verticalHeaderLabel = new QLabel(tr(&quot;&amp;Vertical header:&quot;));
     verticalHeaderLabel-&gt;setBuddy(verticalHeaderCombo);

     connect(localeCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(localeChanged(int)));
     connect(firstDayCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(firstDayChanged(int)));
     connect(selectionModeCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(selectionModeChanged(int)));
     connect(gridCheckBox, SIGNAL(toggled(bool)),
             calendar, SLOT(setGridVisible(bool)));
     connect(navigationCheckBox, SIGNAL(toggled(bool)),
             calendar, SLOT(setNavigationBarVisible(bool)));
     connect(horizontalHeaderCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(horizontalHeaderChanged(int)));
     connect(verticalHeaderCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(verticalHeaderChanged(int)));

     QHBoxLayout *checkBoxLayout = new QHBoxLayout;
     checkBoxLayout-&gt;addWidget(gridCheckBox);
     checkBoxLayout-&gt;addStretch();
     checkBoxLayout-&gt;addWidget(navigationCheckBox);

     QGridLayout *outerLayout = new QGridLayout;
     outerLayout-&gt;addWidget(localeLabel, 0, 0);
     outerLayout-&gt;addWidget(localeCombo, 0, 1);
     outerLayout-&gt;addWidget(firstDayLabel, 1, 0);
     outerLayout-&gt;addWidget(firstDayCombo, 1, 1);
     outerLayout-&gt;addWidget(selectionModeLabel, 2, 0);
     outerLayout-&gt;addWidget(selectionModeCombo, 2, 1);
     outerLayout-&gt;addLayout(checkBoxLayout, 3, 0, 1, 2);
     outerLayout-&gt;addWidget(horizontalHeaderLabel, 4, 0);
     outerLayout-&gt;addWidget(horizontalHeaderCombo, 4, 1);
     outerLayout-&gt;addWidget(verticalHeaderLabel, 5, 0);
     outerLayout-&gt;addWidget(verticalHeaderCombo, 5, 1);
     generalOptionsGroupBox-&gt;setLayout(outerLayout);

     firstDayChanged(firstDayCombo-&gt;currentIndex());
     selectionModeChanged(selectionModeCombo-&gt;currentIndex());
     horizontalHeaderChanged(horizontalHeaderCombo-&gt;currentIndex());
     verticalHeaderChanged(verticalHeaderCombo-&gt;currentIndex());
 }

 void Window::createDatesGroupBox()
 {
     datesGroupBox = new QGroupBox(tr(&quot;Dates&quot;));

     minimumDateEdit = new QDateEdit;
     minimumDateEdit-&gt;setDisplayFormat(&quot;MMM d yyyy&quot;);
     minimumDateEdit-&gt;setDateRange(calendar-&gt;minimumDate(),
                                   calendar-&gt;maximumDate());
     minimumDateEdit-&gt;setDate(calendar-&gt;minimumDate());

     minimumDateLabel = new QLabel(tr(&quot;&amp;Minimum Date:&quot;));
     minimumDateLabel-&gt;setBuddy(minimumDateEdit);

     currentDateEdit = new QDateEdit;
     currentDateEdit-&gt;setDisplayFormat(&quot;MMM d yyyy&quot;);
     currentDateEdit-&gt;setDate(calendar-&gt;selectedDate());
     currentDateEdit-&gt;setDateRange(calendar-&gt;minimumDate(),
                                   calendar-&gt;maximumDate());

     currentDateLabel = new QLabel(tr(&quot;&amp;Current Date:&quot;));
     currentDateLabel-&gt;setBuddy(currentDateEdit);

     maximumDateEdit = new QDateEdit;
     maximumDateEdit-&gt;setDisplayFormat(&quot;MMM d yyyy&quot;);
     maximumDateEdit-&gt;setDateRange(calendar-&gt;minimumDate(),
                                   calendar-&gt;maximumDate());
     maximumDateEdit-&gt;setDate(calendar-&gt;maximumDate());

     maximumDateLabel = new QLabel(tr(&quot;Ma&amp;ximum Date:&quot;));
     maximumDateLabel-&gt;setBuddy(maximumDateEdit);

     connect(currentDateEdit, SIGNAL(dateChanged(QDate)),
             calendar, SLOT(setSelectedDate(QDate)));
     connect(calendar, SIGNAL(selectionChanged()),
             this, SLOT(selectedDateChanged()));
     connect(minimumDateEdit, SIGNAL(dateChanged(QDate)),
             this, SLOT(minimumDateChanged(QDate)));
     connect(maximumDateEdit, SIGNAL(dateChanged(QDate)),
             this, SLOT(maximumDateChanged(QDate)));

     QGridLayout *dateBoxLayout = new QGridLayout;
     dateBoxLayout-&gt;addWidget(currentDateLabel, 1, 0);
     dateBoxLayout-&gt;addWidget(currentDateEdit, 1, 1);
     dateBoxLayout-&gt;addWidget(minimumDateLabel, 0, 0);
     dateBoxLayout-&gt;addWidget(minimumDateEdit, 0, 1);
     dateBoxLayout-&gt;addWidget(maximumDateLabel, 2, 0);
     dateBoxLayout-&gt;addWidget(maximumDateEdit, 2, 1);
     dateBoxLayout-&gt;setRowStretch(3, 1);

     datesGroupBox-&gt;setLayout(dateBoxLayout);
 }

 void Window::createTextFormatsGroupBox()
 {
     textFormatsGroupBox = new QGroupBox(tr(&quot;Text Formats&quot;));

     weekdayColorCombo = createColorComboBox();
     weekdayColorCombo-&gt;setCurrentIndex(
             weekdayColorCombo-&gt;findText(tr(&quot;Black&quot;)));

     weekdayColorLabel = new QLabel(tr(&quot;&amp;Weekday color:&quot;));
     weekdayColorLabel-&gt;setBuddy(weekdayColorCombo);

     weekendColorCombo = createColorComboBox();
     weekendColorCombo-&gt;setCurrentIndex(
             weekendColorCombo-&gt;findText(tr(&quot;Red&quot;)));

     weekendColorLabel = new QLabel(tr(&quot;Week&amp;end color:&quot;));
     weekendColorLabel-&gt;setBuddy(weekendColorCombo);

     headerTextFormatCombo = new QComboBox;
     headerTextFormatCombo-&gt;addItem(tr(&quot;Bold&quot;));
     headerTextFormatCombo-&gt;addItem(tr(&quot;Italic&quot;));
     headerTextFormatCombo-&gt;addItem(tr(&quot;Plain&quot;));

     headerTextFormatLabel = new QLabel(tr(&quot;&amp;Header text:&quot;));
     headerTextFormatLabel-&gt;setBuddy(headerTextFormatCombo);

     firstFridayCheckBox = new QCheckBox(tr(&quot;&amp;First Friday in blue&quot;));

     mayFirstCheckBox = new QCheckBox(tr(&quot;May &amp;1 in red&quot;));

     connect(weekdayColorCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(weekdayFormatChanged()));
     connect(weekendColorCombo, SIGNAL(currentIndexChanged(int)),
             this, SLOT(weekendFormatChanged()));
     connect(headerTextFormatCombo, SIGNAL(currentIndexChanged(QString)),
             this, SLOT(reformatHeaders()));
     connect(firstFridayCheckBox, SIGNAL(toggled(bool)),
             this, SLOT(reformatCalendarPage()));
     connect(mayFirstCheckBox, SIGNAL(toggled(bool)),
             this, SLOT(reformatCalendarPage()));

     QHBoxLayout *checkBoxLayout = new QHBoxLayout;
     checkBoxLayout-&gt;addWidget(firstFridayCheckBox);
     checkBoxLayout-&gt;addStretch();
     checkBoxLayout-&gt;addWidget(mayFirstCheckBox);

     QGridLayout *outerLayout = new QGridLayout;
     outerLayout-&gt;addWidget(weekdayColorLabel, 0, 0);
     outerLayout-&gt;addWidget(weekdayColorCombo, 0, 1);
     outerLayout-&gt;addWidget(weekendColorLabel, 1, 0);
     outerLayout-&gt;addWidget(weekendColorCombo, 1, 1);
     outerLayout-&gt;addWidget(headerTextFormatLabel, 2, 0);
     outerLayout-&gt;addWidget(headerTextFormatCombo, 2, 1);
     outerLayout-&gt;addLayout(checkBoxLayout, 3, 0, 1, 2);
     textFormatsGroupBox-&gt;setLayout(outerLayout);

     weekdayFormatChanged();
     weekendFormatChanged();
     reformatHeaders();
     reformatCalendarPage();
 }

 QComboBox *Window::createColorComboBox()
 {
     QComboBox *comboBox = new QComboBox;
     comboBox-&gt;addItem(tr(&quot;Red&quot;), Qt::red);
     comboBox-&gt;addItem(tr(&quot;Blue&quot;), Qt::blue);
     comboBox-&gt;addItem(tr(&quot;Black&quot;), Qt::black);
     comboBox-&gt;addItem(tr(&quot;Magenta&quot;), Qt::magenta);
     return comboBox;
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>