1 Star 1 Fork 0

sonichy / dde-dock-datetime

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
datetimeplugin.cpp 12.12 KB
一键复制 编辑 原始数据 按行查看 历史
sonichy 提交于 2021-12-24 22:15 . add tooltip color
/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: sbw <sbw@sbw.so>
*
* Maintainer: sbw <sbw@sbw.so>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "datetimeplugin.h"
#include <QLabel>
#include <QApplication>
#include <QDesktopWidget>
#include <QDialog>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QFontDialog>
#include <QColorDialog>
DatetimePlugin::DatetimePlugin(QObject *parent)
: QObject(parent),
m_dateTipsLabel(new QLabel),
m_refershTimer(new QTimer(this)),
m_settings("deepin", "dde-dock-datetime1")
{
m_dateTipsLabel->setObjectName("datetime1");
QColor color(m_settings.value("FontColor", "#FFFFFF").toString());
m_dateTipsLabel->setStyleSheet("color:" + color.name() + "; padding:0px 3px;");
//m_dateTipsLabel->setStyleSheet("color:white; padding:0px 3px;");
m_refershTimer->setInterval(1000);
m_refershTimer->start();
m_centralWidget = new DatetimeWidget;
connect(m_centralWidget, &DatetimeWidget::requestUpdateGeometry, [this] { m_proxyInter->itemUpdate(this, pluginName()); });
connect(m_refershTimer, &QTimer::timeout, this, &DatetimePlugin::updateCurrentTimeString);
calendarWidget = new CalendarWidget;
int i = m_settings.value("FirstDayOfWeek", 0).toInt();
if (i == 0)
calendarWidget->calendar->setFirstDayOfWeek(Qt::Monday);
else if (i == 1)
calendarWidget->calendar->setFirstDayOfWeek(Qt::Sunday);
QString sfont = m_settings.value("Font", "").toString();
if (sfont != "") {
QStringList SL_font = sfont.split(",");
QFont font1(SL_font.at(0), SL_font.at(1).toInt(), SL_font.at(2).toInt(), SL_font.at(3).toInt());
font = font1;
m_dateTipsLabel->setFont(font);
}
}
const QString DatetimePlugin::pluginName() const
{
//return "datetime1";
return "datetime"; //宽度不足hack
}
const QString DatetimePlugin::pluginDisplayName() const
{
return tr("日期时间");
}
void DatetimePlugin::init(PluginProxyInterface *proxyInter)
{
m_proxyInter = proxyInter;
if (m_centralWidget->enabled())
//if (!pluginIsDisable())
m_proxyInter->itemAdded(this, pluginName());
}
void DatetimePlugin::pluginStateSwitched()
{
//m_centralWidget->setEnabled(!m_centralWidget->enabled());
// 获取当前禁用状态的反值作为新的状态值
const bool disabledNew = !pluginIsDisable();
// 存储新的状态值
m_proxyInter->saveValue(this, "disabled", disabledNew);
if (disabledNew)
m_proxyInter->itemAdded(this, pluginName());
else
m_proxyInter->itemRemoved(this, pluginName());
}
bool DatetimePlugin::pluginIsDisable()
{
//return !m_centralWidget->enabled();
return m_proxyInter->getValue(this, "disabled", false).toBool();
}
int DatetimePlugin::itemSortKey(const QString &itemKey)
{
Q_UNUSED(itemKey);
const QString key = QString("pos_%1").arg(displayMode());
return m_settings.value(key, 0).toInt();
}
void DatetimePlugin::setSortKey(const QString &itemKey, const int order)
{
Q_UNUSED(itemKey);
const QString key = QString("pos_%1").arg(displayMode());
m_settings.setValue(key, order);
}
QWidget *DatetimePlugin::itemWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
return m_centralWidget;
}
QWidget *DatetimePlugin::itemTipsWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
return m_dateTipsLabel;
}
QWidget *DatetimePlugin::itemPopupApplet(const QString &itemKey)
{
Q_UNUSED(itemKey);
return calendarWidget;
}
const QString DatetimePlugin::itemContextMenu(const QString &itemKey)
{
Q_UNUSED(itemKey);
QList<QVariant> items;
items.reserve(4);
QMap<QString, QVariant> set;
set["itemId"] = "set";
set["itemText"] = tr("Set");
set["isActive"] = true;
items.push_back(set);
QMap<QString, QVariant> settings;
settings["itemId"] = "settings";
if (m_centralWidget->is24HourFormat())
settings["itemText"] = tr("12 Hour Time");
else
settings["itemText"] = tr("24 Hour Time");
settings["isActive"] = true;
items.push_back(settings);
QMap<QString, QVariant> open;
open["itemId"] = "open";
open["itemText"] = tr("Time Settings");
open["isActive"] = true;
items.push_back(open);
QMap<QString, QVariant> clock;
clock["itemId"] = "clock";
clock["itemText"] = tr("Clock");
clock["isActive"] = true;
items.push_back(clock);
QMap<QString, QVariant> menu;
menu["items"] = items;
menu["checkableMenu"] = false;
menu["singleCheck"] = false;
return QJsonDocument::fromVariant(menu).toJson();
}
void DatetimePlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
{
Q_UNUSED(itemKey)
Q_UNUSED(checked)
if (menuId == "open")
QProcess::startDetached("dbus-send --print-reply --dest=com.deepin.dde.ControlCenter /com/deepin/dde/ControlCenter com.deepin.dde.ControlCenter.ShowModule \"string:datetime\"");
if (menuId == "clock")
m_centralWidget->toggleClock();
if (menuId == "set")
set();
if (menuId == "settings")
m_centralWidget->toggleHourFormat();
}
void DatetimePlugin::updateCurrentTimeString()
{
const QDateTime currentDateTime = QDateTime::currentDateTime();
if (m_settings.value("tooltip", "").toString() == "") {
if (m_centralWidget->is24HourFormat())
m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" HH:mm:ss"));
else
m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" hh:mm:ss A"));
} else {
QString s = m_settings.value("tooltip", "").toString();
if (s.contains("[") && s.contains("]") && s.indexOf("[") < s.indexOf("]")) {
QString stime1 = s.mid(s.indexOf("[")+1, s.indexOf("]") - s.indexOf("[") -1);
QDateTime time1 = QDateTime::fromString(stime1, "yyyy-MM-dd hh:mm:ss");
QDateTime time2 = QDateTime::currentDateTime();
qint64 secs;
if(time1 > time2)
secs = time2.secsTo(time1);
else
secs = time1.secsTo(time2);
QTime t(0,0,0);
t = t.addSecs(secs);
qint64 days = secs/60/60/24;
QString sd = " ";
if (days != 0)
sd += QString::number(days) + " 天";
sd += t.toString(" h 时 m 分 s 秒");
if (time1 > time2)
s.insert(s.indexOf("["), tr("还有"));
else
s.insert(s.indexOf("["), tr("已经过去"));
QString stip = s.replace(QRegExp("\\[.*\\]"), sd);
m_dateTipsLabel->setText(stip.replace("\\n", "\n"));
} else {
m_dateTipsLabel->setText(s.replace("\\n", "\n"));
}
}
m_dateTipsLabel->setFont(font);
const QString currentString = currentDateTime.toString("mm");
//if (currentString == m_currentTimeString)
// return;
m_currentTimeString = currentString;
m_centralWidget->update();
}
void DatetimePlugin::set()
{
QDialog *dialog = new QDialog;
dialog->setWindowTitle(tr("Set"));
dialog->setFixedWidth(400);
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
QLabel *label = new QLabel(tr("Format"));
hbox->addWidget(label);
QLineEdit *lineEdit_format = new QLineEdit;
lineEdit_format->setText(m_settings.value("format", "yyyy/M/d\\nHH:mm ddd").toString());
hbox->addWidget(lineEdit_format);
vbox->addLayout(hbox);
hbox = new QHBoxLayout;
label = new QLabel(tr("Such as"));
hbox->addWidget(label);
label = new QLabel("yyyy/M/d\\nHH:mm ddd\nHH:mm ddd\\nyyyy/M/d");
hbox->addWidget(label);
vbox->addLayout(hbox);
hbox = new QHBoxLayout;
label = new QLabel(tr("Tooltip"));
hbox->addWidget(label);
QLineEdit *lineEdit_tooltip = new QLineEdit;
lineEdit_tooltip->setText(m_settings.value("tooltip", "").toString());
hbox->addWidget(lineEdit_tooltip);
vbox->addLayout(hbox);
QLineEdit *lineEdit = new QLineEdit(tr("[2020-01-25 00:00:00] format will display a count down to now"));
lineEdit->setReadOnly(true);
vbox->addWidget(lineEdit);
hbox = new QHBoxLayout;
label = new QLabel(tr("Font"));
hbox->addWidget(label);
QString sfont0 = m_centralWidget->font().family() + "," + QString::number(m_centralWidget->font().pointSize()) + "," + QString::number(m_centralWidget->font().weight()) + "," + m_centralWidget->font().italic();
QString sfont = m_settings.value("Font", sfont0).toString();
QPushButton *pushButton_font = new QPushButton(sfont);
connect(pushButton_font, &QPushButton::pressed, [=]{
bool ok;
QStringList SL_font = sfont.split(",");
QFont font1(SL_font.at(0), SL_font.at(1).toInt(), SL_font.at(2).toInt(), SL_font.at(3).toInt());
font1 = QFontDialog::getFont(&ok, font, dialog, tr("Choose font"));
if (ok) {
font = font1;
m_centralWidget->setFont(font);
m_dateTipsLabel->setFont(font);
QString sfont = font.family() + "," + QString::number(font.pointSize()) + "," + QString::number(font.weight()) + "," + font.italic();
pushButton_font->setText(sfont);
m_settings.setValue("Font", sfont);
}
});
hbox->addWidget(pushButton_font);
QColor color(m_settings.value("FontColor", "#FFFFFF").toString());
QPushButton *pushButton_font_color = new QPushButton("■");
pushButton_font_color->setStyleSheet("color:" + color.name());
connect(pushButton_font_color, &QPushButton::pressed, [=]{
QColor color1(m_settings.value("FontColor", "#FFFFFF").toString());
color1 = QColorDialog::getColor(color1, dialog);
if (color1.isValid()) {
m_centralWidget->color = color1;
m_dateTipsLabel->setStyleSheet("color:" + color1.name() + "; padding:0px 3px;");
pushButton_font_color->setStyleSheet("color:" + color1.name());
m_settings.setValue("FontColor", color1.name());
}
});
hbox->addWidget(pushButton_font_color);
vbox->addLayout(hbox);
hbox = new QHBoxLayout;
label = new QLabel(tr("First day of week"));
hbox->addWidget(label);
QComboBox *comboBox_firstDayOfWeek = new QComboBox;
QStringList SL;
SL << tr("Monday") << tr("Sunday");
comboBox_firstDayOfWeek->addItems(SL);
comboBox_firstDayOfWeek->setCurrentIndex(m_settings.value("FirstDayOfWeek", 0).toInt());
hbox->addWidget(comboBox_firstDayOfWeek);
vbox->addLayout(hbox);
hbox = new QHBoxLayout;
hbox->addStretch();
QPushButton *pushButton_confirm = new QPushButton(tr("Confirm"));
QPushButton *pushButton_cancel = new QPushButton(tr("Cancel"));
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
if (dialog->exec() == QDialog::Accepted) {
m_settings.setValue("format", lineEdit_format->text());
m_settings.setValue("tooltip", lineEdit_tooltip->text());
int i = comboBox_firstDayOfWeek->currentIndex();
if (i == 0)
calendarWidget->calendar->setFirstDayOfWeek(Qt::Monday);
else if (i == 1)
calendarWidget->calendar->setFirstDayOfWeek(Qt::Sunday);
m_settings.setValue("FirstDayOfWeek", i);
m_centralWidget->update();
}
dialog->close();
}
1
https://gitee.com/sonichy/dde-dock-datetime.git
git@gitee.com:sonichy/dde-dock-datetime.git
sonichy
dde-dock-datetime
dde-dock-datetime
master

搜索帮助