navigatorbar.cpp 7.33 KB
Newer Older
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
/*
  This file is part of KOrganizer.

  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, 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 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.

  As a special exception, permission is given to link this program
  with any edition of Qt, and distribute the resulting executable,
  without including the source code for Qt in the source distribution.
*/

#include "navigatorbar.h"
#include "koglobals.h"

#include <KIconLoader>
29
#include <KLocalizedString>
30
31

#include <QHBoxLayout>
32
#include <QLocale>
33
34
35
#include <QMenu>
#include <QToolButton>

Laurent Montel's avatar
Laurent Montel committed
36
NavigatorBar::NavigatorBar(QWidget *parent) : QWidget(parent)
37
{
Laurent Montel's avatar
Laurent Montel committed
38
39
40
41
42
43
44
    QFont tfont = font();
    tfont.setPointSize(10);
    tfont.setBold(false);

    bool isRTL = KOGlobals::self()->reverseLayout();

    mPrevYear = createNavigationButton(
Laurent Montel's avatar
Laurent Montel committed
45
46
47
48
        isRTL ? QStringLiteral("arrow-right-double") : QStringLiteral("arrow-left-double"),
        i18n("Scroll backward to the previous year"),
        i18n("Click this button to scroll the display to the "
             "same approximate day of the previous year"));
Laurent Montel's avatar
Laurent Montel committed
49
50

    mPrevMonth = createNavigationButton(
Laurent Montel's avatar
Laurent Montel committed
51
52
53
54
        isRTL ? QStringLiteral("arrow-right") : QStringLiteral("arrow-left"),
        i18n("Scroll backward to the previous month"),
        i18n("Click this button to scroll the display to the "
             "same approximate date of the previous month"));
Laurent Montel's avatar
Laurent Montel committed
55
56

    mNextMonth = createNavigationButton(
Laurent Montel's avatar
Laurent Montel committed
57
58
59
60
        isRTL ? QStringLiteral("arrow-left") : QStringLiteral("arrow-right"),
        i18n("Scroll forward to the next month"),
        i18n("Click this button to scroll the display to the "
             "same approximate date of the next month"));
Laurent Montel's avatar
Laurent Montel committed
61
62

    mNextYear = createNavigationButton(
Laurent Montel's avatar
Laurent Montel committed
63
64
65
66
        isRTL ? QStringLiteral("arrow-left-double") : QStringLiteral("arrow-right-double"),
        i18n("Scroll forward to the next year"),
        i18n("Click this button to scroll the display to the "
             "same approximate day of the next year"));
Laurent Montel's avatar
Laurent Montel committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    // Create month name button
    mMonth = new QToolButton(this);
    mMonth->setPopupMode(QToolButton::InstantPopup);
    mMonth->setAutoRaise(true);
    mMonth->setFont(tfont);
    mMonth->setToolTip(i18n("Select a month"));

    // Create year button
    mYear = new QToolButton(this);
    mYear->setPopupMode(QToolButton::InstantPopup);
    mYear->setAutoRaise(true);
    mYear->setFont(tfont);
    mYear->setToolTip(i18n("Select a year"));

    // set up control frame layout
    QHBoxLayout *ctrlLayout = new QHBoxLayout(this);
84
    ctrlLayout->setContentsMargins(0, 0, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    ctrlLayout->addWidget(mPrevYear);
    ctrlLayout->addWidget(mPrevMonth);
    ctrlLayout->addStretch();
    ctrlLayout->addWidget(mMonth);
    ctrlLayout->addWidget(mYear);
    ctrlLayout->addStretch();
    ctrlLayout->addWidget(mNextMonth);
    ctrlLayout->addWidget(mNextYear);

    connect(mPrevYear, &QToolButton::clicked, this, &NavigatorBar::prevYearClicked);
    connect(mPrevMonth, &QToolButton::clicked, this, &NavigatorBar::prevMonthClicked);
    connect(mNextMonth, &QToolButton::clicked, this, &NavigatorBar::nextMonthClicked);
    connect(mNextYear, &QToolButton::clicked, this, &NavigatorBar::nextYearClicked);
    connect(mMonth, &QToolButton::clicked, this, &NavigatorBar::selectMonthFromMenu);
    connect(mYear, &QToolButton::clicked, this, &NavigatorBar::selectYearFromMenu);
100
101
102
103
104
105
}

NavigatorBar::~NavigatorBar()
{
}

Laurent Montel's avatar
Laurent Montel committed
106
void NavigatorBar::showButtons(bool left, bool right)
107
{
Laurent Montel's avatar
Laurent Montel committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    if (left) {
        mPrevYear->show();
        mPrevMonth->show();
    } else {
        mPrevYear->hide();
        mPrevMonth->hide();
    }

    if (right) {
        mNextYear->show();
        mNextMonth->show();
    } else {
        mNextYear->hide();
        mNextMonth->hide();
    }
123
124
}

125
void NavigatorBar::selectDates(const KCalendarCore::DateList &dateList)
126
{
Laurent Montel's avatar
Laurent Montel committed
127
    if (!dateList.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
128
        mDate = dateList.first();
129

Laurent Montel's avatar
Laurent Montel committed
130
        // set the label text at the top of the navigator
Laurent Montel's avatar
Laurent Montel committed
131
        mMonth->setText(i18nc("monthname", "%1",
132
                              QLocale().standaloneMonthName(mDate.month(), QLocale::LongFormat)));
Laurent Montel's avatar
Laurent Montel committed
133
134
        mYear->setText(i18nc("4 digit year", "%1",
                             QLocale().toString(mDate, QLatin1String("yyyy"))));
Laurent Montel's avatar
Laurent Montel committed
135
    }
136
137
138
139
}

void NavigatorBar::selectMonthFromMenu()
{
140
141
    int month = mDate.month();
    const int months = 12;
Laurent Montel's avatar
Laurent Montel committed
142
143

    QMenu *menu = new QMenu(mMonth);
144
    QList<QAction *> act;
Laurent Montel's avatar
Laurent Montel committed
145

Laurent Montel's avatar
Laurent Montel committed
146
    QAction *activateAction = nullptr;
147
    act.reserve(months);
Laurent Montel's avatar
Laurent Montel committed
148
    for (int i = 1; i <= months; ++i) {
149
        QAction *monthAction = menu->addAction(QLocale().standaloneMonthName(i, QLocale::LongFormat));
Laurent Montel's avatar
Laurent Montel committed
150
151
152
153
        act.append(monthAction);
        if (i == month) {
            activateAction = monthAction;
        }
154
    }
Laurent Montel's avatar
Laurent Montel committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    if (activateAction) {
        menu->setActiveAction(activateAction);
    }
    month = 0;
    QAction *selectedAct = menu->exec(mMonth->mapToGlobal(QPoint(0, 0)));
    if (selectedAct && (selectedAct != activateAction)) {
        for (int i = 0; i < months; ++i) {
            if (act[i] == selectedAct) {
                month = i + 1;
            }
        }
    }
    qDeleteAll(act);
    act.clear();
    delete menu;

    if (month > 0) {
Laurent Montel's avatar
Laurent Montel committed
172
        Q_EMIT monthSelected(month);
173
174
175
176
177
    }
}

void NavigatorBar::selectYearFromMenu()
{
178
    int year = mDate.year();
Laurent Montel's avatar
Laurent Montel committed
179
180
181
182
    int years = 11;  // odd number (show a few years ago -> a few years from now)
    int minYear = year - (years / 3);

    QMenu *menu = new QMenu(mYear);
183
184
    QList<QAction *> act;
    act.reserve(years);
Laurent Montel's avatar
Laurent Montel committed
185
186

    QString yearStr;
Laurent Montel's avatar
Laurent Montel committed
187
    QAction *activateAction = nullptr;
188
    int y = minYear;
Laurent Montel's avatar
Laurent Montel committed
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
    for (int i = 0; i < years; ++i) {
        QAction *yearAction = menu->addAction(yearStr.setNum(y));
        act.append(yearAction);
        if (y == year) {
            activateAction = yearAction;
        }
        y++;
    }
    if (activateAction) {
        menu->setActiveAction(activateAction);
    }
    year = 0;
    QAction *selectedAct = menu->exec(mYear->mapToGlobal(QPoint(0, 0)));
    if (selectedAct && (selectedAct != activateAction)) {
        int y = minYear;
        for (int i = 0; i < years; ++i) {
            if (act[i] == selectedAct) {
                year = y;
            }
            y++;
        }
    }
    qDeleteAll(act);
    act.clear();
    delete menu;

    if (year > 0) {
Laurent Montel's avatar
Laurent Montel committed
216
        Q_EMIT yearSelected(year);
217
218
219
    }
}

Laurent Montel's avatar
Laurent Montel committed
220
QToolButton *NavigatorBar::createNavigationButton(const QString &icon, const QString &toolTip, const QString &whatsThis)
221
{
Laurent Montel's avatar
Laurent Montel committed
222
    QToolButton *button = new QToolButton(this);
223

Laurent Montel's avatar
Laurent Montel committed
224
225
226
227
228
229
230
    button->setIcon(
        KIconLoader::global()->loadIcon(icon, KIconLoader::Desktop, KIconLoader::SizeSmall));
    button->setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
    button->setToolButtonStyle(Qt::ToolButtonIconOnly);
    button->setAutoRaise(true);
    button->setToolTip(toolTip);
    button->setWhatsThis(whatsThis);
231

Laurent Montel's avatar
Laurent Montel committed
232
    return button;
233
}