File: FixedDateFormat.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 261,632 kB
  • sloc: cpp: 650,836; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 17
file content (172 lines) | stat: -rw-r--r-- 6,911 bytes parent folder | download | duplicates (2)
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
/* This file is part of the KDE project
 * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "FixedDateFormat.h"
#include "DateVariable.h"

#include <KoIcon.h>

#include <klocalizedstring.h>

#include <QMenu>
#include <QAction>
#include <QLocale>

static void createTimeAction(QMenu *parent, const QString &title, const QString &data)
{
    QAction *action = new QAction(title, parent);
    action->setData(data);
    parent->addAction(action);
}

FixedDateFormat::FixedDateFormat(DateVariable *variable)
        : m_variable(variable),
        m_popup(0)
{
    widget.setupUi(this);

    widget.normalPage->layout()->setMargin(0);
    widget.customPage->layout()->setMargin(0);

    QStringList listDateFormat;
    listDateFormat << i18n("Locale date format");
    listDateFormat << i18n("Short locale date format");
    listDateFormat << i18n("Locale date & time format");
    listDateFormat << i18n("Short locale date & time format");
    listDateFormat << "dd/MM/yy";
    listDateFormat << "dd/MM/yyyy";
    listDateFormat << "MMM dd,yy";
    listDateFormat << "MMM dd,yyyy";
    listDateFormat << "dd.MMM.yyyy";
    listDateFormat << "MMMM dd, yyyy";
    listDateFormat << "ddd, MMM dd,yy";
    listDateFormat << "dddd, MMM dd,yy";
    listDateFormat << "MM-dd";
    listDateFormat << "yyyy-MM-dd";
    listDateFormat << "dd/yy";
    listDateFormat << "MMMM";
    listDateFormat << "yyyy-MM-dd hh:mm";
    listDateFormat << "dd.MMM.yyyy hh:mm";
    listDateFormat << "MMM dd,yyyy h:mm AP";
    listDateFormat << "yyyy-MM-ddThh:mm:ss"; // ISO 8601
    widget.formatList->addItems(listDateFormat);
    widget.customString->setText(variable->definition());

    int index = listDateFormat.indexOf(variable->definition());
    if (index >= 0) {
        widget.widgetStack->setCurrentWidget(widget.normalPage);
        widget.formatList->setItemSelected(widget.formatList->item(index), true);
    } else {
        widget.widgetStack->setCurrentWidget(widget.customPage);
        widget.custom->setChecked(true);
    }

    widget.formatButton->setIcon(koIcon("list-add"));

    connect(widget.custom, SIGNAL(stateChanged(int)), this, SLOT(customClicked(int)));
    connect(widget.formatList, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(listClicked(QListWidgetItem*)));
    connect(widget.correction, SIGNAL(valueChanged(int)), this, SLOT(offsetChanged(int)));
    connect(widget.formatButton, SIGNAL(clicked()), this, SLOT(insertCustomButtonPressed()));
    connect(widget.customString, SIGNAL(textChanged(QString)), this, SLOT(customTextChanged(QString)));
}

void FixedDateFormat::customClicked(int state)
{
    if (state == Qt::Unchecked)
        widget.widgetStack->setCurrentWidget(widget.normalPage);
    else
        widget.widgetStack->setCurrentWidget(widget.customPage);
}

void FixedDateFormat::listClicked(QListWidgetItem *item)
{
    // TODO parse out the first two values...
    QString format;
    switch (widget.formatList->row(item)) {
    case 0: format = QLocale().dateFormat(QLocale::LongFormat); break;
    case 1: format = QLocale().dateFormat(QLocale::ShortFormat); break;
    case 2: format = QLocale().dateTimeFormat(QLocale::LongFormat); break;
    case 3: format = QLocale().dateTimeFormat(QLocale::ShortFormat); break;
    default:
        format = item->text();
    }
    m_variable->setDefinition(format);
    widget.customString->setText(format);
}

void FixedDateFormat::offsetChanged(int offset)
{
    m_variable->setDaysOffset(offset);
}

void FixedDateFormat::insertCustomButtonPressed()
{
    if (m_popup == 0) {
        m_popup = new QMenu(this);
        QMenu *day = new QMenu(i18n("Day"), m_popup);
        QMenu *month = new QMenu(i18n("Month"), m_popup);
        QMenu *year = new QMenu(i18n("Year"), m_popup);
        QMenu *hour = new QMenu(i18n("Hour"), m_popup);
        QMenu *minute = new QMenu(i18n("Minute"), m_popup);
        QMenu *second = new QMenu(i18n("Second"), m_popup);
        m_popup->addMenu(day);
        m_popup->addMenu(month);
        m_popup->addMenu(year);
        m_popup->addMenu(hour);
        m_popup->addMenu(minute);
        m_popup->addMenu(second);

        createTimeAction(day, i18n("Flexible Digits (1-31)"), "d");
        createTimeAction(day, i18n("2 Digits (01-31)"), "dd");
        createTimeAction(day, i18n("Abbreviated Name"), "ddd");
        createTimeAction(day, i18n("Long Name"), "dddd");
        createTimeAction(month, i18n("Flexible Digits (1-12)"), "M");
        createTimeAction(month, i18n("2 Digits (01-12)"), "MM");
        createTimeAction(month, i18n("Abbreviated Name"), "MMM");
        createTimeAction(month, i18n("Long Name"), "MMMM");
        createTimeAction(month, i18n("Possessive Abbreviated Name"), "PPP");
        createTimeAction(month, i18n("Possessive Long Name"), "PPPP");
        createTimeAction(year, i18n("2 Digits (01-99)"), "yy");
        createTimeAction(year, i18n("4 Digits"), "yyyy");
        createTimeAction(hour, i18n("Flexible Digits (1-23)"), "h");
        createTimeAction(hour, i18n("2 Digits (01-23)"), "hh");
        createTimeAction(minute, i18n("Flexible Digits (1-59)"), "m");
        createTimeAction(minute, i18n("2 Digits (01-59)"), "mm");
        createTimeAction(second, i18n("Flexible Digits (1-59)"), "s");
        createTimeAction(second, i18n("2 Digits (01-59)"), "ss");
        createTimeAction(m_popup, i18n("am/pm"), "ap");
        createTimeAction(m_popup, i18n("AM/PM"), "AP");
    }
    QPoint position = widget.formatButton->mapToGlobal(QPoint(0, widget.formatButton->height()));
    QAction *action = m_popup->exec(position);
    if (action)
        widget.customString->insert(qvariant_cast<QString>(action->data()));
}

void FixedDateFormat::customTextChanged(const QString& text)
{
    m_variable->setDefinition(text);

    if (widget.custom->isChecked()) {
        // altering the custom text will deselect the list item so the user can easily switch
        // back by selecting one.
        QListWidgetItem * item = widget.formatList->currentItem();
        if (item) // deselect it.
            widget.formatList->setItemSelected(item, false);
    }
}