changeset 10:877a005c2b42

Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
author Goffi <goffi@goffi.org>
date Fri, 26 Aug 2011 15:58:46 +0200
parents 0d7875c26974
children 98485ebbdb86
files layout_view.cpp layout_view.h
diffstat 2 files changed, 79 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/layout_view.cpp	Fri Aug 26 12:15:21 2011 +0200
+++ b/layout_view.cpp	Fri Aug 26 15:58:46 2011 +0200
@@ -19,17 +19,37 @@
 #include <QDebug>
 
 WidgetView::WidgetView(const QString& name)
-    : QLabel(name)
+{
+    qDebug() << "Contructeur WidgetView";
+}
+
+WidgetView::~WidgetView()
+{
+    qDebug() << "Destructeur WidgetView";
+}
+
+WidgetViewItem::WidgetViewItem(const QString& name)
+    : QLabel(name), WidgetView(name)
 {
 }
 
-LayoutView::LayoutView(QWidget* parent)
+WidgetViewBox::WidgetViewBox(const QString& name)
+    : WidgetView(name)
+{
+    setWidget(new LayoutView(0, HORIZONTAL));
+}
+
+LayoutView::LayoutView(QWidget* parent, LayoutDirection direction)
+    : m_direction(direction)
 {
     setAcceptDrops(true);
-    m_layout = new QVBoxLayout;
+    if (m_direction == VERTICAL)
+        m_layout = new QVBoxLayout;
+    else
+        m_layout = new QHBoxLayout;
     setLayout(m_layout);
     m_layout->addStretch();
-    setStyleSheet("QLabel {border: 1px solid black; \
+    setStyleSheet("WidgetViewItem {border: 1px solid black; \
                            border-radius: 5px; \
                            text-align: center; \
                            background-color: white; \
@@ -63,28 +83,42 @@
 
 void LayoutView::dragMoveEvent (QDragMoveEvent* event )
 {
-    int pos_y = event->pos().y();
-    int top = 0;
+    int ref_pos = m_direction == VERTICAL ? event->pos().y() : event->pos().x();
+    int hint_start = 0;
+    int pos_start, pos_end;
     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 (m_direction == VERTICAL) {
+            pos_start = wid->geometry().top();
+            pos_end = wid->geometry().bottom();
+        }
+        else {
+            pos_start = wid->geometry().left();
+            pos_end = wid->geometry().right();
+        }
 
-        if (wid->geometry().bottom() > pos_y && wid->geometry().top() < pos_y) {
+        if (pos_end > ref_pos && pos_start < ref_pos) {
             //We are inside a widget, we break here
-            top = wid->geometry().bottom();
+            hint_start = pos_end;
             m_insert_pos++;
             break;
         }
-        else if (wid->geometry().top() > pos_y)
+        else if (pos_start > ref_pos)
+            //we are too far, we stop
             break;
         
-        top = wid->geometry().bottom();
+        hint_start = pos_end;
     }
     
-    m_loc_hint.setRect(5, top+1, qMax(5,width()-10), 5);
+    if (m_direction == VERTICAL)
+        m_loc_hint.setRect(5, hint_start+1, qMax(5,width()-10), 5);
+    else
+        m_loc_hint.setRect(hint_start+1, 5, 5, qMax(5,height()-10));
     update();
 }
 
@@ -105,9 +139,15 @@
 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);
+    WidgetView* _widget_view;
+    if (name == "Box") {
+        _widget_view = new WidgetViewBox(name);
+    }
+    else {
+        _widget_view = new WidgetViewItem(name);
+        reinterpret_cast<WidgetViewItem*>(_widget_view)->setAlignment(Qt::AlignCenter);
+    }
+    m_layout->insertWidget(m_insert_pos, reinterpret_cast<QWidget*>(_widget_view), 0, Qt::AlignTop);
 
 }
 
--- a/layout_view.h	Fri Aug 26 12:15:21 2011 +0200
+++ b/layout_view.h	Fri Aug 26 15:58:46 2011 +0200
@@ -25,11 +25,30 @@
 
 #define LAYOUT_WIDGET_MIME "application/x-bellaciao-widget"
 
-class WidgetView : public QLabel
+typedef enum
+{
+    HORIZONTAL, VERTICAL
+} LayoutDirection;
+
+class WidgetView
+{
+    public:
+        WidgetView(const QString& name);
+        ~WidgetView();
+};
+
+class WidgetViewItem : public QLabel, public WidgetView
 {
     Q_OBJECT
     public:
-        WidgetView(const QString& name);
+        WidgetViewItem(const QString& name);
+};
+
+class WidgetViewBox : public QScrollArea, public WidgetView
+{
+    Q_OBJECT
+    public:
+        WidgetViewBox(const QString& name);
 };
 
 class LayoutView : public QWidget
@@ -37,7 +56,7 @@
     Q_OBJECT
 
     public:
-        LayoutView(QWidget* parent=0);
+        LayoutView(QWidget* parent = 0, LayoutDirection direction = VERTICAL);
         ~LayoutView();
         void setLayoutWidgets(const QHash<QString, LayoutWidget*>& layout_widgets);
         void dragEnterEvent (QDragEnterEvent* event );
@@ -50,8 +69,10 @@
     private:
         QHash<QString, LayoutWidget*> m_layout_widgets;
         QList<LayoutWidget> m_widgets_list;
-        QVBoxLayout* m_layout;
+        QBoxLayout* m_layout;
         QRect m_loc_hint;
         int m_insert_pos; //position in the layout when a widget must be inserted
+        LayoutDirection m_direction;
 };
+
 #endif