box_entity.cpp 7.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
// Thomas Nagy 2007-2018 GPLV3

#include <QApplication>
#include <QAbstractTextDocumentLayout>
#include <QTextDocument>
#include <QTextDocumentFragment>
#include <QTextList>
#include <QClipboard>
#include <QPainter>
#include <QtDebug>
#include <QAction>
#include <QTextEdit>
#include <QStringList>
#include <QTextDocument>
#include "box_entity.h"
#include "data_item.h"
 #include "res:zable.h"
#include "box_view.h"
 #include "box_link.h"
#include "sem_mediator.h"
#include "mem_box.h"
#include "box_entity_properties.h"

#include "math.h"
#include <QFont>

#define PAD 0.5
#define MIN_FORK_SIZE 30

//#define DEBUG


box_entity::box_entity(box_view* view, int id) : box_item(view, id)
{
        setZValue(80);
	update_size();
}

void box_entity::force_size()
{
	QSizeF l_o = size();

	int l_iWW = fceil(l_o.width(), GRID);
	int l_iHH = fceil(l_o.height(), GRID);

	setRect(0, 0, l_iWW, l_iHH);

	m_iWW = m_oBox->m_iWW = l_iWW;
	m_iHH = m_oBox->m_iHH = l_iHH;
}

void box_entity::paint(QPainter *i_oPainter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	QPen l_oPen;
	l_oPen.setCosmetic(false);
	l_oPen.setWidthF(0.01 + 1);
	if (isSelected()) l_oPen.setStyle(Qt::DotLine);
	else l_oPen.setStyle(Qt::SolidLine);
	i_oPainter->setPen(l_oPen);

	qreal pad = l_oPen.width() / 2.;
	QRectF l_oRect = rect().adjusted(pad, pad, -pad, -pad);


	QColor bc = m_oBox->getColor(m_oView->m_oMediator);
	i_oPainter->setBrush(bc);
	i_oPainter->drawRect(l_oRect);

	l_oPen.setStyle(Qt::SolidLine);

	QRectF l_oInnerRect = l_oRect.adjusted(1 + PAD, 1 + PAD, -1 - PAD, -1 - PAD);

	#ifdef DEBUG
	i_oPainter->save();
	QColor bic("#00ffff");
	i_oPainter->setBrush(bic);
	i_oPainter->drawRect(l_oInnerRect);
	i_oPainter->restore();
	#endif

	qreal l_fHpos = 0;
	{
		QFont l_oBoldFont(scene()->font());
		l_oBoldFont.setBold(true);
		i_oPainter->setFont(l_oBoldFont);
		QFontMetricsF l_oFm(l_oBoldFont);

		QString l_sText = m_oBox->m_sText;
		if (l_sText.isEmpty()) {
			l_sText = notr(" ");
		}

		QRectF l_oR = l_oFm.boundingRect(l_sText);

		l_oR.setWidth(l_oInnerRect.width());
		l_oR.moveTo(l_oInnerRect.topLeft() + QPointF(0, l_fHpos));
		l_oR.adjust(-1, 0, 1, 0);

		#ifdef DEBUG
		i_oPainter->save();
		QColor bc("#ffff00");
		i_oPainter->setBrush(bc);
		i_oPainter->drawRect(l_oR);
		i_oPainter->restore();
		#endif

		i_oPainter->drawText(l_oR, Qt::AlignCenter | Qt::TextSingleLine | Qt::AlignVCenter, l_sText);

		l_fHpos += l_oR.height();
	}

	QFont l_oNormalFont(scene()->font());
	i_oPainter->setFont(l_oNormalFont);
	QFontMetricsF l_oNormalFm(l_oNormalFont);

	if (m_oBox->m_oEntityValues.size() > 0)
	{
		l_fHpos += PAD;
		i_oPainter->drawLine(l_oRect.topLeft() + QPointF(0, l_fHpos +1 + PAD), l_oRect.topRight() + QPointF(0, l_fHpos + 1 + PAD));
		l_fHpos += 1 + PAD;
	}

	qreal l_fSizeAttr = 0;
	qreal l_fSizeType = 0;
	qreal l_fSizeKey = 0;
	foreach (const data_box_entity_value &l_o, m_oBox->m_oEntityValues)
	{
		QRectF l_oR = l_oNormalFm.boundingRect(l_o.m_sName);
		l_fSizeAttr = qMax(l_fSizeAttr, l_oR.width() + 2);

		if (!l_o.m_sType.isEmpty())
		{
			l_oR = l_oNormalFm.boundingRect(l_o.m_sType);
			l_fSizeType = qMax(l_fSizeType, l_oR.width() + 2);
		}
		if (!l_o.m_sKey.isEmpty())
		{
			l_oR = l_oNormalFm.boundingRect(l_o.m_sKey);
			l_fSizeKey = qMax(l_fSizeKey, l_oR.width() + 2);
		}
	}

	qreal l_fSpacer = l_oNormalFm.boundingRect(notr("  ")).width() / 2.;

	qreal l_fLeftPad = PAD;
	if (l_oInnerRect.width() > l_fSpacer * 2 + l_fSizeAttr + l_fSizeType + l_fSizeKey)
	{
		l_fLeftPad = (l_oInnerRect.width() - l_fSpacer * 2 - l_fSizeAttr - l_fSizeType - l_fSizeKey) / 2.;
	}

	foreach (const data_box_entity_value &l_o, m_oBox->m_oEntityValues)
	{
		QRectF l_oR = l_oNormalFm.boundingRect(l_o.m_sName + l_o.m_sType + l_o.m_sKey);

		l_oR.moveTo(l_oInnerRect.topLeft() + QPointF(l_fLeftPad, l_fHpos));

		l_oR.setWidth(l_fSizeAttr);

		#ifdef DEBUG
		i_oPainter->save();
		QColor bc("#ff0000");
		i_oPainter->setBrush(bc);
		i_oPainter->drawRect(l_oR);
		i_oPainter->restore();
		#endif

		i_oPainter->drawText(l_oR, Qt::AlignLeft | Qt::TextSingleLine | Qt::AlignTop, l_o.m_sName);

		if (!l_o.m_sType.isEmpty())
		{
			l_oR.setWidth(l_fSizeType);
			l_oR.translate(l_fSizeAttr + l_fSpacer, 0);

			#ifdef DEBUG
			i_oPainter->save();
			QColor bc("#ff0000");
			i_oPainter->setBrush(bc);
			i_oPainter->drawRect(l_oR);
			i_oPainter->restore();
			#endif

			i_oPainter->drawText(l_oR, Qt::AlignLeft | Qt::TextSingleLine | Qt::AlignTop, l_o.m_sType);

		}
		if (!l_o.m_sType.isEmpty())
		{
			l_oR.translate(l_fSizeType + l_fSpacer, 0);
			l_oR.setWidth(l_fSizeKey);

			#ifdef DEBUG
			i_oPainter->save();
			QColor bc("#ff0000");
			i_oPainter->setBrush(bc);
			i_oPainter->drawRect(l_oR);
			i_oPainter->restore();
			#endif

			i_oPainter->drawText(l_oR, Qt::AlignLeft | Qt::TextSingleLine | Qt::AlignTop, l_o.m_sKey);
		}

		l_fHpos += l_oR.height();
	}
}

QSizeF box_entity::size()
{
	QFont l_oNormalFont(scene()->font());
	QFontMetricsF l_oNormalFm(l_oNormalFont);

	qreal l_iWW = 0, l_iHH = 2 * PAD;
	if (m_oBox->m_oEntityValues.size() > 0) {
		l_iHH += 2 * PAD + 1; // 1 for the line
	}

	qreal l_fSizeAttr = 0;
	qreal l_fSizeType = 0;
	qreal l_fSizeKey = 0;
	foreach (const data_box_entity_value &l_o, m_oBox->m_oEntityValues)
	{
		QRectF l_oR = l_oNormalFm.boundingRect(l_o.m_sName);
		l_fSizeAttr = qMax(l_fSizeAttr, l_oR.width());

		if (!l_o.m_sType.isEmpty())
		{
			l_oR = l_oNormalFm.boundingRect(l_o.m_sType);
			l_fSizeType = qMax(l_fSizeType, l_oR.width());
		}
		if (!l_o.m_sKey.isEmpty())
		{
			l_oR = l_oNormalFm.boundingRect(l_o.m_sKey);
			l_fSizeKey = qMax(l_fSizeKey, l_oR.width());
		}

		QRectF l_oVer = l_oNormalFm.boundingRect(l_o.m_sName + l_o.m_sType + l_o.m_sKey);
		l_iHH += l_oVer.height();
	}
	qreal l_fSpacer = l_oNormalFm.boundingRect(notr("  ")).width() / 2.;

	l_iWW = qMax(l_iWW, l_fSizeAttr + l_fSizeType + l_fSizeKey + 4 * l_fSpacer + 2 * PAD);

	l_oNormalFont.setBold(true);

	{
		QFontMetricsF l_oFm(l_oNormalFont);
		QString l_sText = m_oBox->m_sText;
		if (l_sText.isEmpty()) {
			l_sText = notr(" ");
		}

		QRectF l_oR = l_oFm.boundingRect(l_sText);

		l_iWW = qMax(l_oR.width() + 2 * l_fSpacer, l_iWW);
		l_iHH += l_oR.height();
	}

	// +2 for the border width
	int l_iWWN = (int) (l_iWW + 2 + 2 * PAD);
	int l_iHHN = (int) (l_iHH + 2 * PAD);

	return QSizeF(l_iWWN, l_iHHN); // adjusted
}

void box_entity::properties()
{
	box_entity_properties props(m_oView, this);
	QStringList l_oS;

	l_oS<<notr("entity ")<<props.m_oEntity->m_oBox->m_sText<<"\n";

	int l_oSizeName = 0;
	int l_oSizeType = 0;
	foreach (const data_box_entity_value &l_o, m_oBox->m_oEntityValues)
	{
		l_oSizeName = qMax(l_o.m_sName.size(), l_oSizeName);
		l_oSizeType = qMax(l_o.m_sType.size(), l_oSizeType);
	}

	foreach (const data_box_entity_value &l_o, m_oBox->m_oEntityValues)
	{
		l_oS<<l_o.m_sName.leftJustified(l_oSizeName);
		if (!l_o.m_sType.isEmpty())
		{
			l_oS<<" "<<l_o.m_sType.leftJustified(l_oSizeType);
		}
		if (!l_o.m_sKey.isEmpty())
		{
			l_oS<<" ("<<l_o.m_sKey<<")";
		}
		l_oS<<notr("\n");
	}

	l_oS<<notr("\n");
	props.m_oEntityDefinition->setText(l_oS.join(notr("")));
294
	props.m_oEntityDefinition->selectAll();
295 296 297
	props.exec();
}