comparison layout_view.cpp @ 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
comparison
equal deleted inserted replaced
9:0d7875c26974 10:877a005c2b42
17 */ 17 */
18 #include "layout_view.h" 18 #include "layout_view.h"
19 #include <QDebug> 19 #include <QDebug>
20 20
21 WidgetView::WidgetView(const QString& name) 21 WidgetView::WidgetView(const QString& name)
22 : QLabel(name) 22 {
23 qDebug() << "Contructeur WidgetView";
24 }
25
26 WidgetView::~WidgetView()
27 {
28 qDebug() << "Destructeur WidgetView";
29 }
30
31 WidgetViewItem::WidgetViewItem(const QString& name)
32 : QLabel(name), WidgetView(name)
23 { 33 {
24 } 34 }
25 35
26 LayoutView::LayoutView(QWidget* parent) 36 WidgetViewBox::WidgetViewBox(const QString& name)
37 : WidgetView(name)
38 {
39 setWidget(new LayoutView(0, HORIZONTAL));
40 }
41
42 LayoutView::LayoutView(QWidget* parent, LayoutDirection direction)
43 : m_direction(direction)
27 { 44 {
28 setAcceptDrops(true); 45 setAcceptDrops(true);
29 m_layout = new QVBoxLayout; 46 if (m_direction == VERTICAL)
47 m_layout = new QVBoxLayout;
48 else
49 m_layout = new QHBoxLayout;
30 setLayout(m_layout); 50 setLayout(m_layout);
31 m_layout->addStretch(); 51 m_layout->addStretch();
32 setStyleSheet("QLabel {border: 1px solid black; \ 52 setStyleSheet("WidgetViewItem {border: 1px solid black; \
33 border-radius: 5px; \ 53 border-radius: 5px; \
34 text-align: center; \ 54 text-align: center; \
35 background-color: white; \ 55 background-color: white; \
36 min-height: 20px; \ 56 min-height: 20px; \
37 max-height: 20px; \ 57 max-height: 20px; \
61 } 81 }
62 82
63 83
64 void LayoutView::dragMoveEvent (QDragMoveEvent* event ) 84 void LayoutView::dragMoveEvent (QDragMoveEvent* event )
65 { 85 {
66 int pos_y = event->pos().y(); 86 int ref_pos = m_direction == VERTICAL ? event->pos().y() : event->pos().x();
67 int top = 0; 87 int hint_start = 0;
88 int pos_start, pos_end;
68 QList<QWidget*> _children = findChildren<QWidget*>(""); 89 QList<QWidget*> _children = findChildren<QWidget*>("");
69 90
70 QWidget* wid = 0; 91 QWidget* wid = 0;
71 92
72 for (m_insert_pos=0; m_insert_pos<layout()->count()-1; m_insert_pos++) { 93 for (m_insert_pos=0; m_insert_pos<layout()->count()-1; m_insert_pos++) {
73 wid = layout()->itemAt(m_insert_pos)->widget(); 94 wid = layout()->itemAt(m_insert_pos)->widget();
95
96 if (m_direction == VERTICAL) {
97 pos_start = wid->geometry().top();
98 pos_end = wid->geometry().bottom();
99 }
100 else {
101 pos_start = wid->geometry().left();
102 pos_end = wid->geometry().right();
103 }
74 104
75 if (wid->geometry().bottom() > pos_y && wid->geometry().top() < pos_y) { 105 if (pos_end > ref_pos && pos_start < ref_pos) {
76 //We are inside a widget, we break here 106 //We are inside a widget, we break here
77 top = wid->geometry().bottom(); 107 hint_start = pos_end;
78 m_insert_pos++; 108 m_insert_pos++;
79 break; 109 break;
80 } 110 }
81 else if (wid->geometry().top() > pos_y) 111 else if (pos_start > ref_pos)
112 //we are too far, we stop
82 break; 113 break;
83 114
84 top = wid->geometry().bottom(); 115 hint_start = pos_end;
85 } 116 }
86 117
87 m_loc_hint.setRect(5, top+1, qMax(5,width()-10), 5); 118 if (m_direction == VERTICAL)
119 m_loc_hint.setRect(5, hint_start+1, qMax(5,width()-10), 5);
120 else
121 m_loc_hint.setRect(hint_start+1, 5, 5, qMax(5,height()-10));
88 update(); 122 update();
89 } 123 }
90 124
91 void LayoutView::dropEvent (QDropEvent* event) 125 void LayoutView::dropEvent (QDropEvent* event)
92 { 126 {
103 } 137 }
104 138
105 void LayoutView::addWidget(const QString& name) 139 void LayoutView::addWidget(const QString& name)
106 { 140 {
107 LayoutWidget* _widget = m_layout_widgets[name]; 141 LayoutWidget* _widget = m_layout_widgets[name];
108 WidgetView* _widget_view = new WidgetView(name); 142 WidgetView* _widget_view;
109 _widget_view->setAlignment(Qt::AlignCenter); 143 if (name == "Box") {
110 m_layout->insertWidget(m_insert_pos, _widget_view, 0, Qt::AlignTop); 144 _widget_view = new WidgetViewBox(name);
145 }
146 else {
147 _widget_view = new WidgetViewItem(name);
148 reinterpret_cast<WidgetViewItem*>(_widget_view)->setAlignment(Qt::AlignCenter);
149 }
150 m_layout->insertWidget(m_insert_pos, reinterpret_cast<QWidget*>(_widget_view), 0, Qt::AlignTop);
111 151
112 } 152 }
113 153
114 void LayoutView::paintEvent(QPaintEvent* event) 154 void LayoutView::paintEvent(QPaintEvent* event)
115 { 155 {