# HG changeset patch # User Goffi # Date 1314353721 -7200 # Node ID 0d7875c2697411c8742617b637db74719a67d130 # Parent c63d67895cbe3681ec6929a7c453b8d364b22e0b layout designer: hints on widget placement during drag and drop diff -r c63d67895cbe -r 0d7875c26974 layout_builder.cpp --- a/layout_builder.cpp Wed Aug 24 20:38:55 2011 +0200 +++ b/layout_builder.cpp Fri Aug 26 12:15:21 2011 +0200 @@ -29,7 +29,6 @@ LayoutBuilder::~LayoutBuilder() { - qDebug() << "Destructeur de LayoutBuilder"; } const QHash& LayoutBuilder::getLayoutWidgetsDict() @@ -45,7 +44,6 @@ LayoutBuilderContactList::LayoutBuilderContactList(QWidget* parent) : LayoutBuilder(parent) { - qDebug() << "constructeur de LayoutBuilderContactList"; addWidget(new LayoutWidget("List")); addWidget(new LayoutWidget("Box")); layoutView->setLayoutWidgets(getLayoutWidgetsDict()); @@ -53,7 +51,6 @@ LayoutBuilderContactList::~LayoutBuilderContactList() { - qDebug() << "Destructeur de LayoutBuilderContactList"; } LayoutBuilder* LayoutBuilderFactory::Create(LayoutType type, QWidget* parent) diff -r c63d67895cbe -r 0d7875c26974 layout_designer.cpp --- a/layout_designer.cpp Wed Aug 24 20:38:55 2011 +0200 +++ b/layout_designer.cpp Fri Aug 26 12:15:21 2011 +0200 @@ -27,5 +27,4 @@ LayoutDesigner::~LayoutDesigner() { - qDebug() << "Destructeur de LayoutDesigner"; } diff -r c63d67895cbe -r 0d7875c26974 layout_view.cpp --- a/layout_view.cpp Wed Aug 24 20:38:55 2011 +0200 +++ b/layout_view.cpp Fri Aug 26 12:15:21 2011 +0200 @@ -18,6 +18,11 @@ #include "layout_view.h" #include +WidgetView::WidgetView(const QString& name) + : QLabel(name) +{ +} + LayoutView::LayoutView(QWidget* parent) { setAcceptDrops(true); @@ -45,21 +50,48 @@ void LayoutView::dragEnterEvent (QDragEnterEvent* event ) { - qDebug() << "Drag enter: " << event->pos(); 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 ) { - qDebug() << "Drag move: " << event->pos(); - QWidget* _wid = childAt(event->pos()); - if (_wid) - qDebug() << "Object: " << _wid->metaObject()->className(); + int pos_y = event->pos().y(); + int top = 0; + QList _children = findChildren(""); + + QWidget* wid = 0; + + for (m_insert_pos=0; m_insert_poscount()-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); @@ -73,9 +105,16 @@ void LayoutView::addWidget(const QString& name) { LayoutWidget* _widget = m_layout_widgets[name]; - qDebug() << "Adding: " << name; - QLabel* _label = new QLabel(name); - _label->setAlignment(Qt::AlignCenter); - m_layout->insertWidget(qMax(0, m_layout->count()-1), _label, 0, Qt::AlignTop); + 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); + } +} diff -r c63d67895cbe -r 0d7875c26974 layout_view.h --- a/layout_view.h Wed Aug 24 20:38:55 2011 +0200 +++ b/layout_view.h Fri Aug 26 12:15:21 2011 +0200 @@ -25,6 +25,12 @@ #define LAYOUT_WIDGET_MIME "application/x-bellaciao-widget" +class WidgetView : public QLabel +{ + Q_OBJECT + public: + WidgetView(const QString& name); +}; class LayoutView : public QWidget { @@ -35,13 +41,17 @@ ~LayoutView(); void setLayoutWidgets(const QHash& layout_widgets); void dragEnterEvent (QDragEnterEvent* event ); + void dragLeaveEvent (QDragLeaveEvent* event ); void dragMoveEvent (QDragMoveEvent* event ); void dropEvent (QDropEvent * event); protected: void addWidget(const QString& name); + void paintEvent(QPaintEvent* event); private: QHash m_layout_widgets; QList m_widgets_list; QVBoxLayout* m_layout; + QRect m_loc_hint; + int m_insert_pos; //position in the layout when a widget must be inserted }; #endif diff -r c63d67895cbe -r 0d7875c26974 layout_widget.cpp --- a/layout_widget.cpp Wed Aug 24 20:38:55 2011 +0200 +++ b/layout_widget.cpp Fri Aug 26 12:15:21 2011 +0200 @@ -32,13 +32,11 @@ LayoutWidget::LayoutWidget(const QString& name, const QString& icon) : m_name(name), m_icon(icon) { - qDebug() << "LayoutWidget constructeur (" << name <<")"; } LayoutWidget::~LayoutWidget() { - qDebug() << "LayoutWidget destructeur"; } @@ -54,11 +52,8 @@ LayoutWidgetModel::~LayoutWidgetModel() { - - qDebug() << "Destructeur de LayoutWidgetModel"; while (!m_layout_widgets.isEmpty()) delete m_layout_widgets.takeFirst(); - qDebug() << "Fin Destructeur de LayoutWidgetModel"; } int LayoutWidgetModel::rowCount(const QModelIndex &parent) const