changeset 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
files layout_builder.cpp layout_designer.cpp layout_view.cpp layout_view.h layout_widget.cpp
diffstat 5 files changed, 58 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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<QString, LayoutWidget*>& 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)
--- 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";
 }
--- 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 <QDebug>
 
+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<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);
@@ -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);
+   }
+}
--- 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<QString, LayoutWidget*>& 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<QString, LayoutWidget*> m_layout_widgets;
         QList<LayoutWidget> 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
--- 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