Mercurial > bellaciao
view layout_view.cpp @ 9:0d7875c26974
layout designer: hints on widget placement during drag and drop
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 26 Aug 2011 12:15:21 +0200 |
parents | c63d67895cbe |
children | 877a005c2b42 |
line wrap: on
line source
/* Bellaciao: a Salut à Toi frontend Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "layout_view.h" #include <QDebug> WidgetView::WidgetView(const QString& name) : QLabel(name) { } LayoutView::LayoutView(QWidget* parent) { setAcceptDrops(true); m_layout = new QVBoxLayout; setLayout(m_layout); m_layout->addStretch(); setStyleSheet("QLabel {border: 1px solid black; \ border-radius: 5px; \ text-align: center; \ background-color: white; \ min-height: 20px; \ max-height: 20px; \ margin: 2px; \ }"); } LayoutView::~LayoutView() { } void LayoutView::setLayoutWidgets(const QHash<QString, LayoutWidget*>& layout_widgets) { m_layout_widgets = layout_widgets; } void LayoutView::dragEnterEvent (QDragEnterEvent* event ) { if (event->mimeData()->hasFormat(LAYOUT_WIDGET_MIME)) event->acceptProposedAction(); } void LayoutView::dragLeaveEvent (QDragLeaveEvent* event ) { m_loc_hint.setCoords(1,1,0,0); //We make the QRect invalid to hide it update(); } void LayoutView::dragMoveEvent (QDragMoveEvent* event ) { int pos_y = event->pos().y(); int top = 0; QList<QWidget*> _children = findChildren<QWidget*>(""); QWidget* wid = 0; for (m_insert_pos=0; m_insert_pos<layout()->count()-1; m_insert_pos++) { wid = layout()->itemAt(m_insert_pos)->widget(); if (wid->geometry().bottom() > pos_y && wid->geometry().top() < pos_y) { //We are inside a widget, we break here top = wid->geometry().bottom(); m_insert_pos++; break; } else if (wid->geometry().top() > pos_y) break; top = wid->geometry().bottom(); } m_loc_hint.setRect(5, top+1, qMax(5,width()-10), 5); update(); } void LayoutView::dropEvent (QDropEvent* event) { m_loc_hint.setCoords(1,1,0,0); //We make the QRect invalid to hide it update(); const QMimeData *data = event->mimeData(); QByteArray encodedData = data->data(LAYOUT_WIDGET_MIME); QDataStream stream(&encodedData, QIODevice::ReadOnly); while (!stream.atEnd()) { QString widget_name; stream >> widget_name; addWidget(widget_name); } } void LayoutView::addWidget(const QString& name) { LayoutWidget* _widget = m_layout_widgets[name]; WidgetView* _widget_view = new WidgetView(name); _widget_view->setAlignment(Qt::AlignCenter); m_layout->insertWidget(m_insert_pos, _widget_view, 0, Qt::AlignTop); } void LayoutView::paintEvent(QPaintEvent* event) { if (m_loc_hint.isValid()) { QPainter newPainter(this); newPainter.fillRect(m_loc_hint, Qt::blue); } }