calwidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 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: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 <QCalendarWidget>
  41. #include <QCheckBox>
  42. #include <QComboBox>
  43. #include <QDateEdit>
  44. #include <QGridLayout>
  45. #include <QGroupBox>
  46. #include <QLabel>
  47. #include <QTextCharFormat>
  48. #include "calwidget.h"
  49. #include "ui_calwidget.h"
  50. #ifdef UI_CALWIDGET_H
  51. #error Definition of UI_CALWIDGET_H should be disabled by file option.
  52. #endif
  53. Window::Window()
  54. : ui(new Ui::Window)
  55. {
  56. createPreviewGroupBox();
  57. createGeneralOptionsGroupBox();
  58. createDatesGroupBox();
  59. createTextFormatsGroupBox();
  60. QGridLayout* layout = new QGridLayout;
  61. layout->addWidget(previewGroupBox, 0, 0);
  62. layout->addWidget(generalOptionsGroupBox, 0, 1);
  63. layout->addWidget(datesGroupBox, 1, 0);
  64. layout->addWidget(textFormatsGroupBox, 1, 1);
  65. layout->setSizeConstraint(QLayout::SetFixedSize);
  66. setLayout(layout);
  67. previewLayout->setRowMinimumHeight(0, calendar->sizeHint().height());
  68. previewLayout->setColumnMinimumWidth(0, calendar->sizeHint().width());
  69. setWindowTitle(tr("Calendar Widget"));
  70. }
  71. void Window::localeChanged(int index)
  72. {
  73. calendar->setLocale(localeCombo->itemData(index).toLocale());
  74. }
  75. void Window::firstDayChanged(int index)
  76. {
  77. calendar->setFirstDayOfWeek(
  78. Qt::DayOfWeek(firstDayCombo->itemData(index).toInt()));
  79. }
  80. void Window::selectionModeChanged(int index)
  81. {
  82. calendar->setSelectionMode(QCalendarWidget::SelectionMode(
  83. selectionModeCombo->itemData(index).toInt()));
  84. }
  85. void Window::horizontalHeaderChanged(int index)
  86. {
  87. calendar->setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat(
  88. horizontalHeaderCombo->itemData(index).toInt()));
  89. }
  90. void Window::verticalHeaderChanged(int index)
  91. {
  92. calendar->setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat(
  93. verticalHeaderCombo->itemData(index).toInt()));
  94. }
  95. void Window::selectedDateChanged()
  96. {
  97. currentDateEdit->setDate(calendar->selectedDate());
  98. }
  99. void Window::minimumDateChanged(const QDate& date)
  100. {
  101. calendar->setMinimumDate(date);
  102. maximumDateEdit->setDate(calendar->maximumDate());
  103. }
  104. void Window::maximumDateChanged(const QDate& date)
  105. {
  106. calendar->setMaximumDate(date);
  107. minimumDateEdit->setDate(calendar->minimumDate());
  108. }
  109. void Window::weekdayFormatChanged()
  110. {
  111. QTextCharFormat format;
  112. format.setForeground(qvariant_cast<QColor>(
  113. weekdayColorCombo->itemData(weekdayColorCombo->currentIndex())));
  114. calendar->setWeekdayTextFormat(Qt::Monday, format);
  115. calendar->setWeekdayTextFormat(Qt::Tuesday, format);
  116. calendar->setWeekdayTextFormat(Qt::Wednesday, format);
  117. calendar->setWeekdayTextFormat(Qt::Thursday, format);
  118. calendar->setWeekdayTextFormat(Qt::Friday, format);
  119. }
  120. void Window::weekendFormatChanged()
  121. {
  122. QTextCharFormat format;
  123. format.setForeground(qvariant_cast<QColor>(
  124. weekendColorCombo->itemData(weekendColorCombo->currentIndex())));
  125. calendar->setWeekdayTextFormat(Qt::Saturday, format);
  126. calendar->setWeekdayTextFormat(Qt::Sunday, format);
  127. }
  128. void Window::reformatHeaders()
  129. {
  130. QString text = headerTextFormatCombo->currentText();
  131. QTextCharFormat format;
  132. if (text == tr("Bold")) {
  133. format.setFontWeight(QFont::Bold);
  134. } else if (text == tr("Italic")) {
  135. format.setFontItalic(true);
  136. } else if (text == tr("Green")) {
  137. format.setForeground(Qt::green);
  138. }
  139. calendar->setHeaderTextFormat(format);
  140. }
  141. void Window::reformatCalendarPage()
  142. {
  143. if (firstFridayCheckBox->isChecked()) {
  144. QDate firstFriday(calendar->yearShown(), calendar->monthShown(), 1);
  145. while (firstFriday.dayOfWeek() != Qt::Friday)
  146. firstFriday = firstFriday.addDays(1);
  147. QTextCharFormat firstFridayFormat;
  148. firstFridayFormat.setForeground(Qt::blue);
  149. calendar->setDateTextFormat(firstFriday, firstFridayFormat);
  150. }
  151. // May First in Red takes precedence
  152. if (mayFirstCheckBox->isChecked()) {
  153. const QDate mayFirst(calendar->yearShown(), 5, 1);
  154. QTextCharFormat mayFirstFormat;
  155. mayFirstFormat.setForeground(Qt::red);
  156. calendar->setDateTextFormat(mayFirst, mayFirstFormat);
  157. }
  158. }
  159. void Window::createPreviewGroupBox()
  160. {
  161. previewGroupBox = new QGroupBox(tr("Preview"));
  162. calendar = new QCalendarWidget;
  163. calendar->setMinimumDate(QDate(1900, 1, 1));
  164. calendar->setMaximumDate(QDate(3000, 1, 1));
  165. calendar->setGridVisible(true);
  166. connect(calendar, SIGNAL(currentPageChanged(int, int)), this,
  167. SLOT(reformatCalendarPage()));
  168. previewLayout = new QGridLayout;
  169. previewLayout->addWidget(calendar, 0, 0, Qt::AlignCenter);
  170. previewGroupBox->setLayout(previewLayout);
  171. }
  172. void Window::createGeneralOptionsGroupBox()
  173. {
  174. generalOptionsGroupBox = new QGroupBox(tr("General Options"));
  175. localeCombo = new QComboBox;
  176. int curLocaleIndex = -1;
  177. int index = 0;
  178. for (int _lang = QLocale::C; _lang <= QLocale::LastLanguage; ++_lang) {
  179. QLocale::Language lang = static_cast<QLocale::Language>(_lang);
  180. QList<QLocale::Country> countries = QLocale::countriesForLanguage(lang);
  181. for (int i = 0; i < countries.count(); ++i) {
  182. QLocale::Country country = countries.at(i);
  183. QString label = QLocale::languageToString(lang);
  184. label += QLatin1Char('/');
  185. label += QLocale::countryToString(country);
  186. QLocale locale(lang, country);
  187. if (this->locale().language() == lang &&
  188. this->locale().country() == country)
  189. curLocaleIndex = index;
  190. localeCombo->addItem(label, locale);
  191. ++index;
  192. }
  193. }
  194. if (curLocaleIndex != -1)
  195. localeCombo->setCurrentIndex(curLocaleIndex);
  196. localeLabel = new QLabel(tr("&Locale"));
  197. localeLabel->setBuddy(localeCombo);
  198. firstDayCombo = new QComboBox;
  199. firstDayCombo->addItem(tr("Sunday"), Qt::Sunday);
  200. firstDayCombo->addItem(tr("Monday"), Qt::Monday);
  201. firstDayCombo->addItem(tr("Tuesday"), Qt::Tuesday);
  202. firstDayCombo->addItem(tr("Wednesday"), Qt::Wednesday);
  203. firstDayCombo->addItem(tr("Thursday"), Qt::Thursday);
  204. firstDayCombo->addItem(tr("Friday"), Qt::Friday);
  205. firstDayCombo->addItem(tr("Saturday"), Qt::Saturday);
  206. firstDayLabel = new QLabel(tr("Wee&k starts on:"));
  207. firstDayLabel->setBuddy(firstDayCombo);
  208. selectionModeCombo = new QComboBox;
  209. selectionModeCombo->addItem(tr("Single selection"),
  210. QCalendarWidget::SingleSelection);
  211. selectionModeCombo->addItem(tr("None"), QCalendarWidget::NoSelection);
  212. selectionModeLabel = new QLabel(tr("&Selection mode:"));
  213. selectionModeLabel->setBuddy(selectionModeCombo);
  214. gridCheckBox = new QCheckBox(tr("&Grid"));
  215. gridCheckBox->setChecked(calendar->isGridVisible());
  216. navigationCheckBox = new QCheckBox(tr("&Navigation bar"));
  217. navigationCheckBox->setChecked(true);
  218. horizontalHeaderCombo = new QComboBox;
  219. horizontalHeaderCombo->addItem(tr("Single letter day names"),
  220. QCalendarWidget::SingleLetterDayNames);
  221. horizontalHeaderCombo->addItem(tr("Short day names"),
  222. QCalendarWidget::ShortDayNames);
  223. horizontalHeaderCombo->addItem(tr("None"),
  224. QCalendarWidget::NoHorizontalHeader);
  225. horizontalHeaderCombo->setCurrentIndex(1);
  226. horizontalHeaderLabel = new QLabel(tr("&Horizontal header:"));
  227. horizontalHeaderLabel->setBuddy(horizontalHeaderCombo);
  228. verticalHeaderCombo = new QComboBox;
  229. verticalHeaderCombo->addItem(tr("ISO week numbers"),
  230. QCalendarWidget::ISOWeekNumbers);
  231. verticalHeaderCombo->addItem(tr("None"), QCalendarWidget::NoVerticalHeader);
  232. verticalHeaderLabel = new QLabel(tr("&Vertical header:"));
  233. verticalHeaderLabel->setBuddy(verticalHeaderCombo);
  234. connect(localeCombo, SIGNAL(currentIndexChanged(int)), this,
  235. SLOT(localeChanged(int)));
  236. connect(firstDayCombo, SIGNAL(currentIndexChanged(int)), this,
  237. SLOT(firstDayChanged(int)));
  238. connect(selectionModeCombo, SIGNAL(currentIndexChanged(int)), this,
  239. SLOT(selectionModeChanged(int)));
  240. connect(gridCheckBox, SIGNAL(toggled(bool)), calendar,
  241. SLOT(setGridVisible(bool)));
  242. connect(navigationCheckBox, SIGNAL(toggled(bool)), calendar,
  243. SLOT(setNavigationBarVisible(bool)));
  244. connect(horizontalHeaderCombo, SIGNAL(currentIndexChanged(int)), this,
  245. SLOT(horizontalHeaderChanged(int)));
  246. connect(verticalHeaderCombo, SIGNAL(currentIndexChanged(int)), this,
  247. SLOT(verticalHeaderChanged(int)));
  248. QHBoxLayout* checkBoxLayout = new QHBoxLayout;
  249. checkBoxLayout->addWidget(gridCheckBox);
  250. checkBoxLayout->addStretch();
  251. checkBoxLayout->addWidget(navigationCheckBox);
  252. QGridLayout* outerLayout = new QGridLayout;
  253. outerLayout->addWidget(localeLabel, 0, 0);
  254. outerLayout->addWidget(localeCombo, 0, 1);
  255. outerLayout->addWidget(firstDayLabel, 1, 0);
  256. outerLayout->addWidget(firstDayCombo, 1, 1);
  257. outerLayout->addWidget(selectionModeLabel, 2, 0);
  258. outerLayout->addWidget(selectionModeCombo, 2, 1);
  259. outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
  260. outerLayout->addWidget(horizontalHeaderLabel, 4, 0);
  261. outerLayout->addWidget(horizontalHeaderCombo, 4, 1);
  262. outerLayout->addWidget(verticalHeaderLabel, 5, 0);
  263. outerLayout->addWidget(verticalHeaderCombo, 5, 1);
  264. generalOptionsGroupBox->setLayout(outerLayout);
  265. firstDayChanged(firstDayCombo->currentIndex());
  266. selectionModeChanged(selectionModeCombo->currentIndex());
  267. horizontalHeaderChanged(horizontalHeaderCombo->currentIndex());
  268. verticalHeaderChanged(verticalHeaderCombo->currentIndex());
  269. }
  270. void Window::createDatesGroupBox()
  271. {
  272. datesGroupBox = new QGroupBox(tr("Dates"));
  273. minimumDateEdit = new QDateEdit;
  274. minimumDateEdit->setDisplayFormat("MMM d yyyy");
  275. minimumDateEdit->setDateRange(calendar->minimumDate(),
  276. calendar->maximumDate());
  277. minimumDateEdit->setDate(calendar->minimumDate());
  278. minimumDateLabel = new QLabel(tr("&Minimum Date:"));
  279. minimumDateLabel->setBuddy(minimumDateEdit);
  280. currentDateEdit = new QDateEdit;
  281. currentDateEdit->setDisplayFormat("MMM d yyyy");
  282. currentDateEdit->setDate(calendar->selectedDate());
  283. currentDateEdit->setDateRange(calendar->minimumDate(),
  284. calendar->maximumDate());
  285. currentDateLabel = new QLabel(tr("&Current Date:"));
  286. currentDateLabel->setBuddy(currentDateEdit);
  287. maximumDateEdit = new QDateEdit;
  288. maximumDateEdit->setDisplayFormat("MMM d yyyy");
  289. maximumDateEdit->setDateRange(calendar->minimumDate(),
  290. calendar->maximumDate());
  291. maximumDateEdit->setDate(calendar->maximumDate());
  292. maximumDateLabel = new QLabel(tr("Ma&ximum Date:"));
  293. maximumDateLabel->setBuddy(maximumDateEdit);
  294. connect(currentDateEdit, SIGNAL(dateChanged(QDate)), calendar,
  295. SLOT(setSelectedDate(QDate)));
  296. connect(calendar, SIGNAL(selectionChanged()), this,
  297. SLOT(selectedDateChanged()));
  298. connect(minimumDateEdit, SIGNAL(dateChanged(QDate)), this,
  299. SLOT(minimumDateChanged(QDate)));
  300. connect(maximumDateEdit, SIGNAL(dateChanged(QDate)), this,
  301. SLOT(maximumDateChanged(QDate)));
  302. QGridLayout* dateBoxLayout = new QGridLayout;
  303. dateBoxLayout->addWidget(currentDateLabel, 1, 0);
  304. dateBoxLayout->addWidget(currentDateEdit, 1, 1);
  305. dateBoxLayout->addWidget(minimumDateLabel, 0, 0);
  306. dateBoxLayout->addWidget(minimumDateEdit, 0, 1);
  307. dateBoxLayout->addWidget(maximumDateLabel, 2, 0);
  308. dateBoxLayout->addWidget(maximumDateEdit, 2, 1);
  309. dateBoxLayout->setRowStretch(3, 1);
  310. datesGroupBox->setLayout(dateBoxLayout);
  311. }
  312. void Window::createTextFormatsGroupBox()
  313. {
  314. textFormatsGroupBox = new QGroupBox(tr("Text Formats"));
  315. weekdayColorCombo = createColorComboBox();
  316. weekdayColorCombo->setCurrentIndex(weekdayColorCombo->findText(tr("Black")));
  317. weekdayColorLabel = new QLabel(tr("&Weekday color:"));
  318. weekdayColorLabel->setBuddy(weekdayColorCombo);
  319. weekendColorCombo = createColorComboBox();
  320. weekendColorCombo->setCurrentIndex(weekendColorCombo->findText(tr("Red")));
  321. weekendColorLabel = new QLabel(tr("Week&end color:"));
  322. weekendColorLabel->setBuddy(weekendColorCombo);
  323. headerTextFormatCombo = new QComboBox;
  324. headerTextFormatCombo->addItem(tr("Bold"));
  325. headerTextFormatCombo->addItem(tr("Italic"));
  326. headerTextFormatCombo->addItem(tr("Plain"));
  327. headerTextFormatLabel = new QLabel(tr("&Header text:"));
  328. headerTextFormatLabel->setBuddy(headerTextFormatCombo);
  329. firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue"));
  330. mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
  331. connect(weekdayColorCombo, SIGNAL(currentIndexChanged(int)), this,
  332. SLOT(weekdayFormatChanged()));
  333. connect(weekendColorCombo, SIGNAL(currentIndexChanged(int)), this,
  334. SLOT(weekendFormatChanged()));
  335. connect(headerTextFormatCombo, SIGNAL(currentIndexChanged(QString)), this,
  336. SLOT(reformatHeaders()));
  337. connect(firstFridayCheckBox, SIGNAL(toggled(bool)), this,
  338. SLOT(reformatCalendarPage()));
  339. connect(mayFirstCheckBox, SIGNAL(toggled(bool)), this,
  340. SLOT(reformatCalendarPage()));
  341. QHBoxLayout* checkBoxLayout = new QHBoxLayout;
  342. checkBoxLayout->addWidget(firstFridayCheckBox);
  343. checkBoxLayout->addStretch();
  344. checkBoxLayout->addWidget(mayFirstCheckBox);
  345. QGridLayout* outerLayout = new QGridLayout;
  346. outerLayout->addWidget(weekdayColorLabel, 0, 0);
  347. outerLayout->addWidget(weekdayColorCombo, 0, 1);
  348. outerLayout->addWidget(weekendColorLabel, 1, 0);
  349. outerLayout->addWidget(weekendColorCombo, 1, 1);
  350. outerLayout->addWidget(headerTextFormatLabel, 2, 0);
  351. outerLayout->addWidget(headerTextFormatCombo, 2, 1);
  352. outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
  353. textFormatsGroupBox->setLayout(outerLayout);
  354. weekdayFormatChanged();
  355. weekendFormatChanged();
  356. reformatHeaders();
  357. reformatCalendarPage();
  358. }
  359. QComboBox* Window::createColorComboBox()
  360. {
  361. QComboBox* comboBox = new QComboBox;
  362. comboBox->addItem(tr("Red"), QColor(Qt::red));
  363. comboBox->addItem(tr("Blue"), QColor(Qt::blue));
  364. comboBox->addItem(tr("Black"), QColor(Qt::black));
  365. comboBox->addItem(tr("Magenta"), QColor(Qt::magenta));
  366. return comboBox;
  367. }
  368. //#include "moc_calwidget.cpp"