Mercurial > bellaciao
annotate layout_view.cpp @ 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 |
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) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
22 : QLabel(name) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
23 { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
24 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
25 |
8 | 26 LayoutView::LayoutView(QWidget* parent) |
27 { | |
28 setAcceptDrops(true); | |
29 m_layout = new QVBoxLayout; | |
30 setLayout(m_layout); | |
31 m_layout->addStretch(); | |
32 setStyleSheet("QLabel {border: 1px solid black; \ | |
33 border-radius: 5px; \ | |
34 text-align: center; \ | |
35 background-color: white; \ | |
36 min-height: 20px; \ | |
37 max-height: 20px; \ | |
38 margin: 2px; \ | |
39 }"); | |
40 } | |
41 | |
42 LayoutView::~LayoutView() | |
43 { | |
44 } | |
45 | |
46 void LayoutView::setLayoutWidgets(const QHash<QString, LayoutWidget*>& layout_widgets) | |
47 { | |
48 m_layout_widgets = layout_widgets; | |
49 } | |
50 | |
51 void LayoutView::dragEnterEvent (QDragEnterEvent* event ) | |
52 { | |
53 if (event->mimeData()->hasFormat(LAYOUT_WIDGET_MIME)) | |
54 event->acceptProposedAction(); | |
55 } | |
56 | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
57 void LayoutView::dragLeaveEvent (QDragLeaveEvent* event ) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
58 { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
59 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
|
60 update(); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
61 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
62 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
63 |
8 | 64 void LayoutView::dragMoveEvent (QDragMoveEvent* event ) |
65 { | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
66 int pos_y = event->pos().y(); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
67 int top = 0; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
68 QList<QWidget*> _children = findChildren<QWidget*>(""); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
69 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
70 QWidget* wid = 0; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
71 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
72 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
|
73 wid = layout()->itemAt(m_insert_pos)->widget(); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
74 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
75 if (wid->geometry().bottom() > pos_y && wid->geometry().top() < pos_y) { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
76 //We are inside a widget, we break here |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
77 top = wid->geometry().bottom(); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
78 m_insert_pos++; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
79 break; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
80 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
81 else if (wid->geometry().top() > pos_y) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
82 break; |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
83 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
84 top = wid->geometry().bottom(); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
85 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
86 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
87 m_loc_hint.setRect(5, top+1, qMax(5,width()-10), 5); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
88 update(); |
8 | 89 } |
90 | |
91 void LayoutView::dropEvent (QDropEvent* event) | |
92 { | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
93 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
|
94 update(); |
8 | 95 const QMimeData *data = event->mimeData(); |
96 QByteArray encodedData = data->data(LAYOUT_WIDGET_MIME); | |
97 QDataStream stream(&encodedData, QIODevice::ReadOnly); | |
98 while (!stream.atEnd()) { | |
99 QString widget_name; | |
100 stream >> widget_name; | |
101 addWidget(widget_name); | |
102 } | |
103 } | |
104 | |
105 void LayoutView::addWidget(const QString& name) | |
106 { | |
107 LayoutWidget* _widget = m_layout_widgets[name]; | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
108 WidgetView* _widget_view = new WidgetView(name); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
109 _widget_view->setAlignment(Qt::AlignCenter); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
110 m_layout->insertWidget(m_insert_pos, _widget_view, 0, Qt::AlignTop); |
8 | 111 |
112 } | |
9
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
113 |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
114 void LayoutView::paintEvent(QPaintEvent* event) |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
115 { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
116 if (m_loc_hint.isValid()) { |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
117 QPainter newPainter(this); |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
118 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
|
119 } |
0d7875c26974
layout designer: hints on widget placement during drag and drop
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
120 } |