summaryrefslogtreecommitdiff
path: root/editor/widget.cc
diff options
context:
space:
mode:
authordeva <deva>2008-07-11 12:54:42 +0000
committerdeva <deva>2008-07-11 12:54:42 +0000
commit5a19d9218a5f3ed7d02b7fabd8f025922e9f7a13 (patch)
tree49ee6645bf55e620db824d0c95508a65d4d596e7 /editor/widget.cc
parent12bd3e7ff088b097bf4e280161aee646fbbb50dc (diff)
Now we have a nice little editor.
Diffstat (limited to 'editor/widget.cc')
-rw-r--r--editor/widget.cc273
1 files changed, 260 insertions, 13 deletions
diff --git a/editor/widget.cc b/editor/widget.cc
index 7b35d03..cc60f9c 100644
--- a/editor/widget.cc
+++ b/editor/widget.cc
@@ -26,35 +26,282 @@
*/
#include "widget.h"
+#include <QApplication>
#include <QDrag>
+
#include <QLayout>
+#include <QBoxLayout>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+
+#include <QPainter>
+
+#include <QFrame>
+#include <QComboBox>
+#include <QLabel>
+#include <QPushButton>
+#include <QCheckBox>
+#include <QLineEdit>
+#include <QTextEdit>
+
+#include <math.h>
+#include <math.h>
+
#include "widgetwrapper.h"
+#include "propertieseditor.h"
+
+Widget::Widget(QDomNode &node)
+ : QWidget()
+{
+ elem = node.toElement();
+
+ iscontainer = false;
+ if(elem.hasAttribute("layout")) {
+ setAcceptDrops(true);
+ dragObject = NULL;
+ iscontainer = true;
+ widget = new QFrame();
+ if(elem.attribute("layout") == "vbox") {
+ orientation = Qt::Vertical;
+ setLayout(new QVBoxLayout());
+ } else {
+ orientation = Qt::Horizontal;
+ setLayout(new QHBoxLayout());
+ }
+ } else {
+ setAcceptDrops(false);
+ if(elem.hasAttribute("name")) {
+ if(elem.attribute("name") == "combo") widget = new QComboBox();
+ else if(elem.attribute("name") == "label") widget = new QLabel();
+ else if(elem.attribute("name") == "button") widget = new QPushButton("OK");
+ else if(elem.attribute("name") == "checkbox") widget = new QCheckBox();
+ else if(elem.attribute("name") == "lineedit") widget = new QLineEdit();
+ else if(elem.attribute("name") == "textedit") widget = new QTextEdit();
+ else widget = new QLabel();
+ } else {
+ widget = new QFrame();
+ }
+ setLayout(new QHBoxLayout());
+ }
+ setSizePolicy(widget->sizePolicy());
+}
-Widget::Widget(QString type)
- : QLabel(type)
+void Widget::setValue(QString name, QString value)
{
- widget = type;
- setAutoFillBackground(true);
- QPalette pal;
- pal.setColor(QPalette::Foreground, Qt::black);
- // pal.setColor(QPalette::Background, Qt::yellow);
- setPalette(pal);
+ elem.attributeNode(name).setValue(value);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
-
- QDrag *drag = new QDrag(this);
- drag->setPixmap(QPixmap("drag.png"));
-
+ dragStartPosition = event->pos();
+
+ // Show properties
+ if(!propertieseditor) propertieseditor = new PropertiesEditor();
+ propertieseditor->setProperties(this);
+ propertieseditor->show();
+ }
+}
+
+void Widget::mouseMoveEvent(QMouseEvent *event)
+{
+ if (!(event->buttons() & Qt::LeftButton)) return;
+
+ if((event->pos() - dragStartPosition).manhattanLength()
+ < QApplication::startDragDistance()) return;
+
+ if(parentWidget()) {
+
+ QDrag *drag = new QDrag(this);
+ widget->resize(width(), height());
+ drag->setPixmap(QPixmap::grabWidget(widget, 0, 0));
+
QMimeData *mimedata = new QMimeData();
mimedata->setData("pracro/widget", wrapWidget(this));
drag->setMimeData(mimedata);
parentWidget()->layout()->removeWidget(this);
setVisible(false);
-
+
drag->exec();
}
}
+
+void Widget::dragEnterEvent(QDragEnterEvent *event)
+{
+ if(!iscontainer) return;
+
+ if(event->mimeData()->hasFormat("pracro/widget")) {
+ event->acceptProposedAction();
+
+ if(dragObject) delete dragObject;
+ QFrame *frame = new QFrame();
+
+ QPalette pal;
+ pal.setColor(QPalette::Foreground, Qt::red);
+ frame->setPalette(pal);
+
+ if(orientation == Qt::Horizontal) {
+ frame->setFixedWidth(1);
+ } else {
+ frame->setFixedHeight(1);
+ }
+
+ frame->setFrameStyle(QFrame::Box | QFrame::Plain);
+ frame->setContentsMargins(1,1,0,0);
+
+ dragObject = frame;
+
+ QWidget *w = findWidget(event->pos());
+ if(w) {
+ int idx = ((QBoxLayout*)layout())->indexOf(w);
+ ((QBoxLayout*)layout())->insertWidget(idx, dragObject);
+ } else {
+ layout()->addWidget(dragObject);
+ }
+ }
+}
+
+void Widget::dragLeaveEvent(QDragLeaveEvent *)
+{
+ if(!iscontainer) return;
+
+ if(dragObject) delete dragObject;
+ dragObject = NULL;
+}
+
+void Widget::dragMoveEvent(QDragMoveEvent *event)
+{
+ if(!iscontainer) return;
+
+ if(event->mimeData()->hasFormat("pracro/widget")) {
+ event->acceptProposedAction();
+
+ layout()->removeWidget(dragObject);
+
+ QWidget *w = findWidget(event->pos());
+ if(w == dragObject) return;
+
+ if(w) {
+ int idx = ((QBoxLayout*)layout())->indexOf(w);
+ ((QBoxLayout*)layout())->insertWidget(idx, dragObject);
+ } else {
+ layout()->addWidget(dragObject);
+ }
+ }
+}
+
+void Widget::dropEvent(QDropEvent *event)
+{
+ if(!iscontainer) return;
+
+ if(event->mimeData()->hasFormat("pracro/widget")) {
+ int idx = layout()->indexOf(dragObject);
+
+ /*
+ QString type = event->mimeData()->data("pracro/widget").data();
+ QWidget *widget;
+ if(type == "horizontal") widget = new Widget(Qt::Horizontal);
+ else if(type == "vertical") widget = new Widget(Qt::Vertical);
+ else widget = new Widget(type);
+ */
+ QWidget *widget = unwrapWidget(event->mimeData()->data("pracro/widget"));
+
+ ((QBoxLayout*)layout())->insertWidget(idx, widget);
+ delete dragObject;
+ dragObject = NULL;
+ event->acceptProposedAction();
+
+ widget->setVisible(true);
+ widget->show();
+ }
+}
+
+QWidget *Widget::findWidget(QPoint pos)
+{
+ QPoint newpos = pos;
+ QWidget *w = childAt(newpos);
+
+ float angle = 0.0;
+ float dist = 0.0;
+ while((!w || QString(w->metaObject()->className()) == "QFrame") && layout()->count()) {
+
+ angle += M_PI / 4;
+ dist += 1;
+
+ newpos = pos + QPoint(sin(angle) * dist, cos(angle) * dist);
+
+ if(newpos.x() > height() && newpos.y() > width() && newpos.y() < 0 && newpos.x() < 0) {
+ break;
+ }
+
+ // printf("%d, %d\n", newpos.x(), newpos.y());
+ w = childAt(newpos);
+ }
+ // printf("Done {%p %s}\n", w, w!=NULL?w->metaObject()->className():"(null)");
+
+ if(w) {
+ int idx = layout()->indexOf(w);
+ // printf("\r%d > %d", pos.y() - w->pos().y(), w->height() / 2); fflush(stdout);
+ if(orientation == Qt::Horizontal) {
+ if(pos.x() - w->pos().x() > w->width() / 2) idx++;
+ } else {
+ if(pos.y() - w->pos().y() > w->height() / 2) idx++;
+ }
+
+ // if(idx < layout()->count()) idx = layout()->count() - 1;
+ if(idx >= 0 && idx < layout()->count()) w = layout()->itemAt(idx)->widget();
+ else w = NULL;
+ }
+
+ return w;
+}
+
+void Widget::paintEvent(QPaintEvent *)
+{
+ int w = width();
+ int h = height();
+ /*
+ if(widget->sizePolicy().controlType() == QSizePolicy::LineEdit ||
+ widget->sizePolicy().controlType() == QSizePolicy::PushButton ||
+ widget->sizePolicy().controlType() == QSizePolicy::ComboBox ||
+ widget->sizePolicy().controlType() == QSizePolicy::CheckBox) {
+ // w > widget->sizeHint().width()) w = widget->sizeHint().width();
+ if(h > widget->sizeHint().height()) h = widget->sizeHint().height();
+ }
+ */
+ widget->resize(w,h);
+ // layout()->addWidget(widget);
+ QPixmap pixmap = QPixmap::grabWidget(widget, 0, 0);
+ // layout()->removeWidget(widget);
+ // widget->setVisible(false);
+
+ QPainter p(this);
+ int y = (height() - pixmap.height()) / 2;
+ p.drawPixmap(0,y,pixmap);
+
+ if(iscontainer) {
+
+ p.setPen(Qt::blue);
+ p.drawRect(0, 0, width()-1, height()-1);
+ p.setPen(QColor(150,150,200));
+ if(orientation == Qt::Vertical) {
+ p.drawLine(4, 2, 2, 4);
+ p.drawLine(4, 2, 6, 4);
+ p.drawLine(4, 2, 4, 12);
+ p.drawLine(4, 12, 2, 10);
+ p.drawLine(4, 12, 6, 10);
+ } else {
+ p.drawLine(2, 4, 4, 2);
+ p.drawLine(2, 4, 4, 6);
+ p.drawLine(2, 4 ,12, 4);
+ p.drawLine(12, 4, 10, 2);
+ p.drawLine(12, 4, 10, 6);
+ }
+ //p.setPen(QColor(128, 128, 128, 128));
+ //p.drawText(0, 10, elem.attribute("name", "Container"));
+ } else {
+ p.setPen(QColor(128, 128, 128, 128));
+ p.drawText(width() / 2 - 20, height() / 2 + 5, elem.attribute("name", "Widget"));
+ }
+}