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 |
|
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 |
|
36 } |
|
37 |
|
38 LayoutWidget::~LayoutWidget() |
|
39 { |
|
40 |
|
41 } |
|
42 |
|
43 const QString& LayoutWidget::getName() const |
|
44 { |
|
45 return m_name; |
|
46 } |
|
47 |
|
48 LayoutWidgetModel::LayoutWidgetModel(QObject* parent) |
|
49 : QAbstractListModel(parent) |
|
50 { |
|
51 } |
|
52 |
|
53 LayoutWidgetModel::~LayoutWidgetModel() |
|
54 { |
|
55 while (!m_layout_widgets.isEmpty()) |
|
56 delete m_layout_widgets.takeFirst(); |
|
57 } |
|
58 |
|
59 int LayoutWidgetModel::rowCount(const QModelIndex &parent) const |
|
60 { |
|
61 return m_layout_widgets.size(); |
|
62 } |
|
63 |
|
64 QVariant LayoutWidgetModel::data(const QModelIndex &index, int role) const |
|
65 { |
|
66 if (!index.isValid()) |
|
67 return QVariant(); |
|
68 |
|
69 if (index.row() >= m_layout_widgets.size()) |
|
70 return QVariant(); |
|
71 |
|
72 if (role == Qt::DisplayRole) |
|
73 return m_layout_widgets[index.row()]->getName(); |
|
74 |
|
75 return QVariant(); |
|
76 } |
|
77 |
|
78 Qt::ItemFlags LayoutWidgetModel::flags (const QModelIndex& index) const |
|
79 { |
|
80 Qt::ItemFlags default_flags = QAbstractListModel::flags(index); |
|
81 |
|
82 if (index.isValid()) |
|
83 return default_flags | Qt::ItemIsDragEnabled; |
|
84 return default_flags; |
|
85 } |
|
86 |
|
87 QStringList LayoutWidgetModel::mimeTypes() const |
|
88 { |
|
89 QStringList types; |
|
90 types << LAYOUT_WIDGET_MIME; |
|
91 return types; |
|
92 } |
|
93 |
|
94 QMimeData* LayoutWidgetModel::mimeData (const QModelIndexList& indexes ) const |
|
95 { |
|
96 QMimeData *mimeData = new QMimeData(); |
|
97 QByteArray encodedData; |
|
98 |
|
99 QDataStream stream(&encodedData, QIODevice::WriteOnly); |
|
100 |
|
101 foreach (const QModelIndex &index, indexes) { |
|
102 if (index.isValid()) { |
|
103 QString text = m_layout_widgets[index.row()]->getName(); |
|
104 stream << text; |
|
105 } |
|
106 } |
|
107 |
|
108 mimeData->setData(LAYOUT_WIDGET_MIME, encodedData); |
|
109 return mimeData; |
|
110 } |
|
111 |
|
112 void LayoutWidgetModel::append(LayoutWidget* layout_widget) |
|
113 { |
|
114 if (m_layout_widgets_dict.contains(layout_widget->getName())) |
|
115 { |
|
116 qWarning() << "Layout widget of this name already exists, can't add an other one: " << layout_widget->getName(); |
|
117 delete layout_widget; |
|
118 return; |
|
119 } |
|
120 beginInsertRows(QModelIndex(), m_layout_widgets.size(), m_layout_widgets.size()+1); |
|
121 m_layout_widgets.append(layout_widget); |
|
122 m_layout_widgets_dict.insert(layout_widget->getName(), layout_widget); |
|
123 endInsertRows(); |
|
124 } |
|
125 |
|
126 |
|
127 const QList<LayoutWidget*>& LayoutWidgetModel::getLayoutWidgets() const |
|
128 { |
|
129 return m_layout_widgets; |
|
130 } |
|
131 |
|
132 const QHash<QString, LayoutWidget*>& LayoutWidgetModel::getLayoutWidgetsDict() const |
|
133 { |
|
134 return m_layout_widgets_dict; |
|
135 } |