-
Notifications
You must be signed in to change notification settings - Fork 5
/
elementeditor.cpp
280 lines (251 loc) · 7.73 KB
/
elementeditor.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
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
/****************************************************************************
** Copyright (C) 2017 Olaf Japp
**
** This file is part of FlatSiteBuilder.
**
** FlatSiteBuilder is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** FlatSiteBuilder is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with FlatSiteBuilder. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "elementeditor.h"
#include "widgetmimedata.h"
#include "flatbutton.h"
#include "moduldialog.h"
#include "mainwindow.h"
#include "roweditor.h"
#include "sectioneditor.h"
#include "plugins.h"
#include "pageeditor.h"
#include "contenteditor.h"
#include <QMimeData>
#include <QDrag>
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QTest>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QXmlStreamWriter>
ElementEditor::ElementEditor()
{
setAutoFillBackground(true);
setMinimumWidth(120);
setMinimumHeight(50);
setMaximumHeight(50);
m_zoom = false;
m_mode = Mode::Empty;
m_normalColor = QColor(palette().base().color().name()).lighter().name();
m_enabledColor = palette().base().color().name();
m_dropColor = QColor(palette().base().color().name()).lighter().lighter().name();
setColor(m_normalColor);
m_link = new Hyperlink("(+) Insert Module");
m_editButton = new FlatButton(":/images/edit_normal.png", ":/images/edit_hover.png");
m_copyButton = new FlatButton(":/images/copy_normal.png", ":/images/copy_hover.png");
m_closeButton = new FlatButton(":/images/trash_normal.png", ":/images/trash_hover.png");
m_editButton->setVisible(false);
m_copyButton->setVisible(false);
m_closeButton->setVisible(false);
m_editButton->setToolTip("Edit Element");
m_closeButton->setToolTip("Delete Element");
m_copyButton->setToolTip("Copy Element");
m_text = new QLabel("Text");
m_text->setVisible(false);
QHBoxLayout *layout= new QHBoxLayout();
layout->addWidget(m_link, 0, Qt::AlignCenter);
layout->addWidget(m_editButton);
layout->addWidget(m_copyButton);
layout->addWidget(m_text, 1, Qt::AlignCenter);
layout->addWidget(m_closeButton);
setLayout(layout);
connect(m_link, SIGNAL(clicked()), this, SLOT(enable()));
connect(m_editButton, SIGNAL(clicked()), this, SLOT(edit()));
connect(m_copyButton, SIGNAL(clicked()), this, SLOT(copy()));
connect(m_closeButton, SIGNAL(clicked()), this, SLOT(close()));
}
void ElementEditor::load(QXmlStreamReader *stream)
{
m_type = stream->name() + "Editor";
QString label = stream->attributes().value("adminlabel").toString();
if(label.isEmpty())
m_text->setText(stream->name().toString());
else
m_text->setText(label);
ElementEditorInterface * editor = Plugins::getElementPlugin(m_type);
if(editor)
m_content = editor->load(stream);
}
void ElementEditor::setContent(QString content)
{
m_content = content;
QXmlStreamReader stream(content);
stream.readNextStartElement();
m_type = stream.name() + "Editor";
QString label = stream.attributes().value("adminlabel").toString();
if(label.isEmpty())
m_text->setText(stream.name().toString());
else
m_text->setText(label);
}
void ElementEditor::save(QXmlStreamWriter *stream)
{
if(m_mode == Mode::Enabled)
{
stream->device()->write(m_content.toUtf8());
}
}
ElementEditor *ElementEditor::clone()
{
ElementEditor *nee = new ElementEditor();
nee->setMode(m_mode);
nee->setText(m_text->text());
nee->setContent(m_content);
return nee;
}
void ElementEditor::setColor(QString name)
{
QPalette pal = palette();
pal.setColor(QPalette::Background, QColor(name));
setPalette(pal);
}
void ElementEditor::mousePressEvent(QMouseEvent *event)
{
if(m_mode != Mode::Enabled || event->button() != Qt::LeftButton)
return;
if(parentWidget()->layout()->count() == 1)
{
emit elementDragged();
}
WidgetMimeData *mimeData = new WidgetMimeData();
mimeData->setData(this);
QPixmap pixmap(this->size());
render(&pixmap);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(event->pos());
drag->setPixmap(pixmap);
this->hide();
if(drag->exec(Qt::MoveAction) == Qt::IgnoreAction)
this->show();
}
void ElementEditor::dropped()
{
//seems to be a bug that after dropping the item the bgcolor changes
setColor(m_enabledColor);
}
void ElementEditor::setMode(Mode mode)
{
m_mode = mode;
if(mode == Mode::Empty)
{
m_link->setVisible(true);
m_editButton->setVisible(false);
m_copyButton->setVisible(false);
m_closeButton->setVisible(false);
m_text->setVisible(false);
setColor(m_normalColor);
}
else if(mode == Mode::Enabled)
{
m_link->setVisible(false);
m_editButton->setVisible(true);
m_copyButton->setVisible(true);
m_closeButton->setVisible(true);
m_text->setVisible(true);
setColor(m_enabledColor);
}
else if(mode == Mode::Dropzone)
{
m_link->setVisible(false);
m_editButton->setVisible(false);
m_copyButton->setVisible(false);
m_closeButton->setVisible(false);
m_text->setVisible(true);
m_text->setText("Drop Here");
setColor(m_dropColor);
}
}
void ElementEditor::enable()
{
ModulDialog *dlg = new ModulDialog();
dlg->exec();
if(dlg->result().isEmpty())
return;
ElementEditorInterface *editor = Plugins::getElementPlugin(dlg->result());
m_text->setText(editor->displayName());
m_content = "<" + editor->tagName() + "/>";
m_type = editor->className();
setMode(Mode::Enabled);
emit elementEnabled();
edit();
}
void ElementEditor::close()
{
parentWidget()->layout()->removeWidget(this);
this->deleteLater();
ContentEditor *ce = getContentEditor();
if(ce)
ce->editChanged("Delete Element");
}
void ElementEditor::edit()
{
ContentEditor *ce = getContentEditor();
if(ce)
ce->elementEdit(this);;
}
SectionEditor *ElementEditor::getSectionEditor()
{
SectionEditor *se = dynamic_cast<SectionEditor*>(parentWidget());
if(se)
return se;
ColumnEditor *ce = dynamic_cast<ColumnEditor*>(parentWidget());
if(ce)
{
RowEditor *re = dynamic_cast<RowEditor*>(ce->parentWidget());
if(re)
{
SectionEditor *se = dynamic_cast<SectionEditor*>(re->parentWidget());
if(se)
{
return se;
}
}
}
return NULL;
}
ContentEditor *ElementEditor::getContentEditor()
{
SectionEditor *se = getSectionEditor();
if(se)
{
PageEditor *pe = dynamic_cast<PageEditor*>(se->parentWidget());
if(pe)
{
QWidget *sa = dynamic_cast<QWidget*>(pe->parentWidget());
if(sa)
{
QWidget *vp = dynamic_cast<QWidget*>(sa->parentWidget());
if(vp)
{
ContentEditor *cee = dynamic_cast<ContentEditor*>(vp->parentWidget());
if(cee)
return cee;
}
}
}
}
return NULL;
}
void ElementEditor::copy()
{
emit elementCopied(this);
}