1
0

calwidget.cpp 16 KB

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