calwidget.cpp 17 KB

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