Mercurial > bellaciao
annotate 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 |
rev | line source |
---|---|
8 | 1 /* |
2 Bellaciao: a Salut à Toi frontend | |
3 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) | |
4 | |
5 This program is free software: you can redistribute it and/or modify | |
6 it under the terms of the GNU Affero General Public License as published by | |
7 the Free Software Foundation, either version 3 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU Affero General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Affero General Public License | |
16 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 */ | |
18 #include "layout_view.h" | |
19 #include <QDebug> | |
20 | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
21 WidgetView::WidgetView(const QString& name) |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
22 { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
23 qDebug() << "Contructeur WidgetView"; |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
24 } |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
25 |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
26 WidgetView::~WidgetView() |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
27 { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
28 qDebug() << "Destructeur WidgetView"; |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
29 } |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
30 |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
31 WidgetViewItem::WidgetViewItem(const QString& name) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
32 : QLabel(name), WidgetView(name) |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
33 { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
34 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
35 |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
36 WidgetViewBox::WidgetViewBox(const QString& name) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
37 : WidgetView(name) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
38 { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
39 setWidget(new LayoutView(0, HORIZONTAL)); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
40 } |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
41 |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
42 LayoutView::LayoutView(QWidget* parent, LayoutDirection direction) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
43 : m_direction(direction) |
8 | 44 { |
45 setAcceptDrops(true); | |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
46 if (m_direction == VERTICAL) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
47 m_layout = new QVBoxLayout; |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
48 else |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
49 m_layout = new QHBoxLayout; |
8 | 50 setLayout(m_layout); |
51 m_layout->addStretch(); | |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
52 setStyleSheet("WidgetViewItem {border: 1px solid black; \ |
8 | 53 border-radius: 5px; \ |
54 text-align: center; \ | |
55 background-color: white; \ | |
56 min-height: 20px; \ | |
57 max-height: 20px; \ | |
58 margin: 2px; \ | |
59 }"); | |
60 } | |
61 | |
62 LayoutView::~LayoutView() | |
63 { | |
64 } | |
65 | |
66 void LayoutView::setLayoutWidgets(const QHash<QString, LayoutWidget*>& layout_widgets) | |
67 { | |
68 m_layout_widgets = layout_widgets; | |
69 } | |
70 | |
71 void LayoutView::dragEnterEvent (QDragEnterEvent* event ) | |
72 { | |
73 if (event->mimeData()->hasFormat(LAYOUT_WIDGET_MIME)) | |
74 event->acceptProposedAction(); | |
75 } | |
76 | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
77 void LayoutView::dragLeaveEvent (QDragLeaveEvent* event ) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
78 { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
79 m_loc_hint.setCoords(1,1,0,0); //We make the QRect invalid to hide it |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
80 update(); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
81 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
82 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
83 |
8 | 84 void LayoutView::dragMoveEvent (QDragMoveEvent* event ) |
85 { | |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
86 int ref_pos = m_direction == VERTICAL ? event->pos().y() : event->pos().x(); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
87 int hint_start = 0; |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
88 int pos_start, pos_end; |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
89 QList<QWidget*> _children = findChildren<QWidget*>(""); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
90 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
91 QWidget* wid = 0; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
92 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
93 for (m_insert_pos=0; m_insert_pos<layout()->count()-1; m_insert_pos++) { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
94 wid = layout()->itemAt(m_insert_pos)->widget(); |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
95 |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
96 if (m_direction == VERTICAL) { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
97 pos_start = wid->geometry().top(); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
98 pos_end = wid->geometry().bottom(); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
99 } |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
100 else { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
101 pos_start = wid->geometry().left(); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
102 pos_end = wid->geometry().right(); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
103 } |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
104 |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
105 if (pos_end > ref_pos && pos_start < ref_pos) { |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
106 //We are inside a widget, we break here |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
107 hint_start = pos_end; |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
108 m_insert_pos++; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
109 break; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
110 } |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
111 else if (pos_start > ref_pos) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
112 //we are too far, we stop |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
113 break; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
114 |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
115 hint_start = pos_end; |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
116 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
117 |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
118 if (m_direction == VERTICAL) |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
119 m_loc_hint.setRect(5, hint_start+1, qMax(5,width()-10), 5); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
120 else |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
121 m_loc_hint.setRect(hint_start+1, 5, 5, qMax(5,height()-10)); |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
122 update(); |
8 | 123 } |
124 | |
125 void LayoutView::dropEvent (QDropEvent* event) | |
126 { | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
127 m_loc_hint.setCoords(1,1,0,0); //We make the QRect invalid to hide it |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
128 update(); |
8 | 129 const QMimeData *data = event->mimeData(); |
130 QByteArray encodedData = data->data(LAYOUT_WIDGET_MIME); | |
131 QDataStream stream(&encodedData, QIODevice::ReadOnly); | |
132 while (!stream.atEnd()) { | |
133 QString widget_name; | |
134 stream >> widget_name; | |
135 addWidget(widget_name); | |
136 } | |
137 } | |
138 | |
139 void LayoutView::addWidget(const QString& name) | |
140 { | |
141 LayoutWidget* _widget = m_layout_widgets[name]; | |
10
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
142 WidgetView* _widget_view; |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
143 if (name == "Box") { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
144 _widget_view = new WidgetViewBox(name); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
145 } |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
146 else { |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
147 _widget_view = new WidgetViewItem(name); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
148 reinterpret_cast<WidgetViewItem*>(_widget_view)->setAlignment(Qt::AlignCenter); |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
149 } |
877a005c2b42
Layout designer: WidgetView class, with two types: WidgetViewItem and WidgetViewBox which contains items.
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
150 m_layout->insertWidget(m_insert_pos, reinterpret_cast<QWidget*>(_widget_view), 0, Qt::AlignTop); |
8 | 151 |
152 } | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
153 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
154 void LayoutView::paintEvent(QPaintEvent* event) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
155 { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
156 if (m_loc_hint.isValid()) { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
157 QPainter newPainter(this); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
158 newPainter.fillRect(m_loc_hint, Qt::blue); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
159 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
160 } |