File indexing completed on 2024-06-09 05:15:59
0001 /* 0002 This file is part of KOrganizer. 0003 0004 SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0 0007 */ 0008 0009 #include "navigatorbar.h" 0010 #include "koglobals.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include <QHBoxLayout> 0015 #include <QLocale> 0016 #include <QMenu> 0017 #include <QToolButton> 0018 0019 NavigatorBar::NavigatorBar(QWidget *parent) 0020 : QWidget(parent) 0021 , mMonth(new QToolButton(this)) 0022 , mYear(new QToolButton(this)) 0023 { 0024 QFont tfont = font(); 0025 tfont.setPointSize(10); 0026 tfont.setBold(false); 0027 0028 const bool isRTL = KOGlobals::self()->reverseLayout(); 0029 0030 mPrevYear = createNavigationButton(isRTL ? QStringLiteral("arrow-right-double") : QStringLiteral("arrow-left-double"), 0031 i18n("Scroll backward to the previous year"), 0032 i18n("Click this button to scroll the display to the " 0033 "same approximate day of the previous year")); 0034 0035 mPrevMonth = createNavigationButton(isRTL ? QStringLiteral("arrow-right") : QStringLiteral("arrow-left"), 0036 i18n("Scroll backward to the previous month"), 0037 i18n("Click this button to scroll the display to the " 0038 "same approximate date of the previous month")); 0039 0040 mNextMonth = createNavigationButton(isRTL ? QStringLiteral("arrow-left") : QStringLiteral("arrow-right"), 0041 i18n("Scroll forward to the next month"), 0042 i18n("Click this button to scroll the display to the " 0043 "same approximate date of the next month")); 0044 0045 mNextYear = createNavigationButton(isRTL ? QStringLiteral("arrow-left-double") : QStringLiteral("arrow-right-double"), 0046 i18n("Scroll forward to the next year"), 0047 i18n("Click this button to scroll the display to the " 0048 "same approximate day of the next year")); 0049 0050 // Create month name button 0051 mMonth->setPopupMode(QToolButton::InstantPopup); 0052 mMonth->setAutoRaise(true); 0053 mMonth->setFont(tfont); 0054 mMonth->setToolTip(i18n("Select a month")); 0055 0056 // Create year button 0057 mYear->setPopupMode(QToolButton::InstantPopup); 0058 mYear->setAutoRaise(true); 0059 mYear->setFont(tfont); 0060 mYear->setToolTip(i18n("Select a year")); 0061 0062 // set up control frame layout 0063 auto ctrlLayout = new QHBoxLayout(this); 0064 ctrlLayout->setContentsMargins({}); 0065 ctrlLayout->addWidget(mPrevYear); 0066 ctrlLayout->addWidget(mPrevMonth); 0067 ctrlLayout->addStretch(); 0068 ctrlLayout->addWidget(mMonth); 0069 ctrlLayout->addWidget(mYear); 0070 ctrlLayout->addStretch(); 0071 ctrlLayout->addWidget(mNextMonth); 0072 ctrlLayout->addWidget(mNextYear); 0073 0074 connect(mPrevYear, &QToolButton::clicked, this, &NavigatorBar::prevYearClicked); 0075 connect(mPrevMonth, &QToolButton::clicked, this, &NavigatorBar::prevMonthClicked); 0076 connect(mNextMonth, &QToolButton::clicked, this, &NavigatorBar::nextMonthClicked); 0077 connect(mNextYear, &QToolButton::clicked, this, &NavigatorBar::nextYearClicked); 0078 connect(mMonth, &QToolButton::clicked, this, &NavigatorBar::selectMonthFromMenu); 0079 connect(mYear, &QToolButton::clicked, this, &NavigatorBar::selectYearFromMenu); 0080 } 0081 0082 NavigatorBar::~NavigatorBar() = default; 0083 0084 void NavigatorBar::showButtons(bool left, bool right) 0085 { 0086 if (left) { 0087 mPrevYear->show(); 0088 mPrevMonth->show(); 0089 } else { 0090 mPrevYear->hide(); 0091 mPrevMonth->hide(); 0092 } 0093 0094 if (right) { 0095 mNextYear->show(); 0096 mNextMonth->show(); 0097 } else { 0098 mNextYear->hide(); 0099 mNextMonth->hide(); 0100 } 0101 } 0102 0103 void NavigatorBar::selectDates(const KCalendarCore::DateList &dateList) 0104 { 0105 if (!dateList.isEmpty()) { 0106 mDate = dateList.first(); 0107 0108 // set the label text at the top of the navigator 0109 mMonth->setText(i18nc("monthname", "%1", QLocale().standaloneMonthName(mDate.month(), QLocale::LongFormat))); 0110 mYear->setText(i18nc("4 digit year", "%1", QLocale().toString(mDate, QStringLiteral("yyyy")))); 0111 } 0112 } 0113 0114 void NavigatorBar::selectMonthFromMenu() 0115 { 0116 int month = mDate.month(); 0117 const int months = 12; 0118 0119 auto menu = new QMenu(mMonth); 0120 QList<QAction *> act; 0121 0122 QAction *activateAction = nullptr; 0123 act.reserve(months); 0124 for (int i = 1; i <= months; ++i) { 0125 QAction *monthAction = menu->addAction(QLocale().standaloneMonthName(i, QLocale::LongFormat)); 0126 act.append(monthAction); 0127 if (i == month) { 0128 activateAction = monthAction; 0129 } 0130 } 0131 if (activateAction) { 0132 menu->setActiveAction(activateAction); 0133 } 0134 month = 0; 0135 QAction *selectedAct = menu->exec(mMonth->mapToGlobal(QPoint(0, 0))); 0136 if (selectedAct && (selectedAct != activateAction)) { 0137 for (int i = 0; i < months; ++i) { 0138 if (act[i] == selectedAct) { 0139 month = i + 1; 0140 } 0141 } 0142 } 0143 qDeleteAll(act); 0144 act.clear(); 0145 delete menu; 0146 0147 if (month > 0) { 0148 Q_EMIT monthSelected(month); 0149 } 0150 } 0151 0152 void NavigatorBar::selectYearFromMenu() 0153 { 0154 int year = mDate.year(); 0155 int years = 11; // odd number (show a few years ago -> a few years from now) 0156 int minYear = year - (years / 3); 0157 0158 auto menu = new QMenu(mYear); 0159 QList<QAction *> act; 0160 act.reserve(years); 0161 0162 QString yearStr; 0163 QAction *activateAction = nullptr; 0164 int y = minYear; 0165 for (int i = 0; i < years; ++i) { 0166 QAction *yearAction = menu->addAction(yearStr.setNum(y)); 0167 act.append(yearAction); 0168 if (y == year) { 0169 activateAction = yearAction; 0170 } 0171 y++; 0172 } 0173 if (activateAction) { 0174 menu->setActiveAction(activateAction); 0175 } 0176 year = 0; 0177 QAction *selectedAct = menu->exec(mYear->mapToGlobal(QPoint(0, 0))); 0178 if (selectedAct && (selectedAct != activateAction)) { 0179 int y = minYear; 0180 for (int i = 0; i < years; ++i) { 0181 if (act[i] == selectedAct) { 0182 year = y; 0183 } 0184 y++; 0185 } 0186 } 0187 qDeleteAll(act); 0188 act.clear(); 0189 delete menu; 0190 0191 if (year > 0) { 0192 Q_EMIT yearSelected(year); 0193 } 0194 } 0195 0196 QToolButton *NavigatorBar::createNavigationButton(const QString &icon, const QString &toolTip, const QString &whatsThis) 0197 { 0198 auto button = new QToolButton(this); 0199 0200 button->setIcon(QIcon::fromTheme(icon)); 0201 button->setToolButtonStyle(Qt::ToolButtonIconOnly); 0202 button->setAutoRaise(true); 0203 button->setToolTip(toolTip); 0204 button->setWhatsThis(whatsThis); 0205 0206 return button; 0207 } 0208 0209 #include "moc_navigatorbar.cpp"