diff options
Diffstat (limited to 'editor/macrowindow.cc')
-rw-r--r-- | editor/macrowindow.cc | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/editor/macrowindow.cc b/editor/macrowindow.cc new file mode 100644 index 0000000..761f107 --- /dev/null +++ b/editor/macrowindow.cc @@ -0,0 +1,198 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * macrowindow.cc + * + * Fri Jul 4 12:29:41 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 "macrowindow.h" + +#include <QLabel> +#include <QBoxLayout> +#include <QVBoxLayout> +#include <QHBoxLayout> + +#include <math.h> + +#include "widget.h" + +MacroWindow::MacroWindow(Qt::Orientation orientation) + : QFrame() +{ + this->orientation = orientation; + setAcceptDrops(true); + if(orientation == Qt::Horizontal) { + setLayout(new QHBoxLayout()); + } else { + setLayout(new QVBoxLayout()); + } + // layout()->setSpacing(0); + // layout()->setContentsMargins(2,2,2,2); + show(); + dragObject = NULL; + + setFrameStyle(QFrame::Box | QFrame::Plain); + 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); + + ((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(); + + if(orientation == Qt::Horizontal) { + mimedata->setData("pracro/widget", "horizontal"); + } else { + mimedata->setData("pracro/widget", "vertical"); + } + drag->setMimeData(mimedata); + + QWidget *parent = parentWidget(); + if(parent) { + parent->layout()->removeWidget(this); + setVisible(false); + drag->exec(); + } + } +} |