Mercurial > bellaciao
comparison layout_widget.cpp @ 8:c63d67895cbe
layout designer: first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 24 Aug 2011 20:38:55 +0200 |
parents | |
children | 0d7875c26974 |
comparison
equal
deleted
inserted
replaced
7:017925589d4c | 8:c63d67895cbe |
---|---|
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 | |
19 #include "layout_widget.h" | |
20 | |
21 WidgetProperty::WidgetProperty(const QString& name, const QString& tooltip) | |
22 : m_name(name), m_type(NONE) | |
23 { | |
24 } | |
25 | |
26 WidgetPropertyBool::WidgetPropertyBool(const QString& name, bool checked, const QString& tooltip) | |
27 : WidgetProperty(name, tooltip), m_checked(checked) | |
28 { | |
29 m_type = BOOL; | |
30 } | |
31 | |
32 LayoutWidget::LayoutWidget(const QString& name, const QString& icon) | |
33 : m_name(name), m_icon(icon) | |
34 { | |
35 qDebug() << "LayoutWidget constructeur (" << name <<")"; | |
36 | |
37 } | |
38 | |
39 LayoutWidget::~LayoutWidget() | |
40 { | |
41 qDebug() << "LayoutWidget destructeur"; | |
42 | |
43 } | |
44 | |
45 const QString& LayoutWidget::getName() const | |
46 { | |
47 return m_name; | |
48 } | |
49 | |
50 LayoutWidgetModel::LayoutWidgetModel(QObject* parent) | |
51 : QAbstractListModel(parent) | |
52 { | |
53 } | |
54 | |
55 LayoutWidgetModel::~LayoutWidgetModel() | |
56 { | |
57 | |
58 qDebug() << "Destructeur de LayoutWidgetModel"; | |
59 while (!m_layout_widgets.isEmpty()) | |
60 delete m_layout_widgets.takeFirst(); | |
61 qDebug() << "Fin Destructeur de LayoutWidgetModel"; | |
62 } | |
63 | |
64 int LayoutWidgetModel::rowCount(const QModelIndex &parent) const | |
65 { | |
66 return m_layout_widgets.size(); | |
67 } | |
68 | |
69 QVariant LayoutWidgetModel::data(const QModelIndex &index, int role) const | |
70 { | |
71 if (!index.isValid()) | |
72 return QVariant(); | |
73 | |
74 if (index.row() >= m_layout_widgets.size()) | |
75 return QVariant(); | |
76 | |
77 if (role == Qt::DisplayRole) | |
78 return m_layout_widgets[index.row()]->getName(); | |
79 | |
80 return QVariant(); | |
81 } | |
82 | |
83 Qt::ItemFlags LayoutWidgetModel::flags (const QModelIndex& index) const | |
84 { | |
85 Qt::ItemFlags default_flags = QAbstractListModel::flags(index); | |
86 | |
87 if (index.isValid()) | |
88 return default_flags | Qt::ItemIsDragEnabled; | |
89 return default_flags; | |
90 } | |
91 | |
92 QStringList LayoutWidgetModel::mimeTypes() const | |
93 { | |
94 QStringList types; | |
95 types << LAYOUT_WIDGET_MIME; | |
96 return types; | |
97 } | |
98 | |
99 QMimeData* LayoutWidgetModel::mimeData (const QModelIndexList& indexes ) const | |
100 { | |
101 QMimeData *mimeData = new QMimeData(); | |
102 QByteArray encodedData; | |
103 | |
104 QDataStream stream(&encodedData, QIODevice::WriteOnly); | |
105 | |
106 foreach (const QModelIndex &index, indexes) { | |
107 if (index.isValid()) { | |
108 QString text = m_layout_widgets[index.row()]->getName(); | |
109 stream << text; | |
110 } | |
111 } | |
112 | |
113 mimeData->setData(LAYOUT_WIDGET_MIME, encodedData); | |
114 return mimeData; | |
115 } | |
116 | |
117 void LayoutWidgetModel::append(LayoutWidget* layout_widget) | |
118 { | |
119 if (m_layout_widgets_dict.contains(layout_widget->getName())) | |
120 { | |
121 qWarning() << "Layout widget of this name already exists, can't add an other one: " << layout_widget->getName(); | |
122 delete layout_widget; | |
123 return; | |
124 } | |
125 beginInsertRows(QModelIndex(), m_layout_widgets.size(), m_layout_widgets.size()+1); | |
126 m_layout_widgets.append(layout_widget); | |
127 m_layout_widgets_dict.insert(layout_widget->getName(), layout_widget); | |
128 endInsertRows(); | |
129 } | |
130 | |
131 | |
132 const QList<LayoutWidget*>& LayoutWidgetModel::getLayoutWidgets() const | |
133 { | |
134 return m_layout_widgets; | |
135 } | |
136 | |
137 const QHash<QString, LayoutWidget*>& LayoutWidgetModel::getLayoutWidgetsDict() const | |
138 { | |
139 return m_layout_widgets_dict; | |
140 } |