/
svgdialog.cpp
131 lines (112 loc) · 4.24 KB
/
svgdialog.cpp
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
#include "svgdialog.h"
#include "ui_svgdialog.h"
#include "util.h"
SvgDialog::SvgDialog(QWidget& aParent,CartoType::Framework& aFramework,CStyleItem& aStyleItem):
QDialog(&aParent),
m_framework(aFramework),
m_ui(new Ui::SvgDialog),
m_style_item(aStyleItem)
{
m_ui->setupUi(this);
m_ui->horizontalLayout->setStretchFactor(m_ui->svgText,1);
m_ui->horizontalLayout->setStretchFactor(m_ui->svgImage,1);
m_ui->svgText->setText(m_style_item.Xml(true).c_str());
/*
Prevent the pick-colour button from getting focus so that the text edit
object keeps focus, so the user can carry on editing after picking a colour.
*/
m_ui->pickColorButton->setFocusPolicy(Qt::NoFocus);
DrawSvgImage();
}
SvgDialog::~SvgDialog()
{
delete m_ui;
}
void SvgDialog::DrawSvgImage()
{
std::string svg { (const char*)(m_ui->svgText->toPlainText().toUtf8()) };
CartoType::MemoryInputStream input((const uint8_t*)svg.data(),svg.length());
// Tell the layout to lay itself out so we can get the actual width and height of the image.
m_ui->horizontalLayout->update();
m_ui->horizontalLayout->activate();
int w = m_ui->svgImage->width();
int h = m_ui->svgImage->height();
int size = std::min(w,h);
CartoType::FileLocation error_location;
auto bitmap = m_framework.CreateBitmapFromSvg(input,error_location,size);
if (bitmap && bitmap->Height() > size)
{
double f = double(size) / double(bitmap->Height());
input.Seek(0);
bitmap = m_framework.CreateBitmapFromSvg(input,error_location,int(size * f));
}
QList<QTextEdit::ExtraSelection> selections;
if (!bitmap)
{
QTextEdit::ExtraSelection sel;
sel.cursor = m_ui->svgText->textCursor(); // we have to get the cursor before setting it
int sel_start = (int)error_location.ByteIndex;
int sel_end = sel_start;
sel_start = std::max(0,sel_start - 2);
sel_end = std::min(m_ui->svgText->document()->characterCount(),sel_end + 2);
sel.cursor.setPosition(sel_start,QTextCursor::MoveAnchor);
sel.cursor.setPosition(sel_end,QTextCursor::KeepAnchor);
sel.format.setFontUnderline(true);
sel.format.setUnderlineColor(QColor(255,0,0));
sel.format.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
sel.format.setForeground(QColor(255,0,0));
selections.append(sel);
char buffer[64];
sprintf(buffer,"error at line %d, column %d",(int)error_location.LineNumber,(int)error_location.ColumnNumber);
m_ui->svgImage->setText(buffer);
}
m_ui->svgText->setExtraSelections(selections);
if (bitmap)
{
QImage image(bitmap->Width(),bitmap->Height(),QImage::Format_ARGB32_Premultiplied);
CopyBitmapToImage(*bitmap,image);
m_ui->svgImage->setPixmap(QPixmap::fromImage(std::move(image)));
}
}
void SvgDialog::on_updateButton_clicked()
{
DrawSvgImage();
}
void SvgDialog::on_pickColorButton_clicked()
{
QTextCursor cursor { m_ui->svgText->textCursor() };
QString sel = cursor.selectedText();
int sel_start = cursor.selectionStart();
std::string color = PickColor(*this,sel.toStdString(),false);
if (color.length())
{
cursor.insertText(QString(color.c_str()));
cursor.setPosition(sel_start);
cursor.setPosition(sel_start + (int)color.length(),QTextCursor::KeepAnchor);
m_ui->svgText->setTextCursor(cursor);
DrawSvgImage();
}
}
std::string SvgDialog::SvgText()
{
return m_ui->svgText->toPlainText().toStdString();
}
void SvgDialog::on_svgText_selectionChanged()
{
QTextCursor cursor { m_ui->svgText->textCursor() };
QString sel = cursor.selectedText();
CartoType::String s(sel.toUtf8());
CartoType::Color color;
size_t length = color.Set(s);
m_ui->pickColorButton->setEnabled(length && length == s.Length());
}
void SvgDialog::on_redoButton_clicked()
{
m_ui->svgText->redo();
DrawSvgImage();
}
void SvgDialog::on_undoButton_clicked()
{
m_ui->svgText->undo();
DrawSvgImage();
}