1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*
* Copyright 2008,2010 Davide Bettio <davide.bettio@kdemail.net>
* Copyright 2009 John Layt <john@layt.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "calendar.h"
//Qt
#include <QtCore/QDate>
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QGraphicsSceneWheelEvent>
#include <QtGui/QGraphicsGridLayout>
#include <QtGui/QGraphicsLinearLayout>
#include <QtGui/QGraphicsProxyWidget>
#include <QtGui/QGraphicsView>
#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QSpinBox>
#include <QtGui/QToolButton>
#include <QtGui/QDesktopWidget>
//KDECore
#include <KCalendarSystem>
#include <KDebug>
#include <KGlobal>
#include <KIcon>
#include <KLineEdit>
#include <KLocale>
#include <KTextBrowser>
#include <KIntSpinBox>
#include <KConfigDialog>
#include <KConfigGroup>
//Plasma
#include <Plasma/Label>
#include <Plasma/LineEdit>
#include <Plasma/Separator>
#include <Plasma/SpinBox>
#include <Plasma/TextBrowser>
#include <Plasma/ToolButton>
#include <Plasma/DataEngine>
#include "wheelytoolbutton.h"
namespace Plasma
{
static const int s_yearWidgetIndex = 3;
class CalendarPrivate
{
public:
CalendarPrivate(Calendar *calendar)
: q(calendar),
back(0),
spacer0(0),
month(0),
yearSpinBox(0),
year(0),
spacer1(0),
forward(0),
calendarTable(0),
dateText(0),
eventsDisplay(0),
jumpToday(0),
monthMenu(0),
weekSpinBox(0),
separator(0)
{
}
void init(const QDate &date = QDate());
void refreshWidgets();
bool addDateDetailsToDisplay(QString &html, const QDate &date, bool showNoEventsMsg = false);
void popupMonthsMenu();
void displayEvents(const QDate &date = QDate());
void updateSize();
Calendar *q;
ToolButton *back;
Plasma::Label *spacer0;
Plasma::ToolButton *month;
Plasma::SpinBox *yearSpinBox;
Plasma::ToolButton *year;
Plasma::Label *spacer1;
Plasma::ToolButton *forward;
Plasma::CalendarTable *calendarTable;
Plasma::LineEdit *dateText;
Plasma::TextBrowser *eventsDisplay;
ToolButton *jumpToday;
QMenu *monthMenu;
Plasma::SpinBox *weekSpinBox;
Plasma::Separator *separator;
QGraphicsLinearLayout *layout;
};
Calendar::Calendar(const QDate &date, QGraphicsWidget *parent)
: QGraphicsWidget(parent),
d(new CalendarPrivate(this))
{
d->init(date);
}
Calendar::Calendar(QGraphicsWidget *parent)
: QGraphicsWidget(parent),
d(new CalendarPrivate(this))
{
d->init();
}
Calendar::~Calendar()
{
delete d->monthMenu;
delete d;
}
void CalendarPrivate::init(const QDate &initialDate)
{
q->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
layout = new QGraphicsLinearLayout(Qt::Horizontal, q);
QGraphicsLinearLayout *calendarLayout = new QGraphicsLinearLayout(Qt::Vertical, layout);
QGraphicsLinearLayout *hLayout = new QGraphicsLinearLayout(layout);
QGraphicsLinearLayout *layoutTools = new QGraphicsLinearLayout(layout);
calendarTable = new CalendarTable(q);
calendarTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QObject::connect(calendarTable, SIGNAL(dateChanged(QDate)), q, SLOT(dateUpdated()));
QObject::connect(calendarTable, SIGNAL(dateHovered(QDate)), q, SIGNAL(dateHovered(QDate)));
QObject::connect(calendarTable, SIGNAL(dateSelected(QDate)), q, SLOT(displayEvents(QDate)));
QObject::connect(calendarTable, SIGNAL(eventsChanged()), q, SLOT(displayEvents()));
QObject::connect(q, SIGNAL(dateHovered(QDate)), q, SLOT(displayEvents(QDate)));
back = new Plasma::ToolButton(q);
back->setText("<");
back->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QObject::connect(back, SIGNAL(clicked()), q, SLOT(prevMonth()));
hLayout->addItem(back);
hLayout->addStretch();
month = new WheelyToolButton(q);
month->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
monthMenu = new QMenu();
QObject::connect(month, SIGNAL(clicked()), q, SLOT(popupMonthsMenu()));
QObject::connect(month, SIGNAL(pressed()), q, SLOT(popupMonthsMenu()));
QObject::connect(month, SIGNAL(wheelUp()), q, SLOT(prevMonth()));
QObject::connect(month, SIGNAL(wheelDown()), q, SLOT(nextMonth()));
hLayout->addItem(month);
year = new WheelyToolButton(q);
year->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QObject::connect(year, SIGNAL(wheelUp()), q, SLOT(prevYear()));
QObject::connect(year, SIGNAL(wheelDown()), q, SLOT(nextYear()));
QObject::connect(year, SIGNAL(clicked()), q, SLOT(showYearSpinBox()));
hLayout->addItem(year);
yearSpinBox = new Plasma::SpinBox(q);
yearSpinBox->setRange(calendarTable->calendar()->year(calendarTable->calendar()->earliestValidDate()),
calendarTable->calendar()->year(calendarTable->calendar()->latestValidDate()));
yearSpinBox->hide();
QObject::connect(yearSpinBox->nativeWidget(), SIGNAL(editingFinished()), q, SLOT(hideYearSpinBox()));
hLayout->addStretch();
forward = new Plasma::ToolButton(q);
forward->setText(">");
forward->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QObject::connect(forward, SIGNAL(clicked()), q, SLOT(nextMonth()));
hLayout->addItem(forward);
jumpToday = new Plasma::ToolButton(q);
jumpToday->nativeWidget()->setIcon(KIcon("go-jump-today"));
jumpToday->nativeWidget()->setToolTip(i18n("Select today"));
jumpToday->nativeWidget()->setMinimumWidth(25);
QObject::connect(jumpToday, SIGNAL(clicked()), q, SLOT(goToToday()));
layoutTools->addItem(jumpToday);
layoutTools->addStretch();
dateText = new Plasma::LineEdit(q);
QObject::connect(dateText->nativeWidget(), SIGNAL(returnPressed()), q, SLOT(manualDateChange()));
layoutTools->addItem(dateText);
layoutTools->addStretch();
weekSpinBox = new Plasma::SpinBox(q);
weekSpinBox->setMinimum(1);
QObject::connect(weekSpinBox, SIGNAL(valueChanged(int)), q, SLOT(goToWeek(int)));
layoutTools->addItem(weekSpinBox);
calendarLayout->addItem(hLayout);
calendarLayout->addItem(calendarTable);
calendarLayout->addItem(layoutTools);
layout->addItem(calendarLayout);
q->setDate(initialDate);
displayEvents();
updateSize();
}
void Calendar::focusInEvent(QFocusEvent* event)
{
Q_UNUSED(event);
grabKeyboard();
}
void Calendar::focusOutEvent(QFocusEvent* event)
{
Q_UNUSED(event);
ungrabKeyboard();
}
void Calendar::keyPressEvent(QKeyEvent* event)
{
switch(event->key()) {
case Qt::Key_Right :
setDate(date().addDays(1));
break;
case Qt::Key_Left :
setDate(date().addDays(-1));
break;
case Qt::Key_Up :
setDate(date().addDays(-7));
break;
case Qt::Key_Down :
setDate(date().addDays(7));
break;
case Qt::Key_PageUp:
nextMonth();
break;
case Qt::Key_PageDown:
prevMonth();
break;
case Qt::Key_Home:
goToToday();
break;
default:
break;
}
}
CalendarTable *Calendar::calendarTable() const
{
return d->calendarTable;
}
void Calendar::setCalendar(int newCalendarType)
{
calendarTable()->setCalendar(newCalendarType);
d->refreshWidgets();
}
void Calendar::setCalendar(const KCalendarSystem *newCalendar)
{
calendarTable()->setCalendar(newCalendar);
d->refreshWidgets();
}
const KCalendarSystem *Calendar::calendar() const
{
return calendarTable()->calendar();
}
void Calendar::setDate(const QDate &toDate)
{
d->calendarTable->setDate(toDate);
//If set date failed force refresh of nav widgets to reset any user entry
//If set date successful refresh will be triggered through signal/slot
if (d->calendarTable->date() != toDate) {
d->refreshWidgets();
}
}
const QDate& Calendar::date() const
{
return calendarTable()->date();
}
void Calendar::clearHolidaysRegions()
{
calendarTable()->clearHolidaysRegions();
}
void Calendar::addHolidaysRegion(const QString ®ion, bool daysOff)
{
calendarTable()->addHolidaysRegion(region, daysOff);
}
QStringList Calendar::holidaysRegions() const
{
return calendarTable()->holidaysRegions();
}
bool Calendar::isDisplayingDateDetails() const
{
return calendarTable()->displayHolidays() || calendarTable()->displayEvents();
}
bool Calendar::dateHasDetails(const QDate &date) const
{
return calendarTable()->dateHasDetails(date);
}
QStringList Calendar::dateDetails(const QDate &date) const
{
return calendarTable()->dateDetails(date);
}
void Calendar::setAutomaticUpdateEnabled(bool automatic)
{
calendarTable()->setAutomaticUpdateEnabled(automatic);
}
bool Calendar::isAutomaticUpdateEnabled() const
{
return calendarTable()->isAutomaticUpdateEnabled();
}
void Calendar::setCurrentDate(const QDate &date)
{
calendarTable()->setCurrentDate(date);
}
const QDate& Calendar::currentDate() const
{
return calendarTable()->currentDate();
}
void Calendar::writeConfiguration(KConfigGroup cg)
{
calendarTable()->writeConfiguration(cg);
}
void Calendar::createConfigurationInterface(KConfigDialog *parent)
{
calendarTable()->createConfigurationInterface(parent);
}
void Calendar::configAccepted(KConfigGroup cg)
{
calendarTable()->configAccepted(cg);
applyConfiguration(cg);
}
void Calendar::resizeEvent(QGraphicsSceneResizeEvent * event)
{
// just forward the event. Not removed due to BC.
QGraphicsWidget::resizeEvent(event);
}
void CalendarPrivate::updateSize()
{
QSize minSize = QSize(200, 200);
QSize prefSize = calendarTable ? calendarTable->size().toSize() : QSize(250, 250);
if (q->isDisplayingDateDetails()) {
// our seperator's width is the vertical line + space for the vertical scrollbar + spacing
const int sepWidth = (separator ? separator->size().width() : 6 ) + 24;
prefSize.setWidth(prefSize.width() * 2 + sepWidth * 2);
minSize.setWidth(minSize.width() * 2); // should be enough even for the separator
}
q->setMinimumSize(minSize);
q->setPreferredSize(prefSize);
}
void Calendar::applyConfiguration(KConfigGroup cg)
{
calendarTable()->applyConfiguration(cg);
d->updateSize();
d->displayEvents();
}
void Calendar::manualDateChange()
{
setDate(calendar()->readDate(((QLineEdit*)sender())->text()));
}
void Calendar::goToToday()
{
setDate(QDate::currentDate());
}
void Calendar::dateUpdated()
{
// Ignore the date passed in, only ever show the date to match the CalendarTable
d->refreshWidgets();
emit dateChanged(date());
d->displayEvents();
}
void CalendarPrivate::displayEvents(const QDate &date)
{
if (!q->isDisplayingDateDetails()) {
if (eventsDisplay) {
kDebug() << "deleting events display!";
delete eventsDisplay;
eventsDisplay = 0;
delete separator;
separator = 0;
}
return;
} else if (!eventsDisplay) {
separator = new Plasma::Separator(q);
separator->setOrientation(Qt::Vertical);
layout->addItem(separator);
eventsDisplay = new Plasma::TextBrowser(q);
layout->addItem(eventsDisplay);
}
QString html;
if (!addDateDetailsToDisplay(html, date, date.isValid())) {
QDate dt = calendarTable->date();
QDate end = calendarTable->endDate();
if (dt.isValid() && end.isValid()) {
while (dt <= end) {
addDateDetailsToDisplay(html, dt);
dt = dt.addDays(1);
}
}
}
if (html.isEmpty()) {
html = "<div align=\"center\">";
html += i18nc("No events on the calendar starting from today",
"No upcoming events.");
html += "</div>";
}
eventsDisplay->setText(html);
}
bool CalendarPrivate::addDateDetailsToDisplay(QString &html, const QDate &date, bool showNoEventsMsg)
{
const bool hasEvents = calendarTable->dateHasDetails(date);
if (!hasEvents && !showNoEventsMsg) {
return false;
}
html += "<b>" + calendarTable->calendar()->formatDate(date, KLocale::LongDate) + "</b>";
html += "<ul style=\"-qt-list-indent: 0;\">";
if (hasEvents) {
const QStringList details = calendarTable->dateDetails(date);
foreach (const QString &detail, details) {
html += "<li style='margin-left: 0.5em;'>" + detail + "</li>";
}
} else {
html += "<li style='type: none; margin-left: 0.5em;'><i>";
html += i18nc("No events on the calendar", "No events for this date.");
html += "</i></li>";
}
html += "</ul>";
return hasEvents;
}
// Update the nav widgets to show the current date in the CalendarTable
void CalendarPrivate::refreshWidgets()
{
const KCalendarSystem *calendar = calendarTable->calendar();
const QDate date = calendarTable->date();
month->setText(calendar->monthName(calendar->month(date), calendar->year(date)));
month->setMinimumSize(static_cast<QToolButton*>(month->widget())->sizeHint());
year->setText(calendar->formatDate(date, KLocale::Year, KLocale::LongNumber));
dateText->setText(calendar->formatDate(date, KLocale::ShortDate));
// Block the signals to prevent changing the date again
yearSpinBox->blockSignals(true);
yearSpinBox->setRange(calendar->year(calendar->earliestValidDate()),
calendar->year(calendar->latestValidDate()));
yearSpinBox->setValue(calendar->year(date));
yearSpinBox->blockSignals(false);
// Block the signals to prevent changing the date again
weekSpinBox->blockSignals(true);
weekSpinBox->setMaximum(calendar->weeksInYear(date));
weekSpinBox->setValue(calendar->week(date, KLocale::IsoWeekNumber));
weekSpinBox->blockSignals(false);
}
void Calendar::prevMonth()
{
setDate(calendar()->addMonths(date(), -1));
}
void Calendar::nextMonth()
{
setDate(calendar()->addMonths(date(), 1));
}
void Calendar::prevYear()
{
setDate(calendar()->addYears(date(), -1));
}
void Calendar::nextYear()
{
setDate(calendar()->addYears(date(), 1));
}
void CalendarPrivate::popupMonthsMenu()
{
monthMenu->clear();
const KCalendarSystem *calendar = calendarTable->calendar();
const QDate date = calendarTable->date();
const int year = calendar->year(date);
const int monthsInYear = calendar->monthsInYear(date);
for (int i = 1; i <= monthsInYear; i++){
QAction *tmpAction = new QAction(calendar->monthName(i, year), monthMenu);
tmpAction->setProperty("month", i);
QObject::connect(tmpAction, SIGNAL(triggered()), q, SLOT(monthTriggered()));
monthMenu->addAction(tmpAction);
}
QGraphicsView *view = Plasma::viewFor(month);
if (view) {
monthMenu->adjustSize();
const int x = month->sceneBoundingRect().center().x() - monthMenu->width() / 2;
QPoint pos(x, month->sceneBoundingRect().bottom());
pos = view->mapToGlobal(view->mapFromScene(pos));
QRect r = qApp->desktop()->screenGeometry(view->geometry().center());
if (pos.y() + monthMenu->height() > r.bottom()) {
pos = QPoint(x, month->sceneBoundingRect().top() - monthMenu->height());
pos = view->mapToGlobal(view->mapFromScene(pos));
}
monthMenu->popup(pos);
} else {
monthMenu->popup(QCursor::pos());
}
month->nativeWidget()->setDown(false);
}
void Calendar::monthTriggered()
{
QAction *action = dynamic_cast<QAction*> (sender());
if (action && action->property("month").type() == QVariant::Int) {
int newMonth = action->property("month").toInt();
int currMonth = calendar()->month(date());
setDate(calendar()->addMonths(date(), newMonth - currMonth));
}
}
void Calendar::goToWeek(int newWeek)
{
int currWeek = calendar()->week(date(), KLocale::IsoWeekNumber);
int daysInWeek = calendar()->daysInWeek(date());
setDate(calendar()->addDays(date(), (newWeek - currWeek) * daysInWeek));
}
void Calendar::showYearSpinBox()
{
QGraphicsLinearLayout *hLayout = (QGraphicsLinearLayout*)d->year->parentLayoutItem();
if (!hLayout) {
// already hidden!
return;
}
d->year->hide();
hLayout->removeItem(d->year);
d->yearSpinBox->setValue(calendar()->year(date()));
d->yearSpinBox->setMinimumWidth(d->yearSpinBox->preferredSize().width());
hLayout->insertItem(s_yearWidgetIndex, d->yearSpinBox);
hLayout->activate();
d->yearSpinBox->show();
d->yearSpinBox->nativeWidget()->setFocus(Qt::MouseFocusReason);
}
void Calendar::hideYearSpinBox()
{
QGraphicsLinearLayout *hLayout = (QGraphicsLinearLayout*)d->yearSpinBox->parentLayoutItem();
if (!hLayout) {
// already hidden!
return;
}
hLayout->removeItem(d->yearSpinBox);
hLayout->insertItem(s_yearWidgetIndex, d->year);
d->yearSpinBox->hide();
int newYear = d->yearSpinBox->value();
int currYear = calendar()->year(date());
setDate(calendar()->addYears(date(), newYear - currYear));
d->year->show();
}
}
#include "calendar.moc"
|