chore: 调整格式化配置

This commit is contained in:
sleepwithoutbz
2025-09-16 17:07:56 +08:00
parent e172afa4cc
commit 16c1e759ff
6 changed files with 236 additions and 217 deletions

View File

@@ -19,7 +19,7 @@ AccessModifierOffset: -4
IndentCaseLabels: false IndentCaseLabels: false
# 要使用的预处理器指令缩进样式 # 要使用的预处理器指令缩进样式
IndentPPDirectives: BeforeHash IndentPPDirectives: Leave
# 缩进宽度 # 缩进宽度
IndentWidth: 4 IndentWidth: 4

2
.gitignore vendored
View File

@@ -1,4 +1,6 @@
build/ build/
Debug/
Release/
.cache/ .cache/
.vs/ .vs/
.vscode/ .vscode/

View File

@@ -2,7 +2,7 @@
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(systray LANGUAGES CXX) project(tido LANGUAGES CXX)
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
@@ -10,41 +10,42 @@ qt_standard_project_setup()
# 导出src下的cpp文件 # 导出src下的cpp文件
file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS src/*.cpp) file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS src/*.cpp)
qt_add_executable(systray ${SRC_FILES}) qt_add_executable(tido ${SRC_FILES})
set_target_properties(systray PROPERTIES set_target_properties(tido PROPERTIES
WIN32_EXECUTABLE TRUE # 控制是否显示终端为TRUE则不显示
WIN32_EXECUTABLE FALSE
MACOSX_BUNDLE TRUE MACOSX_BUNDLE TRUE
) )
target_link_libraries(systray PRIVATE target_link_libraries(tido PRIVATE
Qt6::Core Qt6::Core
Qt6::Gui Qt6::Gui
Qt6::Widgets Qt6::Widgets
) )
# Resources: # Resources:
set(systray_resource_files set(tido_resource_files
"images/bad.png" "images/bad.png"
"images/heart.png" "images/heart.png"
"images/trash.png" "images/trash.png"
) )
qt_add_resources(systray "systray" qt_add_resources(tido "tido"
PREFIX PREFIX
"/" "/"
FILES FILES
${systray_resource_files} ${tido_resource_files}
) )
install(TARGETS systray install(TARGETS tido
BUNDLE DESTINATION . BUNDLE DESTINATION .
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
) )
qt_generate_deploy_app_script( qt_generate_deploy_app_script(
TARGET systray TARGET tido
OUTPUT_SCRIPT deploy_script OUTPUT_SCRIPT deploy_script
NO_UNSUPPORTED_PLATFORM_ERROR NO_UNSUPPORTED_PLATFORM_ERROR
) )

View File

@@ -2,45 +2,45 @@
#ifndef QT_NO_SYSTEMTRAYICON #ifndef QT_NO_SYSTEMTRAYICON
#include "window.h" #include "window.h"
#include <QMessageBox> #include <QMessageBox>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QApplication app(argc, argv); QApplication app(argc, argv);
if (!QSystemTrayIcon::isSystemTrayAvailable()) { if (!QSystemTrayIcon::isSystemTrayAvailable()) {
auto choice = auto choice = QMessageBox::critical(
QMessageBox::critical(nullptr, QObject::tr("Systray"), nullptr, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray on this system."), QObject::tr("I couldn't detect any system tray on this system."),
QMessageBox::Close | QMessageBox::Ignore); QMessageBox::Close | QMessageBox::Ignore);
if (choice == QMessageBox::Close) if (choice == QMessageBox::Close)
return 1; return 1;
// Otherwise "lurk": if a system tray is started later, the icon will // Otherwise "lurk": if a system tray is started later, the icon will
// appear. // appear.
} }
QApplication::setQuitOnLastWindowClosed(false); QApplication::setQuitOnLastWindowClosed(false);
Window window; Window window;
window.show(); window.show();
return app.exec(); return app.exec();
} }
#else #else
#include <QDebug> #include <QDebug>
#include <QLabel> #include <QLabel>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
QApplication app(argc, argv); QApplication app(argc, argv);
QString text("QSystemTrayIcon is not supported on this platform"); QString text("QSystemTrayIcon is not supported on this platform");
QLabel *label = new QLabel(text); QLabel *label = new QLabel(text);
label->setWordWrap(true); label->setWordWrap(true);
label->show(); label->show();
qDebug() << text; qDebug() << text;
app.exec(); app.exec();
} }
#endif #endif

View File

@@ -2,226 +2,242 @@
#ifndef QT_NO_SYSTEMTRAYICON #ifndef QT_NO_SYSTEMTRAYICON
#include <QAction> #include <QAction>
#include <QCheckBox> #include <QCheckBox>
#include <QCloseEvent> #include <QCloseEvent>
#include <QComboBox> #include <QComboBox>
#include <QCoreApplication> #include <QCoreApplication>
#include <QGroupBox> #include <QGroupBox>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QPushButton> #include <QPushButton>
#include <QSpinBox> #include <QSpinBox>
#include <QTextEdit> #include <QTextEdit>
#include <QVBoxLayout> #include <QVBoxLayout>
//! [0] //! [0]
Window::Window() { Window::Window() {
createIconGroupBox(); createIconGroupBox();
createMessageGroupBox(); createMessageGroupBox();
iconLabel->setMinimumWidth(durationLabel->sizeHint().width()); iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
createActions(); createActions();
createTrayIcon(); createTrayIcon();
connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage); connect(showMessageButton, &QAbstractButton::clicked, this,
connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible); &Window::showMessage);
connect(iconComboBox, &QComboBox::currentIndexChanged, this, &Window::setIcon); connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon,
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked); &QSystemTrayIcon::setVisible);
connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated); connect(iconComboBox, &QComboBox::currentIndexChanged, this,
&Window::setIcon);
connect(trayIcon, &QSystemTrayIcon::messageClicked, this,
&Window::messageClicked);
connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);
QVBoxLayout *mainLayout = new QVBoxLayout; QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(iconGroupBox); mainLayout->addWidget(iconGroupBox);
mainLayout->addWidget(messageGroupBox); mainLayout->addWidget(messageGroupBox);
setLayout(mainLayout); setLayout(mainLayout);
iconComboBox->setCurrentIndex(1); iconComboBox->setCurrentIndex(1);
trayIcon->show(); trayIcon->show();
setWindowTitle(tr("Systray")); setWindowTitle(tr("Tido"));
resize(400, 300); resize(400, 300);
} }
//! [0] //! [0]
//! [1] //! [1]
void Window::setVisible(bool visible) { void Window::setVisible(bool visible) {
minimizeAction->setEnabled(visible); minimizeAction->setEnabled(visible);
maximizeAction->setEnabled(!isMaximized()); maximizeAction->setEnabled(!isMaximized());
restoreAction->setEnabled(isMaximized() || !visible); restoreAction->setEnabled(isMaximized() || !visible);
QDialog::setVisible(visible); QDialog::setVisible(visible);
} }
//! [1] //! [1]
//! [2] //! [2]
void Window::closeEvent(QCloseEvent *event) { void Window::closeEvent(QCloseEvent *event) {
if (!event->spontaneous() || !isVisible()) if (!event->spontaneous() || !isVisible())
return; return;
if (trayIcon->isVisible()) { if (trayIcon->isVisible()) {
QMessageBox::information(this, tr("Systray"), QMessageBox::StandardButtons reply =
tr("The program will keep running in the " QMessageBox::question(this, tr("关闭Tido"),
"system tray. To terminate the program, " tr("The program will keep running in the "
"choose <b>Quit</b> in the context menu " "system tray. To terminate the program, "
"of the system tray entry.")); "choose <b>Quit</b> in the context menu "
hide(); "of the system tray entry."),
event->ignore(); QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
qDebug() << "Button `Yes` was clicked";
hide();
} else if (reply == QMessageBox::No) {
qDebug() << "Button `No` was clicked";
} else {
qDebug() << "No button was clicked";
} }
event->ignore();
}
} }
//! [2] //! [2]
//! [3] //! [3]
void Window::setIcon(int index) { void Window::setIcon(int index) {
QIcon icon = iconComboBox->itemIcon(index); QIcon icon = iconComboBox->itemIcon(index);
trayIcon->setIcon(icon); trayIcon->setIcon(icon);
setWindowIcon(icon); setWindowIcon(icon);
trayIcon->setToolTip(iconComboBox->itemText(index)); trayIcon->setToolTip(iconComboBox->itemText(index));
} }
//! [3] //! [3]
//! [4] //! [4]
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason) { void Window::iconActivated(QSystemTrayIcon::ActivationReason reason) {
switch (reason) { switch (reason) {
case QSystemTrayIcon::Trigger: case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick: case QSystemTrayIcon::DoubleClick:
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count()); iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) %
break; iconComboBox->count());
case QSystemTrayIcon::MiddleClick: break;
showMessage(); case QSystemTrayIcon::MiddleClick:
break; showMessage();
default:; break;
} default:;
}
} }
//! [4] //! [4]
//! [5] //! [5]
void Window::showMessage() { void Window::showMessage() {
showIconCheckBox->setChecked(true); showIconCheckBox->setChecked(true);
int selectedIcon = typeComboBox->itemData(typeComboBox->currentIndex()).toInt(); int selectedIcon =
QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(selectedIcon); typeComboBox->itemData(typeComboBox->currentIndex()).toInt();
QSystemTrayIcon::MessageIcon msgIcon =
QSystemTrayIcon::MessageIcon(selectedIcon);
if (selectedIcon == -1) { // custom icon if (selectedIcon == -1) { // custom icon
QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex())); QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
durationSpinBox->value() * 1000); durationSpinBox->value() * 1000);
} else { } else {
trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon,
durationSpinBox->value() * 1000); durationSpinBox->value() * 1000);
} }
} }
//! [5] //! [5]
//! [6] //! [6]
void Window::messageClicked() { void Window::messageClicked() {
QMessageBox::information(nullptr, tr("Systray"), QMessageBox::information(nullptr, tr("tido"),
tr("Sorry, I already gave what help I could.\n" tr("Sorry, I already gave what help I could.\n"
"Maybe you should try asking a human?")); "Maybe you should try asking a human?"));
} }
//! [6] //! [6]
void Window::createIconGroupBox() { void Window::createIconGroupBox() {
iconGroupBox = new QGroupBox(tr("Tray Icon")); iconGroupBox = new QGroupBox(tr("托盘图标"));
iconLabel = new QLabel("Icon:"); iconLabel = new QLabel("Icon:");
iconComboBox = new QComboBox; iconComboBox = new QComboBox;
iconComboBox->addItem(QIcon(":/images/bad.png"), tr("Bad")); iconComboBox->addItem(QIcon(":/images/bad.png"), tr("Bad"));
iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart")); iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart"));
iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash")); iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash"));
showIconCheckBox = new QCheckBox(tr("Show icon")); showIconCheckBox = new QCheckBox(tr("Show icon"));
showIconCheckBox->setChecked(true); showIconCheckBox->setChecked(true);
QHBoxLayout *iconLayout = new QHBoxLayout; QHBoxLayout *iconLayout = new QHBoxLayout;
iconLayout->addWidget(iconLabel); iconLayout->addWidget(iconLabel);
iconLayout->addWidget(iconComboBox); iconLayout->addWidget(iconComboBox);
iconLayout->addStretch(); iconLayout->addStretch();
iconLayout->addWidget(showIconCheckBox); iconLayout->addWidget(showIconCheckBox);
iconGroupBox->setLayout(iconLayout); iconGroupBox->setLayout(iconLayout);
} }
void Window::createMessageGroupBox() { void Window::createMessageGroupBox() {
messageGroupBox = new QGroupBox(tr("Balloon Message")); messageGroupBox = new QGroupBox(tr("气泡消息"));
typeLabel = new QLabel(tr("Type:")); typeLabel = new QLabel(tr("Type:"));
typeComboBox = new QComboBox; typeComboBox = new QComboBox;
typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon); typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxInformation), typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxInformation),
tr("Information"), QSystemTrayIcon::Information); tr("Information"), QSystemTrayIcon::Information);
typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxWarning), tr("Warning"), typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxWarning),
QSystemTrayIcon::Warning); tr("Warning"), QSystemTrayIcon::Warning);
typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxCritical), tr("Critical"), typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxCritical),
QSystemTrayIcon::Critical); tr("Critical"), QSystemTrayIcon::Critical);
typeComboBox->addItem(QIcon(), tr("Custom icon"), -1); typeComboBox->addItem(QIcon(), tr("Custom icon"), -1);
typeComboBox->setCurrentIndex(1); typeComboBox->setCurrentIndex(1);
durationLabel = new QLabel(tr("Duration:")); durationLabel = new QLabel(tr("Duration:"));
durationSpinBox = new QSpinBox; durationSpinBox = new QSpinBox;
durationSpinBox->setRange(5, 60); durationSpinBox->setRange(5, 60);
durationSpinBox->setSuffix(" s"); durationSpinBox->setSuffix(" s");
durationSpinBox->setValue(15); durationSpinBox->setValue(15);
durationWarningLabel = new QLabel(tr("(some systems might ignore this " durationWarningLabel = new QLabel(tr("(some systems might ignore this "
"hint)")); "hint)"));
durationWarningLabel->setIndent(10); durationWarningLabel->setIndent(10);
titleLabel = new QLabel(tr("Title:")); titleLabel = new QLabel(tr("Title:"));
titleEdit = new QLineEdit(tr("Cannot connect to network")); titleEdit = new QLineEdit(tr("Cannot connect to network"));
bodyLabel = new QLabel(tr("Body:")); bodyLabel = new QLabel(tr("Body:"));
bodyEdit = new QTextEdit; bodyEdit = new QTextEdit;
bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a " bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
"clue.\nClick this balloon for details.")); "clue.\nClick this balloon for details."));
showMessageButton = new QPushButton(tr("Show Message")); showMessageButton = new QPushButton(tr("Show Message"));
showMessageButton->setDefault(true); showMessageButton->setDefault(true);
QGridLayout *messageLayout = new QGridLayout; QGridLayout *messageLayout = new QGridLayout;
messageLayout->addWidget(typeLabel, 0, 0); messageLayout->addWidget(typeLabel, 0, 0);
messageLayout->addWidget(typeComboBox, 0, 1, 1, 2); messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
messageLayout->addWidget(durationLabel, 1, 0); messageLayout->addWidget(durationLabel, 1, 0);
messageLayout->addWidget(durationSpinBox, 1, 1); messageLayout->addWidget(durationSpinBox, 1, 1);
messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3); messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
messageLayout->addWidget(titleLabel, 2, 0); messageLayout->addWidget(titleLabel, 2, 0);
messageLayout->addWidget(titleEdit, 2, 1, 1, 4); messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
messageLayout->addWidget(bodyLabel, 3, 0); messageLayout->addWidget(bodyLabel, 3, 0);
messageLayout->addWidget(bodyEdit, 3, 1, 2, 4); messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
messageLayout->addWidget(showMessageButton, 5, 4); messageLayout->addWidget(showMessageButton, 5, 4);
messageLayout->setColumnStretch(3, 1); messageLayout->setColumnStretch(3, 1);
messageLayout->setRowStretch(4, 1); messageLayout->setRowStretch(4, 1);
messageGroupBox->setLayout(messageLayout); messageGroupBox->setLayout(messageLayout);
} }
void Window::createActions() { void Window::createActions() {
minimizeAction = new QAction(tr("Mi&nimize"), this); minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, &QAction::triggered, this, &QWidget::hide); connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);
maximizeAction = new QAction(tr("Ma&ximize"), this); maximizeAction = new QAction(tr("Ma&ximize"), this);
connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized); connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);
restoreAction = new QAction(tr("&Restore"), this); restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal); connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);
quitAction = new QAction(tr("&Quit"), this); quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
} }
void Window::createTrayIcon() { void Window::createTrayIcon() {
trayIconMenu = new QMenu(this); trayIconMenu = new QMenu(this);
trayIconMenu->addAction(minimizeAction); trayIconMenu->addAction(minimizeAction);
trayIconMenu->addAction(maximizeAction); trayIconMenu->addAction(maximizeAction);
trayIconMenu->addAction(restoreAction); trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator(); trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction); trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this); trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu); trayIcon->setContextMenu(trayIconMenu);
} }
#endif #endif

View File

@@ -4,7 +4,7 @@
#ifndef QT_NO_SYSTEMTRAYICON #ifndef QT_NO_SYSTEMTRAYICON
#include <QDialog> #include <QDialog>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QAction; class QAction;
@@ -21,52 +21,52 @@ QT_END_NAMESPACE
//! [0] //! [0]
class Window : public QDialog { class Window : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
Window(); Window();
void setVisible(bool visible) override; void setVisible(bool visible) override;
protected: protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
private slots: private slots:
void setIcon(int index); void setIcon(int index);
void iconActivated(QSystemTrayIcon::ActivationReason reason); void iconActivated(QSystemTrayIcon::ActivationReason reason);
void showMessage(); void showMessage();
void messageClicked(); void messageClicked();
private: private:
void createIconGroupBox(); void createIconGroupBox();
void createMessageGroupBox(); void createMessageGroupBox();
void createActions(); void createActions();
void createTrayIcon(); void createTrayIcon();
QGroupBox *iconGroupBox; QGroupBox *iconGroupBox;
QLabel *iconLabel; QLabel *iconLabel;
QComboBox *iconComboBox; QComboBox *iconComboBox;
QCheckBox *showIconCheckBox; QCheckBox *showIconCheckBox;
QGroupBox *messageGroupBox; QGroupBox *messageGroupBox;
QLabel *typeLabel; QLabel *typeLabel;
QLabel *durationLabel; QLabel *durationLabel;
QLabel *durationWarningLabel; QLabel *durationWarningLabel;
QLabel *titleLabel; QLabel *titleLabel;
QLabel *bodyLabel; QLabel *bodyLabel;
QComboBox *typeComboBox; QComboBox *typeComboBox;
QSpinBox *durationSpinBox; QSpinBox *durationSpinBox;
QLineEdit *titleEdit; QLineEdit *titleEdit;
QTextEdit *bodyEdit; QTextEdit *bodyEdit;
QPushButton *showMessageButton; QPushButton *showMessageButton;
QAction *minimizeAction; QAction *minimizeAction;
QAction *maximizeAction; QAction *maximizeAction;
QAction *restoreAction; QAction *restoreAction;
QAction *quitAction; QAction *quitAction;
QSystemTrayIcon *trayIcon; QSystemTrayIcon *trayIcon;
QMenu *trayIconMenu; QMenu *trayIconMenu;
}; };
//! [0] //! [0]