-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
161 lines (137 loc) · 6.11 KB
/
mainwindow.cpp
File metadata and controls
161 lines (137 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "mainwindow.h"
#include "autostartmanager.hpp"
#include <QApplication>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Create central widget and main layout
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
// Title
QLabel *titleLabel = new QLabel("Auto-start Manager", this);
titleLabel->setStyleSheet("font-size: 16px; font-weight: bold; margin: 10px;");
mainLayout->addWidget(titleLabel);
// Status display
QLabel *statusLabel = new QLabel("Checking auto-start status...", this);
statusLabel->setAlignment(Qt::AlignCenter);
statusLabel->setFrameStyle(QFrame::Box);
statusLabel->setStyleSheet("padding: 10px; background-color: #f0f0f0; border-radius: 5px;");
mainLayout->addWidget(statusLabel);
// Auto-start checkbox
QCheckBox *autoRunCheckBox = new QCheckBox("Start automatically at system boot", this);
mainLayout->addWidget(autoRunCheckBox);
// Button area
QHBoxLayout *buttonLayout = new QHBoxLayout();
QPushButton *refreshButton = new QPushButton("Refresh Status", this);
buttonLayout->addWidget(refreshButton);
buttonLayout->addStretch();
QPushButton *aboutButton = new QPushButton("About", this);
buttonLayout->addWidget(aboutButton);
mainLayout->addLayout(buttonLayout);
// Information
QLabel *infoLabel
= new QLabel("Note: When auto-start is enabled, the application will automatically launch "
"when you log in to the system.\n"
"This feature is implemented differently across operating systems.",
this);
infoLabel->setStyleSheet("color: #666; font-size: 12px; margin-top: 20px;");
infoLabel->setWordWrap(true);
mainLayout->addWidget(infoLabel);
mainLayout->addStretch();
// Set window properties
setWindowTitle("Auto-start Example");
resize(550, 250);
// Helper lambda function to update status label
auto updateStatusLabel = [](QLabel *label, bool isAutoRun) {
if (isAutoRun) {
label->setText(
"✅ Auto-start is ENABLED - Application will start automatically at login");
label->setStyleSheet(
"padding: 10px; background-color: #e8f5e8; border-radius: 5px; color: #2e7d32;");
} else {
label->setText("❌ Auto-start is DISABLED - Application will not start automatically");
label->setStyleSheet(
"padding: 10px; background-color: #ffebee; border-radius: 5px; color: #c62828;");
}
// Add platform information
QString platformInfo;
#ifdef Q_OS_WINDOWS
platformInfo = " (Windows Registry)";
#elif defined(Q_OS_MACOS)
platformInfo = " (macOS LaunchAgents)";
#elif defined(Q_OS_LINUX)
platformInfo = " (Linux Auto-start)";
#endif
label->setText(label->text() + platformInfo);
};
// Initialize checkbox state
bool isAutoRun = Utils::isAutoRunStart();
autoRunCheckBox->setChecked(isAutoRun);
updateStatusLabel(statusLabel, isAutoRun);
// Connect signals and slots using lambdas
connect(autoRunCheckBox,
&QCheckBox::toggled,
this,
[this, statusLabel, updateStatusLabel](bool checked) {
// Show confirmation dialog
QMessageBox::StandardButton reply = QMessageBox::question(
this,
checked ? "Enable Auto-start" : "Disable Auto-start",
checked ? "Are you sure you want to enable auto-start? The "
"application will run automatically when you log in."
: "Are you sure you want to disable auto-start? The "
"application will no longer run automatically.",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (reply == QMessageBox::Yes) {
// Set auto-start state
Utils::setAutoRunStart(checked);
// Update status display
updateStatusLabel(statusLabel, checked);
// Show operation result
if (checked) {
QMessageBox::information(this, "Success", "Auto-start has been enabled!");
} else {
QMessageBox::information(this, "Success", "Auto-start has been disabled!");
}
} else {
// User canceled, revert checkbox state
QCheckBox *checkBox = qobject_cast<QCheckBox *>(sender());
if (checkBox) {
checkBox->blockSignals(true);
checkBox->setChecked(!checked);
checkBox->blockSignals(false);
}
}
});
connect(refreshButton,
&QPushButton::clicked,
this,
[this, statusLabel, autoRunCheckBox, updateStatusLabel]() {
bool isAutoRun = Utils::isAutoRunStart();
autoRunCheckBox->blockSignals(true);
autoRunCheckBox->setChecked(isAutoRun);
autoRunCheckBox->blockSignals(false);
updateStatusLabel(statusLabel, isAutoRun);
QMessageBox::information(this, "Refreshed", "Auto-start status has been refreshed!");
});
connect(aboutButton, &QPushButton::clicked, this, []() {
QMessageBox::about(nullptr,
"About",
"Auto-start Manager Example\n\n"
"Demonstrates how to implement cross-platform auto-start management in "
"a Qt application.\n"
"Supports Windows, macOS and Linux systems.");
});
// Initialize the status label
updateStatusLabel(statusLabel, isAutoRun);
}
MainWindow::~MainWindow() {}