From 12bd3e7ff088b097bf4e280161aee646fbbb50dc Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 7 Jul 2008 07:36:42 +0000 Subject: Made actual widgets get dragged around --- editor/editor.cc | 3 ++ editor/editor.pro | 16 +++++++++-- editor/macrowindow.cc | 14 +++++---- editor/tool.cc | 11 +++++++- editor/widget.cc | 3 +- editor/widgetwrapper.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ editor/widgetwrapper.h | 36 ++++++++++++++++++++++++ 7 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 editor/widgetwrapper.cc create mode 100644 editor/widgetwrapper.h (limited to 'editor') diff --git a/editor/editor.cc b/editor/editor.cc index 780b6dc..b2d418f 100644 --- a/editor/editor.cc +++ b/editor/editor.cc @@ -29,6 +29,8 @@ #include "toolbox.h" #include "macrowindow.h" +#include "widgetwrapper.h" + int main(int argc, char *argv[]) { QApplication app(argc, argv); @@ -36,6 +38,7 @@ int main(int argc, char *argv[]) Toolbox toolbox; MacroWindow macrowindow(Qt::Horizontal); macrowindow.resize(400, 300); + macrowindow.show(); return app.exec(); } diff --git a/editor/editor.pro b/editor/editor.pro index 8d27503..eda7158 100644 --- a/editor/editor.pro +++ b/editor/editor.pro @@ -9,5 +9,17 @@ INCLUDEPATH += . QMAKE_CXXFLAGS += -g -Wall -Werror # Input -HEADERS += toolbox.h tool.h macrowindow.h widget.h -SOURCES += editor.cc toolbox.cc tool.cc macrowindow.cc widget.cc +HEADERS += \ + toolbox.h \ + tool.h \ + macrowindow.h \ + widget.h \ + widgetwrapper.h + +SOURCES += \ + editor.cc \ + toolbox.cc \ + tool.cc \ + macrowindow.cc \ + widget.cc \ + widgetwrapper.cc diff --git a/editor/macrowindow.cc b/editor/macrowindow.cc index 761f107..c3f8185 100644 --- a/editor/macrowindow.cc +++ b/editor/macrowindow.cc @@ -34,6 +34,7 @@ #include #include "widget.h" +#include "widgetwrapper.h" MacroWindow::MacroWindow(Qt::Orientation orientation) : QFrame() @@ -47,7 +48,7 @@ MacroWindow::MacroWindow(Qt::Orientation orientation) } // layout()->setSpacing(0); // layout()->setContentsMargins(2,2,2,2); - show(); + // show(); dragObject = NULL; setFrameStyle(QFrame::Box | QFrame::Plain); @@ -119,11 +120,15 @@ 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; @@ -181,11 +186,8 @@ void MacroWindow::mousePressEvent(QMouseEvent *event) QMimeData *mimedata = new QMimeData(); - if(orientation == Qt::Horizontal) { - mimedata->setData("pracro/widget", "horizontal"); - } else { - mimedata->setData("pracro/widget", "vertical"); - } + mimedata->setData("pracro/widget", wrapWidget(this)); + drag->setMimeData(mimedata); QWidget *parent = parentWidget(); diff --git a/editor/tool.cc b/editor/tool.cc index 4a40c47..b9ed07d 100644 --- a/editor/tool.cc +++ b/editor/tool.cc @@ -28,6 +28,9 @@ #include //#include +#include "widgetwrapper.h" +#include "macrowindow.h" +#include "widget.h" Tool::Tool(QPixmap &pixmap, QString widget) : QLabel() @@ -46,7 +49,13 @@ void Tool::mousePressEvent(QMouseEvent *event) drag->setPixmap(pixmap); QMimeData *mimedata = new QMimeData(); - mimedata->setData("pracro/widget", widget.toAscii()); + 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()))); + } drag->setMimeData(mimedata); drag->exec(); diff --git a/editor/widget.cc b/editor/widget.cc index 15bcd2f..7b35d03 100644 --- a/editor/widget.cc +++ b/editor/widget.cc @@ -28,6 +28,7 @@ #include #include +#include "widgetwrapper.h" Widget::Widget(QString type) : QLabel(type) @@ -48,7 +49,7 @@ void Widget::mousePressEvent(QMouseEvent *event) drag->setPixmap(QPixmap("drag.png")); QMimeData *mimedata = new QMimeData(); - mimedata->setData("pracro/widget", widget.toAscii()); + mimedata->setData("pracro/widget", wrapWidget(this)); drag->setMimeData(mimedata); parentWidget()->layout()->removeWidget(this); diff --git a/editor/widgetwrapper.cc b/editor/widgetwrapper.cc new file mode 100644 index 0000000..83aa524 --- /dev/null +++ b/editor/widgetwrapper.cc @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * widgetwrapper.cc + * + * Mon Jul 7 07:42:04 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 "widgetwrapper.h" + +#include +#include +#include + +QByteArray wrapWidget(QWidget *widget) +{ + printf("Wrapping %p\n", widget); + QByteArray ba; + + /* + QVariant var = qVariantFromValue(widget); + ba = var.toByteArray(); + */ + /* + QDataStream ds(&ba, QIODevice::WriteOnly); + bool err = QMetaType::save(ds, QMetaType::QWidgetStar, widget); + */ + char buf[32]; + sprintf(buf, "%p", widget); + ba = buf; + + printf(" = (%d) %s\n", ba.size(), ba.data()); + return ba; + +} + +QWidget *unwrapWidget(QByteArray bytes) +{ + QWidget *widget; + printf("Unwrapping (%d) %s\n", bytes.size(), bytes.data()); + + /* + QVariant var; + var << *bytes; + widget = (QWidget*)((void*)qvariant_cast(var)); + */ + /* + QDataStream ds(&bytes, QIODevice::ReadOnly); + bool err = QMetaType::load(ds, QMetaType::QWidgetStar, &widget); + if(err) printf("Failed!\n"); + */ + sscanf(bytes.data(), "%p", &widget); + + printf(" = %p\n", widget); + return widget; + +} diff --git a/editor/widgetwrapper.h b/editor/widgetwrapper.h new file mode 100644 index 0000000..8d65aa1 --- /dev/null +++ b/editor/widgetwrapper.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * widgetwrapper.h + * + * Mon Jul 7 07:42:03 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_WIDGETWRAPPER_H__ +#define __PRACRO_WIDGETWRAPPER_H__ + +#include +#include + +QByteArray wrapWidget(QWidget *widget); +QWidget *unwrapWidget(QByteArray bytes); + +#endif/*__PRACRO_WIDGETWRAPPER_H__*/ -- cgit v1.2.3