1 /*
2 # PostgreSQL Database Modeler (pgModeler)
3 #
4 # Copyright 2006-2020 - Raphael Araújo e Silva <raphael@pgmodeler.io>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # The complete text of GPLv3 is at LICENSE file on source code root directory.
16 # Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
17 */
18
19 #include "rulewidget.h"
20
RuleWidget(QWidget * parent)21 RuleWidget::RuleWidget(QWidget *parent): BaseObjectWidget(parent, ObjectType::Rule)
22 {
23 try
24 {
25 QStringList list;
26 QFrame *frame=nullptr;
27
28 Ui_RuleWidget::setupUi(this);
29
30 cond_expr_hl=new SyntaxHighlighter(cond_expr_txt, false, true);
31 cond_expr_hl->loadConfiguration(GlobalAttributes::getSQLHighlightConfPath());
32
33 command_hl=new SyntaxHighlighter(comando_txt, false, true);
34 command_hl->loadConfiguration(GlobalAttributes::getSQLHighlightConfPath());
35 command_cp=new CodeCompletionWidget(comando_txt);
36
37 commands_tab=new ObjectsTableWidget(ObjectsTableWidget::AllButtons ^ ObjectsTableWidget::DuplicateButton, true, this);
38 commands_tab->setHeaderLabel(tr("SQL command"),0);
39 commands_tab->setHeaderIcon(QPixmap(PgModelerUiNs::getIconPath("codigosql")),0);
40 dynamic_cast<QGridLayout *>(commands_gb->layout())->addWidget(commands_tab, 1, 0, 1, 2);
41
42 frame=generateInformationFrame(tr("To create a rule that does not perform any action (<strong>DO NOTHING</strong>) simply do not specify commands in the SQL commands table."));
43 rule_grid->addWidget(frame, rule_grid->count()+1, 0, 1, 0);
44 frame->setParent(this);
45
46 configureFormLayout(rule_grid, ObjectType::Rule);
47
48 event_cmb->addItems(EventType::getTypes());
49 exec_type_cmb->addItems(ExecutionType::getTypes());
50
51 connect(commands_tab, SIGNAL(s_rowAdded(int)), this, SLOT(handleCommand(int)));
52 connect(commands_tab, SIGNAL(s_rowUpdated(int)), this, SLOT(handleCommand(int)));
53 connect(commands_tab, SIGNAL(s_rowEdited(int)), this, SLOT(editCommand(int)));
54
55 setRequiredField(event_lbl);
56 configureTabOrder();
57
58 setMinimumSize(550, 500);
59 }
60 catch(Exception &e)
61 {
62 throw Exception(e.getErrorMessage(),e.getErrorCode(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
63 }
64 }
65
editCommand(int row)66 void RuleWidget::editCommand(int row)
67 {
68 comando_txt->setPlainText(commands_tab->getCellText(row,0));
69 }
70
handleCommand(int row)71 void RuleWidget::handleCommand(int row)
72 {
73 if(!comando_txt->toPlainText().isEmpty())
74 {
75 commands_tab->setCellText(comando_txt->toPlainText(),row,0);
76 comando_txt->clear();
77 }
78 else if(commands_tab->getCellText(row,0).isEmpty())
79 commands_tab->removeRow(row);
80 }
81
setAttributes(DatabaseModel * model,OperationList * op_list,BaseTable * parent_tab,Rule * rule)82 void RuleWidget::setAttributes(DatabaseModel *model, OperationList *op_list, BaseTable *parent_tab, Rule *rule)
83 {
84 unsigned qtd, i;
85
86 if(!parent_tab)
87 throw Exception(ErrorCode::AsgNotAllocattedObject,__PRETTY_FUNCTION__,__FILE__,__LINE__);
88
89 BaseObjectWidget::setAttributes(model, op_list, rule, parent_tab);
90
91 command_cp->configureCompletion(model, command_hl);
92
93 if(rule)
94 {
95 event_cmb->setCurrentIndex(event_cmb->findText(~rule->getEventType()));
96 exec_type_cmb->setCurrentIndex(exec_type_cmb->findText(~rule->getExecutionType()));
97 cond_expr_txt->setPlainText(rule->getConditionalExpression());
98
99 commands_tab->blockSignals(true);
100 qtd=rule->getCommandCount();
101 for(i=0; i < qtd; i++)
102 {
103 commands_tab->addRow();
104 commands_tab->setCellText(rule->getCommand(i),i,0);
105 }
106 commands_tab->blockSignals(false);
107 }
108 }
109
applyConfiguration()110 void RuleWidget::applyConfiguration()
111 {
112 try
113 {
114 Rule *rule=nullptr;
115 unsigned count, i;
116
117 startConfiguration<Rule>();
118
119 rule=dynamic_cast<Rule *>(this->object);
120 rule->setEventType(EventType(event_cmb->currentText()));
121 rule->setExecutionType(ExecutionType(exec_type_cmb->currentText()));
122 rule->setConditionalExpression(cond_expr_txt->toPlainText().toUtf8());
123 rule->removeCommands();
124
125 count=commands_tab->getRowCount();
126
127 for(i=0; i < count; i++)
128 rule->addCommand(commands_tab->getCellText(i,0).toUtf8());
129
130 BaseObjectWidget::applyConfiguration();
131 finishConfiguration();
132 }
133 catch(Exception &e)
134 {
135 cancelConfiguration();
136 throw Exception(e.getErrorMessage(),e.getErrorCode(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
137 }
138 }
139
140