add basic view
parent
fe6ef9d639
commit
7629745dd0
|
|
@ -59,7 +59,6 @@
|
|||
#include <QList>
|
||||
#include <QMap>
|
||||
|
||||
//! [0]
|
||||
class Cluster
|
||||
{
|
||||
public:
|
||||
|
|
@ -89,12 +88,10 @@ private:
|
|||
int m_cpus;
|
||||
int m_memory;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
typedef QList<Cluster> ClusterList;
|
||||
typedef QHash<QString, Cluster> ClusterHash;
|
||||
|
||||
//! [1]
|
||||
class ClusterModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -115,6 +112,5 @@ public:
|
|||
private:
|
||||
ClusterList clusterList;
|
||||
};
|
||||
//! [1]
|
||||
|
||||
#endif // CLUSTER_H
|
||||
|
|
|
|||
238
gui/window.cpp
238
gui/window.cpp
|
|
@ -81,6 +81,7 @@
|
|||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
#include <QFontDialog>
|
||||
#include <QStackedWidget>
|
||||
|
||||
#ifndef QT_NO_TERMWIDGET
|
||||
#include <QApplication>
|
||||
|
|
@ -88,16 +89,82 @@
|
|||
#include "qtermwidget.h"
|
||||
#endif
|
||||
|
||||
//! [0]
|
||||
Window::Window()
|
||||
{
|
||||
trayIconIcon = new QIcon(":/images/minikube.png");
|
||||
checkForMinikube();
|
||||
isBasicView = true;
|
||||
|
||||
stackedWidget = new QStackedWidget;
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
dashboardProcess = 0;
|
||||
|
||||
createClusterGroupBox();
|
||||
|
||||
createActions();
|
||||
createTrayIcon();
|
||||
|
||||
createBasicView();
|
||||
createAdvancedView();
|
||||
trayIcon->show();
|
||||
updateButtons();
|
||||
layout->addWidget(stackedWidget);
|
||||
setLayout(layout);
|
||||
resize(200, 250);
|
||||
|
||||
|
||||
setWindowTitle(tr("minikube"));
|
||||
setWindowIcon(*trayIconIcon);
|
||||
}
|
||||
|
||||
void Window::createBasicView()
|
||||
{
|
||||
basicStartButton = new QPushButton(tr("Start"));
|
||||
basicStopButton = new QPushButton(tr("Stop"));
|
||||
basicDeleteButton = new QPushButton(tr("Delete"));
|
||||
basicRefreshButton = new QPushButton(tr("Refresh"));
|
||||
basicSSHButton = new QPushButton(tr("SSH"));
|
||||
basicDashboardButton = new QPushButton(tr("Dashboard"));
|
||||
QPushButton *advancedViewButton = new QPushButton(tr("Advanced View"));
|
||||
|
||||
QVBoxLayout *buttonLayout = new QVBoxLayout;
|
||||
QGroupBox *catBox = new QGroupBox(tr("Clusters"));
|
||||
catBox->setLayout(buttonLayout);
|
||||
buttonLayout->addWidget(basicStartButton);
|
||||
buttonLayout->addWidget(basicStopButton);
|
||||
buttonLayout->addWidget(basicDeleteButton);
|
||||
buttonLayout->addWidget(basicRefreshButton);
|
||||
buttonLayout->addWidget(basicSSHButton);
|
||||
buttonLayout->addWidget(basicDashboardButton);
|
||||
buttonLayout->addWidget(advancedViewButton);
|
||||
catBox->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
||||
stackedWidget->addWidget(catBox);
|
||||
|
||||
connect(basicSSHButton, &QAbstractButton::clicked, this, &Window::sshConsole);
|
||||
connect(basicDashboardButton, &QAbstractButton::clicked, this, &Window::dashboardBrowser);
|
||||
connect(basicStartButton, &QAbstractButton::clicked, this, &Window::startSelectedMinikube);
|
||||
connect(basicStopButton, &QAbstractButton::clicked, this, &Window::stopMinikube);
|
||||
connect(basicDeleteButton, &QAbstractButton::clicked, this, &Window::deleteMinikube);
|
||||
connect(basicRefreshButton, &QAbstractButton::clicked, this, &Window::updateClusters);
|
||||
connect(advancedViewButton, &QAbstractButton::clicked, this, &Window::toAdvancedView);
|
||||
}
|
||||
|
||||
void Window::toAdvancedView()
|
||||
{
|
||||
isBasicView = false;
|
||||
stackedWidget->setCurrentIndex(1);
|
||||
resize(600, 400);
|
||||
}
|
||||
|
||||
void Window::toBasicView()
|
||||
{
|
||||
isBasicView = true;
|
||||
stackedWidget->setCurrentIndex(0);
|
||||
resize(200, 250);
|
||||
}
|
||||
|
||||
void Window::createAdvancedView()
|
||||
{
|
||||
connect(sshButton, &QAbstractButton::clicked, this, &Window::sshConsole);
|
||||
connect(dashboardButton, &QAbstractButton::clicked, this, &Window::dashboardBrowser);
|
||||
connect(startButton, &QAbstractButton::clicked, this, &Window::startSelectedMinikube);
|
||||
|
|
@ -107,30 +174,17 @@ Window::Window()
|
|||
connect(createButton, &QAbstractButton::clicked, this, &Window::initMachine);
|
||||
connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
|
||||
|
||||
dashboardProcess = 0;
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addWidget(clusterGroupBox);
|
||||
setLayout(mainLayout);
|
||||
|
||||
trayIcon->show();
|
||||
|
||||
setWindowTitle(tr("minikube"));
|
||||
setWindowIcon(*trayIconIcon);
|
||||
resize(600, 400);
|
||||
clusterGroupBox->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
||||
stackedWidget->addWidget(clusterGroupBox);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void Window::setVisible(bool visible)
|
||||
{
|
||||
minimizeAction->setEnabled(visible);
|
||||
restoreAction->setEnabled(!visible);
|
||||
QDialog::setVisible(visible);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void Window::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
#ifdef Q_OS_OSX
|
||||
|
|
@ -148,16 +202,13 @@ void Window::closeEvent(QCloseEvent *event)
|
|||
event->ignore();
|
||||
}
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [6]
|
||||
void Window::messageClicked()
|
||||
{
|
||||
QMessageBox::information(0, tr("Systray"),
|
||||
tr("Sorry, I already gave what help I could.\n"
|
||||
"Maybe you should try asking a human?"));
|
||||
}
|
||||
//! [6]
|
||||
|
||||
void Window::createActions()
|
||||
{
|
||||
|
|
@ -240,15 +291,13 @@ ClusterList Window::getClusters()
|
|||
ClusterList clusters;
|
||||
QStringList args = { "profile", "list", "-o", "json" };
|
||||
QString text;
|
||||
bool success = sendMinikubeCommand(args, text);
|
||||
sendMinikubeCommand(args, text);
|
||||
QStringList lines;
|
||||
if (success) {
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
||||
lines = text.split("\n", Qt::SkipEmptyParts);
|
||||
lines = text.split("\n", Qt::SkipEmptyParts);
|
||||
#else
|
||||
lines = text.split("\n", QString::SkipEmptyParts);
|
||||
lines = text.split("\n", QString::SkipEmptyParts);
|
||||
#endif
|
||||
}
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
QString line = lines.at(i);
|
||||
QJsonParseError error;
|
||||
|
|
@ -257,58 +306,67 @@ ClusterList Window::getClusters()
|
|||
qDebug() << error.errorString();
|
||||
continue;
|
||||
}
|
||||
if (json.isObject()) {
|
||||
QJsonObject par = json.object();
|
||||
QJsonArray a = par["valid"].toArray();
|
||||
for (int j = 0; j < a.size(); j++) {
|
||||
QJsonObject obj = a[j].toObject();
|
||||
QString name;
|
||||
if (obj.contains("Name")) {
|
||||
name = obj["Name"].toString();
|
||||
}
|
||||
if (name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
Cluster cluster(name);
|
||||
if (obj.contains("Status")) {
|
||||
QString status = obj["Status"].toString();
|
||||
cluster.setStatus(status);
|
||||
}
|
||||
if (!obj.contains("Config")) {
|
||||
clusters << cluster;
|
||||
continue;
|
||||
}
|
||||
QJsonObject config = obj["Config"].toObject();
|
||||
if (config.contains("CPUs")) {
|
||||
int cpus = config["CPUs"].toInt();
|
||||
cluster.setCpus(cpus);
|
||||
}
|
||||
if (config.contains("Memory")) {
|
||||
int memory = config["Memory"].toInt();
|
||||
cluster.setMemory(memory);
|
||||
}
|
||||
if (config.contains("Driver")) {
|
||||
QString driver = config["Driver"].toString();
|
||||
cluster.setDriver(driver);
|
||||
}
|
||||
if (!config.contains("KubernetesConfig")) {
|
||||
clusters << cluster;
|
||||
continue;
|
||||
}
|
||||
QJsonObject k8sConfig = config["KubernetesConfig"].toObject();
|
||||
if (k8sConfig.contains("ContainerRuntime")) {
|
||||
QString containerRuntime = k8sConfig["ContainerRuntime"].toString();
|
||||
cluster.setContainerRuntime(containerRuntime);
|
||||
}
|
||||
clusters << cluster;
|
||||
}
|
||||
if (!json.isObject()) {
|
||||
continue;
|
||||
}
|
||||
QJsonObject par = json.object();
|
||||
QJsonArray a = par["valid"].toArray();
|
||||
QJsonArray b = par["invalid"].toArray();
|
||||
for (int i = 0; i < b.size(); i++) {
|
||||
a.append(b[i]);
|
||||
}
|
||||
for (int i = 0; i < a.size(); i++) {
|
||||
QJsonObject obj = a[i].toObject();
|
||||
Cluster cluster = createClusterObject(obj);
|
||||
clusters << cluster;
|
||||
}
|
||||
}
|
||||
return clusters;
|
||||
}
|
||||
|
||||
Cluster Window::createClusterObject(QJsonObject obj)
|
||||
{
|
||||
QString name;
|
||||
if (obj.contains("Name")) {
|
||||
name = obj["Name"].toString();
|
||||
}
|
||||
Cluster cluster(name);
|
||||
if (obj.contains("Status")) {
|
||||
QString status = obj["Status"].toString();
|
||||
cluster.setStatus(status);
|
||||
}
|
||||
if (!obj.contains("Config")) {
|
||||
return cluster;
|
||||
}
|
||||
QJsonObject config = obj["Config"].toObject();
|
||||
if (config.contains("CPUs")) {
|
||||
int cpus = config["CPUs"].toInt();
|
||||
cluster.setCpus(cpus);
|
||||
}
|
||||
if (config.contains("Memory")) {
|
||||
int memory = config["Memory"].toInt();
|
||||
cluster.setMemory(memory);
|
||||
}
|
||||
if (config.contains("Driver")) {
|
||||
QString driver = config["Driver"].toString();
|
||||
cluster.setDriver(driver);
|
||||
}
|
||||
if (!config.contains("KubernetesConfig")) {
|
||||
return cluster;
|
||||
}
|
||||
QJsonObject k8sConfig = config["KubernetesConfig"].toObject();
|
||||
if (k8sConfig.contains("ContainerRuntime")) {
|
||||
QString containerRuntime = k8sConfig["ContainerRuntime"].toString();
|
||||
cluster.setContainerRuntime(containerRuntime);
|
||||
}
|
||||
return cluster;
|
||||
}
|
||||
|
||||
QString Window::selectedCluster()
|
||||
{
|
||||
if (isBasicView) {
|
||||
return "minikube";
|
||||
}
|
||||
QModelIndex index = clusterListView->currentIndex();
|
||||
QVariant variant = index.data(Qt::DisplayRole);
|
||||
if (variant.isNull()) {
|
||||
|
|
@ -356,12 +414,13 @@ void Window::createClusterGroupBox()
|
|||
createButton = new QPushButton(tr("Create"));
|
||||
sshButton = new QPushButton(tr("SSH"));
|
||||
dashboardButton = new QPushButton(tr("Dashboard"));
|
||||
|
||||
updateButtons();
|
||||
QPushButton *basicViewButton = new QPushButton(tr("Basic View"));
|
||||
connect(basicViewButton, &QAbstractButton::clicked, this, &Window::toBasicView);
|
||||
|
||||
QHBoxLayout *topButtonLayout = new QHBoxLayout;
|
||||
topButtonLayout->addWidget(createButton);
|
||||
topButtonLayout->addWidget(refreshButton);
|
||||
topButtonLayout->addWidget(basicViewButton);
|
||||
topButtonLayout->addSpacing(340);
|
||||
|
||||
QHBoxLayout *bottomButtonLayout = new QHBoxLayout;
|
||||
|
|
@ -379,6 +438,40 @@ void Window::createClusterGroupBox()
|
|||
}
|
||||
|
||||
void Window::updateButtons()
|
||||
{
|
||||
if (isBasicView) {
|
||||
updateBasicButtons();
|
||||
} else {
|
||||
updateAdvancedButtons();
|
||||
}
|
||||
}
|
||||
|
||||
void Window::updateBasicButtons()
|
||||
{
|
||||
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;
|
||||
}
|
||||
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()) {
|
||||
|
|
@ -441,6 +534,7 @@ bool Window::sendMinikubeCommand(QStringList cmds, QString &text)
|
|||
text = process->readAllStandardOutput();
|
||||
if (success) {
|
||||
} else {
|
||||
qDebug() << text;
|
||||
qDebug() << process->readAllStandardError();
|
||||
}
|
||||
delete process;
|
||||
|
|
@ -631,7 +725,7 @@ void Window::sshConsole()
|
|||
console->setTerminalFont(font);
|
||||
console->setColorScheme("Tango");
|
||||
console->setShellProgram(program);
|
||||
QStringList args = { "ssh" };
|
||||
QStringList args = { "ssh", "-p", selectedCluster() };
|
||||
console->setArgs(args);
|
||||
console->startShellProgram();
|
||||
|
||||
|
|
|
|||
107
gui/window.h
107
gui/window.h
|
|
@ -56,6 +56,7 @@
|
|||
|
||||
#include <QSystemTrayIcon>
|
||||
#include <QFormLayout>
|
||||
#include <QStackedWidget>
|
||||
|
||||
#ifndef QT_NO_SYSTEMTRAYICON
|
||||
|
||||
|
|
@ -79,7 +80,6 @@ QT_END_NAMESPACE
|
|||
|
||||
#include "cluster.h"
|
||||
|
||||
//! [0]
|
||||
class Window : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -98,53 +98,84 @@ private slots:
|
|||
void dashboardClose();
|
||||
|
||||
private:
|
||||
void createActionGroupBox();
|
||||
void createActions();
|
||||
// Tray icon
|
||||
void createTrayIcon();
|
||||
void startMinikube(QStringList args);
|
||||
void startSelectedMinikube();
|
||||
void stopMinikube();
|
||||
void deleteMinikube();
|
||||
ClusterList getClusters();
|
||||
QString selectedCluster();
|
||||
void setSelectedCluster(QString cluster);
|
||||
QTableView *clusterListView;
|
||||
void createClusterGroupBox();
|
||||
QGroupBox *clusterGroupBox;
|
||||
ClusterModel *clusterModel;
|
||||
ClusterHash getClusterHash();
|
||||
bool sendMinikubeCommand(QStringList cmds);
|
||||
bool sendMinikubeCommand(QStringList cmds, QString &text);
|
||||
void updateClusters();
|
||||
void askCustom();
|
||||
void askName();
|
||||
QComboBox *driverComboBox;
|
||||
QComboBox *containerRuntimeComboBox;
|
||||
void initMachine();
|
||||
void sshConsole();
|
||||
void dashboardBrowser();
|
||||
void checkForMinikube();
|
||||
void outputFailedStart(QString text);
|
||||
QLabel *createLabel(QString title, QString text, QFormLayout *form, bool isLink);
|
||||
QPushButton *sshButton;
|
||||
QPushButton *dashboardButton;
|
||||
QProcess *dashboardProcess;
|
||||
void createActions();
|
||||
QAction *minimizeAction;
|
||||
QAction *restoreAction;
|
||||
QAction *quitAction;
|
||||
QSystemTrayIcon *trayIcon;
|
||||
QMenu *trayIconMenu;
|
||||
QIcon *trayIconIcon;
|
||||
|
||||
// Basic view
|
||||
void createBasicView();
|
||||
void toBasicView();
|
||||
void updateBasicButtons();
|
||||
void basicStartMinikube();
|
||||
void basicStopMinikube();
|
||||
void basicDeleteMinikube();
|
||||
void basicRefreshMinikube();
|
||||
void basicSSHMinikube();
|
||||
void basicDashboardMinikube();
|
||||
QPushButton *basicStartButton;
|
||||
QPushButton *basicStopButton;
|
||||
QPushButton *basicDeleteButton;
|
||||
QPushButton *basicRefreshButton;
|
||||
QPushButton *basicSSHButton;
|
||||
QPushButton *basicDashboardButton;
|
||||
|
||||
// Advanced view
|
||||
void createAdvancedView();
|
||||
void toAdvancedView();
|
||||
void createClusterGroupBox();
|
||||
void updateAdvancedButtons();
|
||||
QPushButton *startButton;
|
||||
QPushButton *stopButton;
|
||||
QPushButton *deleteButton;
|
||||
QPushButton *refreshButton;
|
||||
QPushButton *createButton;
|
||||
QPushButton *sshButton;
|
||||
QPushButton *dashboardButton;
|
||||
QGroupBox *clusterGroupBox;
|
||||
|
||||
QAction *minimizeAction;
|
||||
QAction *restoreAction;
|
||||
QAction *quitAction;
|
||||
// Cluster table
|
||||
QString selectedCluster();
|
||||
void setSelectedCluster(QString cluster);
|
||||
ClusterHash getClusterHash();
|
||||
ClusterList getClusters();
|
||||
void updateClusters();
|
||||
ClusterModel *clusterModel;
|
||||
QTableView *clusterListView;
|
||||
|
||||
QSystemTrayIcon *trayIcon;
|
||||
QMenu *trayIconMenu;
|
||||
QIcon *trayIconIcon;
|
||||
|
||||
// Create cluster
|
||||
void askCustom();
|
||||
void askName();
|
||||
QComboBox *driverComboBox;
|
||||
QComboBox *containerRuntimeComboBox;
|
||||
|
||||
// Commands
|
||||
void startMinikube(QStringList args);
|
||||
void startSelectedMinikube();
|
||||
void stopMinikube();
|
||||
void deleteMinikube();
|
||||
bool sendMinikubeCommand(QStringList cmds);
|
||||
bool sendMinikubeCommand(QStringList cmds, QString &text);
|
||||
void initMachine();
|
||||
void sshConsole();
|
||||
void dashboardBrowser();
|
||||
Cluster createClusterObject(QJsonObject obj);
|
||||
QProcess *dashboardProcess;
|
||||
|
||||
// Error messaging
|
||||
void outputFailedStart(QString text);
|
||||
QLabel *createLabel(QString title, QString text, QFormLayout *form, bool isLink);
|
||||
|
||||
void checkForMinikube();
|
||||
QStackedWidget *stackedWidget;
|
||||
bool isBasicView;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // QT_NO_SYSTEMTRAYICON
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue