calwidget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #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(Qt::DayOfWeek(
  78. 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)),
  167. this, 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 && this->locale().country() == country)
  188. curLocaleIndex = index;
  189. localeCombo->addItem(label, locale);
  190. ++index;
  191. }
  192. }
  193. if (curLocaleIndex != -1)
  194. localeCombo->setCurrentIndex(curLocaleIndex);
  195. localeLabel = new QLabel(tr("&Locale"));
  196. localeLabel->setBuddy(localeCombo);
  197. firstDayCombo = new QComboBox;
  198. firstDayCombo->addItem(tr("Sunday"), Qt::Sunday);
  199. firstDayCombo->addItem(tr("Monday"), Qt::Monday);
  200. firstDayCombo->addItem(tr("Tuesday"), Qt::Tuesday);
  201. firstDayCombo->addItem(tr("Wednesday"), Qt::Wednesday);
  202. firstDayCombo->addItem(tr("Thursday"), Qt::Thursday);
  203. firstDayCombo->addItem(tr("Friday"), Qt::Friday);
  204. firstDayCombo->addItem(tr("Saturday"), Qt::Saturday);
  205. firstDayLabel = new QLabel(tr("Wee&k starts on:"));
  206. firstDayLabel->setBuddy(firstDayCombo);
  207. selectionModeCombo = new QComboBox;
  208. selectionModeCombo->addItem(tr("Single selection"),
  209. QCalendarWidget::SingleSelection);
  210. selectionModeCombo->addItem(tr("None"), QCalendarWidget::NoSelection);
  211. selectionModeLabel = new QLabel(tr("&Selection mode:"));
  212. selectionModeLabel->setBuddy(selectionModeCombo);
  213. gridCheckBox = new QCheckBox(tr("&Grid"));
  214. gridCheckBox->setChecked(calendar->isGridVisible());
  215. navigationCheckBox = new QCheckBox(tr("&Navigation bar"));
  216. navigationCheckBox->setChecked(true);
  217. horizontalHeaderCombo = new QComboBox;
  218. horizontalHeaderCombo->addItem(tr("Single letter day names"),
  219. QCalendarWidget::SingleLetterDayNames);
  220. horizontalHeaderCombo->addItem(tr("Short day names"),
  221. QCalendarWidget::ShortDayNames);
  222. horizontalHeaderCombo->addItem(tr("None"),
  223. QCalendarWidget::NoHorizontalHeader);
  224. horizontalHeaderCombo->setCurrentIndex(1);
  225. horizontalHeaderLabel = new QLabel(tr("&Horizontal header:"));
  226. horizontalHeaderLabel->setBuddy(horizontalHeaderCombo);
  227. verticalHeaderCombo = new QComboBox;
  228. verticalHeaderCombo->addItem(tr("ISO week numbers"),
  229. QCalendarWidget::ISOWeekNumbers);
  230. verticalHeaderCombo->addItem(tr("None"), QCalendarWidget::NoVerticalHeader);
  231. verticalHeaderLabel = new QLabel(tr("&Vertical header:"));
  232. verticalHeaderLabel->setBuddy(verticalHeaderCombo);
  233. connect(localeCombo, SIGNAL(currentIndexChanged(int)),
  234. this, SLOT(localeChanged(int)));
  235. connect(firstDayCombo, SIGNAL(currentIndexChanged(int)),
  236. this, SLOT(firstDayChanged(int)));
  237. connect(selectionModeCombo, SIGNAL(currentIndexChanged(int)),
  238. this, SLOT(selectionModeChanged(int)));
  239. connect(gridCheckBox, SIGNAL(toggled(bool)),
  240. calendar, SLOT(setGridVisible(bool)));
  241. connect(navigationCheckBox, SIGNAL(toggled(bool)),
  242. calendar, SLOT(setNavigationBarVisible(bool)));
  243. connect(horizontalHeaderCombo, SIGNAL(currentIndexChanged(int)),
  244. this, SLOT(horizontalHeaderChanged(int)));
  245. connect(verticalHeaderCombo, SIGNAL(currentIndexChanged(int)),
  246. this, SLOT(verticalHeaderChanged(int)));
  247. QHBoxLayout *checkBoxLayout = new QHBoxLayout;
  248. checkBoxLayout->addWidget(gridCheckBox);
  249. checkBoxLayout->addStretch();
  250. checkBoxLayout->addWidget(navigationCheckBox);
  251. QGridLayout *outerLayout = new QGridLayout;
  252. outerLayout->addWidget(localeLabel, 0, 0);
  253. outerLayout->addWidget(localeCombo, 0, 1);
  254. outerLayout->addWidget(firstDayLabel, 1, 0);
  255. outerLayout->addWidget(firstDayCombo, 1, 1);
  256. outerLayout->addWidget(selectionModeLabel, 2, 0);
  257. outerLayout->addWidget(selectionModeCombo, 2, 1);
  258. outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
  259. outerLayout->addWidget(horizontalHeaderLabel, 4, 0);
  260. outerLayout->addWidget(horizontalHeaderCombo, 4, 1);
  261. outerLayout->addWidget(verticalHeaderLabel, 5, 0);
  262. outerLayout->addWidget(verticalHeaderCombo, 5, 1);
  263. generalOptionsGroupBox->setLayout(outerLayout);
  264. firstDayChanged(firstDayCombo->currentIndex());
  265. selectionModeChanged(selectionModeCombo->currentIndex());
  266. horizontalHeaderChanged(horizontalHeaderCombo->currentIndex());
  267. verticalHeaderChanged(verticalHeaderCombo->currentIndex());
  268. }
  269. void Window::createDatesGroupBox()
  270. {
  271. datesGroupBox = new QGroupBox(tr("Dates"));
  272. minimumDateEdit = new QDateEdit;
  273. minimumDateEdit->setDisplayFormat("MMM d yyyy");
  274. minimumDateEdit->setDateRange(calendar->minimumDate(),
  275. calendar->maximumDate());
  276. minimumDateEdit->setDate(calendar->minimumDate());
  277. minimumDateLabel = new QLabel(tr("&Minimum Date:"));
  278. minimumDateLabel->setBuddy(minimumDateEdit);
  279. currentDateEdit = new QDateEdit;
  280. currentDateEdit->setDisplayFormat("MMM d yyyy");
  281. currentDateEdit->setDate(calendar->selectedDate());
  282. currentDateEdit->setDateRange(calendar->minimumDate(),
  283. calendar->maximumDate());
  284. currentDateLabel = new QLabel(tr("&Current Date:"));
  285. currentDateLabel->setBuddy(currentDateEdit);
  286. maximumDateEdit = new QDateEdit;
  287. maximumDateEdit->setDisplayFormat("MMM d yyyy");
  288. maximumDateEdit->setDateRange(calendar->minimumDate(),
  289. calendar->maximumDate());
  290. maximumDateEdit->setDate(calendar->maximumDate());
  291. maximumDateLabel = new QLabel(tr("Ma&ximum Date:"));
  292. maximumDateLabel->setBuddy(maximumDateEdit);
  293. connect(currentDateEdit, SIGNAL(dateChanged(QDate)),
  294. calendar, SLOT(setSelectedDate(QDate)));
  295. connect(calendar, SIGNAL(selectionChanged()),
  296. this, SLOT(selectedDateChanged()));
  297. connect(minimumDateEdit, SIGNAL(dateChanged(QDate)),
  298. this, SLOT(minimumDateChanged(QDate)));
  299. connect(maximumDateEdit, SIGNAL(dateChanged(QDate)),
  300. this, SLOT(maximumDateChanged(QDate)));
  301. QGridLayout *dateBoxLayout = new QGridLayout;
  302. dateBoxLayout->addWidget(currentDateLabel, 1, 0);
  303. dateBoxLayout->addWidget(currentDateEdit, 1, 1);
  304. dateBoxLayout->addWidget(minimumDateLabel, 0, 0);
  305. dateBoxLayout->addWidget(minimumDateEdit, 0, 1);
  306. dateBoxLayout->addWidget(maximumDateLabel, 2, 0);
  307. dateBoxLayout->addWidget(maximumDateEdit, 2, 1);
  308. dateBoxLayout->setRowStretch(3, 1);
  309. datesGroupBox->setLayout(dateBoxLayout);
  310. }
  311. void Window::createTextFormatsGroupBox()
  312. {
  313. textFormatsGroupBox = new QGroupBox(tr("Text Formats"));
  314. weekdayColorCombo = createColorComboBox();
  315. weekdayColorCombo->setCurrentIndex(
  316. weekdayColorCombo->findText(tr("Black")));
  317. weekdayColorLabel = new QLabel(tr("&Weekday color:"));
  318. weekdayColorLabel->setBuddy(weekdayColorCombo);
  319. weekendColorCombo = createColorComboBox();
  320. weekendColorCombo->setCurrentIndex(
  321. weekendColorCombo->findText(tr("Red")));
  322. weekendColorLabel = new QLabel(tr("Week&end color:"));
  323. weekendColorLabel->setBuddy(weekendColorCombo);
  324. headerTextFormatCombo = new QComboBox;
  325. headerTextFormatCombo->addItem(tr("Bold"));
  326. headerTextFormatCombo->addItem(tr("Italic"));
  327. headerTextFormatCombo->addItem(tr("Plain"));
  328. headerTextFormatLabel = new QLabel(tr("&Header text:"));
  329. headerTextFormatLabel->setBuddy(headerTextFormatCombo);
  330. firstFridayCheckBox = new QCheckBox(tr("&First Friday in blue"));
  331. mayFirstCheckBox = new QCheckBox(tr("May &1 in red"));
  332. connect(weekdayColorCombo, SIGNAL(currentIndexChanged(int)),
  333. this, SLOT(weekdayFormatChanged()));
  334. connect(weekendColorCombo, SIGNAL(currentIndexChanged(int)),
  335. this, SLOT(weekendFormatChanged()));
  336. connect(headerTextFormatCombo, SIGNAL(currentIndexChanged(QString)),
  337. this, SLOT(reformatHeaders()));
  338. connect(firstFridayCheckBox, SIGNAL(toggled(bool)),
  339. this, SLOT(reformatCalendarPage()));
  340. connect(mayFirstCheckBox, SIGNAL(toggled(bool)),
  341. this, SLOT(reformatCalendarPage()));
  342. QHBoxLayout *checkBoxLayout = new QHBoxLayout;
  343. checkBoxLayout->addWidget(firstFridayCheckBox);
  344. checkBoxLayout->addStretch();
  345. checkBoxLayout->addWidget(mayFirstCheckBox);
  346. QGridLayout *outerLayout = new QGridLayout;
  347. outerLayout->addWidget(weekdayColorLabel, 0, 0);
  348. outerLayout->addWidget(weekdayColorCombo, 0, 1);
  349. outerLayout->addWidget(weekendColorLabel, 1, 0);
  350. outerLayout->addWidget(weekendColorCombo, 1, 1);
  351. outerLayout->addWidget(headerTextFormatLabel, 2, 0);
  352. outerLayout->addWidget(headerTextFormatCombo, 2, 1);
  353. outerLayout->addLayout(checkBoxLayout, 3, 0, 1, 2);
  354. textFormatsGroupBox->setLayout(outerLayout);
  355. weekdayFormatChanged();
  356. weekendFormatChanged();
  357. reformatHeaders();
  358. reformatCalendarPage();
  359. }
  360. QComboBox *Window::createColorComboBox()
  361. {
  362. QComboBox *comboBox = new QComboBox;
  363. comboBox->addItem(tr("Red"), QColor(Qt::red));
  364. comboBox->addItem(tr("Blue"), QColor(Qt::blue));
  365. comboBox->addItem(tr("Black"), QColor(Qt::black));
  366. comboBox->addItem(tr("Magenta"), QColor(Qt::magenta));
  367. return comboBox;
  368. }
  369. //#include "moc_calwidget.cpp"