calwidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation ([email protected])
  6. **
  7. ** This file is part of the examples of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:BSD$
  10. ** You may use this file under the terms of the BSD license as follows:
  11. **
  12. ** "Redistribution and use in source and binary forms, with or without
  13. ** modification, are permitted provided that the following conditions are
  14. ** met:
  15. ** * Redistributions of source code must retain the above copyright
  16. ** notice, this list of conditions and the following disclaimer.
  17. ** * Redistributions in binary form must reproduce the above copyright
  18. ** notice, this list of conditions and the following disclaimer in
  19. ** the documentation and/or other materials provided with the
  20. ** distribution.
  21. ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
  22. ** the names of its contributors may be used to endorse or promote
  23. ** products derived from this software without specific prior written
  24. ** permission.
  25. **
  26. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. #include <QtGui>
  41. #include "calwidget.h"
  42. Window::Window()
  43. {
  44. createPreviewGroupBox();
  45. createGeneralOptionsGroupBox();
  46. createDatesGroupBox();
  47. createTextFormatsGroupBox();
  48. QGridLayout *layout = new QGridLayout;
  49. layout->addWidget(previewGroupBox, 0, 0);
  50. layout->addWidget(generalOptionsGroupBox, 0, 1);
  51. layout->addWidget(datesGroupBox, 1, 0);
  52. layout->addWidget(textFormatsGroupBox, 1, 1);
  53. layout->setSizeConstraint(QLayout::SetFixedSize);
  54. setLayout(layout);
  55. previewLayout->setRowMinimumHeight(0, calendar->sizeHint().height());
  56. previewLayout->setColumnMinimumWidth(0, calendar->sizeHint().width());
  57. setWindowTitle(tr("Calendar Widget"));
  58. }
  59. void Window::localeChanged(int index)
  60. {
  61. calendar->setLocale(localeCombo->itemData(index).toLocale());
  62. }
  63. void Window::firstDayChanged(int index)
  64. {
  65. calendar->setFirstDayOfWeek(Qt::DayOfWeek(
  66. firstDayCombo->itemData(index).toInt()));
  67. }
  68. void Window::selectionModeChanged(int index)
  69. {
  70. calendar->setSelectionMode(QCalendarWidget::SelectionMode(
  71. selectionModeCombo->itemData(index).toInt()));
  72. }
  73. void Window::horizontalHeaderChanged(int index)
  74. {
  75. calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
  76. horizontalHeaderCombo->itemData(index).toInt()));
  77. }
  78. void Window::verticalHeaderChanged(int index)
  79. {
  80. calendar->setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
  81. verticalHeaderCombo->itemData(index).toInt()));
  82. }
  83. void Window::selectedDateChanged()
  84. {
  85. currentDateEdit->setDate(calendar->selectedDate());
  86. }
  87. void Window::minimumDateChanged(const QDate &date)
  88. {
  89. calendar->setMinimumDate(date);
  90. maximumDateEdit->setDate(calendar->maximumDate());
  91. }
  92. void Window::maximumDateChanged(const QDate &date)
  93. {
  94. calendar->setMaximumDate(date);
  95. minimumDateEdit->setDate(calendar->minimumDate());
  96. }
  97. void Window::weekdayFormatChanged()
  98. {
  99. QTextCharFormat format;
  100. format.setForeground(qvariant_cast<QColor>(
  101. weekdayColorCombo->itemData(weekdayColorCombo->currentIndex())));
  102. calendar->setWeekdayTextFormat(Qt::Monday, format);
  103. calendar->setWeekdayTextFormat(Qt::Tuesday, format);
  104. calendar->setWeekdayTextFormat(Qt::Wednesday, format);
  105. calendar->setWeekdayTextFormat(Qt::Thursday, format);
  106. calendar->setWeekdayTextFormat(Qt::Friday, format);
  107. }
  108. void Window::weekendFormatChanged()
  109. {
  110. QTextCharFormat format;
  111. format.setForeground(qvariant_cast<QColor>(
  112. weekendColorCombo->itemData(weekendColorCombo->currentIndex())));
  113. calendar->setWeekdayTextFormat(Qt::Saturday, format);
  114. calendar->setWeekdayTextFormat(Qt::Sunday, format);
  115. }
  116. void Window::reformatHeaders()
  117. {
  118. QString text = headerTextFormatCombo->currentText();
  119. QTextCharFormat format;
  120. if (text == tr("Bold")) {
  121. format.setFontWeight(QFont::Bold);
  122. } else if (text == tr("Italic")) {
  123. format.setFontItalic(true);
  124. } else if (text == tr("Green")) {
  125. format.setForeground(Qt::green);
  126. }
  127. calendar->setHeaderTextFormat(format);
  128. }
  129. void Window::reformatCalendarPage()
  130. {
  131. if (firstFridayCheckBox->isChecked()) {
  132. QDate firstFriday(calendar->yearShown(), calendar->monthShown(), 1);
  133. while (firstFriday.dayOfWeek() != Qt::Friday)
  134. firstFriday = firstFriday.addDays(1);
  135. QTextCharFormat firstFridayFormat;
  136. firstFridayFormat.setForeground(Qt::blue);
  137. calendar->setDateTextFormat(firstFriday, firstFridayFormat);
  138. }
  139. //May First in Red takes precedence
  140. if (mayFirstCheckBox->isChecked()) {
  141. const QDate mayFirst(calendar->yearShown(), 5, 1);
  142. QTextCharFormat mayFirstFormat;
  143. mayFirstFormat.setForeground(Qt::red);
  144. calendar->setDateTextFormat(mayFirst, mayFirstFormat);
  145. }
  146. }
  147. void Window::createPreviewGroupBox()
  148. {
  149. previewGroupBox = new QGroupBox(tr("Preview"));
  150. calendar = new QCalendarWidget;
  151. calendar->setMinimumDate(QDate(1900, 1, 1));
  152. calendar->setMaximumDate(QDate(3000, 1, 1));
  153. calendar->setGridVisible(true);
  154. connect(calendar, SIGNAL(currentPageChanged(int,int)),
  155. this, SLOT(reformatCalendarPage()));
  156. previewLayout = new QGridLayout;
  157. previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
  158. previewGroupBox->setLayout(previewLayout);
  159. }
  160. void Window::createGeneralOptionsGroupBox()
  161. {
  162. generalOptionsGroupBox = new QGroupBox(tr("General Options"));
  163. localeCombo = new QComboBox;
  164. int curLocaleIndex = -1;
  165. int index = 0;
  166. for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) {
  167. QLocale::Language lang = static_cast<QLocale::Language>(_lang);
  168. QList<QLocale::Country> countries = QLocale::countriesForLanguage(lang);
  169. for (int i = 0; i < countries.count(); ++i) {
  170. QLocale::Country country = countries.at(i);
  171. QString label = QLocale::languageToString(lang);
  172. label += QLatin1Char('/');
  173. label += QLocale::countryToString(country);
  174. QLocale locale(lang, country);
  175. if (this->locale().language() == lang && this->locale().country() == country)
  176. curLocaleIndex = index;
  177. localeCombo->addItem(label, locale);
  178. ++index;
  179. }
  180. }
  181. if (curLocaleIndex != -1)
  182. localeCombo->setCurrentIndex(curLocaleIndex);
  183. localeLabel = new QLabel(tr("&Locale"));
  184. localeLabel->setBuddy(localeCombo);
  185. firstDayCombo = new QComboBox;
  186. firstDayCombo->addItem(tr("Sunday"), Qt::Sunday);
  187. firstDayCombo->addItem(tr("Monday"), Qt::Monday);
  188. firstDayCombo->addItem(tr("Tuesday"), Qt::Tuesday);
  189. firstDayCombo->addItem(tr("Wednesday"), Qt::Wednesday);
  190. firstDayCombo->addItem(tr("Thursday"), Qt::Thursday);
  191. firstDayCombo->addItem(tr("Friday"), Qt::Friday);
  192. firstDayCombo->addItem(tr("Saturday"), Qt::Saturday);
  193. firstDayLabel = new QLabel(tr("Wee&k starts on:"));
  194. firstDayLabel->setBuddy(firstDayCombo);
  195. selectionModeCombo = new QComboBox;
  196. selectionModeCombo->addItem(tr("Single selection"),
  197. QCalendarWidget::SingleSelection);
  198. selectionModeCombo->addItem(tr("None"), QCalendarWidget::NoSelection);
  199. selectionModeLabel = new QLabel(tr("&Selection mode:"));
  200. selectionModeLabel->setBuddy(selectionModeCombo);
  201. gridCheckBox = new QCheckBox(tr("&Grid"));
  202. gridCheckBox->setChecked(calendar->isGridVisible());
  203. navigationCheckBox = new QCheckBox(tr("&Navigation bar"));
  204. navigationCheckBox->setChecked(true);
  205. horizontalHeaderCombo = new QComboBox;
  206. horizontalHeaderCombo->addItem(tr("Single letter day names"),
  207. QCalendarWidget::SingleLetterDayNames);
  208. horizontalHeaderCombo->addItem(tr("Short day names"),
  209. QCalendarWidget::ShortDayNames);
  210. horizontalHeaderCombo->addItem(tr("None"),
  211. QCalendarWidget::NoHorizontalHeader);
  212. horizontalHeaderCombo->setCurrentIndex(1);
  213. horizontalHeaderLabel = new QLabel(tr("&Horizontal header:"));
  214. horizontalHeaderLabel->setBuddy(horizontalHeaderCombo);
  215. verticalHeaderCombo = new QComboBox;
  216. verticalHeaderCombo->addItem(tr("ISO week numbers"),
  217. QCalendarWidget::ISOWeekNumbers);
  218. verticalHeaderCombo->addItem(tr("None"), QCalendarWidget::NoVerticalHeader);
  219. verticalHeaderLabel = new QLabel(tr("&Vertical header:"));
  220. verticalHeaderLabel->setBuddy(verticalHeaderCombo);
  221. connect(localeCombo, SIGNAL(currentIndexChanged(int)),
  222. this, SLOT(localeChanged(int)));
  223. connect(firstDayCombo, SIGNAL(currentIndexChanged(int)),
  224. this, SLOT(firstDayChanged(int)));
  225. connect(selectionModeCombo, SIGNAL(currentIndexChanged(int)),
  226. this, SLOT(selectionModeChanged(int)));
  227. connect(gridCheckBox, SIGNAL(toggled(bool)),
  228. calendar, SLOT(setGridVisible(bool)));
  229. connect(navigationCheckBox, SIGNAL(toggled(bool)),
  230. calendar, SLOT(setNavigationBarVisible(bool)));
  231. connect(horizontalHeaderCombo, SIGNAL(currentIndexChanged(int)),
  232. this, SLOT(horizontalHeaderChanged(int)));
  233. connect(verticalHeaderCombo, SIGNAL(currentIndexChanged(int)),
  234. this, SLOT(verticalHeaderChanged(int)));
  235. QHBoxLayout *checkBoxLayout = new QHBoxLayout;
  236. checkBoxLayout->addWidget(gridCheckBox);
  237. checkBoxLayout->addStretch();
  238. checkBoxLayout->addWidget(navigationCheckBox);
  239. QGridLayout *outerLayout = new QGridLayout;
  240. outerLayout->addWidget(localeLabel, 0, 0);
  241. outerLayout->addWidget(localeCombo, 0, 1);
  242. outerLayout->addWidget(firstDayLabel, 1, 0);
  243. outerLayout->addWidget(firstDayCombo, 1, 1);
  244. outerLayout->addWidget(selectionModeLabel, 2, 0);
  245. outerLayout->addWidget(selectionModeCombo, 2, 1);
  246. outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
  247. outerLayout->addWidget(horizontalHeaderLabel, 4, 0);
  248. outerLayout->addWidget(horizontalHeaderCombo, 4, 1);
  249. outerLayout->addWidget(verticalHeaderLabel, 5, 0);
  250. outerLayout->addWidget(verticalHeaderCombo, 5, 1);
  251. generalOptionsGroupBox->setLayout(outerLayout);
  252. firstDayChanged(firstDayCombo->currentIndex());
  253. selectionModeChanged(selectionModeCombo->currentIndex());
  254. horizontalHeaderChanged(horizontalHeaderCombo->currentIndex());
  255. verticalHeaderChanged(verticalHeaderCombo->currentIndex());
  256. }
  257. void Window::createDatesGroupBox()
  258. {
  259. datesGroupBox = new QGroupBox(tr("Dates"));
  260. minimumDateEdit = new QDateEdit;
  261. minimumDateEdit->setDisplayFormat("MMM d yyyy");
  262. minimumDateEdit->setDateRange(calendar->minimumDate(),
  263. calendar->maximumDate());
  264. minimumDateEdit->setDate(calendar->minimumDate());
  265. minimumDateLabel = new QLabel(tr("&Minimum Date:"));
  266. minimumDateLabel->setBuddy(minimumDateEdit);
  267. currentDateEdit = new QDateEdit;
  268. currentDateEdit->setDisplayFormat("MMM d yyyy");
  269. currentDateEdit->setDate(calendar->selectedDate());
  270. currentDateEdit->setDateRange(calendar->minimumDate(),
  271. calendar->maximumDate());
  272. currentDateLabel = new QLabel(tr("&Current Date:"));
  273. currentDateLabel->setBuddy(currentDateEdit);
  274. maximumDateEdit = new QDateEdit;
  275. maximumDateEdit->setDisplayFormat("MMM d yyyy");
  276. maximumDateEdit->setDateRange(calendar->minimumDate(),
  277. calendar->maximumDate());
  278. maximumDateEdit->setDate(calendar->maximumDate());
  279. maximumDateLabel = new QLabel(tr("Ma&ximum Date:"));
  280. maximumDateLabel->setBuddy(maximumDateEdit);
  281. connect(currentDateEdit, SIGNAL(dateChanged(QDate)),
  282. calendar, SLOT(setSelectedDate(QDate)));
  283. connect(calendar, SIGNAL(selectionChanged()),
  284. this, SLOT(selectedDateChanged()));
  285. connect(minimumDateEdit, SIGNAL(dateChanged(QDate)),
  286. this, SLOT(minimumDateChanged(QDate)));
  287. connect(maximumDateEdit, SIGNAL(dateChanged(QDate)),
  288. this, SLOT(maximumDateChanged(QDate)));
  289. QGridLayout *dateBoxLayout = new QGridLayout;
  290. dateBoxLayout->addWidget(currentDateLabel, 1, 0);
  291. dateBoxLayout->addWidget(currentDateEdit, 1, 1);
  292. dateBoxLayout->addWidget(minimumDateLabel, 0, 0);
  293. dateBoxLayout->addWidget(minimumDateEdit, 0, 1);
  294. dateBoxLayout->addWidget(maximumDateLabel, 2, 0);
  295. dateBoxLayout->addWidget(maximumDateEdit, 2, 1);
  296. dateBoxLayout->setRowStretch(3, 1);
  297. datesGroupBox->setLayout(dateBoxLayout);
  298. }
  299. void Window::createTextFormatsGroupBox()
  300. {
  301. textFormatsGroupBox = new QGroupBox(tr("Text Formats"));
  302. weekdayColorCombo = createColorComboBox();
  303. weekdayColorCombo->setCurrentIndex(
  304. weekdayColorCombo->findText(tr("Black")));
  305. weekdayColorLabel = new QLabel(tr("&Weekday color:"));
  306. weekdayColorLabel->setBuddy(weekdayColorCombo);
  307. weekendColorCombo = createColorComboBox();
  308. weekendColorCombo->setCurrentIndex(
  309. weekendColorCombo->findText(tr("Red")));
  310. weekendColorLabel = new QLabel(tr("Week&end color:"));
  311. weekendColorLabel->setBuddy(weekendColorCombo);
  312. headerTextFormatCombo = new QComboBox;
  313. headerTextFormatCombo->addItem(tr("Bold"));
  314. headerTextFormatCombo->addItem(tr("Italic"));
  315. headerTextFormatCombo->addItem(tr("Plain"));
  316. headerTextFormatLabel = new QLabel(tr("&Header text:"));
  317. headerTextFormatLabel->setBuddy(headerTextFormatCombo);
  318. firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue"));
  319. mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
  320. connect(weekdayColorCombo, SIGNAL(currentIndexChanged(int)),
  321. this, SLOT(weekdayFormatChanged()));
  322. connect(weekendColorCombo, SIGNAL(currentIndexChanged(int)),
  323. this, SLOT(weekendFormatChanged()));
  324. connect(headerTextFormatCombo, SIGNAL(currentIndexChanged(QString)),
  325. this, SLOT(reformatHeaders()));
  326. connect(firstFridayCheckBox, SIGNAL(toggled(bool)),
  327. this, SLOT(reformatCalendarPage()));
  328. connect(mayFirstCheckBox, SIGNAL(toggled(bool)),
  329. this, SLOT(reformatCalendarPage()));
  330. QHBoxLayout *checkBoxLayout = new QHBoxLayout;
  331. checkBoxLayout->addWidget(firstFridayCheckBox);
  332. checkBoxLayout->addStretch();
  333. checkBoxLayout->addWidget(mayFirstCheckBox);
  334. QGridLayout *outerLayout = new QGridLayout;
  335. outerLayout->addWidget(weekdayColorLabel, 0, 0);
  336. outerLayout->addWidget(weekdayColorCombo, 0, 1);
  337. outerLayout->addWidget(weekendColorLabel, 1, 0);
  338. outerLayout->addWidget(weekendColorCombo, 1, 1);
  339. outerLayout->addWidget(headerTextFormatLabel, 2, 0);
  340. outerLayout->addWidget(headerTextFormatCombo, 2, 1);
  341. outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
  342. textFormatsGroupBox->setLayout(outerLayout);
  343. weekdayFormatChanged();
  344. weekendFormatChanged();
  345. reformatHeaders();
  346. reformatCalendarPage();
  347. }
  348. QComboBox *Window::createColorComboBox()
  349. {
  350. QComboBox *comboBox = new QComboBox;
  351. comboBox->addItem(tr("Red"), Qt::red);
  352. comboBox->addItem(tr("Blue"), Qt::blue);
  353. comboBox->addItem(tr("Black"), Qt::black);
  354. comboBox->addItem(tr("Magenta"), Qt::magenta);
  355. return comboBox;
  356. }
  357. //#include "moc_calwidget.cpp"