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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
/* Copyright (c) 2013-2016, Stanislaw Halik <sthalik@misaki.pl>
* Permission to use, copy, modify, and/or distribute this
* software for any purpose with or without fee is hereby granted,
* provided that the above copyright notice and this permission
* notice appear in all copies.
*/
#include "main-window.hpp"
#include "logic/tracker.h"
#include "options/options.hpp"
#include "opentrack-library-path.h"
#include "new_file_dialog.h"
#include "migration/migration.hpp"
#include <QFile>
#include <QFileDialog>
#include <QDesktopServices>
#include <QCoreApplication>
#include <QApplication>
#include <QIcon>
#include <QString>
#include <QChar>
#include <QSignalBlocker>
#ifdef _WIN32
# include <windows.h>
#endif
extern "C" const char* opentrack_version;
MainWindow::MainWindow() :
State(OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH),
pose_update_timer(this),
kbd_quit(QKeySequence("Ctrl+Q"), this),
menu_action_header(&tray_menu),
menu_action_show(&tray_menu),
menu_action_exit(&tray_menu),
menu_action_tracker(&tray_menu),
menu_action_filter(&tray_menu),
menu_action_proto(&tray_menu),
menu_action_options(&tray_menu),
menu_action_mappings(&tray_menu)
{
ui.setupUi(this);
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
adjustSize();
setFixedSize(size());
setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | windowFlags());
}
updateButtonState(false, false);
if (group::ini_directory().size() == 0)
{
die_on_config_not_writable();
return;
}
if (!refresh_config_list())
return;
connect(ui.btnEditCurves, SIGNAL(clicked()), this, SLOT(showCurveConfiguration()));
connect(ui.btnShortcuts, SIGNAL(clicked()), this, SLOT(show_options_dialog()));
connect(ui.btnShowEngineControls, SIGNAL(clicked()), this, SLOT(showTrackerSettings()));
connect(ui.btnShowServerControls, SIGNAL(clicked()), this, SLOT(showProtocolSettings()));
connect(ui.btnShowFilterControls, SIGNAL(clicked()), this, SLOT(showFilterSettings()));
connect(ui.btnStartTracker, SIGNAL(clicked()), this, SLOT(startTracker()));
connect(ui.btnStopTracker, SIGNAL(clicked()), this, SLOT(stopTracker()));
connect(ui.iconcomboProfile, &QComboBox::currentTextChanged, this, [&](const QString& x) { set_profile(x); });
// fill dylib comboboxen
{
modules.filters().push_front(std::make_shared<dylib>("", dylib::Filter));
for (std::shared_ptr<dylib>& x : modules.trackers())
ui.iconcomboTrackerSource->addItem(x->icon, x->name);
for (std::shared_ptr<dylib>& x : modules.protocols())
ui.iconcomboProtocol->addItem(x->icon, x->name);
for (std::shared_ptr<dylib>& x : modules.filters())
ui.iconcomboFilter->addItem(x->icon, x->name);
}
// timers
connect(&config_list_timer, &QTimer::timeout, this, [this]() { refresh_config_list(); });
connect(&pose_update_timer, SIGNAL(timeout()), this, SLOT(showHeadPose()), Qt::DirectConnection);
connect(&det_timer, SIGNAL(timeout()), this, SLOT(maybe_start_profile_from_executable()));
// ctrl+q exits
connect(&kbd_quit, SIGNAL(activated()), this, SLOT(exit()));
// profile menu
{
profile_menu.addAction(tr("Create new empty config"), this, SLOT(make_empty_config()));
profile_menu.addAction(tr("Create new copied config"), this, SLOT(make_copied_config()));
profile_menu.addAction(tr("Open configuration directory"), this, SLOT(open_config_directory()));
ui.profile_button->setMenu(&profile_menu);
}
if (!progn(
const QString cur = group::ini_filename();
if (is_config_listed(cur))
return set_profile(cur);
else
return set_profile(OPENTRACK_DEFAULT_CONFIG);
))
return;
// only tie and connect main screen options after migrations are done
// below is fine, set_profile() is called already
// dylibs
{
connect(&m.tracker_dll,
static_cast<void(base_value::*)(const QString&) const>(&base_value::valueChanged),
this,
[&](const QString&) { if (pTrackerDialog) pTrackerDialog = nullptr; save_modules(); });
connect(&m.protocol_dll,
static_cast<void(base_value::*)(const QString&) const>(&base_value::valueChanged),
this,
[&](const QString&) { if (pProtocolDialog) pProtocolDialog = nullptr; save_modules(); });
connect(&m.filter_dll,
static_cast<void(base_value::*)(const QString&) const>(&base_value::valueChanged),
this,
[&](const QString&) { if (pFilterDialog) pFilterDialog = nullptr; save_modules(); });
}
tie_setting(m.tracker_dll, ui.iconcomboTrackerSource);
tie_setting(m.protocol_dll, ui.iconcomboProtocol);
tie_setting(m.filter_dll, ui.iconcomboFilter);
connect(this, &MainWindow::emit_start_tracker,
this, [&]() -> void { qDebug() << "start tracker"; startTracker(); },
Qt::QueuedConnection);
connect(this, &MainWindow::emit_stop_tracker,
this, [&]() -> void { qDebug() << "stop tracker"; stopTracker(); },
Qt::QueuedConnection);
connect(this, &MainWindow::emit_toggle_tracker,
this, [&]() -> void { qDebug() << "toggle tracker"; if (work) stopTracker(); else startTracker(); },
Qt::QueuedConnection);
connect(this, &MainWindow::emit_restart_tracker,
this, [&]() -> void { qDebug() << "restart tracker"; stopTracker(); startTracker(); },
Qt::QueuedConnection);
// tray
{
init_tray_menu();
connect(&s.tray_enabled,
static_cast<void (base_value::*)(bool) const>(&base_value::valueChanged),
this,
[&](bool) { ensure_tray(); });
ensure_tray();
}
register_shortcuts();
det_timer.start(1000);
config_list_timer.start(1000 * 5);
kbd_quit.setEnabled(true);
}
void MainWindow::init_tray_menu()
{
tray_menu.clear();
QString display_name(opentrack_version);
if (display_name.startsWith("opentrack-"))
{
display_name = tr("opentrack") + " " + display_name.mid(sizeof("opentrack-") - 1);
}
if (display_name.endsWith("-DEBUG"))
display_name.replace(display_name.size() - int(sizeof("DEBUG")), display_name.size(), tr(" (debug)"));
menu_action_header.setEnabled(false);
menu_action_header.setText(display_name);
menu_action_header.setIcon(QIcon(":/images/facetracknoir.png"));
tray_menu.addAction(&menu_action_header);
menu_action_show.setIconVisibleInMenu(true);
menu_action_show.setText(isHidden() ? tr("Show the Octopus") : tr("Hide the Octopus"));
menu_action_show.setIcon(QIcon(":/images/facetracknoir.png"));
QObject::connect(&menu_action_show, &QAction::triggered, this, [&]() { toggle_restore_from_tray(QSystemTrayIcon::Trigger); });
tray_menu.addAction(&menu_action_show);
tray_menu.addSeparator();
menu_action_tracker.setText(tr("Tracker settings"));
menu_action_tracker.setIcon(QIcon(":/images/tools.png"));
QObject::connect(&menu_action_tracker, &QAction::triggered, this, &MainWindow::showTrackerSettings);
tray_menu.addAction(&menu_action_tracker);
menu_action_filter.setText(tr("Filter settings"));
menu_action_filter.setIcon(QIcon(":/images/filter-16.png"));
QObject::connect(&menu_action_filter, &QAction::triggered, this, &MainWindow::showFilterSettings);
tray_menu.addAction(&menu_action_filter);
menu_action_proto.setText(tr("Protocol settings"));
menu_action_proto.setIcon(QIcon(":/images/settings16.png"));
QObject::connect(&menu_action_proto, &QAction::triggered, this, &MainWindow::showProtocolSettings);
tray_menu.addAction(&menu_action_proto);
tray_menu.addSeparator();
menu_action_mappings.setIcon(QIcon(":/images/curves.png"));
menu_action_mappings.setText(tr("Mappings"));
QObject::connect(&menu_action_mappings, &QAction::triggered, this, &MainWindow::showCurveConfiguration);
tray_menu.addAction(&menu_action_mappings);
menu_action_options.setIcon(QIcon(":/images/tools.png"));
menu_action_options.setText(tr("Options"));
QObject::connect(&menu_action_options, &QAction::triggered, this, &MainWindow::show_options_dialog);
tray_menu.addAction(&menu_action_options);
tray_menu.addSeparator();
menu_action_exit.setText(tr("Exit"));
QObject::connect(&menu_action_exit, &QAction::triggered, this, &MainWindow::exit);
tray_menu.addAction(&menu_action_exit);
}
void MainWindow::register_shortcuts()
{
using t_key = Shortcuts::t_key;
using t_keys = Shortcuts::t_keys;
t_keys keys
{
t_key(s.key_start_tracking1, [&](bool) -> void { emit_start_tracker(); }, true),
t_key(s.key_start_tracking2, [&](bool) -> void { emit_start_tracker(); }, true),
t_key(s.key_stop_tracking1, [&](bool) -> void { emit_stop_tracker(); }, true),
t_key(s.key_stop_tracking2, [&](bool) -> void { emit_stop_tracker(); }, true),
t_key(s.key_toggle_tracking1, [&](bool) -> void { emit_toggle_tracker(); }, true),
t_key(s.key_toggle_tracking2, [&](bool) -> void { emit_toggle_tracker(); }, true),
t_key(s.key_restart_tracking1, [&](bool) -> void { emit_restart_tracker(); }, true),
t_key(s.key_restart_tracking2, [&](bool) -> void { emit_restart_tracker(); }, true),
};
global_shortcuts.reload(keys);
if (work)
work->reload_shortcuts();
}
void MainWindow::die_on_config_not_writable()
{
stopTracker();
static const QString pad(16, QChar(' '));
QMessageBox::critical(this,
tr("The Octopus is sad"),
tr("Check permissions for your .ini directory:\n\n\"%1\"%2\n\nExiting now.").arg(group::ini_directory()).arg(pad),
QMessageBox::Close, QMessageBox::NoButton);
// signals main() to short-circuit
if (!isVisible())
setEnabled(false);
setVisible(false);
// tray related
qApp->setQuitOnLastWindowClosed(true);
close();
}
bool MainWindow::maybe_die_on_config_not_writable(const QString& current, QStringList* ini_list_)
{
const bool writable =
group::with_settings_object([&](QSettings& s) {
return s.isWritable();
});
if (writable)
return false;
const bool open = QFile(group::ini_combine(current)).open(QFile::ReadWrite);
const QStringList ini_list = group::ini_list();
if (!ini_list.contains(current) || !open)
{
die_on_config_not_writable();
return true;
}
if (ini_list_ != nullptr)
*ini_list_ = ini_list;
return false;
}
bool MainWindow::get_new_config_name_from_dialog(QString& ret)
{
new_file_dialog dlg;
dlg.exec();
return dlg.is_ok(ret);
}
MainWindow::~MainWindow()
{
if (tray)
tray->hide();
stopTracker();
}
void MainWindow::set_working_directory()
{
QDir::setCurrent(OPENTRACK_BASE_PATH);
}
void MainWindow::save_modules()
{
m.b->save();
}
void MainWindow::make_empty_config()
{
QString name;
if (get_new_config_name_from_dialog(name))
{
QFile(group::ini_combine(name)).open(QFile::ReadWrite);
if (!refresh_config_list())
return;
if (is_config_listed(name))
{
QSignalBlocker q(ui.iconcomboProfile);
if (!set_profile(name))
return;
mark_config_as_not_needing_migration();
}
}
}
void MainWindow::make_copied_config()
{
const QString cur = group::ini_pathname();
QString name;
if (cur != "" && get_new_config_name_from_dialog(name))
{
const QString new_name = group::ini_combine(name);
(void) QFile::remove(new_name);
QFile::copy(cur, new_name);
if (!refresh_config_list())
return;
if (is_config_listed(name))
{
QSignalBlocker q(ui.iconcomboProfile);
if (!set_profile(name))
return;
mark_config_as_not_needing_migration();
}
}
}
void MainWindow::open_config_directory()
{
QDesktopServices::openUrl("file:///" + QDir::toNativeSeparators(group::ini_directory()));
}
bool MainWindow::refresh_config_list()
{
if (work)
return true;
QStringList ini_list = group::ini_list();
// check for sameness
const bool exact_same = ini_list.size() > 0 && progn(
if (ini_list.size() == ui.iconcomboProfile->count())
{
const int sz = ini_list.size();
for (int i = 0; i < sz; i++)
{
if (ini_list[i] != ui.iconcomboProfile->itemText(i))
return false;
}
return true;
}
return false;
);
QString current = group::ini_filename();
if (!ini_list.contains(current))
current = OPENTRACK_DEFAULT_CONFIG_Q;
if (maybe_die_on_config_not_writable(current, &ini_list))
return false;
if (exact_same)
return true;
const QIcon icon(":/images/settings16.png");
QSignalBlocker l(ui.iconcomboProfile);
ui.iconcomboProfile->clear();
ui.iconcomboProfile->addItems(ini_list);
for (int i = 0; i < ini_list.size(); i++)
ui.iconcomboProfile->setItemIcon(i, icon);
ui.iconcomboProfile->setCurrentText(current);
return true;
}
void MainWindow::updateButtonState(bool running, bool inertialp)
{
bool not_running = !running;
ui.iconcomboProfile->setEnabled(not_running);
ui.btnStartTracker->setEnabled(not_running);
ui.btnStopTracker->setEnabled(running);
ui.iconcomboProtocol->setEnabled(not_running);
ui.iconcomboFilter->setEnabled(not_running);
ui.iconcomboTrackerSource->setEnabled(not_running);
ui.profile_button->setEnabled(not_running);
ui.video_frame_label->setVisible(not_running || inertialp);
if(not_running)
{
ui.video_frame_label->setPixmap(QPixmap(":/images/tracking-not-started.png"));
}
else {
ui.video_frame_label->setPixmap(QPixmap(":/images/no-feed.png"));
}
}
void MainWindow::startTracker()
{
if (work)
return;
{
double p[6] = {0,0,0, 0,0,0};
display_pose(p, p);
}
work = std::make_shared<Work>(pose, ui.video_frame, current_tracker(), current_protocol(), current_filter());
if (!work->is_ok())
{
QMessageBox::warning(this, tr("Library load error"),
tr("One of libraries failed to load. Check installation."),
QMessageBox::Ok,
QMessageBox::NoButton);
work = nullptr;
return;
}
if (pTrackerDialog)
pTrackerDialog->register_tracker(work->libs.pTracker.get());
if (pFilterDialog)
pFilterDialog->register_filter(work->libs.pFilter.get());
if (pProtocolDialog)
pProtocolDialog->register_protocol(work->libs.pProtocol.get());
pose_update_timer.start(50);
// NB check valid since SelectedLibraries ctor called
// trackers take care of layout state updates
const bool is_inertial = ui.video_frame->layout() == nullptr;
updateButtonState(true, is_inertial);
ui.btnStopTracker->setFocus();
}
void MainWindow::stopTracker()
{
if (!work)
return;
opts::set_teardown_flag(true); // XXX hack -sh 20160926
pose_update_timer.stop();
ui.pose_display->rotate_sync(0,0,0, 0,0,0);
if (pTrackerDialog)
pTrackerDialog->unregister_tracker();
if (pProtocolDialog)
pProtocolDialog->unregister_protocol();
if (pFilterDialog)
pFilterDialog->unregister_filter();
work = nullptr;
{
double p[6] = {0,0,0, 0,0,0};
display_pose(p, p);
}
opts::set_teardown_flag(false); // XXX hack -sh 20160926
updateButtonState(false, false);
set_title();
ui.btnStartTracker->setFocus();
}
void MainWindow::display_pose(const double *mapped, const double *raw)
{
ui.pose_display->rotate_async(mapped[Yaw], mapped[Pitch], -mapped[Roll],
mapped[TX], mapped[TY], mapped[TZ]);
if (mapping_widget)
mapping_widget->refresh_tab();
QLCDNumber* raw_[] = {
ui.raw_x, ui.raw_y, ui.raw_z,
ui.raw_yaw, ui.raw_pitch, ui.raw_roll,
};
QLCDNumber* mapped_[] = {
ui.pose_x, ui.pose_y, ui.pose_z,
ui.pose_yaw, ui.pose_pitch, ui.pose_roll,
};
for (int k = 0; k < 6; k++)
{
raw_[k]->display(iround(raw[k]));
mapped_[k]->display(iround(mapped[k]));
}
QString game_title;
if (work && work->libs.pProtocol)
game_title = work->libs.pProtocol->game_name();
set_title(game_title);
}
void MainWindow::set_title(const QString& game_title_)
{
QString game_title;
if (game_title_ != "")
game_title = tr(" :: ") + game_title_;
QString current = group::ini_filename();
QString version(opentrack_version);
version = tr("opentrack") + " " + version.mid(sizeof("opentrack-") - 1);
setWindowTitle(version + tr(" :: ") + current + game_title);
}
void MainWindow::showHeadPose()
{
double mapped[6], raw[6];
work->tracker->raw_and_mapped_pose(mapped, raw);
display_pose(mapped, raw);
}
template<typename t, typename F>
bool MainWindow::mk_window_common(ptr<t>& d, F&& ctor)
{
if (d)
{
d->show();
d->raise();
return false;
}
else if ((d = ptr<t>(ctor())))
{
QEventLoop e(d.get());
e.processEvents(); d->adjustSize(); e.processEvents();
// drain the event loop to reflow properly
d->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | d->windowFlags()); e.processEvents();
d->show();
return true;
}
return false;
}
template<typename t, typename... Args>
inline bool MainWindow::mk_window(ptr<t>& place, Args&&... params)
{
return mk_window_common(place, [&]() { return new t(std::forward<Args>(params)...); });
}
template<typename t>
bool MainWindow::mk_dialog(std::shared_ptr<dylib> lib, ptr<t>& d)
{
const bool just_created = mk_window_common(d, [&]() -> t* {
if (lib && lib->Dialog)
return reinterpret_cast<t*>(lib->Dialog());
return nullptr;
});
if (just_created)
{
using plugin_api::detail::BaseDialog;
QObject::connect(static_cast<BaseDialog*>(d.get()), &BaseDialog::closing,
this, [&d]() { d = nullptr; },
Qt::QueuedConnection);
}
return just_created;
}
void MainWindow::showTrackerSettings()
{
if (mk_dialog(current_tracker(), pTrackerDialog) && work && work->libs.pTracker)
pTrackerDialog->register_tracker(work->libs.pTracker.get());
}
void MainWindow::showProtocolSettings()
{
if (mk_dialog(current_protocol(), pProtocolDialog) && work && work->libs.pProtocol)
pProtocolDialog->register_protocol(work->libs.pProtocol.get());
}
void MainWindow::showFilterSettings()
{
if (mk_dialog(current_filter(), pFilterDialog) && work && work->libs.pFilter)
pFilterDialog->register_filter(work->libs.pFilter.get());
}
void MainWindow::show_options_dialog()
{
if (mk_window(options_widget, [&](bool flag) -> void { set_keys_enabled(!flag); }))
{
connect(options_widget.get(), &OptionsDialog::closing, this, &MainWindow::register_shortcuts);
}
}
void MainWindow::showCurveConfiguration()
{
mk_window(mapping_widget, pose);
}
void MainWindow::exit()
{
QCoreApplication::exit(0);
}
bool MainWindow::set_profile(const QString& new_name_)
{
if (!refresh_config_list())
return false;
QString new_name = new_name_;
if (!is_config_listed(new_name))
new_name = OPENTRACK_DEFAULT_CONFIG_Q;
if (maybe_die_on_config_not_writable(new_name, nullptr))
return false;
ui.iconcomboProfile->setCurrentText(new_name);
set_profile_in_registry(new_name);
// migrations are for config layout changes and other user-visible
// incompatibilities in future versions
run_migrations();
set_title();
options::detail::bundler::refresh_all_bundles();
return true;
}
void MainWindow::ensure_tray()
{
if (!QSystemTrayIcon::isSystemTrayAvailable())
return;
if (s.tray_enabled)
{
if (!tray)
{
tray = std::make_unique<QSystemTrayIcon>(this);
tray->setIcon(QIcon(":/images/facetracknoir.png"));
tray->setContextMenu(&tray_menu);
tray->show();
connect(tray.get(),
&QSystemTrayIcon::activated,
this,
&MainWindow::toggle_restore_from_tray);
}
}
else
{
const bool is_hidden = isHidden() || !isVisible();
if (is_hidden)
{
show();
setVisible(true);
raise(); // for OSX
activateWindow(); // for Windows
}
if (tray)
tray->hide();
tray = nullptr;
}
}
void MainWindow::toggle_restore_from_tray(QSystemTrayIcon::ActivationReason e)
{
if (progn(
switch (e)
{
// if we enable double click also then it causes
// toggle back to the original state
//case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::Trigger: // single click
return false;
default:
return true;
}
))
{
return;
}
ensure_tray();
const bool is_minimized = isHidden() || !is_tray_enabled();
menu_action_show.setText(!isHidden() ? tr("Show the Octopus") : tr("Hide the Octopus"));
setVisible(is_minimized);
setHidden(!is_minimized);
setWindowState(progn(
using ws = Qt::WindowStates;
if (is_minimized)
return ws(windowState() & (~Qt::WindowMinimized));
else
return ws(Qt::WindowNoState);
));
if (is_minimized)
{
raise(); // for OSX
activateWindow(); // for Windows
}
else
{
lower();
clearFocus();
}
}
bool MainWindow::maybe_hide_to_tray(QEvent* e)
{
if (e->type() == QEvent::WindowStateChange &&
(windowState() & Qt::WindowMinimized) &&
is_tray_enabled())
{
e->accept();
ensure_tray();
hide();
return true;
}
return false;
}
void MainWindow::maybe_start_profile_from_executable()
{
if (!work)
{
QString prof;
if (det.config_to_start(prof))
{
ui.iconcomboProfile->setCurrentText(prof);
startTracker();
}
}
else
{
if (det.should_stop())
stopTracker();
}
}
void MainWindow::set_keys_enabled(bool flag)
{
if (!flag)
{
if (work)
work->sc->reload({});
global_shortcuts.reload({});
}
else
{
register_shortcuts();
}
}
bool MainWindow::is_config_listed(const QString& name)
{
const int sz = ui.iconcomboProfile->count();
for (int i = 0; i < sz; i++)
if (ui.iconcomboProfile->itemText(i) == name)
return true;
return false;
}
void MainWindow::changeEvent(QEvent* e)
{
if (maybe_hide_to_tray(e))
e->accept();
else
{
QMainWindow::changeEvent(e);
}
}
void MainWindow::closeEvent(QCloseEvent*)
{
exit();
}
bool MainWindow::is_tray_enabled()
{
return s.tray_enabled && QSystemTrayIcon::isSystemTrayAvailable();
}
bool MainWindow::start_in_tray()
{
return s.tray_enabled && s.tray_start && QSystemTrayIcon::isSystemTrayAvailable();
}
void MainWindow::set_profile_in_registry(const QString &profile)
{
QSettings settings(OPENTRACK_ORG);
settings.setValue(OPENTRACK_CONFIG_FILENAME_KEY, profile);
}
|