File indexing completed on 2024-04-28 04:50:48
0001 /* 0002 * datetimeedit.cpp 0003 * 0004 * Copyright (C) 2009-2011 Christoph Pfister <christophpfister@gmail.com> 0005 * 0006 * This program is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU General Public License as published by 0008 * the Free Software Foundation; either version 2 of the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License along 0017 * with this program; if not, write to the Free Software Foundation, Inc., 0018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0019 */ 0020 0021 #include <QLocale> 0022 0023 #include "datetimeedit.h" 0024 0025 static QString localQtDateFormat() 0026 { 0027 QString dateFormat = QLocale().dateFormat(QLocale::ShortFormat); 0028 0029 for (int i = 0; (i + 1) < dateFormat.size(); ++i) { 0030 if (dateFormat.at(i) != QLatin1Char('%')) { 0031 continue; 0032 } 0033 0034 switch (dateFormat.at(i + 1).unicode()) { 0035 case 'Y': 0036 dateFormat.replace(i, 2, QLatin1String("yyyy")); 0037 break; 0038 case 'y': 0039 dateFormat.replace(i, 2, QLatin1String("yy")); 0040 break; 0041 case 'n': 0042 dateFormat.replace(i, 2, QLatin1Char('M')); 0043 break; 0044 case 'm': 0045 dateFormat.replace(i, 2, QLatin1String("MM")); 0046 break; 0047 case 'e': 0048 dateFormat.replace(i, 2, QLatin1Char('d')); 0049 break; 0050 case 'd': 0051 dateFormat.replace(i, 2, QLatin1String("dd")); 0052 break; 0053 case 'b': 0054 dateFormat.replace(i, 2, QLatin1String("MMM")); 0055 break; 0056 case 'B': 0057 dateFormat.replace(i, 2, QLatin1String("MMMM")); 0058 break; 0059 case 'a': 0060 dateFormat.replace(i, 2, QLatin1String("ddd")); 0061 break; 0062 case 'A': 0063 dateFormat.replace(i, 2, QLatin1String("dddd")); 0064 break; 0065 } 0066 } 0067 0068 return dateFormat; 0069 } 0070 0071 static QString localQtTimeFormat(bool showSeconds, bool duration) 0072 { 0073 QString timeFormat = QLocale().timeFormat(QLocale::ShortFormat); 0074 0075 for (int i = 0; (i + 1) < timeFormat.size(); ++i) { 0076 if (timeFormat.at(i) != QLatin1Char('%')) { 0077 continue; 0078 } 0079 0080 bool strip = false; 0081 0082 switch (timeFormat.at(i + 1).unicode()) { 0083 case 'H': 0084 case 'I': 0085 timeFormat.replace(i, 2, QLatin1String("hh")); 0086 break; 0087 case 'k': 0088 case 'l': 0089 timeFormat.replace(i, 2, QLatin1Char('h')); 0090 break; 0091 case 'M': 0092 timeFormat.replace(i, 2, QLatin1String("mm")); 0093 break; 0094 case 'S': 0095 if (showSeconds) { 0096 timeFormat.replace(i, 2, QLatin1String("ss")); 0097 } else { 0098 strip = true; 0099 } 0100 0101 break; 0102 case 'p': 0103 if (!duration) { 0104 timeFormat.replace(i, 2, QLatin1String("AP")); 0105 } else { 0106 strip = true; 0107 } 0108 0109 break; 0110 } 0111 0112 if (strip) { 0113 int beginRemove = i; 0114 int endRemove = i + 2; 0115 0116 while ((beginRemove > 0) && timeFormat.at(beginRemove - 1).isSpace()) { 0117 --beginRemove; 0118 } 0119 0120 if ((beginRemove > 0) && timeFormat.at(beginRemove - 1).isPunct() && 0121 (timeFormat.at(beginRemove - 1) != QLatin1Char('%'))) { 0122 --beginRemove; 0123 0124 while ((beginRemove > 0) && 0125 timeFormat.at(beginRemove - 1).isSpace()) { 0126 --beginRemove; 0127 } 0128 } 0129 0130 if (beginRemove == 0) { 0131 while ((endRemove < timeFormat.size()) && 0132 timeFormat.at(endRemove).isSpace()) { 0133 ++endRemove; 0134 } 0135 0136 if ((endRemove < timeFormat.size()) && 0137 timeFormat.at(endRemove).isPunct() && 0138 (timeFormat.at(endRemove) != QLatin1Char('%'))) { 0139 ++endRemove; 0140 0141 while ((endRemove < timeFormat.size()) && 0142 timeFormat.at(endRemove).isSpace()) { 0143 ++endRemove; 0144 } 0145 } 0146 } 0147 0148 timeFormat.remove(beginRemove, endRemove - beginRemove); 0149 i = beginRemove - 1; 0150 } 0151 } 0152 0153 return timeFormat; 0154 } 0155 0156 DateTimeEdit::DateTimeEdit(QWidget *parent) : QDateTimeEdit(parent) 0157 { 0158 setDisplayFormat(localQtDateFormat() + QLatin1Char(' ') + localQtTimeFormat(false, false)); 0159 } 0160 0161 DateTimeEdit::~DateTimeEdit() 0162 { 0163 } 0164 0165 DurationEdit::DurationEdit(QWidget *parent) : QTimeEdit(parent) 0166 { 0167 setDisplayFormat("HH:mm"); 0168 } 0169 0170 DurationEdit::~DurationEdit() 0171 { 0172 }