File indexing completed on 2025-01-26 05:07:37
0001 /* 0002 SPDX-FileCopyrightText: 2016 Kai Uwe Broulik <kde@privat.broulik.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "clipboardmenu.h" 0008 #include "klocalizedstring.h" 0009 0010 #include <QClipboard> 0011 #include <QGuiApplication> 0012 #include <QMenu> 0013 #include <QRegularExpression> 0014 0015 ClipboardMenu::ClipboardMenu(QObject *parent) 0016 : QObject(parent) 0017 { 0018 } 0019 0020 ClipboardMenu::~ClipboardMenu() = default; 0021 0022 QDateTime ClipboardMenu::currentDate() const 0023 { 0024 return m_currentDate; 0025 } 0026 0027 void ClipboardMenu::setCurrentDate(const QDateTime ¤tDate) 0028 { 0029 if (m_currentDate != currentDate) { 0030 m_currentDate = currentDate; 0031 Q_EMIT currentDateChanged(); 0032 } 0033 } 0034 0035 bool ClipboardMenu::secondsIncluded() const 0036 { 0037 return m_secondsIncluded; 0038 } 0039 0040 void ClipboardMenu::setSecondsIncluded(bool secondsIncluded) 0041 { 0042 if (m_secondsIncluded != secondsIncluded) { 0043 m_secondsIncluded = secondsIncluded; 0044 Q_EMIT secondsIncludedChanged(); 0045 } 0046 } 0047 0048 void ClipboardMenu::setupMenu(QAction *action) 0049 { 0050 QMenu *menu = new QMenu; 0051 0052 /* 0053 * The refresh rate of m_currentDate depends of what is shown in the plasmoid. 0054 * If only minutes are shown, the value is updated at the full minute and 0055 * seconds are always 0 or 59 and thus useless/confusing to offer for copy. 0056 * Use a reference to the config's showSeconds to decide if seconds are sent 0057 * to the clipboard. There was no workaround found ... 0058 */ 0059 connect(menu, &QMenu::aboutToShow, this, [this, menu] { 0060 menu->clear(); 0061 0062 const QDate date = m_currentDate.date(); 0063 const QTime time = m_currentDate.time(); 0064 const QRegularExpression rx(QStringLiteral("[^0-9:]")); 0065 const QChar spaceCharacter = QLatin1Char(' '); 0066 QString timeString; 0067 QAction *menuOption; 0068 0069 // e.g 12:30 PM or 12:30:01 PM 0070 timeString = m_secondsIncluded ? QLocale::system().toString(time, QLocale::LongFormat) : QLocale::system().toString(time, QLocale::ShortFormat); 0071 menuOption = menu->addAction(timeString); 0072 menuOption->setData(timeString); 0073 0074 // the same as the option above but shows the opposite of the "show seconds" setting 0075 // e.g if "show seconds" is enabled it will show the time without seconds and vice-versa 0076 timeString = m_secondsIncluded ? QLocale::system().toString(time, QLocale::ShortFormat) : QLocale::system().toString(time, QLocale::LongFormat); 0077 menuOption = menu->addAction(timeString); 0078 menuOption->setData(timeString); 0079 0080 // e.g 4/28/22 0081 timeString = QLocale::system().toString(date, QLocale::ShortFormat); 0082 menuOption = menu->addAction(timeString); 0083 menuOption->setData(timeString); 0084 0085 // e.g Thursday, April 28, 2022 0086 timeString = QLocale::system().toString(date, QLocale::LongFormat); 0087 menuOption = menu->addAction(timeString); 0088 menuOption->setData(timeString); 0089 0090 // e.g Thursday, April 28, 2022 12:30 PM or Thursday, April 28, 2022 12:30:01 PM -03 0091 timeString = m_secondsIncluded 0092 ? QLocale::system().toString(date, QLocale::LongFormat) + spaceCharacter + QLocale::system().toString(time, QLocale::LongFormat) 0093 : QLocale::system().toString(date, QLocale::LongFormat) + spaceCharacter + QLocale::system().toString(time, QLocale::ShortFormat); 0094 menuOption = menu->addAction(timeString); 0095 menuOption->setData(timeString); 0096 0097 // e.g 2022-04-28 0098 timeString = date.toString(Qt::ISODate); 0099 menuOption = menu->addAction(timeString); 0100 menuOption->setData(timeString); 0101 0102 // e.g 2022-04-28 12:30 PM or 2022-04-28 12:30:01 PM -03 0103 timeString = m_secondsIncluded ? date.toString(Qt::ISODate) + spaceCharacter + QLocale::system().toString(time, QLocale::LongFormat) 0104 : date.toString(Qt::ISODate) + spaceCharacter + QLocale::system().toString(time, QLocale::ShortFormat); 0105 menuOption = menu->addAction(timeString); 0106 menuOption->setData(timeString); 0107 0108 menu->addSeparator(); 0109 0110 QMenu *otherCalendarsMenu = menu->addMenu(i18n("Other Calendars")); 0111 0112 /* Add ICU Calendars if QLocale is ready for 0113 Chinese, Coptic, Ethiopic, (Gregorian), Hebrew, Indian, Islamic, Persian 0114 0115 otherCalendarsMenu->addSeparator(); 0116 */ 0117 timeString = QString::number(m_currentDate.toMSecsSinceEpoch() / 1000); 0118 menuOption = otherCalendarsMenu->addAction(i18nc("unix timestamp (seconds since 1.1.1970)", "%1 (UNIX Time)", timeString)); 0119 menuOption->setData(timeString); 0120 timeString = QString::number(qreal(2440587.5) + qreal(m_currentDate.toMSecsSinceEpoch()) / qreal(86400000), 'f', 5); 0121 menuOption = otherCalendarsMenu->addAction(i18nc("for astronomers (days and decimals since ~7000 years ago)", "%1 (Julian Date)", timeString)); 0122 menuOption->setData(timeString); 0123 }); 0124 0125 connect(menu, &QMenu::triggered, menu, [](QAction *action) { 0126 qApp->clipboard()->setText(action->data().toString()); 0127 qApp->clipboard()->setText(action->data().toString(), QClipboard::Selection); 0128 }); 0129 0130 // QMenu cannot have QAction as parent and setMenu doesn't take ownership 0131 connect(action, &QObject::destroyed, menu, &QObject::deleteLater); 0132 0133 action->setMenu(menu); 0134 }