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 "messagebox.h"
20 #include "pgmodeleruins.h"
21 #include "baseobjectview.h"
22 
Messagebox(QWidget * parent,Qt::WindowFlags f)23 Messagebox::Messagebox(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
24 {
25 	setupUi(this);
26 	this->setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
27 	cancelled=false;
28 	connect(yes_ok_btn,SIGNAL(clicked()),this,SLOT(handleYesOkClick()));
29 	connect(no_btn,SIGNAL(clicked()),this,SLOT(handleNoCancelClick()));
30 	connect(cancel_btn,SIGNAL(clicked()),this,SLOT(handleNoCancelClick()));
31 	connect(show_errors_tb,SIGNAL(clicked()),this,SLOT(showExceptionList()));
32 	connect(show_errors_tb,SIGNAL(toggled(bool)),show_raw_info_tb,SLOT(setVisible(bool)));
33 	connect(show_raw_info_tb,SIGNAL(toggled(bool)),this,SLOT(showExceptionList()));
34 
35 	show_raw_info_tb->setVisible(false);
36 	error_show_btns_wgt->setVisible(false);
37 	custom_option_chk->setVisible(false);
38 }
39 
handleYesOkClick()40 void Messagebox::handleYesOkClick()
41 {
42 	exceptions_trw->clear();
43 	accept();
44 }
45 
handleNoCancelClick()46 void Messagebox::handleNoCancelClick()
47 {
48 	exceptions_trw->clear();
49 
50 	if((sender()==no_btn && !cancel_btn->isVisible()) ||
51 			(sender()==cancel_btn && !no_btn->isVisible()))
52 		reject();
53 	else if(sender()==no_btn && cancel_btn->isVisible())
54 		reject();
55 	else if(sender()==cancel_btn && no_btn->isVisible())
56 	{
57 		cancelled=true;
58 		reject();
59 	}
60 }
61 
isCancelled()62 bool Messagebox::isCancelled()
63 {
64 	return cancelled;
65 }
66 
setCustomOptionText(const QString & text)67 void Messagebox::setCustomOptionText(const QString &text)
68 {
69 	custom_option_chk->setVisible(!text.isEmpty());
70 	custom_option_chk->setText(text);
71 }
72 
isCustomOptionChecked()73 bool Messagebox::isCustomOptionChecked()
74 {
75 	return custom_option_chk->isChecked();
76 }
77 
showExceptionList()78 void Messagebox::showExceptionList()
79 {
80 	if(show_errors_tb->isChecked())
81 	{
82 		show_errors_tb->setIcon(QPixmap(PgModelerUiNs::getIconPath("desfazer")));
83 
84 		if(show_raw_info_tb->isChecked())
85 			objs_group_wgt->setCurrentIndex(2);
86 		else
87 			objs_group_wgt->setCurrentIndex(1);
88 	}
89 	else if(!show_errors_tb->isVisible() && show_raw_info_tb->isChecked())
90 	{
91 		objs_group_wgt->setCurrentIndex(2);
92 	}
93 	else
94 	{
95 		show_errors_tb->setIcon(QPixmap(PgModelerUiNs::getIconPath("refazer")));
96 		objs_group_wgt->setCurrentIndex(0);
97 	}
98 }
99 
show(Exception e,const QString & msg,unsigned icon_type,unsigned buttons,const QString & yes_lbl,const QString & no_lbl,const QString & cancel_lbl,const QString & yes_ico,const QString & no_ico,const QString & cancel_ico)100 void Messagebox::show(Exception e, const QString &msg, unsigned icon_type, unsigned buttons, const QString &yes_lbl, const QString &no_lbl, const QString &cancel_lbl,
101 					  const QString &yes_ico, const QString &no_ico, const QString &cancel_ico)
102 {
103 	QString str_aux, title;
104 
105 	show_raw_info_tb->blockSignals(true);
106 	show_raw_info_tb->setChecked(false);
107 	show_raw_info_tb->blockSignals(false);
108 
109 	raw_info_txt->setPlainText(e.getExceptionsText());
110 	PgModelerUiNs::createExceptionsTree(exceptions_trw, e, nullptr);
111 	exceptions_trw->expandAll();
112 	exceptions_trw->scrollToTop();
113 
114 	if(msg.isEmpty())
115 		str_aux=PgModelerUiNs::formatMessage(e.getErrorMessage());
116 	else
117 		str_aux=PgModelerUiNs::formatMessage(msg);
118 
119 	this->show(title, str_aux, icon_type, buttons, yes_lbl, no_lbl, cancel_lbl, yes_ico, no_ico, cancel_ico);
120 }
121 
show(const QString & msg,unsigned icon_type,unsigned buttons)122 void Messagebox::show(const QString &msg, unsigned icon_type, unsigned buttons)
123 {
124 	this->show("", msg,  icon_type, buttons);
125 }
126 
show(const QString & title,const QString & msg,unsigned icon_type,unsigned buttons,const QString & yes_lbl,const QString & no_lbl,const QString & cancel_lbl,const QString & yes_ico,const QString & no_ico,const QString & cancel_ico)127 void Messagebox::show(const QString &title, const QString &msg, unsigned icon_type, unsigned buttons, const QString &yes_lbl, const QString &no_lbl,
128 					  const QString &cancel_lbl, const QString &yes_ico, const QString &no_ico, const QString &cancel_ico)
129 {
130 	QString icon_name, aux_title=title;
131 
132 	if(!yes_lbl.isEmpty())
133 		yes_ok_btn->setText(yes_lbl);
134 	else
135 		yes_ok_btn->setText(buttons==OkButton ? tr("&Ok") : tr("&Yes"));
136 
137 	yes_ok_btn->setIcon(!yes_ico.isEmpty() ? QIcon(yes_ico) : QPixmap(PgModelerUiNs::getIconPath("confirmar")));
138 
139 	no_btn->setText(!no_lbl.isEmpty() ? no_lbl : tr("&No"));
140 	no_btn->setIcon(!no_ico.isEmpty() ? QIcon(no_ico) : QPixmap(PgModelerUiNs::getIconPath("fechar1")));
141 
142 	cancel_btn->setText(!cancel_lbl.isEmpty() ? cancel_lbl : tr("&Cancel"));
143 	cancel_btn->setIcon(!cancel_ico.isEmpty() ? QIcon(cancel_ico) : QPixmap(PgModelerUiNs::getIconPath("cancelar")));
144 
145 	no_btn->setVisible(buttons==YesNoButtons || buttons==AllButtons);
146 	cancel_btn->setVisible(buttons==OkCancelButtons || buttons==AllButtons);
147 
148 	if(title.isEmpty())
149 	{
150 		switch(icon_type)
151 		{
152 			case ErrorIcon:
153 				aux_title=tr("Error");
154 			break;
155 
156 			case AlertIcon:
157 				aux_title=tr("Alert");
158 			break;
159 
160 			case InfoIcon:
161 				aux_title=tr("Information");
162 			break;
163 
164 			case ConfirmIcon:
165 				aux_title=tr("Confirmation");
166 			break;
167 		}
168 	}
169 
170 	switch(icon_type)
171 	{
172 		case ErrorIcon:
173 			icon_name=QString("msgbox_erro");
174 		break;
175 
176 		case InfoIcon:
177 			icon_name=QString("msgbox_info");
178 		break;
179 
180 		case AlertIcon:
181 			icon_name=QString("msgbox_alerta");
182 		break;
183 
184 		case ConfirmIcon:
185 			icon_name=QString("msgbox_quest");
186 		break;
187 
188 		default:
189 			icon_name="";
190 		break;
191 	}
192 
193 	cancelled=false;
194 	icon_lbl->setVisible(!icon_name.isEmpty());
195 
196 	if(!icon_name.isEmpty())
197 		icon_lbl->setPixmap(QPixmap(PgModelerUiNs::getIconPath(icon_name)));
198 
199 	msg_lbl->setText(msg);
200 
201 	this->setWindowTitle(aux_title);
202 	this->objs_group_wgt->setCurrentIndex(0);
203 	this->show_errors_tb->setChecked(false);
204 	error_show_btns_wgt->setVisible((exceptions_trw->topLevelItemCount() > 0));
205 	showExceptionList();
206 
207 	this->resize(this->minimumWidth(), this->minimumHeight());
208 
209 	QFontMetrics fm(msg_lbl->font());
210 	QString aux_msg=msg;
211 	aux_msg.replace(QRegExp(QString("(<)(br)(/)?(>)"), Qt::CaseInsensitive), QString("\n"));
212 	QSize size=QSize(msg_lbl->width(), fm.height() * (aux_msg.count('\n') + 1));
213 	int max_h=msg_lbl->minimumHeight() * 3;
214 
215 	//Resizing the message box if the text height is greater than the default size
216 	if(size.height() > msg_lbl->minimumHeight() && size.height() < max_h)
217 		this->setMinimumHeight((size.height() + (size.height() * 0.25))  + show_raw_info_tb->height() + name_lbl->height() + 30);
218 	else if(size.height() >= max_h)
219 		this->setMinimumHeight(max_h);
220 
221 	double factor = BaseObjectView::getScreenDpiFactor();
222 	this->resize(this->minimumWidth() * factor, this->minimumHeight() * factor);
223 
224 	QDialog::exec();
225 }
226