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 "conversionwidget.h"
20
ConversionWidget(QWidget * parent)21 ConversionWidget::ConversionWidget(QWidget *parent): BaseObjectWidget(parent, ObjectType::Conversion)
22 {
23 try
24 {
25 QFrame *frame=nullptr;
26 Ui_ConversionWidget::setupUi(this);
27
28 conv_func_sel=nullptr;
29 conv_func_sel=new ObjectSelectorWidget(ObjectType::Function, true, this);
30 convcod_grid->addWidget(conv_func_sel,1,1,1,3);
31
32 setRequiredField(src_encoding_lbl);
33 setRequiredField(trg_encoding_lbl);
34 setRequiredField(conv_func_lbl);
35 setRequiredField(conv_func_sel);
36
37 configureFormLayout(convcod_grid, ObjectType::Conversion);
38 frame=generateInformationFrame(tr("The function to be assigned to an encoding conversion must have the following signature: <em>void function(integer, integer, cstring, internal, integer)</em>."));
39 convcod_grid->addItem(new QSpacerItem(10,10,QSizePolicy::Minimum,QSizePolicy::Expanding), convcod_grid->count()+1, 0, 1, 0);
40 convcod_grid->addWidget(frame, convcod_grid->count()+1, 0, 1, 0);
41 frame->setParent(this);
42
43 src_encoding_cmb->addItems(EncodingType::getTypes());
44 trg_encoding_cmb->addItems(EncodingType::getTypes());
45
46 configureTabOrder({ src_encoding_cmb, trg_encoding_cmb, conv_func_sel });
47
48 setMinimumSize(500, 300);
49 }
50 catch(Exception &e)
51 {
52 throw Exception(e.getErrorMessage(),e.getErrorCode(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
53 }
54 }
55
setAttributes(DatabaseModel * model,OperationList * op_list,Schema * schema,Conversion * conv)56 void ConversionWidget::setAttributes(DatabaseModel *model, OperationList *op_list, Schema *schema, Conversion *conv)
57 {
58 BaseObjectWidget::setAttributes(model, op_list, conv, schema);
59 conv_func_sel->setModel(model);
60
61 if(conv)
62 {
63 conv_func_sel->setSelectedObject(conv->getConversionFunction());
64 default_conv_chk->setChecked(conv->isDefault());
65 src_encoding_cmb->setCurrentIndex(trg_encoding_cmb->findText(~(conv->getEncoding(Conversion::SrcEncoding))));
66 trg_encoding_cmb->setCurrentIndex(trg_encoding_cmb->findText(~(conv->getEncoding(Conversion::DstEncoding))));
67 }
68 }
69
applyConfiguration()70 void ConversionWidget::applyConfiguration()
71 {
72 try
73 {
74 Conversion *conv=nullptr;
75
76 startConfiguration<Conversion>();
77 conv=dynamic_cast<Conversion *>(this->object);
78
79 BaseObjectWidget::applyConfiguration();
80
81 conv->setEncoding(Conversion::SrcEncoding, src_encoding_cmb->currentText());
82 conv->setEncoding(Conversion::DstEncoding, trg_encoding_cmb->currentText());
83 conv->setDefault(default_conv_chk->isChecked());
84 conv->setConversionFunction(dynamic_cast<Function*>(conv_func_sel->getSelectedObject()));
85
86 finishConfiguration();
87 }
88 catch(Exception &e)
89 {
90 cancelConfiguration();
91 throw Exception(e.getErrorMessage(),e.getErrorCode(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
92 }
93 }
94
95