1 /*
2   launcherwindow.cpp
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2011-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include "launcherwindow.h"
30 #include "ui_launcherwindow.h"
31 
32 #include <launcher/core/launchoptions.h>
33 
34 #include <ui/aboutdata.h>
35 #include <ui/helpcontroller.h>
36 #include <ui/uiresources.h>
37 #include <ui/uiintegration.h>
38 
39 #include <QPushButton>
40 #include <QSettings>
41 
42 using namespace GammaRay;
43 
LauncherWindow(QWidget * parent)44 LauncherWindow::LauncherWindow(QWidget *parent)
45     : QDialog(parent)
46     , ui(new Ui::LauncherWindow)
47 {
48     UIResources::setTheme(UiIntegration::hasDarkUI()
49                               ? UIResources::Light
50                               : UIResources::Dark);
51 
52     ui->setupUi(this);
53     ui->aboutPage->setThemeLogo(QStringLiteral("gammaray-trademark.png"));
54     ui->aboutPage->setTitle(AboutData::aboutTitle());
55     ui->aboutPage->setHeader(AboutData::aboutHeader());
56     ui->aboutPage->setAuthors(AboutData::aboutAuthors());
57     ui->aboutPage->setFooter(AboutData::aboutFooter());
58     ui->aboutPage->layout()->setContentsMargins(ui->selfTestPage->layout()->contentsMargins());
59     ui->aboutPage->setBackgroundWindow(this);
60 
61     connect(ui->tabWidget, &QTabWidget::currentChanged, this, &LauncherWindow::tabChanged);
62     connect(ui->attachPage, &AttachDialog::updateButtonState, this, &LauncherWindow::tabChanged);
63     connect(ui->launchPage, &LaunchPage::updateButtonState, this, &LauncherWindow::tabChanged);
64     connect(ui->connectPage, &ConnectPage::updateButtonState, this, &LauncherWindow::tabChanged);
65     connect(ui->attachPage, &AttachDialog::activate,
66             ui->buttonBox->button(QDialogButtonBox::Ok), &QAbstractButton::click);
67     connect(ui->connectPage, &ConnectPage::activate,
68             ui->buttonBox->button(QDialogButtonBox::Ok), &QAbstractButton::click);
69     connect(ui->buttonBox, &QDialogButtonBox::helpRequested, this, &LauncherWindow::help);
70 
71     setWindowTitle(tr("GammaRay Launcher"));
72     ui->buttonBox->button(QDialogButtonBox::Help)->setEnabled(HelpController::isAvailable());
73     ui->buttonBox->button(QDialogButtonBox::Help)->setShortcut(QKeySequence::HelpContents);
74 
75     QSettings settings;
76     ui->tabWidget->setCurrentIndex(settings.value(QStringLiteral("Launcher/TabIndex")).toInt());
77 }
78 
~LauncherWindow()79 LauncherWindow::~LauncherWindow()
80 {
81     delete ui;
82 }
83 
launchOptions() const84 LaunchOptions LauncherWindow::launchOptions() const
85 {
86     QWidget *current = ui->tabWidget->currentWidget();
87     if (current == ui->launchPage)
88         return ui->launchPage->launchOptions();
89     else if (current == ui->attachPage)
90         return ui->attachPage->launchOptions();
91     return LaunchOptions();
92 }
93 
tabChanged()94 void LauncherWindow::tabChanged()
95 {
96     if (ui->tabWidget->currentWidget() == ui->attachPage) {
97         ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Attach"));
98         ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ui->attachPage->isValid());
99     } else if (ui->tabWidget->currentWidget() == ui->launchPage) {
100         ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Launch"));
101         ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ui->launchPage->isValid());
102     } else if (ui->tabWidget->currentWidget() == ui->connectPage) {
103         ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Connect"));
104         ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ui->connectPage->isValid());
105     } else {
106         ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
107     }
108 }
109 
accept()110 void LauncherWindow::accept()
111 {
112     QSettings settings;
113     settings.setValue(QStringLiteral("Launcher/TabIndex"), ui->tabWidget->currentIndex());
114 
115     ui->launchPage->writeSettings();
116     ui->attachPage->writeSettings();
117     ui->connectPage->writeSettings();
118 
119     if (ui->tabWidget->currentWidget() == ui->connectPage)
120         ui->connectPage->launchClient();
121 
122     QDialog::accept();
123 }
124 
help()125 void LauncherWindow::help()
126 {
127     HelpController::openPage("gammaray/gammaray-launcher-gui.html");
128 }
129