QtBase  v6.3.1
dateformats.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "dateformats.h"
30 
31 #include <QLineEdit>
32 #include <QScrollArea>
33 #include <QGridLayout>
34 #include <QComboBox>
35 #include <QLabel>
36 #include <QDateTime>
37 
39 {
40  scrollArea = new QScrollArea;
41  scrollAreaWidget = new QWidget;
42  scrollArea->setWidget(scrollAreaWidget);
43  scrollArea->setWidgetResizable(true);
44  layout = new QGridLayout(scrollAreaWidget);
45  QVBoxLayout *l = new QVBoxLayout(this);
46  l->addWidget(scrollArea);
47 
48  shortDateFormat = addItem("Date format (short):");
49  longDateFormat = addItem("Date format (long):");
50  shortTimeFormat = addItem("Time format (short):");
51  longTimeFormat = addItem("Time format (long):");
52  shortDateTimeFormat = addItem("DateTime format (short):");
53  longDateTimeFormat = addItem("DateTime format (long):");
54  amText = addItem("Before noon:");
55  pmText = addItem("After noon:");
56  firstDayOfWeek = addItem("First day of week:");
57 
58  monthNamesShort = new QComboBox;
59  monthNamesLong = new QComboBox;
60  standaloneMonthNamesShort = new QComboBox;
61  standaloneMonthNamesLong = new QComboBox;
62  dayNamesShort = new QComboBox;
63  dayNamesLong = new QComboBox;
64  standaloneDayNamesShort = new QComboBox;
65  standaloneDayNamesLong = new QComboBox;
66 
67  int row = layout->rowCount();
68  layout->addWidget(new QLabel("Month names [short/long]:"), row, 0);
69  layout->addWidget(monthNamesShort, row, 1);
70  layout->addWidget(monthNamesLong, row, 2);
71  row = layout->rowCount();
72  layout->addWidget(new QLabel("Standalone month names [short/long]:"), row, 0);
73  layout->addWidget(standaloneMonthNamesShort, row, 1);
74  layout->addWidget(standaloneMonthNamesLong, row, 2);
75  row = layout->rowCount();
76  layout->addWidget(new QLabel("Day names [short/long]:"), row, 0);
77  layout->addWidget(dayNamesShort, row, 1);
78  layout->addWidget(dayNamesLong, row, 2);
79  row = layout->rowCount();
80  layout->addWidget(new QLabel("Standalone day names [short/long]:"), row, 0);
81  layout->addWidget(standaloneDayNamesShort, row, 1);
82  layout->addWidget(standaloneDayNamesLong, row, 2);
83 }
84 
86 {
87  static const char *names[] = {
88  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
89  };
90  return QString::fromLatin1(names[dow-1]);
91 }
92 
93 void DateFormatsWidget::localeChanged(QLocale locale)
94 {
97  shortDateFormat->setText(locale.toString(now.date(), QLocale::ShortFormat));
98  shortDateFormat->setToolTip(locale.dateFormat(QLocale::ShortFormat));
99  longDateFormat->setText(locale.toString(now.date(), QLocale::LongFormat));
100  longDateFormat->setToolTip(locale.dateFormat(QLocale::LongFormat));
101  shortTimeFormat->setText(locale.toString(now.time(), QLocale::ShortFormat));
102  shortTimeFormat->setToolTip(locale.timeFormat(QLocale::ShortFormat));
103  longTimeFormat->setText(locale.toString(now.time(), QLocale::LongFormat));
104  longTimeFormat->setToolTip(locale.timeFormat(QLocale::LongFormat));
105  shortDateTimeFormat->setText(locale.toString(now, QLocale::ShortFormat));
106  shortDateTimeFormat->setToolTip(locale.dateTimeFormat(QLocale::ShortFormat));
107  longDateTimeFormat->setText(locale.toString(now, QLocale::LongFormat));
108  longDateTimeFormat->setToolTip(locale.dateTimeFormat(QLocale::LongFormat));
109  amText->setText(locale.amText());
110  pmText->setText(locale.pmText());
111  firstDayOfWeek->setText(toString(locale.firstDayOfWeek()));
112 
113  int mns = monthNamesShort->currentIndex();
114  int mnl = monthNamesLong->currentIndex();
115  int smns = standaloneMonthNamesShort->currentIndex();
116  int smnl = standaloneMonthNamesLong->currentIndex();
117  int dnl = dayNamesLong->currentIndex();
118  int dns = dayNamesShort->currentIndex();
119  int sdnl = standaloneDayNamesLong->currentIndex();
120  int sdns = standaloneDayNamesShort->currentIndex();
121 
122  monthNamesShort->clear();
123  monthNamesLong->clear();
124  standaloneMonthNamesShort->clear();
125  standaloneMonthNamesLong->clear();
126  dayNamesLong->clear();
127  dayNamesShort->clear();
128  standaloneDayNamesLong->clear();
129  standaloneDayNamesShort->clear();
130 
131  for (int i = 1; i <= 12; ++i)
132  monthNamesShort->addItem(locale.monthName(i, QLocale::ShortFormat));
133  monthNamesShort->setCurrentIndex(mns >= 0 ? mns : 0);
134  for (int i = 1; i <= 12; ++i)
135  monthNamesLong->addItem(locale.monthName(i, QLocale::LongFormat));
136  monthNamesLong->setCurrentIndex(mnl >= 0 ? mnl : 0);
137 
138  for (int i = 1; i <= 12; ++i)
139  standaloneMonthNamesShort->addItem(locale.standaloneMonthName(i, QLocale::ShortFormat));
140  standaloneMonthNamesShort->setCurrentIndex(smns >= 0 ? smns : 0);
141  for (int i = 1; i <= 12; ++i)
142  standaloneMonthNamesLong->addItem(locale.standaloneMonthName(i, QLocale::LongFormat));
143  standaloneMonthNamesLong->setCurrentIndex(smnl >= 0 ? smnl : 0);
144 
145  for (int i = 1; i <= 7; ++i)
146  dayNamesLong->addItem(locale.dayName(i, QLocale::LongFormat));
147  dayNamesLong->setCurrentIndex(dnl >= 0 ? dnl : 0);
148  for (int i = 1; i <= 7; ++i)
149  dayNamesShort->addItem(locale.dayName(i, QLocale::ShortFormat));
150  dayNamesShort->setCurrentIndex(dns >= 0 ? dns : 0);
151 
152  for (int i = 1; i <= 7; ++i)
153  standaloneDayNamesLong->addItem(locale.standaloneDayName(i, QLocale::LongFormat));
154  standaloneDayNamesLong->setCurrentIndex(sdnl >= 0 ? sdnl : 0);
155  for (int i = 1; i <= 7; ++i)
156  standaloneDayNamesShort->addItem(locale.standaloneDayName(i, QLocale::ShortFormat));
157  standaloneDayNamesShort->setCurrentIndex(sdns >= 0 ? sdns : 0);
158 }
159 
160 void DateFormatsWidget::addItem(const QString &label, QWidget *w)
161 {
162  QLabel *lbl = new QLabel(label);
163  int row = layout->rowCount();
164  layout->addWidget(lbl, row, 0);
165  layout->addWidget(w, row, 1, 1, 2);
166 }
167 
168 QLineEdit *DateFormatsWidget::addItem(const QString &label)
169 {
170  QLineEdit *le = new QLineEdit;
171  le->setReadOnly(true);
172  addItem(label, le);
173  return le;
174 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QComboBox widget is a combined button and popup list.
Definition: qcombobox.h:60
void addItem(const QString &text, const QVariant &userData=QVariant())
Definition: qcombobox.h:260
void clear()
Definition: qcombobox.cpp:2901
int currentIndex
the index of the current item in the combobox.
Definition: qcombobox.h:67
void setCurrentIndex(int index)
Definition: qcombobox.cpp:2133
The QDateTime class provides date and time functions.
Definition: qdatetime.h:238
static QDateTime currentDateTime()
The QGridLayout class lays out widgets in a grid.
Definition: qgridlayout.h:57
void addWidget(QWidget *w)
Definition: qgridlayout.h:100
int rowCount() const
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:64
void setText(const QString &)
Definition: qlineedit.cpp:316
QString dateTimeFormat(FormatType format=LongFormat) const
Definition: qlocale.cpp:2327
QString dateFormat(FormatType format=LongFormat) const
Definition: qlocale.cpp:2265
Qt::DayOfWeek firstDayOfWeek() const
Definition: qlocale.cpp:3118
QString dayName(int, FormatType format=LongFormat) const
Definition: qlocale.cpp:2853
@ LongFormat
Definition: qlocale.h:894
@ ShortFormat
Definition: qlocale.h:894
QString timeFormat(FormatType format=LongFormat) const
Definition: qlocale.cpp:2296
QString monthName(int, FormatType format=LongFormat) const
Definition: qlocale.cpp:2819
QString amText() const
Definition: qlocale.cpp:3285
QString pmText() const
Definition: qlocale.cpp:3305
QString toString(qlonglong i) const
Definition: qlocale.cpp:1981
QString standaloneMonthName(int, FormatType format=LongFormat) const
Definition: qlocale.cpp:2835
QString standaloneDayName(int, FormatType format=LongFormat) const
Definition: qlocale.cpp:2870
friend class QWidget
Definition: qobject.h:445
The QScrollArea class provides a scrolling view onto another widget.
Definition: qscrollarea.h:53
void setWidget(QWidget *widget)
void setWidgetResizable(bool resizable)
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString fromLatin1(QByteArrayView ba)
Definition: qstring.cpp:5488
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
QLocale locale
the widget's locale
Definition: qwidget.h:210
void setLocale(const QLocale &locale)
Definition: qwidget.cpp:5927
QString toString(Qt::DayOfWeek dow)
Definition: dateformats.cpp:85
DayOfWeek
Definition: qnamespace.h:1266
GLfloat GLfloat GLfloat w
[0]
GLuint GLsizei const GLchar * label
[43]
GLuint GLuint * names
Definition: qopenglext.h:5654
GLenum GLenum GLsizei void * row
Definition: qopenglext.h:2747
QPointer< QLineEdit > le