From 5a19d9218a5f3ed7d02b7fabd8f025922e9f7a13 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 11 Jul 2008 12:54:42 +0000 Subject: Now we have a nice little editor. --- editor/editor.cc | 47 +++++++- editor/editor.pro | 10 +- editor/macrowindow.cc | 158 +------------------------- editor/macrowindow.h | 21 +--- editor/propertieseditor.cc | 79 +++++++++++++ editor/propertieseditor.h | 52 +++++++++ editor/property.cc | 89 +++++++++++++++ editor/property.h | 66 +++++++++++ editor/tool.cc | 26 +++-- editor/tool.h | 4 +- editor/toolbox.cc | 29 ++--- editor/toolbox.h | 3 +- editor/widget.cc | 273 ++++++++++++++++++++++++++++++++++++++++++--- editor/widget.h | 32 +++++- editor/widgets.xml | 12 ++ editor/widgetwrapper.cc | 8 +- 16 files changed, 679 insertions(+), 230 deletions(-) create mode 100644 editor/propertieseditor.cc create mode 100644 editor/propertieseditor.h create mode 100644 editor/property.cc create mode 100644 editor/property.h create mode 100644 editor/widgets.xml (limited to 'editor') diff --git a/editor/editor.cc b/editor/editor.cc index b2d418f..e7d8781 100644 --- a/editor/editor.cc +++ b/editor/editor.cc @@ -28,17 +28,58 @@ #include "toolbox.h" #include "macrowindow.h" +#include "propertieseditor.h" -#include "widgetwrapper.h" +#include +#include + +#define OFFSET_X 300 +#define OFFSET_Y 300 +#define SPACING 10 int main(int argc, char *argv[]) { QApplication app(argc, argv); - Toolbox toolbox; - MacroWindow macrowindow(Qt::Horizontal); + + // + // Load xml data + // + QDomDocument doc("widgets"); + QFile file("widgets.xml"); + if (!file.open(QIODevice::ReadOnly)) + return 1; + if (!doc.setContent(&file)) { + file.close(); + return 1; + } + file.close(); + + QDomElement docElem = doc.documentElement(); + + QDomNode n = docElem.firstChild(); + QDomNode node; + while(!n.isNull()) { + QDomElement e = n.toElement(); + if(!e.isNull()) { + if(e.hasAttribute("name") && e.attribute("name") == "mainwidget") node = e; + } + n = n.nextSibling(); + } + + Toolbox toolbox(docElem); + toolbox.move(OFFSET_X, OFFSET_Y); + toolbox.show(); + + MacroWindow macrowindow(node); macrowindow.resize(400, 300); + macrowindow.move(toolbox.width() + OFFSET_X + SPACING, OFFSET_Y); macrowindow.show(); + propertieseditor = new PropertiesEditor(); + propertieseditor->setProperties(¯owindow); + propertieseditor->move(macrowindow.width() + toolbox.width() + OFFSET_X + 2 * SPACING, OFFSET_Y); + propertieseditor->show(); + return app.exec(); } diff --git a/editor/editor.pro b/editor/editor.pro index eda7158..e14b068 100644 --- a/editor/editor.pro +++ b/editor/editor.pro @@ -4,6 +4,7 @@ TEMPLATE = app TARGET = DEPENDPATH += . INCLUDEPATH += . +QT += core gui network xml # For debugging QMAKE_CXXFLAGS += -g -Wall -Werror @@ -14,7 +15,10 @@ HEADERS += \ tool.h \ macrowindow.h \ widget.h \ - widgetwrapper.h + widgetwrapper.h \ + propertieseditor.h \ + property.h + SOURCES += \ editor.cc \ @@ -22,4 +26,6 @@ SOURCES += \ tool.cc \ macrowindow.cc \ widget.cc \ - widgetwrapper.cc + widgetwrapper.cc \ + propertieseditor.cc \ + property.cc diff --git a/editor/macrowindow.cc b/editor/macrowindow.cc index c3f8185..13a4a3b 100644 --- a/editor/macrowindow.cc +++ b/editor/macrowindow.cc @@ -26,19 +26,11 @@ */ #include "macrowindow.h" -#include -#include -#include -#include - -#include - -#include "widget.h" -#include "widgetwrapper.h" - -MacroWindow::MacroWindow(Qt::Orientation orientation) - : QFrame() +MacroWindow::MacroWindow(QDomNode &node) + : Widget(node) { + + /* this->orientation = orientation; setAcceptDrops(true); if(orientation == Qt::Horizontal) { @@ -55,146 +47,6 @@ MacroWindow::MacroWindow(Qt::Orientation orientation) QPalette pal; pal.setColor(QPalette::Foreground, Qt::blue); setPalette(pal); + */ } -void MacroWindow::dragEnterEvent(QDragEnterEvent *event) -{ - 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 MacroWindow::dragLeaveEvent(QDragLeaveEvent *) -{ - if(dragObject) delete dragObject; - dragObject = NULL; -} - -void MacroWindow::dragMoveEvent(QDragMoveEvent *event) -{ - 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 MacroWindow::dropEvent(QDropEvent *event) -{ - 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 MacroWindow(Qt::Horizontal); - else if(type == "vertical") widget = new MacroWindow(Qt::Vertical); - else widget = new Widget(type); - */ - QWidget *widget = unwrapWidget(event->mimeData()->data("pracro/widget")); - widget->setVisible(true); - - ((QBoxLayout*)layout())->insertWidget(idx, widget); - delete dragObject; - dragObject = NULL; - event->acceptProposedAction(); - } -} - -QWidget *MacroWindow::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 MacroWindow::mousePressEvent(QMouseEvent *event) -{ - if(event->button() == Qt::LeftButton) { - - QDrag *drag = new QDrag(this); - drag->setPixmap(QPixmap("drag.png")); - - QMimeData *mimedata = new QMimeData(); - - mimedata->setData("pracro/widget", wrapWidget(this)); - - drag->setMimeData(mimedata); - - QWidget *parent = parentWidget(); - if(parent) { - parent->layout()->removeWidget(this); - setVisible(false); - drag->exec(); - } - } -} diff --git a/editor/macrowindow.h b/editor/macrowindow.h index 5ff375f..e6c2876 100644 --- a/editor/macrowindow.h +++ b/editor/macrowindow.h @@ -27,27 +27,14 @@ #ifndef __PRACRO_MACROWINDOW_H__ #define __PRACRO_MACROWINDOW_H__ -#include -#include -#include +#include "widget.h" +#include -class MacroWindow : public QFrame +class MacroWindow : public Widget { Q_OBJECT public: - MacroWindow(Qt::Orientation orientation); - -protected: - void dragEnterEvent(QDragEnterEvent *event); - void dragLeaveEvent(QDragLeaveEvent *event); - void dragMoveEvent(QDragMoveEvent *event); - void dropEvent(QDropEvent *event); - void mousePressEvent(QMouseEvent *event); - -private: - Qt::Orientation orientation; - QWidget *dragObject; - QWidget *findWidget(QPoint pos); + MacroWindow(QDomNode &node); }; #endif/*__PRACRO_MACROWINDOW_H__*/ diff --git a/editor/propertieseditor.cc b/editor/propertieseditor.cc new file mode 100644 index 0000000..5a1d4da --- /dev/null +++ b/editor/propertieseditor.cc @@ -0,0 +1,79 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * propertieseditor.cc + * + * Mon Jul 7 09:37:21 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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 2 of the License, or + * (at your option) any later version. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "propertieseditor.h" + +#include "property.h" +#include + +PropertiesEditor::PropertiesEditor() + : QDialog() +{ + + setAutoFillBackground(true); + QPalette pal; + // pal.setColor(QPalette::Foreground, Qt::black); + pal.setColor(QPalette::Background, Qt::black); + setPalette(pal); + + setContentsMargins(0,0,0,0); + setLayout(new QVBoxLayout()); + layout()->setSpacing(1); + layout()->setContentsMargins(0,0,0,0); + + setFixedWidth(250); +} + +void PropertiesEditor::setProperties(Widget *widget) +{ + while(layout()->count()) delete (layout()->takeAt(0))->widget(); + + this->widget = widget; + + QDomNamedNodeMap map = widget->elem.attributes(); + for(size_t i = 0; i < map.length(); i++) { + QDomAttr attr = map.item(i).toAttr(); + QString name = attr.name(); + QString value = attr.value(); + layout()->addWidget(new Property(widget, name, LINEEDIT, value)); + } + + /* + layout()->addWidget(new Property("Dims2", LINEEDIT, "somevalue")); + layout()->addWidget(new Property("Dims3", TEXTEDIT, "somevalue")); + layout()->addWidget(new Property("Dims4", CHECKBOX, "somevalue")); + layout()->addWidget(new Property("Dims5", TEXTEDIT, "somevalue")); + layout()->addWidget(new Property("Dims6", LINEEDIT, "somevalue")); + */ +} +/* +void PropertiesEditor::destroy(bool , bool ) +{ + printf("DETROY!\n"); + propertieseditor = NULL; +} +*/ +PropertiesEditor *propertieseditor = NULL; diff --git a/editor/propertieseditor.h b/editor/propertieseditor.h new file mode 100644 index 0000000..dae4082 --- /dev/null +++ b/editor/propertieseditor.h @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * propertieseditor.h + * + * Mon Jul 7 09:37:21 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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 2 of the License, or + * (at your option) any later version. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_PROPERTIESEDITOR_H__ +#define __PRACRO_PROPERTIESEDITOR_H__ + +#include + +#include "widget.h" +#include "macrowindow.h" + +class PropertiesEditor : public QDialog { +Q_OBJECT +public: + PropertiesEditor(); + + void setProperties(Widget *widget); + +protected: + // void destroy(bool destroyWindow = true, bool destroySubWindows = true); + +private: + Widget *widget; + MacroWindow *macrowindow; +}; + +extern PropertiesEditor *propertieseditor; + +#endif/*__PRACRO_PROPERTIESEDITOR_H__*/ diff --git a/editor/property.cc b/editor/property.cc new file mode 100644 index 0000000..82db9c3 --- /dev/null +++ b/editor/property.cc @@ -0,0 +1,89 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * property.cc + * + * Mon Jul 7 09:57:31 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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 2 of the License, or + * (at your option) any later version. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "property.h" + +#include +#include + +Property::Property(Widget *widget, QString label, PropertyType type, QString value) +{ + setAutoFillBackground(true); + QPalette pal; + // pal.setColor(QPalette::Foreground, Qt::black); + pal.setColor(QPalette::Background, QColor(230, 230, 230)); + setPalette(pal); + + this->widget = widget; + + this->type = type; + this->label = new QLabel(label); + this->label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft); + this->label->setFixedWidth(80); + + setContentsMargins(0,0,0,0); + setLayout(new QHBoxLayout()); + layout()->addWidget(this->label); + layout()->setSpacing(1); + layout()->setContentsMargins(0,0,0,0); + + switch(this->type) { + case COMBO: + combo = new QComboBox(); + combo->addItem("Item1"); + combo->addItem("Item2"); + combo->addItem("Item3"); + combo->addItem("Item4"); + combo->addItem("Item5"); + combo->addItem(value); + combo->setCurrentIndex(combo->findText(value)); + layout()->addWidget(combo); + break; + case LINEEDIT: + lineedit = new QLineEdit(value); + layout()->addWidget(lineedit); + connect(lineedit, SIGNAL(textChanged(QString)), this, SLOT(changed())); + break; + case TEXTEDIT: + textedit = new QTextEdit(); + textedit->setPlainText(value); + layout()->addWidget(textedit); + this->label->setAlignment(Qt::AlignTop | Qt::AlignLeft); + break; + case CHECKBOX: + checkbox = new QCheckBox(); + if(value == "true") checkbox->setChecked(true); + else checkbox->setChecked(false); + layout()->addWidget(checkbox); + break; + } +} + +void Property::changed() +{ + QString value = lineedit->text(); + widget->setValue(label->text(), value); +} diff --git a/editor/property.h b/editor/property.h new file mode 100644 index 0000000..704f972 --- /dev/null +++ b/editor/property.h @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * property.h + * + * Mon Jul 7 09:57:31 CEST 2008 + * Copyright 2008 Bent Bisballe Nyeng + * deva@aasimon.org + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro 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 2 of the License, or + * (at your option) any later version. + * + * Pracro 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 Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_PROPERTY_H__ +#define __PRACRO_PROPERTY_H__ + +#include +#include +#include +#include +#include +#include + +#include "widget.h" + +typedef enum { + COMBO, + LINEEDIT, + TEXTEDIT, + CHECKBOX +} PropertyType; + +class Property : public QWidget { +Q_OBJECT +public: + Property(Widget *widget, QString label, PropertyType type, QString value); + +public slots: + void changed(); + +private: + PropertyType type; + + Widget *widget; + + QLabel *label; + QComboBox *combo; + QLineEdit *lineedit; + QTextEdit *textedit; + QCheckBox *checkbox; +}; + +#endif/*__PRACRO_PROPERTY_H__*/ diff --git a/editor/tool.cc b/editor/tool.cc index b9ed07d..c4686d2 100644 --- a/editor/tool.cc +++ b/editor/tool.cc @@ -32,12 +32,18 @@ #include "macrowindow.h" #include "widget.h" -Tool::Tool(QPixmap &pixmap, QString widget) +#include + +Tool::Tool(QDomNode &node) : QLabel() { - setPixmap(pixmap); - this->pixmap = pixmap; - this->widget = widget; + this->node = node; + QDomElement elem = node.toElement(); + if(elem.hasAttribute("name")) { + setText(elem.attribute("name")); + } else { + setText("Unknown"); + } show(); } @@ -46,16 +52,12 @@ void Tool::mousePressEvent(QMouseEvent *event) if(event->button() == Qt::LeftButton) { QDrag *drag = new QDrag(this); - drag->setPixmap(pixmap); + drag->setPixmap(QPixmap::grabWidget(this, 0, 0)); QMimeData *mimedata = new QMimeData(); - if(widget == "vertical") { - mimedata->setData("pracro/widget", wrapWidget(new MacroWindow( Qt::Vertical))); - } else if(widget == "horizontal") { - mimedata->setData("pracro/widget", wrapWidget(new MacroWindow( Qt::Horizontal))); - } else { - mimedata->setData("pracro/widget", wrapWidget(new Widget(widget.toAscii()))); - } + + mimedata->setData("pracro/widget", wrapWidget(new Widget(node))); + drag->setMimeData(mimedata); drag->exec(); diff --git a/editor/tool.h b/editor/tool.h index a9ef1a6..8561f94 100644 --- a/editor/tool.h +++ b/editor/tool.h @@ -30,16 +30,18 @@ #include #include #include +#include class Tool : public QLabel { Q_OBJECT public: - Tool(QPixmap &pixmap, QString widget); + Tool(QDomNode &node); protected: void mousePressEvent(QMouseEvent *event); private: + QDomNode node; QPixmap pixmap; QString widget; }; diff --git a/editor/toolbox.cc b/editor/toolbox.cc index 9e75e66..57c2ad1 100644 --- a/editor/toolbox.cc +++ b/editor/toolbox.cc @@ -29,27 +29,18 @@ #include #include "tool.h" -Toolbox::Toolbox() +Toolbox::Toolbox(QDomNode &node) : QDialog() { setLayout(new QVBoxLayout()); - QPixmap pixmap("drag.png"); - - Tool *tool1 = new Tool(pixmap, "Tool1"); - layout()->addWidget(tool1); - - Tool *tool2 = new Tool(pixmap, "Tool2"); - layout()->addWidget(tool2); - - Tool *tool3 = new Tool(pixmap, "Tool3"); - layout()->addWidget(tool3); - - Tool *tool4 = new Tool(pixmap, "vertical"); - layout()->addWidget(tool4); - - Tool *tool5 = new Tool(pixmap, "horizontal"); - layout()->addWidget(tool5); - - show(); + QDomNode n = node.firstChild(); + while(!n.isNull()) { + QDomElement e = n.toElement(); + if(!e.isNull()) { + Tool *tool = new Tool(e); + layout()->addWidget(tool); + } + n = n.nextSibling(); + } } diff --git a/editor/toolbox.h b/editor/toolbox.h index e703c35..a74293a 100644 --- a/editor/toolbox.h +++ b/editor/toolbox.h @@ -28,11 +28,12 @@ #define __PRACRO_TOOLBOX_H__ #include +#include class Toolbox : public QDialog { Q_OBJECT public: - Toolbox(); + Toolbox(QDomNode &node); }; #endif/*__PRACRO_TOOLBOX_H__*/ 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 #include + #include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + #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")); + } +} diff --git a/editor/widget.h b/editor/widget.h index 1cb7d9e..20baef9 100644 --- a/editor/widget.h +++ b/editor/widget.h @@ -27,21 +27,43 @@ #ifndef __PRACRO_WIDGET_H__ #define __PRACRO_WIDGET_H__ -#include +#include #include #include +#include +#include -class Widget : public QLabel { +class Widget : public QWidget { Q_OBJECT public: - Widget(QString type); + Widget(QDomNode &node); + + QString type; + + void setValue(QString name, QString value); + + QDomElement elem; protected: void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + + void dragEnterEvent(QDragEnterEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dropEvent(QDropEvent *event); + + void paintEvent(QPaintEvent *event); private: - QPixmap pixmap; - QString widget; + bool iscontainer; + Qt::Orientation orientation; + + QWidget *dragObject; + QWidget *findWidget(QPoint pos); + + QWidget *widget; + QPoint dragStartPosition; }; #endif/*__PRACRO_WIDGET_H__*/ diff --git a/editor/widgets.xml b/editor/widgets.xml new file mode 100644 index 0000000..e1ed490 --- /dev/null +++ b/editor/widgets.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/editor/widgetwrapper.cc b/editor/widgetwrapper.cc index 83aa524..435903b 100644 --- a/editor/widgetwrapper.cc +++ b/editor/widgetwrapper.cc @@ -32,7 +32,7 @@ QByteArray wrapWidget(QWidget *widget) { - printf("Wrapping %p\n", widget); + // printf("Wrapping %p\n", widget); QByteArray ba; /* @@ -47,7 +47,7 @@ QByteArray wrapWidget(QWidget *widget) sprintf(buf, "%p", widget); ba = buf; - printf(" = (%d) %s\n", ba.size(), ba.data()); + // printf(" = (%d) %s\n", ba.size(), ba.data()); return ba; } @@ -55,7 +55,7 @@ QByteArray wrapWidget(QWidget *widget) QWidget *unwrapWidget(QByteArray bytes) { QWidget *widget; - printf("Unwrapping (%d) %s\n", bytes.size(), bytes.data()); + // printf("Unwrapping (%d) %s\n", bytes.size(), bytes.data()); /* QVariant var; @@ -69,7 +69,7 @@ QWidget *unwrapWidget(QByteArray bytes) */ sscanf(bytes.data(), "%p", &widget); - printf(" = %p\n", widget); + // printf(" = %p\n", widget); return widget; } -- cgit v1.2.3