diff --git a/.clang-format b/.clang-format index c59111e..b1412d8 100644 --- a/.clang-format +++ b/.clang-format @@ -19,7 +19,7 @@ AccessModifierOffset: -4 IndentCaseLabels: false # 要使用的预处理器指令缩进样式 -IndentPPDirectives: BeforeHash +IndentPPDirectives: Leave # 缩进宽度 IndentWidth: 4 diff --git a/.gitignore b/.gitignore index 4b7653d..2b5e8b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build/ +Debug/ +Release/ .cache/ .vs/ .vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt index f98d6bb..ceac410 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) -project(systray LANGUAGES CXX) +project(tido LANGUAGES CXX) find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) @@ -10,41 +10,42 @@ qt_standard_project_setup() # 导出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 - WIN32_EXECUTABLE TRUE +set_target_properties(tido PROPERTIES + # 控制是否显示终端,为TRUE则不显示 + WIN32_EXECUTABLE FALSE MACOSX_BUNDLE TRUE ) -target_link_libraries(systray PRIVATE +target_link_libraries(tido PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets ) # Resources: -set(systray_resource_files +set(tido_resource_files "images/bad.png" "images/heart.png" "images/trash.png" ) -qt_add_resources(systray "systray" +qt_add_resources(tido "tido" PREFIX "/" FILES - ${systray_resource_files} + ${tido_resource_files} ) -install(TARGETS systray +install(TARGETS tido BUNDLE DESTINATION . RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) qt_generate_deploy_app_script( - TARGET systray + TARGET tido OUTPUT_SCRIPT deploy_script NO_UNSUPPORTED_PLATFORM_ERROR ) diff --git a/src/main.cpp b/src/main.cpp index d15ede8..404b084 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,45 +2,45 @@ #ifndef QT_NO_SYSTEMTRAYICON - #include "window.h" - #include +#include "window.h" +#include int main(int argc, char *argv[]) { - QApplication app(argc, argv); + QApplication app(argc, argv); - if (!QSystemTrayIcon::isSystemTrayAvailable()) { - auto choice = - QMessageBox::critical(nullptr, QObject::tr("Systray"), - QObject::tr("I couldn't detect any system tray on this system."), - QMessageBox::Close | QMessageBox::Ignore); - if (choice == QMessageBox::Close) - return 1; - // Otherwise "lurk": if a system tray is started later, the icon will - // appear. - } - QApplication::setQuitOnLastWindowClosed(false); + if (!QSystemTrayIcon::isSystemTrayAvailable()) { + auto choice = QMessageBox::critical( + nullptr, QObject::tr("Systray"), + QObject::tr("I couldn't detect any system tray on this system."), + QMessageBox::Close | QMessageBox::Ignore); + if (choice == QMessageBox::Close) + return 1; + // Otherwise "lurk": if a system tray is started later, the icon will + // appear. + } + QApplication::setQuitOnLastWindowClosed(false); - Window window; - window.show(); - return app.exec(); + Window window; + window.show(); + return app.exec(); } #else - #include - #include +#include +#include int main(int argc, char *argv[]) { - QApplication app(argc, argv); - QString text("QSystemTrayIcon is not supported on this platform"); + QApplication app(argc, argv); + QString text("QSystemTrayIcon is not supported on this platform"); - QLabel *label = new QLabel(text); - label->setWordWrap(true); + QLabel *label = new QLabel(text); + label->setWordWrap(true); - label->show(); - qDebug() << text; + label->show(); + qDebug() << text; - app.exec(); + app.exec(); } #endif diff --git a/src/window.cpp b/src/window.cpp index 6499a85..f3f9f4d 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2,226 +2,242 @@ #ifndef QT_NO_SYSTEMTRAYICON - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include //! [0] Window::Window() { - createIconGroupBox(); - createMessageGroupBox(); + createIconGroupBox(); + createMessageGroupBox(); - iconLabel->setMinimumWidth(durationLabel->sizeHint().width()); + iconLabel->setMinimumWidth(durationLabel->sizeHint().width()); - createActions(); - createTrayIcon(); + createActions(); + createTrayIcon(); - connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage); - connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible); - connect(iconComboBox, &QComboBox::currentIndexChanged, this, &Window::setIcon); - connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked); - connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated); + connect(showMessageButton, &QAbstractButton::clicked, this, + &Window::showMessage); + connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, + &QSystemTrayIcon::setVisible); + 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; - mainLayout->addWidget(iconGroupBox); - mainLayout->addWidget(messageGroupBox); - setLayout(mainLayout); + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(iconGroupBox); + mainLayout->addWidget(messageGroupBox); + setLayout(mainLayout); - iconComboBox->setCurrentIndex(1); - trayIcon->show(); + iconComboBox->setCurrentIndex(1); + trayIcon->show(); - setWindowTitle(tr("Systray")); - resize(400, 300); + setWindowTitle(tr("Tido")); + resize(400, 300); } //! [0] //! [1] void Window::setVisible(bool visible) { - minimizeAction->setEnabled(visible); - maximizeAction->setEnabled(!isMaximized()); - restoreAction->setEnabled(isMaximized() || !visible); - QDialog::setVisible(visible); + minimizeAction->setEnabled(visible); + maximizeAction->setEnabled(!isMaximized()); + restoreAction->setEnabled(isMaximized() || !visible); + QDialog::setVisible(visible); } //! [1] //! [2] void Window::closeEvent(QCloseEvent *event) { - if (!event->spontaneous() || !isVisible()) - return; - if (trayIcon->isVisible()) { - QMessageBox::information(this, tr("Systray"), - tr("The program will keep running in the " - "system tray. To terminate the program, " - "choose Quit in the context menu " - "of the system tray entry.")); - hide(); - event->ignore(); + if (!event->spontaneous() || !isVisible()) + return; + if (trayIcon->isVisible()) { + QMessageBox::StandardButtons reply = + QMessageBox::question(this, tr("关闭Tido"), + tr("The program will keep running in the " + "system tray. To terminate the program, " + "choose Quit in the context menu " + "of the system tray entry."), + 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] //! [3] void Window::setIcon(int index) { - QIcon icon = iconComboBox->itemIcon(index); - trayIcon->setIcon(icon); - setWindowIcon(icon); + QIcon icon = iconComboBox->itemIcon(index); + trayIcon->setIcon(icon); + setWindowIcon(icon); - trayIcon->setToolTip(iconComboBox->itemText(index)); + trayIcon->setToolTip(iconComboBox->itemText(index)); } //! [3] //! [4] void Window::iconActivated(QSystemTrayIcon::ActivationReason reason) { - switch (reason) { - case QSystemTrayIcon::Trigger: - case QSystemTrayIcon::DoubleClick: - iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count()); - break; - case QSystemTrayIcon::MiddleClick: - showMessage(); - break; - default:; - } + switch (reason) { + case QSystemTrayIcon::Trigger: + case QSystemTrayIcon::DoubleClick: + iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % + iconComboBox->count()); + break; + case QSystemTrayIcon::MiddleClick: + showMessage(); + break; + default:; + } } //! [4] //! [5] void Window::showMessage() { - showIconCheckBox->setChecked(true); - int selectedIcon = typeComboBox->itemData(typeComboBox->currentIndex()).toInt(); - QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(selectedIcon); + showIconCheckBox->setChecked(true); + int selectedIcon = + typeComboBox->itemData(typeComboBox->currentIndex()).toInt(); + QSystemTrayIcon::MessageIcon msgIcon = + QSystemTrayIcon::MessageIcon(selectedIcon); - if (selectedIcon == -1) { // custom icon - QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex())); - trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, - durationSpinBox->value() * 1000); - } else { - trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, - durationSpinBox->value() * 1000); - } + if (selectedIcon == -1) { // custom icon + QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex())); + trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, + durationSpinBox->value() * 1000); + } else { + trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, + durationSpinBox->value() * 1000); + } } //! [5] //! [6] void Window::messageClicked() { - QMessageBox::information(nullptr, tr("Systray"), - tr("Sorry, I already gave what help I could.\n" - "Maybe you should try asking a human?")); + QMessageBox::information(nullptr, tr("tido"), + tr("Sorry, I already gave what help I could.\n" + "Maybe you should try asking a human?")); } //! [6] 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->addItem(QIcon(":/images/bad.png"), tr("Bad")); - iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart")); - iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash")); + iconComboBox = new QComboBox; + iconComboBox->addItem(QIcon(":/images/bad.png"), tr("Bad")); + iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart")); + iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash")); - showIconCheckBox = new QCheckBox(tr("Show icon")); - showIconCheckBox->setChecked(true); + showIconCheckBox = new QCheckBox(tr("Show icon")); + showIconCheckBox->setChecked(true); - QHBoxLayout *iconLayout = new QHBoxLayout; - iconLayout->addWidget(iconLabel); - iconLayout->addWidget(iconComboBox); - iconLayout->addStretch(); - iconLayout->addWidget(showIconCheckBox); - iconGroupBox->setLayout(iconLayout); + QHBoxLayout *iconLayout = new QHBoxLayout; + iconLayout->addWidget(iconLabel); + iconLayout->addWidget(iconComboBox); + iconLayout->addStretch(); + iconLayout->addWidget(showIconCheckBox); + iconGroupBox->setLayout(iconLayout); } 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->addItem(tr("None"), QSystemTrayIcon::NoIcon); - typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxInformation), - tr("Information"), QSystemTrayIcon::Information); - typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxWarning), tr("Warning"), - QSystemTrayIcon::Warning); - typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxCritical), tr("Critical"), - QSystemTrayIcon::Critical); - typeComboBox->addItem(QIcon(), tr("Custom icon"), -1); - typeComboBox->setCurrentIndex(1); + typeComboBox = new QComboBox; + typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon); + typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxInformation), + tr("Information"), QSystemTrayIcon::Information); + typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxWarning), + tr("Warning"), QSystemTrayIcon::Warning); + typeComboBox->addItem(style()->standardIcon(QStyle::SP_MessageBoxCritical), + tr("Critical"), QSystemTrayIcon::Critical); + typeComboBox->addItem(QIcon(), tr("Custom icon"), -1); + typeComboBox->setCurrentIndex(1); - durationLabel = new QLabel(tr("Duration:")); + durationLabel = new QLabel(tr("Duration:")); - durationSpinBox = new QSpinBox; - durationSpinBox->setRange(5, 60); - durationSpinBox->setSuffix(" s"); - durationSpinBox->setValue(15); + durationSpinBox = new QSpinBox; + durationSpinBox->setRange(5, 60); + durationSpinBox->setSuffix(" s"); + durationSpinBox->setValue(15); - durationWarningLabel = new QLabel(tr("(some systems might ignore this " - "hint)")); - durationWarningLabel->setIndent(10); + durationWarningLabel = new QLabel(tr("(some systems might ignore this " + "hint)")); + 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->setPlainText(tr("Don't believe me. Honestly, I don't have a " - "clue.\nClick this balloon for details.")); + bodyEdit = new QTextEdit; + bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a " + "clue.\nClick this balloon for details.")); - showMessageButton = new QPushButton(tr("Show Message")); - showMessageButton->setDefault(true); + showMessageButton = new QPushButton(tr("Show Message")); + showMessageButton->setDefault(true); - QGridLayout *messageLayout = new QGridLayout; - messageLayout->addWidget(typeLabel, 0, 0); - messageLayout->addWidget(typeComboBox, 0, 1, 1, 2); - messageLayout->addWidget(durationLabel, 1, 0); - messageLayout->addWidget(durationSpinBox, 1, 1); - messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3); - messageLayout->addWidget(titleLabel, 2, 0); - messageLayout->addWidget(titleEdit, 2, 1, 1, 4); - messageLayout->addWidget(bodyLabel, 3, 0); - messageLayout->addWidget(bodyEdit, 3, 1, 2, 4); - messageLayout->addWidget(showMessageButton, 5, 4); - messageLayout->setColumnStretch(3, 1); - messageLayout->setRowStretch(4, 1); - messageGroupBox->setLayout(messageLayout); + QGridLayout *messageLayout = new QGridLayout; + messageLayout->addWidget(typeLabel, 0, 0); + messageLayout->addWidget(typeComboBox, 0, 1, 1, 2); + messageLayout->addWidget(durationLabel, 1, 0); + messageLayout->addWidget(durationSpinBox, 1, 1); + messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3); + messageLayout->addWidget(titleLabel, 2, 0); + messageLayout->addWidget(titleEdit, 2, 1, 1, 4); + messageLayout->addWidget(bodyLabel, 3, 0); + messageLayout->addWidget(bodyEdit, 3, 1, 2, 4); + messageLayout->addWidget(showMessageButton, 5, 4); + messageLayout->setColumnStretch(3, 1); + messageLayout->setRowStretch(4, 1); + messageGroupBox->setLayout(messageLayout); } void Window::createActions() { - minimizeAction = new QAction(tr("Mi&nimize"), this); - connect(minimizeAction, &QAction::triggered, this, &QWidget::hide); + minimizeAction = new QAction(tr("Mi&nimize"), this); + connect(minimizeAction, &QAction::triggered, this, &QWidget::hide); - maximizeAction = new QAction(tr("Ma&ximize"), this); - connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized); + maximizeAction = new QAction(tr("Ma&ximize"), this); + connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized); - restoreAction = new QAction(tr("&Restore"), this); - connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal); + restoreAction = new QAction(tr("&Restore"), this); + connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal); - quitAction = new QAction(tr("&Quit"), this); - connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); + quitAction = new QAction(tr("&Quit"), this); + connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit); } void Window::createTrayIcon() { - trayIconMenu = new QMenu(this); - trayIconMenu->addAction(minimizeAction); - trayIconMenu->addAction(maximizeAction); - trayIconMenu->addAction(restoreAction); - trayIconMenu->addSeparator(); - trayIconMenu->addAction(quitAction); + trayIconMenu = new QMenu(this); + trayIconMenu->addAction(minimizeAction); + trayIconMenu->addAction(maximizeAction); + trayIconMenu->addAction(restoreAction); + trayIconMenu->addSeparator(); + trayIconMenu->addAction(quitAction); - trayIcon = new QSystemTrayIcon(this); - trayIcon->setContextMenu(trayIconMenu); + trayIcon = new QSystemTrayIcon(this); + trayIcon->setContextMenu(trayIconMenu); } #endif diff --git a/src/window.h b/src/window.h index 11a6e97..9cc80c0 100644 --- a/src/window.h +++ b/src/window.h @@ -4,7 +4,7 @@ #ifndef QT_NO_SYSTEMTRAYICON - #include +#include QT_BEGIN_NAMESPACE class QAction; @@ -21,52 +21,52 @@ QT_END_NAMESPACE //! [0] class Window : public QDialog { - Q_OBJECT + Q_OBJECT public: - Window(); + Window(); - void setVisible(bool visible) override; + void setVisible(bool visible) override; protected: - void closeEvent(QCloseEvent *event) override; + void closeEvent(QCloseEvent *event) override; private slots: - void setIcon(int index); - void iconActivated(QSystemTrayIcon::ActivationReason reason); - void showMessage(); - void messageClicked(); + void setIcon(int index); + void iconActivated(QSystemTrayIcon::ActivationReason reason); + void showMessage(); + void messageClicked(); private: - void createIconGroupBox(); - void createMessageGroupBox(); - void createActions(); - void createTrayIcon(); + void createIconGroupBox(); + void createMessageGroupBox(); + void createActions(); + void createTrayIcon(); - QGroupBox *iconGroupBox; - QLabel *iconLabel; - QComboBox *iconComboBox; - QCheckBox *showIconCheckBox; + QGroupBox *iconGroupBox; + QLabel *iconLabel; + QComboBox *iconComboBox; + QCheckBox *showIconCheckBox; - QGroupBox *messageGroupBox; - QLabel *typeLabel; - QLabel *durationLabel; - QLabel *durationWarningLabel; - QLabel *titleLabel; - QLabel *bodyLabel; - QComboBox *typeComboBox; - QSpinBox *durationSpinBox; - QLineEdit *titleEdit; - QTextEdit *bodyEdit; - QPushButton *showMessageButton; + QGroupBox *messageGroupBox; + QLabel *typeLabel; + QLabel *durationLabel; + QLabel *durationWarningLabel; + QLabel *titleLabel; + QLabel *bodyLabel; + QComboBox *typeComboBox; + QSpinBox *durationSpinBox; + QLineEdit *titleEdit; + QTextEdit *bodyEdit; + QPushButton *showMessageButton; - QAction *minimizeAction; - QAction *maximizeAction; - QAction *restoreAction; - QAction *quitAction; + QAction *minimizeAction; + QAction *maximizeAction; + QAction *restoreAction; + QAction *quitAction; - QSystemTrayIcon *trayIcon; - QMenu *trayIconMenu; + QSystemTrayIcon *trayIcon; + QMenu *trayIconMenu; }; //! [0]