1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-2012 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "calendar.h"
21 
22 #include "config.h"
23 
24 #if (QT_VERSION >= QT_VERSION_CHECK(4, 8, 0))
25 #include <QLocale>
26 #elif defined(__GLIBC__)
27 #include <langinfo.h>
28 #endif
29 
30 #include <QPainter>
31 #include <QTextCharFormat>
32 
33 
34 using namespace LicqQtGui;
35 /* TRANSLATOR LicqQtGui::Calendar */
36 
Calendar(QWidget * parent)37 Calendar::Calendar(QWidget* parent)
38     : QCalendarWidget(parent)
39 {
40 #if (QT_VERSION >= QT_VERSION_CHECK(4, 8, 0))
41   setFirstDayOfWeek(QLocale::system().firstDayOfWeek());
42 #else
43 
44 #ifdef __GLIBC__
45   // Non-standard locale parameter available in gnu libc only
46   int firstday = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY);
47   if (firstday > 0)
48   {
49     // locale data uses: 1=Sunday, 2=Monday..., 7=Saturday
50     // Qt data uses: 1=Monday, 2=Tuesday..., 7=Sunday
51     firstday -= 1;
52     if (firstday == 0)
53       firstday = Qt::Sunday;
54     setFirstDayOfWeek(static_cast<Qt::DayOfWeek>(firstday));
55   }
56   else
57 #endif
58     setFirstDayOfWeek(Qt::Monday);
59 
60 #endif
61 }
62 
markDate(const QDate & date)63 void Calendar::markDate(const QDate& date)
64 {
65   QTextCharFormat textFormat = dateTextFormat(date);
66   // Mark dates with bold
67   textFormat.setFontWeight(QFont::Bold);
68   // Background must be transparent to not overwrite the elipse
69   textFormat.setBackground(Qt::transparent);
70   setDateTextFormat(date, textFormat);
71 }
72 
addMatch(const QDate & date)73 void Calendar::addMatch(const QDate& date)
74 {
75   if (myMatches.contains(date))
76     return;
77 
78   myMatches.append(date);
79 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
80   updateCell(date);
81 #else
82   update();
83 #endif
84 }
85 
clearMatches()86 void Calendar::clearMatches()
87 {
88   myMatches.clear();
89 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
90   updateCells();
91 #else
92   update();
93 #endif
94 }
95 
paintCell(QPainter * painter,const QRect & rect,const QDate & date) const96 void Calendar::paintCell(QPainter* painter, const QRect& rect, const QDate& date) const
97 {
98   QTextCharFormat format = dateTextFormat(date);
99   if (format.fontWeight() == QFont::Bold)
100   {
101     painter->save();
102     const int adjust = 1;
103     QRect center = rect.adjusted(adjust, adjust, -adjust, -adjust);
104     painter->setPen(Qt::NoPen);
105     painter->setRenderHints(painter->renderHints() | QPainter::Antialiasing);
106     painter->setBrush(myMatches.contains(date) ? Qt::green : Qt::yellow);
107     painter->drawEllipse(center);
108     painter->restore();
109   }
110 
111   QCalendarWidget::paintCell(painter, rect, date);
112 }
113