mirror of
https://github.com/loimu/zeit.git
synced 2025-09-15 12:58:44 +08:00
zeit: do not depend on Ui::MainWindow outside of main window
This commit is contained in:
parent
e6373ecc26
commit
6fb368ab4d
@ -17,13 +17,14 @@
|
||||
* along with Zeit. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QListWidget>
|
||||
|
||||
#include "basedelegate.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
#define QSL QStringLiteral
|
||||
|
||||
|
||||
BaseDelegate::BaseDelegate(Ui::MainWindow* ui_) : ui(ui_)
|
||||
BaseDelegate::BaseDelegate(QListWidget* widget_) : widget(widget_)
|
||||
{
|
||||
}
|
||||
|
||||
@ -33,8 +34,8 @@ void BaseDelegate::setIcon(QListWidgetItem* item, bool enabled) {
|
||||
}
|
||||
|
||||
QString BaseDelegate::elideText(const QString& text) const {
|
||||
const QFontMetrics& fm = ui->listWidget->fontMetrics();
|
||||
const QFontMetrics& fm = widget->fontMetrics();
|
||||
return fm.elidedText(text,
|
||||
isElidedTextEnabled ? Qt::ElideRight : Qt::ElideNone,
|
||||
ui->listWidget->width() - 48 /* hardcoded icon size */);
|
||||
widget->width() - 48 /* hardcoded icon size */);
|
||||
}
|
||||
|
||||
@ -29,11 +29,7 @@
|
||||
#define ROOT_ACTIONS false
|
||||
#endif // BUILD_HELPER
|
||||
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
}
|
||||
|
||||
class QListWidget;
|
||||
class QListWidgetItem;
|
||||
|
||||
class BaseDelegate
|
||||
@ -42,7 +38,7 @@ public:
|
||||
QString caption{};
|
||||
QString toolTip{};
|
||||
|
||||
BaseDelegate(Ui::MainWindow* ui = nullptr);
|
||||
BaseDelegate(QListWidget* widget = nullptr);
|
||||
virtual ~BaseDelegate() {}
|
||||
|
||||
void enableElidedText(bool enabled) { isElidedTextEnabled = enabled; }
|
||||
@ -56,7 +52,7 @@ public:
|
||||
|
||||
protected:
|
||||
bool isElidedTextEnabled = false;
|
||||
Ui::MainWindow* ui;
|
||||
QListWidget* widget;
|
||||
|
||||
void setIcon(QListWidgetItem* item, bool enabled);
|
||||
QString elideText(const QString& text) const;
|
||||
|
||||
@ -18,15 +18,15 @@
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QApplication>
|
||||
#include <QListWidget>
|
||||
|
||||
#include "commands.h"
|
||||
#include "commanddialog.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include "commanddelegate.h"
|
||||
|
||||
|
||||
CommandDelegate::CommandDelegate(Ui::MainWindow* ui, Commands* commands_)
|
||||
: BaseDelegate(ui),
|
||||
CommandDelegate::CommandDelegate(QListWidget* widget, Commands* commands_)
|
||||
: BaseDelegate(widget),
|
||||
commands(commands_)
|
||||
{
|
||||
caption = tr("Command");
|
||||
@ -34,19 +34,18 @@ CommandDelegate::CommandDelegate(Ui::MainWindow* ui, Commands* commands_)
|
||||
}
|
||||
|
||||
void CommandDelegate::view() {
|
||||
ui->labelWarning->hide();
|
||||
ui->listWidget->setEnabled(true);
|
||||
ui->listWidget->clear();
|
||||
widget->setEnabled(true);
|
||||
widget->clear();
|
||||
for(const Command& c: commands->getCommands()) {
|
||||
QListWidgetItem* item = new QListWidgetItem(
|
||||
elideText(c.description) + QChar::fromLatin1('\n') +
|
||||
elideText(tr("Command: ") + c.command));
|
||||
ui->listWidget->addItem(item);
|
||||
widget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void CommandDelegate::createEntry() {
|
||||
CommandDialog* cd = new CommandDialog(commands, ui->listWidget);
|
||||
CommandDialog* cd = new CommandDialog(commands, widget);
|
||||
cd->show();
|
||||
QApplication::connect(cd, &CommandDialog::accepted, cd, [this] { view(); });
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ class CommandDelegate : public BaseDelegate
|
||||
Commands* commands;
|
||||
|
||||
public:
|
||||
CommandDelegate(Ui::MainWindow* ui, Commands* commands);
|
||||
CommandDelegate(QListWidget* widget, Commands* commands);
|
||||
void view() override;
|
||||
void copyEntry(int index) override { Q_UNUSED(index); assert(false); };
|
||||
void createEntry() override;
|
||||
|
||||
@ -18,16 +18,16 @@
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QApplication>
|
||||
#include <QListWidget>
|
||||
|
||||
#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),
|
||||
TaskDelegate::TaskDelegate(QListWidget* widget, CTCron* cron_)
|
||||
: BaseDelegate(widget),
|
||||
cron(cron_)
|
||||
{
|
||||
caption = tr("Task");
|
||||
@ -35,9 +35,8 @@ TaskDelegate::TaskDelegate(Ui::MainWindow* ui, CTCron* cron_)
|
||||
}
|
||||
|
||||
void TaskDelegate::view() {
|
||||
ui->listWidget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
ui->labelWarning->setVisible(ROOT_ACTIONS);
|
||||
ui->listWidget->clear();
|
||||
widget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
widget->clear();
|
||||
for(CTTask* task: cron->tasks()) {
|
||||
QListWidgetItem* item = new QListWidgetItem();
|
||||
setIcon(item, task->enabled);
|
||||
@ -47,7 +46,7 @@ void TaskDelegate::view() {
|
||||
+ task->describe()),
|
||||
elideText(tr("Command: ") + task->command)));
|
||||
item->setText(text);
|
||||
ui->listWidget->addItem(item);
|
||||
widget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,31 +56,28 @@ void TaskDelegate::copyEntry(int index) {
|
||||
newTask->comment = newTask->comment + QChar(0x20) + tr("(Copy)");
|
||||
cron->addTask(newTask);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
view();
|
||||
view();
|
||||
}
|
||||
|
||||
void TaskDelegate::createEntry() {
|
||||
auto* task = new CTTask({}, {}, cron->userLogin(), false);
|
||||
auto* td = new TaskDialog(task, tr("New Task"), ui->listWidget);
|
||||
auto* td = new TaskDialog(task, tr("New Task"), widget);
|
||||
td->show();
|
||||
QApplication::connect(td, &TaskDialog::accepted, td, [this, task] {
|
||||
cron->addTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
view();
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
void TaskDelegate::modifyEntry(int index) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
auto* td = new TaskDialog(task, tr("Edit Task"), ui->listWidget);
|
||||
auto* td = new TaskDialog(task, tr("Edit Task"), widget);
|
||||
td->show();
|
||||
QApplication::connect(td, &TaskDialog::accepted, td, [this, task] {
|
||||
cron->modifyTask(task);
|
||||
cron->save();
|
||||
if(ui->actionTasks->isChecked())
|
||||
view();
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
@ -95,5 +91,5 @@ void TaskDelegate::toggleEntry(int index) {
|
||||
CTTask* task = cron->tasks().at(index);
|
||||
task->enabled = !task->enabled;
|
||||
cron->save();
|
||||
setIcon(ui->listWidget->item(index), task->enabled);
|
||||
setIcon(widget->item(index), task->enabled);
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ class TaskDelegate : public BaseDelegate
|
||||
CTCron* cron;
|
||||
|
||||
public:
|
||||
TaskDelegate(Ui::MainWindow* ui, CTCron* cron);
|
||||
TaskDelegate(QListWidget* widget, CTCron* cron);
|
||||
void view() override;
|
||||
void copyEntry(int index) override;
|
||||
void createEntry() override;
|
||||
|
||||
@ -18,16 +18,16 @@
|
||||
* ======================================================================== */
|
||||
|
||||
#include <QApplication>
|
||||
#include <QListWidget>
|
||||
|
||||
#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),
|
||||
VariableDelegate::VariableDelegate(QListWidget* widget, CTCron* cron_)
|
||||
: BaseDelegate(widget),
|
||||
cron(cron_)
|
||||
{
|
||||
caption = tr("Variable");
|
||||
@ -35,9 +35,8 @@ VariableDelegate::VariableDelegate(Ui::MainWindow* ui, CTCron* cron_)
|
||||
}
|
||||
|
||||
void VariableDelegate::view() {
|
||||
ui->listWidget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
ui->labelWarning->setVisible(ROOT_ACTIONS);
|
||||
ui->listWidget->clear();
|
||||
widget->setEnabled(cron->isCurrentUserCron() || ROOT_ACTIONS);
|
||||
widget->clear();
|
||||
for(CTVariable* var: cron->variables()) {
|
||||
QListWidgetItem* item = new QListWidgetItem();
|
||||
item->setText(QString(QStringLiteral("%1%2=%3"))
|
||||
@ -46,7 +45,7 @@ void VariableDelegate::view() {
|
||||
: QString(QStringLiteral("## %1\n")).arg(var->comment),
|
||||
var->variable, var->value));
|
||||
setIcon(item, var->enabled);
|
||||
ui->listWidget->addItem(item);
|
||||
widget->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,31 +55,28 @@ void VariableDelegate::copyEntry(int index) {
|
||||
newVariable->comment = newVariable->comment + QChar(0x20) + tr("(Copy)");
|
||||
cron->addVariable(newVariable);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
view();
|
||||
view();
|
||||
}
|
||||
|
||||
void VariableDelegate::createEntry() {
|
||||
auto* var = new CTVariable({}, {}, cron->userLogin());
|
||||
auto* vd = new VariableDialog(var, tr("New Variable"), ui->listWidget);
|
||||
auto* vd = new VariableDialog(var, tr("New Variable"), widget);
|
||||
vd->show();
|
||||
QApplication::connect(vd, &VariableDialog::accepted, vd, [this, var] {
|
||||
cron->addVariable(var);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
view();
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
void VariableDelegate::modifyEntry(int index) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
auto* vd = new VariableDialog(var, tr("Edit Variable"), ui->listWidget);
|
||||
auto* vd = new VariableDialog(var, tr("Edit Variable"), widget);
|
||||
vd->show();
|
||||
QApplication::connect(vd, &VariableDialog::accepted, vd, [this, var] {
|
||||
cron->modifyVariable(var);
|
||||
cron->save();
|
||||
if(ui->actionVariables->isChecked())
|
||||
view();
|
||||
view();
|
||||
});
|
||||
}
|
||||
|
||||
@ -94,5 +90,5 @@ void VariableDelegate::toggleEntry(int index) {
|
||||
CTVariable* var = cron->variables().at(index);
|
||||
var->enabled = !var->enabled;
|
||||
cron->save();
|
||||
setIcon(ui->listWidget->item(index), var->enabled);
|
||||
setIcon(widget->item(index), var->enabled);
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ class VariableDelegate : public BaseDelegate
|
||||
CTCron* cron;
|
||||
|
||||
public:
|
||||
VariableDelegate(Ui::MainWindow* ui, CTCron* cron);
|
||||
VariableDelegate(QListWidget* widget, CTCron* cron);
|
||||
void view() override;
|
||||
void copyEntry(int index) override;
|
||||
void createEntry() override;
|
||||
|
||||
@ -73,7 +73,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent),
|
||||
CTInitializationError error;
|
||||
ctHost = new CTHost(QStringLiteral("crontab"), error);
|
||||
cron = ctHost->findCurrentUserCron();
|
||||
list = new TaskDelegate(ui, cron);
|
||||
list = new TaskDelegate(ui->listWidget, cron);
|
||||
/* check if `at` binary is available */
|
||||
QProcess proc;
|
||||
proc.start(QStringLiteral("which"), QStringList{QStringLiteral("at")});
|
||||
@ -211,11 +211,12 @@ void MainWindow::updateWindow() {
|
||||
void MainWindow::switchView() {
|
||||
if(list) { delete list; list = nullptr; }
|
||||
if(ui->actionVariables->isChecked())
|
||||
list = new VariableDelegate(ui, cron);
|
||||
list = new VariableDelegate(ui->listWidget, cron);
|
||||
else if(ui->actionCommands->isChecked())
|
||||
list = new CommandDelegate(ui, commands);
|
||||
list = new CommandDelegate(ui->listWidget, commands);
|
||||
else
|
||||
list = new TaskDelegate(ui, cron);
|
||||
list = new TaskDelegate(ui->listWidget, cron);
|
||||
ui->labelWarning->setVisible(!ui->actionCommands->isChecked() && ROOT_ACTIONS);
|
||||
list->enableElidedText(ui->actionShortenText->isChecked());
|
||||
updateWindow();
|
||||
}
|
||||
|
||||
@ -121,7 +121,7 @@
|
||||
<translation>Команды, выполняются один раз</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/commanddelegate.cpp" line="43"/>
|
||||
<location filename="../src/data/commanddelegate.cpp" line="42"/>
|
||||
<source>Command: </source>
|
||||
<translation>Команда: </translation>
|
||||
</message>
|
||||
@ -358,12 +358,12 @@
|
||||
<translation>Удалить </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/mainwindow.cpp" line="226"/>
|
||||
<location filename="../src/mainwindow.cpp" line="227"/>
|
||||
<source>Deleting %1</source>
|
||||
<translation>Удалить %1</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/mainwindow.cpp" line="227"/>
|
||||
<location filename="../src/mainwindow.cpp" line="228"/>
|
||||
<source>Delete %1?</source>
|
||||
<translation>Удалить %1?</translation>
|
||||
</message>
|
||||
@ -381,33 +381,33 @@
|
||||
<translation>Задания crontab, выполняются периодически</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="45"/>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="44"/>
|
||||
<source>Description: </source>
|
||||
<translation>Описание: </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="46"/>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="45"/>
|
||||
<source>Runs </source>
|
||||
<comment>Runs at 'period described'</comment>
|
||||
<translation>Запуск </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="48"/>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="47"/>
|
||||
<source>Command: </source>
|
||||
<translation>Команда: </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="57"/>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="56"/>
|
||||
<source>(Copy)</source>
|
||||
<translation>(Копия)</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="66"/>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="64"/>
|
||||
<source>New Task</source>
|
||||
<translation>Новая задача</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="78"/>
|
||||
<location filename="../src/data/taskdelegate.cpp" line="75"/>
|
||||
<source>Edit Task</source>
|
||||
<translation>Изменить задачу</translation>
|
||||
</message>
|
||||
@ -602,17 +602,17 @@
|
||||
<translation>Переменные окружения для crontab</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/variabledelegate.cpp" line="56"/>
|
||||
<location filename="../src/data/variabledelegate.cpp" line="55"/>
|
||||
<source>(Copy)</source>
|
||||
<translation>(Копия)</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/variabledelegate.cpp" line="65"/>
|
||||
<location filename="../src/data/variabledelegate.cpp" line="63"/>
|
||||
<source>New Variable</source>
|
||||
<translation>Новая переменная</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../src/data/variabledelegate.cpp" line="77"/>
|
||||
<location filename="../src/data/variabledelegate.cpp" line="74"/>
|
||||
<source>Edit Variable</source>
|
||||
<translation>Изменить переменную</translation>
|
||||
</message>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user