mirror of
https://github.com/loimu/zeit.git
synced 2025-09-15 12:58:44 +08:00
zeit: refactor the main window code in order to make everything less cluttered
This commit is contained in:
parent
ce4f63dce4
commit
da5081e2bb
@ -12,6 +12,10 @@ set(zeit_SRCS
|
||||
taskdialog.cpp
|
||||
timerdialog.cpp
|
||||
variabledialog.cpp
|
||||
data/basedelegate.cpp
|
||||
data/commanddelegate.cpp
|
||||
data/taskdelegate.cpp
|
||||
data/variabledelegate.cpp
|
||||
)
|
||||
set(zeit_FRMS
|
||||
alarmdialog.ui
|
||||
|
||||
31
src/data/basedelegate.cpp
Normal file
31
src/data/basedelegate.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#include "basedelegate.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
|
||||
BaseDelegate::BaseDelegate(Ui::MainWindow* ui_) : ui(ui_)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseDelegate::setIcon(QListWidgetItem* item, bool enabled) {
|
||||
QString icon = enabled ? QSL("dialog-ok-apply") : QSL("edit-delete");
|
||||
item->setIcon(QIcon::fromTheme(icon, QIcon(QSL(":/icons/") + icon)));
|
||||
}
|
||||
62
src/data/basedelegate.h
Normal file
62
src/data/basedelegate.h
Normal file
@ -0,0 +1,62 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#ifndef BASEDELEGATE_H
|
||||
#define BASEDELEGATE_H
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include "config.h"
|
||||
|
||||
#ifdef BUILD_HELPER
|
||||
#define ROOT_ACTIONS cron->isSystemCron()
|
||||
#else
|
||||
#define ROOT_ACTIONS false
|
||||
#endif // BUILD_HELPER
|
||||
|
||||
#define QSL QStringLiteral
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
}
|
||||
|
||||
class QListWidgetItem;
|
||||
|
||||
class BaseDelegate
|
||||
{
|
||||
public:
|
||||
QString caption{};
|
||||
QString toolTip{};
|
||||
|
||||
BaseDelegate(Ui::MainWindow* ui = nullptr);
|
||||
virtual ~BaseDelegate() {}
|
||||
|
||||
virtual void view()=0;
|
||||
virtual void copyEntry()=0;
|
||||
virtual void createEntry()=0;
|
||||
virtual void modifyEntry(int index)=0;
|
||||
virtual void deleteEntry(int index)=0;
|
||||
virtual void toggleEntry(int index)=0;
|
||||
|
||||
protected:
|
||||
Ui::MainWindow* ui;
|
||||
|
||||
void setIcon(QListWidgetItem* item, bool enabled);
|
||||
};
|
||||
|
||||
#endif // BASEDELEGATE_H
|
||||
55
src/data/commanddelegate.cpp
Normal file
55
src/data/commanddelegate.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "../commands.h"
|
||||
#include "../commanddialog.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "commanddelegate.h"
|
||||
|
||||
|
||||
CommandDelegate::CommandDelegate(Ui::MainWindow* ui, Commands* commands_)
|
||||
: BaseDelegate(ui),
|
||||
commands(commands_)
|
||||
{
|
||||
caption = tr("Command");
|
||||
toolTip = tr("commands, scheduled to be executed once");
|
||||
}
|
||||
|
||||
void CommandDelegate::view() {
|
||||
ui->labelWarning->hide();
|
||||
ui->listWidget->setEnabled(true);
|
||||
ui->listWidget->clear();
|
||||
for(const Command& c: commands->getCommands()) {
|
||||
QListWidgetItem* item = new QListWidgetItem(
|
||||
c.description + tr("\nCommand: ") + c.command);
|
||||
ui->listWidget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void CommandDelegate::createEntry() {
|
||||
CommandDialog* cd = new CommandDialog(commands, ui->listWidget);
|
||||
cd->show();
|
||||
QApplication::connect(cd, &CommandDialog::accepted, cd, [this] { view(); });
|
||||
}
|
||||
|
||||
void CommandDelegate::deleteEntry(int index) {
|
||||
commands->deleteCommand(index);
|
||||
}
|
||||
44
src/data/commanddelegate.h
Normal file
44
src/data/commanddelegate.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#ifndef COMMANDDELEGATE_H
|
||||
#define COMMANDDELEGATE_H
|
||||
|
||||
#include "basedelegate.h"
|
||||
|
||||
|
||||
class Commands;
|
||||
|
||||
class CommandDelegate : public BaseDelegate
|
||||
{
|
||||
Q_DECLARE_TR_FUNCTIONS(CommandController)
|
||||
|
||||
Commands* commands;
|
||||
|
||||
public:
|
||||
CommandDelegate(Ui::MainWindow* ui, Commands* commands);
|
||||
void view() override;
|
||||
void copyEntry() override { assert(false); };
|
||||
void createEntry() override;
|
||||
void modifyEntry(int index) override { Q_UNUSED(index); assert(false); };
|
||||
void deleteEntry(int index) override;
|
||||
void toggleEntry(int index) override { Q_UNUSED(index); assert(false); };
|
||||
};
|
||||
|
||||
#endif // COMMANDDELEGATE_H
|
||||
91
src/data/taskdelegate.cpp
Normal file
91
src/data/taskdelegate.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "ctcron.h"
|
||||
#include "cttask.h"
|
||||
#include "../taskdialog.h"
|
||||
#include "taskdelegate.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
|
||||
TaskDelegate::TaskDelegate(Ui::MainWindow* ui, CTCron* cron_)
|
||||
: BaseDelegate(ui),
|
||||
cron(cron_)
|
||||
{
|
||||
caption = tr("Task");
|
||||
toolTip = tr("crontab tasks, running periodically");
|
||||
}
|
||||
|
||||
void TaskDelegate::view()
|
||||
{
|
||||
ui->listWidget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
ui->labelWarning->setVisible(ROOT_ACTIONS);
|
||||
ui->listWidget->clear();
|
||||
for(CTTask* task: cron->tasks()) {
|
||||
QListWidgetItem* item = new QListWidgetItem();
|
||||
QString text(tr("Description: %1\n"
|
||||
"Runs %2\n"
|
||||
"Command: %3",
|
||||
"Runs at 'period described'")
|
||||
.arg(task->comment, task->describe(), task->command));
|
||||
item->setText(text);
|
||||
setIcon(item, task->enabled);
|
||||
ui->listWidget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void TaskDelegate::createEntry() {
|
||||
CTTask* task = new CTTask(QString(), QString(),
|
||||
cron->userLogin(), false);
|
||||
TaskDialog *td = new TaskDialog(task, tr("New Task"), ui->listWidget);
|
||||
td->show();
|
||||
QApplication::connect(td, &TaskDialog::accepted, td, [this, task] {
|
||||
cron->addTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
void TaskDelegate::modifyEntry(int index) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
TaskDialog* td = new TaskDialog(task, tr("Edit Task"), ui->listWidget);
|
||||
td->show();
|
||||
QApplication::connect(td, &TaskDialog::accepted, td, [this, task] {
|
||||
cron->modifyTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
void TaskDelegate::deleteEntry(int index) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
cron->removeTask(task);
|
||||
cron->save();
|
||||
}
|
||||
|
||||
void TaskDelegate::toggleEntry(int index) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
task->enabled = !task->enabled;
|
||||
cron->save();
|
||||
setIcon(ui->listWidget->item(index), task->enabled);
|
||||
}
|
||||
43
src/data/taskdelegate.h
Normal file
43
src/data/taskdelegate.h
Normal file
@ -0,0 +1,43 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#ifndef TASKDELEGATE_H
|
||||
#define TASKDELEGATE_H
|
||||
|
||||
#include "basedelegate.h"
|
||||
|
||||
class CTCron;
|
||||
|
||||
class TaskDelegate : public BaseDelegate
|
||||
{
|
||||
Q_DECLARE_TR_FUNCTIONS(TaskController)
|
||||
|
||||
CTCron* cron;
|
||||
|
||||
public:
|
||||
TaskDelegate(Ui::MainWindow* ui, CTCron* cron);
|
||||
void view() override;
|
||||
void copyEntry() override { assert(false); };
|
||||
void createEntry() override;
|
||||
void modifyEntry(int index) override;
|
||||
void deleteEntry(int index) override;
|
||||
void toggleEntry(int index) override;
|
||||
};
|
||||
|
||||
#endif // TASKDELEGATE_H
|
||||
88
src/data/variabledelegate.cpp
Normal file
88
src/data/variabledelegate.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "ctcron.h"
|
||||
#include "ctvariable.h"
|
||||
#include "../variabledialog.h"
|
||||
#include "variabledelegate.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
|
||||
VariableDelegate::VariableDelegate(Ui::MainWindow* ui, CTCron* cron_)
|
||||
: BaseDelegate(ui),
|
||||
cron(cron_)
|
||||
{
|
||||
caption = tr("Variable");
|
||||
toolTip = tr("environment variables for crontab");
|
||||
}
|
||||
|
||||
void VariableDelegate::view() {
|
||||
ui->listWidget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
ui->labelWarning->setVisible(ROOT_ACTIONS);
|
||||
ui->listWidget->clear();
|
||||
for(CTVariable* var: cron->variables()) {
|
||||
QListWidgetItem* item = new QListWidgetItem();
|
||||
item->setText(QString(QStringLiteral("%1%2=%3"))
|
||||
.arg(var->comment.isEmpty()
|
||||
? QString()
|
||||
: QString(QSL("## %1\n")).arg(var->comment),
|
||||
var->variable, var->value));
|
||||
setIcon(item, var->enabled);
|
||||
ui->listWidget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void VariableDelegate::createEntry() {
|
||||
CTVariable* var = new CTVariable({}, {}, cron->userLogin());
|
||||
VariableDialog* vd = new VariableDialog(var, tr("New Variable"), ui->listWidget);
|
||||
vd->show();
|
||||
QApplication::connect(vd, &VariableDialog::accepted, vd, [this, var] {
|
||||
cron->addVariable(var);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
void VariableDelegate::modifyEntry(int index) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
VariableDialog* vd = new VariableDialog(var, tr("Edit Variable"), ui->listWidget);
|
||||
vd->show();
|
||||
QApplication::connect(vd, &VariableDialog::accepted, vd, [this, var] {
|
||||
cron->modifyVariable(var);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
void VariableDelegate::deleteEntry(int index) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
cron->removeVariable(var);
|
||||
cron->save();
|
||||
}
|
||||
|
||||
void VariableDelegate::toggleEntry(int index) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
var->enabled = !var->enabled;
|
||||
cron->save();
|
||||
setIcon(ui->listWidget->item(index), var->enabled);
|
||||
}
|
||||
44
src/data/variabledelegate.h
Normal file
44
src/data/variabledelegate.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* ========================================================================
|
||||
* Copyright (C) 2015-2021 Blaze <blaze@vivaldi.net>
|
||||
*
|
||||
* This file is part of Zeit.
|
||||
*
|
||||
* Zeit is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Zeit is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#ifndef VARIABLEDELEGATE_H
|
||||
#define VARIABLEDELEGATE_H
|
||||
|
||||
#include "basedelegate.h"
|
||||
|
||||
|
||||
class CTCron;
|
||||
|
||||
class VariableDelegate : public BaseDelegate
|
||||
{
|
||||
Q_DECLARE_TR_FUNCTIONS(VariableController)
|
||||
|
||||
CTCron* cron;
|
||||
|
||||
public:
|
||||
VariableDelegate(Ui::MainWindow* ui, CTCron* cron);
|
||||
void view() override;
|
||||
void copyEntry() override { assert(false); };
|
||||
void createEntry() override;
|
||||
void modifyEntry(int index) override;
|
||||
void deleteEntry(int index) override;
|
||||
void toggleEntry(int index) override;
|
||||
};
|
||||
|
||||
#endif // VARIABLEDELEGATE_H
|
||||
@ -35,6 +35,9 @@
|
||||
#include "ctvariable.h"
|
||||
#include "ctInitializationError.h"
|
||||
|
||||
#include "data/commanddelegate.h"
|
||||
#include "data/taskdelegate.h"
|
||||
#include "data/variabledelegate.h"
|
||||
#include "aboutdialog.h"
|
||||
#include "alarmdialog.h"
|
||||
#include "taskdialog.h"
|
||||
@ -80,6 +83,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent),
|
||||
CTInitializationError error;
|
||||
ctHost = new CTHost(QStringLiteral("crontab"), error);
|
||||
cron = ctHost->findCurrentUserCron();
|
||||
list = new TaskDelegate(ui, cron);
|
||||
/* check if `at` binary is available */
|
||||
QProcess proc;
|
||||
proc.start(QStringLiteral("which"), QStringList{QStringLiteral("at")});
|
||||
@ -95,21 +99,8 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent),
|
||||
ui->actionTasks->setActionGroup(group);
|
||||
ui->actionVariables->setActionGroup(group);
|
||||
ui->actionCommands->setActionGroup(group);
|
||||
connect(ui->toggleItemAction, &QAction::triggered, this, [this] {
|
||||
int index = ui->listWidget->currentRow();
|
||||
if(ui->actionTasks->isChecked()) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
task->enabled = !task->enabled;
|
||||
cron->save();
|
||||
setIcon(ui->listWidget->item(index), task->enabled);
|
||||
}
|
||||
if(ui->actionVariables->isChecked()) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
var->enabled = !var->enabled;
|
||||
cron->save();
|
||||
setIcon(ui->listWidget->item(index), var->enabled);
|
||||
}
|
||||
});
|
||||
connect(ui->toggleItemAction, &QAction::triggered,
|
||||
this, [this] { list->toggleEntry(ui->listWidget->currentRow()); });
|
||||
ui->listWidget->addAction(ui->toggleItemAction);
|
||||
ui->listWidget->addAction(ui->actionModifyEntry);
|
||||
ui->listWidget->addAction(ui->actionDeleteEntry);
|
||||
@ -131,40 +122,21 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent),
|
||||
});
|
||||
// Main menu
|
||||
connect(ui->actionAddEntry, &QAction::triggered,
|
||||
this, &MainWindow::addEntry);
|
||||
this, [this] { list->createEntry(); });
|
||||
connect(ui->actionModifyEntry, &QAction::triggered,
|
||||
this, &MainWindow::modifyEntry);
|
||||
this, [this] { list->modifyEntry(ui->listWidget->currentRow()); });
|
||||
connect(ui->actionDeleteEntry, &QAction::triggered,
|
||||
this, &MainWindow::deleteEntry);
|
||||
connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
|
||||
// View menu
|
||||
connect(ui->actionRefresh, &QAction::triggered, this, [this] {
|
||||
if(ui->actionTasks->isChecked())
|
||||
showTasks();
|
||||
if(ui->actionVariables->isChecked())
|
||||
showVariables();
|
||||
if(ui->actionCommands->isChecked())
|
||||
showCommands();
|
||||
});
|
||||
connect(ui->actionRefresh, &QAction::triggered, this, [this] {list->view();});
|
||||
connect(ui->actionSystem, &QAction::toggled, this, [this] (bool check) {
|
||||
cron = check ? ctHost->findSystemCron() : ctHost->findCurrentUserCron();
|
||||
ui->actionRefresh->trigger();
|
||||
});
|
||||
connect(ui->actionTasks, &QAction::triggered, this, &MainWindow::viewTasks);
|
||||
connect(ui->actionVariables, &QAction::triggered,
|
||||
this, &MainWindow::viewVariables);
|
||||
connect(ui->actionCommands, &QAction::triggered,
|
||||
this, &MainWindow::viewCommands);
|
||||
connect(ui->actionShowFilter, &QAction::toggled, this, [this] (bool check) {
|
||||
ui->filterEdit->setVisible(check);
|
||||
ui->hideFilterButton->setVisible(check);
|
||||
if(check) {
|
||||
ui->filterEdit->setFocus();
|
||||
} else {
|
||||
ui->filterEdit->clear();
|
||||
emit ui->filterEdit->textEdited(QString());
|
||||
}
|
||||
switchView();
|
||||
});
|
||||
connect(group, &QActionGroup::triggered, this, &MainWindow::switchView);
|
||||
connect(ui->actionShowFilter, &QAction::toggled,
|
||||
this, &MainWindow::toggleFilter);
|
||||
connect(ui->actionWrapText, &QAction::toggled,
|
||||
ui->listWidget, &QListWidget::setWordWrap);
|
||||
// Tools menu
|
||||
@ -175,8 +147,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent),
|
||||
// Help menu
|
||||
connect(ui->actionAbout, &QAction::triggered,
|
||||
this, &MainWindow::showAboutDialog);
|
||||
/* refresh state */
|
||||
viewTasks();
|
||||
updateWindow();
|
||||
refreshActions(false);
|
||||
}
|
||||
|
||||
@ -202,183 +173,68 @@ void MainWindow::refreshActions(bool enabled) {
|
||||
ui->actionTimer->setEnabled(currentUser);
|
||||
}
|
||||
|
||||
void MainWindow::setIcon(QListWidgetItem* item, bool enabled) {
|
||||
QString icon = enabled ? QSL("dialog-ok-apply") : QSL("edit-delete");
|
||||
item->setIcon(QIcon::fromTheme(icon, QIcon(QSL(":/icons/") + icon)));
|
||||
void MainWindow::updateWindow() {
|
||||
ui->actionAddEntry->setText(tr("Add ") + list->caption);
|
||||
ui->actionModifyEntry->setText(tr("Modify ") + list->caption);
|
||||
ui->actionDeleteEntry->setText(tr("Delete ") + list->caption);
|
||||
if(ui->actionCommands->isChecked())
|
||||
ui->actionSystem->setChecked(false);
|
||||
ui->actionSystem->setEnabled(!ui->actionCommands->isChecked());
|
||||
list->view();
|
||||
ui->listWidget->setToolTip(list->toolTip);
|
||||
}
|
||||
|
||||
void MainWindow::showTasks() {
|
||||
ui->listWidget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
ui->labelWarning->setVisible(ROOT_ACTIONS);
|
||||
ui->listWidget->clear();
|
||||
for(CTTask* task: cron->tasks()) {
|
||||
QListWidgetItem* item = new QListWidgetItem();
|
||||
QString text(tr("Description: %1\n"
|
||||
"Runs %2\n"
|
||||
"Command: %3",
|
||||
"Runs at 'period described'")
|
||||
.arg(task->comment, task->describe(), task->command));
|
||||
item->setText(text);
|
||||
setIcon(item, task->enabled);
|
||||
ui->listWidget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::showVariables() {
|
||||
ui->listWidget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
ui->labelWarning->setVisible(ROOT_ACTIONS);
|
||||
ui->listWidget->clear();
|
||||
for(CTVariable* var: cron->variables()) {
|
||||
QListWidgetItem* item = new QListWidgetItem();
|
||||
item->setText(QString(QStringLiteral("%1%2=%3"))
|
||||
.arg(var->comment.isEmpty()
|
||||
? QString()
|
||||
: QString(QSL("## %1\n")).arg(var->comment),
|
||||
var->variable, var->value));
|
||||
setIcon(item, var->enabled);
|
||||
ui->listWidget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::showCommands() {
|
||||
ui->labelWarning->hide();
|
||||
ui->listWidget->setEnabled(true);
|
||||
ui->listWidget->clear();
|
||||
for(const Command& c: commands->getCommands()) {
|
||||
QListWidgetItem* item = new QListWidgetItem(
|
||||
c.description + tr("\nCommand: ") + c.command);
|
||||
ui->listWidget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::addEntry() {
|
||||
if(ui->actionTasks->isChecked()) {
|
||||
CTTask* task = new CTTask(QString(), QString(),
|
||||
cron->userLogin(), false);
|
||||
TaskDialog *td = new TaskDialog(task, tr("New Task"), this);
|
||||
td->show();
|
||||
connect(td, &TaskDialog::accepted, this, [this, task] {
|
||||
cron->addTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
showTasks();
|
||||
});
|
||||
}
|
||||
if(ui->actionVariables->isChecked()) {
|
||||
CTVariable* var = new CTVariable(QString(),
|
||||
QString(), cron->userLogin());
|
||||
VariableDialog* vd = new VariableDialog(var, tr("New Variable"), this);
|
||||
vd->show();
|
||||
connect(vd, &VariableDialog::accepted, this, [this, var] {
|
||||
cron->addVariable(var);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
showVariables();
|
||||
});
|
||||
}
|
||||
if(ui->actionCommands->isChecked()) {
|
||||
CommandDialog* cd = new CommandDialog(commands, this);
|
||||
cd->show();
|
||||
connect(cd, &CommandDialog::accepted, this, &MainWindow::showCommands);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::modifyEntry() {
|
||||
int index = ui->listWidget->currentRow();
|
||||
if(ui->actionTasks->isChecked()) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
TaskDialog* td = new TaskDialog(task, tr("Edit Task"), this);
|
||||
td->show();
|
||||
connect(td, &TaskDialog::accepted, this, [this, task] {
|
||||
cron->modifyTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
showTasks();
|
||||
});
|
||||
}
|
||||
if(ui->actionVariables->isChecked()) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
VariableDialog* vd = new VariableDialog(var, tr("Edit Variable"), this);
|
||||
vd->show();
|
||||
connect(vd, &VariableDialog::accepted, this, [this, var] {
|
||||
cron->modifyVariable(var);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
showVariables();
|
||||
});
|
||||
}
|
||||
void MainWindow::switchView() {
|
||||
if(list) { delete list; list = nullptr; }
|
||||
if(ui->actionVariables->isChecked())
|
||||
list = new VariableDelegate(ui, cron);
|
||||
else if(ui->actionCommands->isChecked())
|
||||
list = new CommandDelegate(ui, commands);
|
||||
else
|
||||
list = new TaskDelegate(ui, cron);
|
||||
updateWindow();
|
||||
}
|
||||
|
||||
void MainWindow::deleteEntry() {
|
||||
QMessageBox::StandardButton reply;
|
||||
reply = QMessageBox::question(this,
|
||||
tr("Deleting Entry"), tr("Delete entry?"),
|
||||
tr("Deleting %1").arg(list->caption),
|
||||
tr("Delete %1?").arg(list->caption),
|
||||
QMessageBox::Yes|QMessageBox::No);
|
||||
if(reply == QMessageBox::No)
|
||||
return;
|
||||
int index = ui->listWidget->currentRow();
|
||||
if(ui->actionTasks->isChecked()) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
cron->removeTask(task);
|
||||
cron->save();
|
||||
}
|
||||
if(ui->actionVariables->isChecked()) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
cron->removeVariable(var);
|
||||
cron->save();
|
||||
}
|
||||
if(ui->actionCommands->isChecked())
|
||||
commands->deleteCommand(index);
|
||||
list->deleteEntry(ui->listWidget->currentRow());
|
||||
ui->actionRefresh->trigger();
|
||||
}
|
||||
|
||||
void MainWindow::viewTasks() {
|
||||
ui->actionAddEntry->setText(tr("Add Task"));
|
||||
ui->actionModifyEntry->setText(tr("Modify Task"));
|
||||
ui->actionDeleteEntry->setText(tr("Delete Task"));
|
||||
ui->actionSystem->setEnabled(true);
|
||||
showTasks();
|
||||
ui->listWidget->setToolTip(tr("crontab tasks, running periodically"));
|
||||
}
|
||||
|
||||
void MainWindow::viewVariables() {
|
||||
ui->actionAddEntry->setText(tr("Add Variable"));
|
||||
ui->actionModifyEntry->setText(tr("Modify Variable"));
|
||||
ui->actionDeleteEntry->setText(tr("Delete Variable"));
|
||||
ui->actionSystem->setEnabled(true);
|
||||
showVariables();
|
||||
ui->listWidget->setToolTip(tr("environment variables for crontab"));
|
||||
}
|
||||
|
||||
void MainWindow::viewCommands() {
|
||||
ui->actionAddEntry->setText(tr("Add Command"));
|
||||
ui->actionModifyEntry->setText(tr("Modify Command"));
|
||||
ui->actionDeleteEntry->setText(tr("Delete Command"));
|
||||
ui->actionSystem->setChecked(false);
|
||||
ui->actionSystem->setEnabled(false);
|
||||
showCommands();
|
||||
ui->listWidget->setToolTip(tr("commands, scheduled to be executed once"));
|
||||
void MainWindow::toggleFilter(bool check) {
|
||||
ui->filterEdit->setVisible(check);
|
||||
ui->hideFilterButton->setVisible(check);
|
||||
if(check) {
|
||||
ui->filterEdit->setFocus();
|
||||
} else {
|
||||
ui->filterEdit->clear();
|
||||
emit ui->filterEdit->textEdited({});
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::showAlarmDialog() {
|
||||
CTTask* task = new CTTask(QString(), QString(), cron->userLogin(), false);
|
||||
CTTask* task = new CTTask({}, {}, cron->userLogin(), false);
|
||||
AlarmDialog* ad = new AlarmDialog(task, this);
|
||||
ad->show();
|
||||
connect(ad, &AlarmDialog::accepted, this, [this, task] {
|
||||
cron->addTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
showTasks();
|
||||
list->view();
|
||||
});
|
||||
}
|
||||
|
||||
void MainWindow::showTimerDialog() {
|
||||
TimerDialog* td = new TimerDialog(commands, this);
|
||||
td->show();
|
||||
connect(td, &TimerDialog::accepted, this, [this] {
|
||||
if(ui->actionCommands->isChecked())
|
||||
showCommands();
|
||||
});
|
||||
connect(td, &TimerDialog::accepted,
|
||||
this, [this] { if(ui->actionCommands->isChecked()) list->view(); });
|
||||
}
|
||||
|
||||
void MainWindow::showAboutDialog() {
|
||||
|
||||
@ -32,33 +32,25 @@ class CTTask;
|
||||
class CTVariable;
|
||||
class QListWidgetItem;
|
||||
class Commands;
|
||||
class BaseDelegate;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
CTHost* ctHost;
|
||||
CTCron* cron;
|
||||
CTHost* ctHost = nullptr;
|
||||
CTCron* cron = nullptr;
|
||||
Ui::MainWindow *ui;
|
||||
Commands* commands;
|
||||
BaseDelegate* list = nullptr;
|
||||
void keyPressEvent(QKeyEvent*);
|
||||
void refreshActions(bool);
|
||||
void setIcon(QListWidgetItem* item, bool enabled);
|
||||
void showTasks();
|
||||
void showVariables();
|
||||
void showCommands();
|
||||
/* Main menu */
|
||||
void addEntry();
|
||||
void modifyEntry();
|
||||
void updateWindow();
|
||||
void switchView();
|
||||
void deleteEntry();
|
||||
/* View menu */
|
||||
void viewTasks();
|
||||
void viewVariables();
|
||||
void viewCommands();
|
||||
/* Tools menu */
|
||||
void toggleFilter(bool check);
|
||||
void showAlarmDialog();
|
||||
void showTimerDialog();
|
||||
/* Help menu */
|
||||
void showAboutDialog();
|
||||
|
||||
public:
|
||||
|
||||
@ -267,7 +267,7 @@
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>&Wrap Text</string>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user