Skip to content

Commit a24cf24

Browse files
committed
"Always on top" stateful in config
1 parent 1767629 commit a24cf24

3 files changed

Lines changed: 170 additions & 102 deletions

File tree

configurator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ Configurator::Configurator(QWidget *parent) :
8080

8181
continue;
8282
}
83+
if (tmp.startsWith("$always_on_top:")) { // ALWAYS ON TOP
84+
85+
// Delete comments after "//"
86+
int index = tmp.indexOf("//");
87+
if (index != -1) tmp = tmp.left(index);
88+
89+
tmp.replace("$always_on_top:", "");
90+
91+
m_always_on_top = tmp;
92+
93+
continue;
94+
}
8395

8496
symbol_list.append(tmp);
8597
}
@@ -149,6 +161,7 @@ void Configurator::on_applyButton_clicked()
149161
fout << "$position:" << m_widget_position << Qt::endl;
150162
fout << "$auto_update:" << m_auto_update << Qt::endl;
151163
fout << "$use_binance_us:" << m_use_binance_us << Qt::endl;
164+
fout << "$always_on_top:" << m_always_on_top << Qt::endl;
152165
fout << Qt::endl;
153166

154167
QVector<QString> symbol_list = ui->symbol_textEdit->toPlainText().split("\n");

configurator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private slots:
7878
QString m_use_binance_us;
7979
QString m_text_style;
8080
QString m_symbols_string;
81+
QString m_always_on_top;
8182

8283
WarningUi *warning_ui;
8384
};

main.cpp

Lines changed: 156 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,40 @@ void remove_soft_from_autorun()
249249
}
250250
*/
251251

252+
void replace_or_add_str_to_file (const QString& filename, const QString& search_str, const QString& new_str) {
253+
254+
QFile file(filename);
255+
256+
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
257+
qInfo() << "Failed to open file " << filename;
258+
return;
259+
}
260+
261+
bool string_found = false;
262+
263+
QTextStream in(&file);
264+
QString line = in.readLine();
265+
QString file_contents;
266+
267+
while (!line.isNull()) {
268+
if (line.startsWith(search_str)) {
269+
string_found = true;
270+
file_contents.append(new_str + "\n");
271+
} else {
272+
file_contents.append(line + "\n");
273+
}
274+
275+
line = in.readLine();
276+
}
277+
278+
if (!string_found) {
279+
file_contents.append(new_str + "\n");
280+
}
281+
282+
file.resize(0);
283+
in << file_contents;
284+
}
285+
252286
int main(int argc, char *argv[])
253287
{
254288
QApplication app(argc, argv);
@@ -282,113 +316,12 @@ int main(int argc, char *argv[])
282316
return 0;
283317
}
284318

285-
// Create a tray icon and a menu
286-
QSystemTrayIcon trayIcon;
287-
QMenu trayMenu;
288-
289-
// Remove the empty filed for Icons
290-
trayMenu.setStyleSheet("QMenu::item::icon{width: 0px;} QMenu::item::icon:disabled {width: 0px;} QMenu::item::icon:off {width: 0px;} QMenu::item::icon:on {width: 0px;} QMenu::item::icon:selected {width: 0px;} QMenu::item::icon:checked {width: 0px;} QMenu::indicator { width: 0px;}");
291-
292-
// Add a "Config" action to the menu
293-
Configurator configurator;
294-
QAction *configuratorAction = trayMenu.addAction("Edit Config");
295-
QObject::connect(configuratorAction, &QAction::triggered, &app, [&]() {
296-
297-
configurator.show();
298-
299-
});
300-
301-
// Add a "Edit Config" action to the menu
302-
// QAction *editAction = trayMenu.addAction("Edit Config");
303-
// QObject::connect(editAction, &QAction::triggered, &app, [&]() {
304-
// QProcess::startDetached("notepad.exe", {CONFIG_NAME});
305-
// });
306-
307-
// Add a "Restart App" action to the menu
308-
QAction *restartAction = trayMenu.addAction("Restart App");
309-
QObject::connect(restartAction, &QAction::triggered, &app, [&]() {
310-
// Start a new instance of the application
311-
QProcess::startDetached(qApp->applicationFilePath());
312-
313-
// Close the current instance
314-
qApp->quit();
315-
});
316-
317-
// Add a "Autorun" action with CheckBox to the menu
318-
QAction *autorunAction = trayMenu.addAction("Autorun");
319-
autorunAction->setCheckable(true);
320-
autorunAction->setChecked(is_soft_in_autorun());
321-
// Add the action to toggle "Autorun" to the tray menu
322-
QObject::connect(autorunAction, &QAction::toggled, [&](bool checked) {
323-
if (checked) {
324-
add_soft_to_autorun(app);
325-
} else {
326-
remove_soft_from_autorun();
327-
}
328-
});
329-
330-
331-
// Add a "Always on top" action with CheckBox to the menu
332-
QAction *always_on_topAction = trayMenu.addAction("Always on top");
333-
always_on_topAction->setCheckable(true);
334-
always_on_topAction->setChecked(false);
335-
// Add the action to toggle "Always on top" to the tray menu
336-
QObject::connect(always_on_topAction, &QAction::toggled, [&](bool checked) {
337-
if (checked) {
338-
mainWindow.setWindowFlag(Qt::WindowStaysOnTopHint, true);
339-
} else {
340-
mainWindow.setWindowFlag(Qt::WindowStaysOnTopHint, false);
341-
}
342-
mainWindow.show();
343-
});
344-
345-
// Separator
346-
QAction* separator_1 = new QAction();
347-
separator_1->setSeparator(true);
348-
trayMenu.addAction(separator_1);
349-
350-
// Add an "About Qt" action to the menu
351-
AboutMe AboutMe;
352-
QAction *aboutMeAction = trayMenu.addAction("About Me");
353-
QObject::connect(aboutMeAction, &QAction::triggered, qApp, [&](){
354-
AboutMe.show();
355-
});
356-
357-
// Add an "About Qt" action to the menu
358-
AboutQt AboutQt;
359-
QAction *aboutQtAction = trayMenu.addAction("About Qt");
360-
QObject::connect(aboutQtAction, &QAction::triggered, qApp, [&](){
361-
AboutQt.show();
362-
// QMessageBox aboutQtBox;
363-
// aboutQtBox.setWindowIcon(QPixmap(":/img/icon-bg.svg"));
364-
// aboutQtBox.setWindowTitle("About Qt — " + SOFT_NAME + " " + SOFT_VERSION);
365-
// QMessageBox::aboutQt(&aboutQtBox);
366-
});
367-
368-
// Separator
369-
QAction* separator_2 = new QAction();
370-
separator_2->setSeparator(true);
371-
trayMenu.addAction(separator_2);
372-
373-
// Add a "Close" action to the menu
374-
QAction *closeAction = trayMenu.addAction("Close");
375-
QObject::connect(closeAction, &QAction::triggered, &app, &QApplication::quit);
376-
377-
// Set the tray icon and menu
378-
trayIcon.setContextMenu(&trayMenu);
379-
380-
QString pixmapPath = ":/img/icon.svg";
381-
QPixmap pixmap(pixmapPath);
382-
QIcon icon(pixmap);
383-
trayIcon.setIcon(icon);
384-
385-
trayIcon.show();
386-
387319
QString text_style = "color:rgba(255, 255, 255, 0.65);";
388320
QString widget_position = "1,0";
389321

390322
bool auto_update = 1;
391323
bool use_binance_us = 0;
324+
bool always_on_top = 0;
392325

393326
// DATA
394327
QVector<QString> symbol_list;
@@ -445,6 +378,18 @@ int main(int argc, char *argv[])
445378

446379
continue;
447380
}
381+
if (tmp.startsWith("$always_on_top:")) { // ALWAYS ON TOP
382+
383+
// Delete comments after "//"
384+
int index = tmp.indexOf("//");
385+
if (index != -1) tmp = tmp.left(index);
386+
387+
tmp.replace("$always_on_top:", "");
388+
389+
always_on_top = tmp.toInt();
390+
391+
continue;
392+
}
448393

449394
symbol_list.append(tmp);
450395
}
@@ -471,6 +416,7 @@ int main(int argc, char *argv[])
471416
fout << "$position:" << widget_position << Qt::endl;
472417
fout << "$auto_update:" << "1" << Qt::endl;
473418
fout << "$use_binance_us:" << "0" << Qt::endl;
419+
fout << "$always_on_top:" << "0" << Qt::endl;
474420
fout << Qt::endl;
475421

476422
for (QString symbol : symbol_list) {
@@ -490,6 +436,114 @@ int main(int argc, char *argv[])
490436
}
491437
}
492438

439+
// Tray Block
440+
// Create a tray icon and a menu
441+
QSystemTrayIcon trayIcon;
442+
QMenu trayMenu;
443+
444+
// Remove the empty filed for Icons
445+
trayMenu.setStyleSheet("QMenu::item::icon{width: 0px;} QMenu::item::icon:disabled {width: 0px;} QMenu::item::icon:off {width: 0px;} QMenu::item::icon:on {width: 0px;} QMenu::item::icon:selected {width: 0px;} QMenu::item::icon:checked {width: 0px;} QMenu::indicator { width: 0px;}");
446+
447+
// Add a "Config" action to the menu
448+
Configurator configurator;
449+
QAction *configuratorAction = trayMenu.addAction("Edit Config");
450+
QObject::connect(configuratorAction, &QAction::triggered, &app, [&]() {
451+
452+
configurator.show();
453+
454+
});
455+
456+
// Add a "Edit Config" action to the menu
457+
// QAction *editAction = trayMenu.addAction("Edit Config");
458+
// QObject::connect(editAction, &QAction::triggered, &app, [&]() {
459+
// QProcess::startDetached("notepad.exe", {CONFIG_NAME});
460+
// });
461+
462+
// Add a "Restart App" action to the menu
463+
QAction *restartAction = trayMenu.addAction("Restart App");
464+
QObject::connect(restartAction, &QAction::triggered, &app, [&]() {
465+
// Start a new instance of the application
466+
QProcess::startDetached(qApp->applicationFilePath());
467+
468+
// Close the current instance
469+
qApp->quit();
470+
});
471+
472+
// Add a "Autorun" action with CheckBox to the menu
473+
QAction *autorunAction = trayMenu.addAction("Autorun");
474+
autorunAction->setCheckable(true);
475+
autorunAction->setChecked(is_soft_in_autorun());
476+
// Add the action to toggle "Autorun" to the tray menu
477+
QObject::connect(autorunAction, &QAction::toggled, [&](bool checked) {
478+
if (checked) {
479+
add_soft_to_autorun(app);
480+
} else {
481+
remove_soft_from_autorun();
482+
}
483+
});
484+
485+
// Add a "Always on top" action with CheckBox to the menu
486+
QAction *always_on_topAction = trayMenu.addAction("Always on top");
487+
always_on_topAction->setCheckable(true);
488+
always_on_topAction->setChecked(always_on_top);
489+
if (always_on_top) { // Set current state for Qt::WindowStaysOnTopHint
490+
mainWindow.setWindowFlag(Qt::WindowStaysOnTopHint, true);
491+
mainWindow.show();
492+
}
493+
// Add the action to toggle "Always on top" to the tray menu
494+
QObject::connect(always_on_topAction, &QAction::toggled, [&](bool checked) {
495+
if (checked) {
496+
replace_or_add_str_to_file(CONFIG_NAME, "$always_on_top:0", "$always_on_top:1");
497+
mainWindow.setWindowFlag(Qt::WindowStaysOnTopHint, true);
498+
} else {
499+
replace_or_add_str_to_file(CONFIG_NAME, "$always_on_top:1", "$always_on_top:0");
500+
mainWindow.setWindowFlag(Qt::WindowStaysOnTopHint, false);
501+
}
502+
mainWindow.show();
503+
});
504+
505+
// Separator
506+
QAction* separator_1 = new QAction();
507+
separator_1->setSeparator(true);
508+
trayMenu.addAction(separator_1);
509+
510+
// Add an "About Qt" action to the menu
511+
AboutMe AboutMe;
512+
QAction *aboutMeAction = trayMenu.addAction("About Me");
513+
QObject::connect(aboutMeAction, &QAction::triggered, qApp, [&](){
514+
AboutMe.show();
515+
});
516+
517+
// Add an "About Qt" action to the menu
518+
AboutQt AboutQt;
519+
QAction *aboutQtAction = trayMenu.addAction("About Qt");
520+
QObject::connect(aboutQtAction, &QAction::triggered, qApp, [&](){
521+
AboutQt.show();
522+
// QMessageBox aboutQtBox;
523+
// aboutQtBox.setWindowIcon(QPixmap(":/img/icon-bg.svg"));
524+
// aboutQtBox.setWindowTitle("About Qt — " + SOFT_NAME + " " + SOFT_VERSION);
525+
// QMessageBox::aboutQt(&aboutQtBox);
526+
});
527+
528+
// Separator
529+
QAction* separator_2 = new QAction();
530+
separator_2->setSeparator(true);
531+
trayMenu.addAction(separator_2);
532+
533+
// Add a "Close" action to the menu
534+
QAction *closeAction = trayMenu.addAction("Close");
535+
QObject::connect(closeAction, &QAction::triggered, &app, &QApplication::quit);
536+
537+
// Set the tray icon and menu
538+
trayIcon.setContextMenu(&trayMenu);
539+
540+
QString pixmapPath = ":/img/icon.svg";
541+
QPixmap pixmap(pixmapPath);
542+
QIcon icon(pixmap);
543+
trayIcon.setIcon(icon);
544+
545+
trayIcon.show();
546+
493547
// Auto-Update Block
494548
UpdateInfo info = getUpdateInfo();
495549

0 commit comments

Comments
 (0)