File indexing completed on 2024-05-12 16:35:03

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 #include "FixedDateFormat.h"
0020 #include "DateVariable.h"
0021 
0022 #include <KoIcon.h>
0023 
0024 #include <klocalizedstring.h>
0025 
0026 #include <QMenu>
0027 #include <QAction>
0028 #include <QLocale>
0029 
0030 static void createTimeAction(QMenu *parent, const QString &title, const QString &data)
0031 {
0032     QAction *action = new QAction(title, parent);
0033     action->setData(data);
0034     parent->addAction(action);
0035 }
0036 
0037 FixedDateFormat::FixedDateFormat(DateVariable *variable)
0038         : m_variable(variable),
0039         m_popup(0)
0040 {
0041     widget.setupUi(this);
0042 
0043     widget.normalPage->layout()->setMargin(0);
0044     widget.customPage->layout()->setMargin(0);
0045 
0046     QStringList listDateFormat;
0047     listDateFormat << i18n("Locale date format");
0048     listDateFormat << i18n("Short locale date format");
0049     listDateFormat << i18n("Locale date & time format");
0050     listDateFormat << i18n("Short locale date & time format");
0051     listDateFormat << "dd/MM/yy";
0052     listDateFormat << "dd/MM/yyyy";
0053     listDateFormat << "MMM dd,yy";
0054     listDateFormat << "MMM dd,yyyy";
0055     listDateFormat << "dd.MMM.yyyy";
0056     listDateFormat << "MMMM dd, yyyy";
0057     listDateFormat << "ddd, MMM dd,yy";
0058     listDateFormat << "dddd, MMM dd,yy";
0059     listDateFormat << "MM-dd";
0060     listDateFormat << "yyyy-MM-dd";
0061     listDateFormat << "dd/yy";
0062     listDateFormat << "MMMM";
0063     listDateFormat << "yyyy-MM-dd hh:mm";
0064     listDateFormat << "dd.MMM.yyyy hh:mm";
0065     listDateFormat << "MMM dd,yyyy h:mm AP";
0066     listDateFormat << "yyyy-MM-ddThh:mm:ss"; // ISO 8601
0067     widget.formatList->addItems(listDateFormat);
0068     widget.customString->setText(variable->definition());
0069 
0070     int index = listDateFormat.indexOf(variable->definition());
0071     if (index >= 0) {
0072         widget.widgetStack->setCurrentWidget(widget.normalPage);
0073         widget.formatList->setItemSelected(widget.formatList->item(index), true);
0074     } else {
0075         widget.widgetStack->setCurrentWidget(widget.customPage);
0076         widget.custom->setChecked(true);
0077     }
0078 
0079     widget.formatButton->setIcon(koIcon("list-add"));
0080 
0081     connect(widget.custom, SIGNAL(stateChanged(int)), this, SLOT(customClicked(int)));
0082     connect(widget.formatList, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(listClicked(QListWidgetItem*)));
0083     connect(widget.correction, SIGNAL(valueChanged(int)), this, SLOT(offsetChanged(int)));
0084     connect(widget.formatButton, SIGNAL(clicked()), this, SLOT(insertCustomButtonPressed()));
0085     connect(widget.customString, SIGNAL(textChanged(QString)), this, SLOT(customTextChanged(QString)));
0086 }
0087 
0088 void FixedDateFormat::customClicked(int state)
0089 {
0090     if (state == Qt::Unchecked)
0091         widget.widgetStack->setCurrentWidget(widget.normalPage);
0092     else
0093         widget.widgetStack->setCurrentWidget(widget.customPage);
0094 }
0095 
0096 void FixedDateFormat::listClicked(QListWidgetItem *item)
0097 {
0098     // TODO parse out the first two values...
0099     QString format;
0100     switch (widget.formatList->row(item)) {
0101     case 0: format = QLocale().dateFormat(QLocale::LongFormat); break;
0102     case 1: format = QLocale().dateFormat(QLocale::ShortFormat); break;
0103     case 2: format = QLocale().dateTimeFormat(QLocale::LongFormat); break;
0104     case 3: format = QLocale().dateTimeFormat(QLocale::ShortFormat); break;
0105     default:
0106         format = item->text();
0107     }
0108     m_variable->setDefinition(format);
0109     widget.customString->setText(format);
0110 }
0111 
0112 void FixedDateFormat::offsetChanged(int offset)
0113 {
0114     m_variable->setDaysOffset(offset);
0115 }
0116 
0117 void FixedDateFormat::insertCustomButtonPressed()
0118 {
0119     if (m_popup == 0) {
0120         m_popup = new QMenu(this);
0121         QMenu *day = new QMenu(i18n("Day"), m_popup);
0122         QMenu *month = new QMenu(i18n("Month"), m_popup);
0123         QMenu *year = new QMenu(i18n("Year"), m_popup);
0124         QMenu *hour = new QMenu(i18n("Hour"), m_popup);
0125         QMenu *minute = new QMenu(i18n("Minute"), m_popup);
0126         QMenu *second = new QMenu(i18n("Second"), m_popup);
0127         m_popup->addMenu(day);
0128         m_popup->addMenu(month);
0129         m_popup->addMenu(year);
0130         m_popup->addMenu(hour);
0131         m_popup->addMenu(minute);
0132         m_popup->addMenu(second);
0133 
0134         createTimeAction(day, i18n("Flexible Digits (1-31)"), "d");
0135         createTimeAction(day, i18n("2 Digits (01-31)"), "dd");
0136         createTimeAction(day, i18n("Abbreviated Name"), "ddd");
0137         createTimeAction(day, i18n("Long Name"), "dddd");
0138         createTimeAction(month, i18n("Flexible Digits (1-12)"), "M");
0139         createTimeAction(month, i18n("2 Digits (01-12)"), "MM");
0140         createTimeAction(month, i18n("Abbreviated Name"), "MMM");
0141         createTimeAction(month, i18n("Long Name"), "MMMM");
0142         createTimeAction(month, i18n("Possessive Abbreviated Name"), "PPP");
0143         createTimeAction(month, i18n("Possessive Long Name"), "PPPP");
0144         createTimeAction(year, i18n("2 Digits (01-99)"), "yy");
0145         createTimeAction(year, i18n("4 Digits"), "yyyy");
0146         createTimeAction(hour, i18n("Flexible Digits (1-23)"), "h");
0147         createTimeAction(hour, i18n("2 Digits (01-23)"), "hh");
0148         createTimeAction(minute, i18n("Flexible Digits (1-59)"), "m");
0149         createTimeAction(minute, i18n("2 Digits (01-59)"), "mm");
0150         createTimeAction(second, i18n("Flexible Digits (1-59)"), "s");
0151         createTimeAction(second, i18n("2 Digits (01-59)"), "ss");
0152         createTimeAction(m_popup, i18n("am/pm"), "ap");
0153         createTimeAction(m_popup, i18n("AM/PM"), "AP");
0154     }
0155     QPoint position = widget.formatButton->mapToGlobal(QPoint(0, widget.formatButton->height()));
0156     QAction *action = m_popup->exec(position);
0157     if (action)
0158         widget.customString->insert(qvariant_cast<QString>(action->data()));
0159 }
0160 
0161 void FixedDateFormat::customTextChanged(const QString& text)
0162 {
0163     m_variable->setDefinition(text);
0164 
0165     if (widget.custom->isChecked()) {
0166         // altering the custom text will deselect the list item so the user can easily switch
0167         // back by selecting one.
0168         QListWidgetItem * item = widget.formatList->currentItem();
0169         if (item) // deselect it.
0170             widget.formatList->setItemSelected(item, false);
0171     }
0172 }