Merge pull request #14048 from spowelljr/addPauseButton

GUI: Add pause/unpause buttons
pull/14055/head
Steven Powell 2022-04-26 13:21:52 -07:00 committed by GitHub
commit 94254f6782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 72 deletions

View File

@ -87,6 +87,7 @@ public:
void setCpus(int cpus) { m_cpus = cpus; }
int memory() const { return m_memory; }
void setMemory(int memory) { m_memory = memory; }
bool isEmpty() { return m_name.isEmpty(); }
private:
QString m_name;

View File

@ -129,6 +129,7 @@ void Window::createBasicView()
{
basicStartButton = new QPushButton(tr("Start"));
basicStopButton = new QPushButton(tr("Stop"));
basicPauseButton = new QPushButton(tr("Pause"));
basicDeleteButton = new QPushButton(tr("Delete"));
basicRefreshButton = new QPushButton(tr("Refresh"));
basicSSHButton = new QPushButton(tr("SSH"));
@ -140,6 +141,7 @@ void Window::createBasicView()
catBox->setLayout(buttonLayout);
buttonLayout->addWidget(basicStartButton);
buttonLayout->addWidget(basicStopButton);
buttonLayout->addWidget(basicPauseButton);
buttonLayout->addWidget(basicDeleteButton);
buttonLayout->addWidget(basicRefreshButton);
buttonLayout->addWidget(basicSSHButton);
@ -152,6 +154,7 @@ void Window::createBasicView()
connect(basicDashboardButton, &QAbstractButton::clicked, this, &Window::dashboardBrowser);
connect(basicStartButton, &QAbstractButton::clicked, this, &Window::startSelectedMinikube);
connect(basicStopButton, &QAbstractButton::clicked, this, &Window::stopMinikube);
connect(basicPauseButton, &QAbstractButton::clicked, this, &Window::pauseOrUnpauseMinikube);
connect(basicDeleteButton, &QAbstractButton::clicked, this, &Window::deleteMinikube);
connect(basicRefreshButton, &QAbstractButton::clicked, this, &Window::updateClusters);
connect(advancedViewButton, &QAbstractButton::clicked, this, &Window::toAdvancedView);
@ -177,6 +180,7 @@ void Window::createAdvancedView()
connect(dashboardButton, &QAbstractButton::clicked, this, &Window::dashboardBrowser);
connect(startButton, &QAbstractButton::clicked, this, &Window::startSelectedMinikube);
connect(stopButton, &QAbstractButton::clicked, this, &Window::stopMinikube);
connect(pauseButton, &QAbstractButton::clicked, this, &Window::pauseOrUnpauseMinikube);
connect(deleteButton, &QAbstractButton::clicked, this, &Window::deleteMinikube);
connect(refreshButton, &QAbstractButton::clicked, this, &Window::updateClusters);
connect(createButton, &QAbstractButton::clicked, this, &Window::initMachine);
@ -274,29 +278,43 @@ void Window::startMinikube(QStringList moreArgs)
void Window::startSelectedMinikube()
{
QStringList args = { "-p", selectedCluster() };
QStringList args = { "-p", selectedClusterName() };
return startMinikube(args);
}
void Window::stopMinikube()
{
QStringList args = { "stop", "-p", selectedCluster() };
QStringList args = { "stop", "-p", selectedClusterName() };
sendMinikubeCommand(args);
updateClusters();
}
void Window::pauseMinikube()
{
QStringList args = { "pause", "-p", selectedClusterName() };
sendMinikubeCommand(args);
updateClusters();
}
void Window::unpauseMinikube()
{
QStringList args = { "unpause", "-p", selectedClusterName() };
sendMinikubeCommand(args);
updateClusters();
}
void Window::deleteMinikube()
{
QStringList args = { "delete", "-p", selectedCluster() };
QStringList args = { "delete", "-p", selectedClusterName() };
sendMinikubeCommand(args);
updateClusters();
}
void Window::updateClusters()
{
QString cluster = selectedCluster();
QString cluster = selectedClusterName();
clusterModel->setClusters(getClusters());
setSelectedCluster(cluster);
setSelectedClusterName(cluster);
updateButtons();
}
@ -380,7 +398,7 @@ Cluster Window::createClusterObject(QJsonObject obj)
return cluster;
}
QString Window::selectedCluster()
QString Window::selectedClusterName()
{
if (isBasicView) {
return "minikube";
@ -393,7 +411,7 @@ QString Window::selectedCluster()
return variant.toString();
}
void Window::setSelectedCluster(QString cluster)
void Window::setSelectedClusterName(QString cluster)
{
QAbstractItemModel *model = clusterListView->model();
QModelIndex start = model->index(0, 0);
@ -422,12 +440,13 @@ void Window::createClusterGroupBox()
clusterListView->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(5, QHeaderView::ResizeToContents);
clusterListView->horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
setSelectedCluster("default");
setSelectedClusterName("default");
connect(clusterListView, SIGNAL(clicked(QModelIndex)), this, SLOT(updateButtons()));
startButton = new QPushButton(tr("Start"));
stopButton = new QPushButton(tr("Stop"));
pauseButton = new QPushButton(tr("Pause"));
deleteButton = new QPushButton(tr("Delete"));
refreshButton = new QPushButton(tr("Refresh"));
createButton = new QPushButton(tr("Create"));
@ -445,6 +464,7 @@ void Window::createClusterGroupBox()
QHBoxLayout *bottomButtonLayout = new QHBoxLayout;
bottomButtonLayout->addWidget(startButton);
bottomButtonLayout->addWidget(stopButton);
bottomButtonLayout->addWidget(pauseButton);
bottomButtonLayout->addWidget(deleteButton);
bottomButtonLayout->addWidget(sshButton);
bottomButtonLayout->addWidget(dashboardButton);
@ -465,66 +485,74 @@ void Window::updateButtons()
}
}
void Window::updateBasicButtons()
Cluster Window::selectedCluster()
{
Cluster *cluster = new Cluster();
ClusterList list = getClusters();
for (int i = 0; i < list.length(); i++) {
Cluster curr = list[i];
if (curr.name() != "minikube") {
continue;
}
cluster = &curr;
break;
QString clusterName = selectedClusterName();
if (clusterName.isEmpty()) {
return Cluster();
}
bool exists = cluster->name() == "minikube";
bool isRunning = exists && cluster->status() == "Running";
basicStartButton->setEnabled(isRunning == false);
basicStopButton->setEnabled(isRunning == true);
basicDeleteButton->setEnabled(exists == true);
basicDashboardButton->setEnabled(isRunning == true);
#if __linux__
basicSSHButton->setEnabled(isRunning == true);
#else
basicSSHButton->setEnabled(false);
#endif
}
void Window::updateAdvancedButtons()
{
QString cluster = selectedCluster();
if (cluster.isEmpty()) {
startButton->setEnabled(false);
stopButton->setEnabled(false);
deleteButton->setEnabled(false);
sshButton->setEnabled(false);
dashboardButton->setEnabled(false);
return;
}
deleteButton->setEnabled(true);
Cluster clusterHash = getClusterHash()[cluster];
if (clusterHash.status() == "Running") {
startButton->setEnabled(false);
stopButton->setEnabled(true);
#if __linux__
sshButton->setEnabled(true);
#endif
dashboardButton->setEnabled(true);
} else {
startButton->setEnabled(true);
stopButton->setEnabled(false);
}
}
ClusterHash Window::getClusterHash()
{
ClusterList clusters = getClusters();
ClusterHash clusterHash;
for (int i = 0; i < clusters.size(); i++) {
Cluster cluster = clusters.at(i);
clusterHash[cluster.name()] = cluster;
}
return clusterHash;
return clusterHash[clusterName];
}
void Window::updateBasicButtons()
{
Cluster cluster = selectedCluster();
bool exists = !cluster.isEmpty();
bool isRunning = cluster.status() == "Running";
bool isPaused = cluster.status() == "Paused";
basicStopButton->setEnabled(isRunning || isPaused);
basicPauseButton->setEnabled(isRunning || isPaused);
basicDeleteButton->setEnabled(exists);
basicDashboardButton->setEnabled(isRunning);
#if __linux__
basicSSHButton->setEnabled(exists);
#else
basicSSHButton->setEnabled(false);
#endif
QString pauseLabel = tr("Pause");
if (isPaused) {
pauseLabel = tr("Unpause");
}
basicPauseButton->setText(pauseLabel);
}
void Window::pauseOrUnpauseMinikube()
{
Cluster cluster = selectedCluster();
if (cluster.status() == "Paused") {
unpauseMinikube();
return;
}
pauseMinikube();
}
void Window::updateAdvancedButtons()
{
Cluster cluster = selectedCluster();
bool exists = !cluster.isEmpty();
bool isRunning = cluster.status() == "Running";
bool isPaused = cluster.status() == "Paused";
startButton->setEnabled(exists);
stopButton->setEnabled(isRunning || isPaused);
pauseButton->setEnabled(isRunning || isPaused);
deleteButton->setEnabled(exists);
dashboardButton->setEnabled(isRunning);
#if __linux__
sshButton->setEnabled(exists);
#else
sshButton->setEnabled(false);
#endif
QString pauseLabel = tr("Pause");
if (isPaused) {
pauseLabel = tr("Unpause");
}
pauseButton->setText(pauseLabel);
}
bool Window::sendMinikubeCommand(QStringList cmds)
@ -753,7 +781,7 @@ void Window::sshConsole()
console->setTerminalFont(font);
console->setColorScheme("Tango");
console->setShellProgram(program);
QStringList args = { "ssh", "-p", selectedCluster() };
QStringList args = { "ssh", "-p", selectedClusterName() };
console->setArgs(args);
console->startShellProgram();
@ -772,7 +800,7 @@ void Window::sshConsole()
}
}
QStringList arguments = { "-e", QString("%1 ssh -p %2").arg(program, selectedCluster()) };
QStringList arguments = { "-e", QString("%1 ssh -p %2").arg(program, selectedClusterName()) };
QProcess *process = new QProcess(this);
process->start(QStandardPaths::findExecutable(terminal), arguments);
#endif
@ -784,7 +812,7 @@ void Window::dashboardBrowser()
QString program = minikubePath();
QProcess *process = new QProcess(this);
QStringList arguments = { "dashboard", "-p", selectedCluster() };
QStringList arguments = { "dashboard", "-p", selectedClusterName() };
process->start(program, arguments);
dashboardProcess = process;

View File

@ -113,14 +113,9 @@ private:
void createBasicView();
void toBasicView();
void updateBasicButtons();
void basicStartMinikube();
void basicStopMinikube();
void basicDeleteMinikube();
void basicRefreshMinikube();
void basicSSHMinikube();
void basicDashboardMinikube();
QPushButton *basicStartButton;
QPushButton *basicStopButton;
QPushButton *basicPauseButton;
QPushButton *basicDeleteButton;
QPushButton *basicRefreshButton;
QPushButton *basicSSHButton;
@ -133,6 +128,7 @@ private:
void updateAdvancedButtons();
QPushButton *startButton;
QPushButton *stopButton;
QPushButton *pauseButton;
QPushButton *deleteButton;
QPushButton *refreshButton;
QPushButton *createButton;
@ -141,9 +137,9 @@ private:
QGroupBox *clusterGroupBox;
// Cluster table
QString selectedCluster();
void setSelectedCluster(QString cluster);
ClusterHash getClusterHash();
QString selectedClusterName();
void setSelectedClusterName(QString cluster);
Cluster selectedCluster();
ClusterList getClusters();
void updateClusters();
ClusterModel *clusterModel;
@ -160,6 +156,9 @@ private:
void startMinikube(QStringList args);
void startSelectedMinikube();
void stopMinikube();
void pauseMinikube();
void unpauseMinikube();
void pauseOrUnpauseMinikube();
void deleteMinikube();
bool sendMinikubeCommand(QStringList cmds);
bool sendMinikubeCommand(QStringList cmds, QString &text);