annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 /*
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 Bellaciao: a Salut à Toi frontend
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 it under the terms of the GNU Affero General Public License as published by
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 the Free Software Foundation, either version 3 of the License, or
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 (at your option) any later version.
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 GNU Affero General Public License for more details.
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 You should have received a copy of the GNU Affero General Public License
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 */
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 #include "layout_widget.h"
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 WidgetProperty::WidgetProperty(const QString& name, const QString& tooltip)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 : m_name(name), m_type(NONE)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 WidgetPropertyBool::WidgetPropertyBool(const QString& name, bool checked, const QString& tooltip)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 : WidgetProperty(name, tooltip), m_checked(checked)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 m_type = BOOL;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 LayoutWidget::LayoutWidget(const QString& name, const QString& icon)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 : m_name(name), m_icon(icon)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 qDebug() << "LayoutWidget constructeur (" << name <<")";
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 LayoutWidget::~LayoutWidget()
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 qDebug() << "LayoutWidget destructeur";
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 const QString& LayoutWidget::getName() const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 return m_name;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 LayoutWidgetModel::LayoutWidgetModel(QObject* parent)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 : QAbstractListModel(parent)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 LayoutWidgetModel::~LayoutWidgetModel()
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 qDebug() << "Destructeur de LayoutWidgetModel";
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 while (!m_layout_widgets.isEmpty())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 delete m_layout_widgets.takeFirst();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 qDebug() << "Fin Destructeur de LayoutWidgetModel";
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 int LayoutWidgetModel::rowCount(const QModelIndex &parent) const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 return m_layout_widgets.size();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 QVariant LayoutWidgetModel::data(const QModelIndex &index, int role) const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if (!index.isValid())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return QVariant();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 if (index.row() >= m_layout_widgets.size())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return QVariant();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 if (role == Qt::DisplayRole)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 return m_layout_widgets[index.row()]->getName();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return QVariant();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 Qt::ItemFlags LayoutWidgetModel::flags (const QModelIndex& index) const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 Qt::ItemFlags default_flags = QAbstractListModel::flags(index);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if (index.isValid())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return default_flags | Qt::ItemIsDragEnabled;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return default_flags;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 QStringList LayoutWidgetModel::mimeTypes() const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 QStringList types;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 types << LAYOUT_WIDGET_MIME;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return types;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 QMimeData* LayoutWidgetModel::mimeData (const QModelIndexList& indexes ) const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 QMimeData *mimeData = new QMimeData();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 QByteArray encodedData;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 QDataStream stream(&encodedData, QIODevice::WriteOnly);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 foreach (const QModelIndex &index, indexes) {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 if (index.isValid()) {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 QString text = m_layout_widgets[index.row()]->getName();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 stream << text;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 mimeData->setData(LAYOUT_WIDGET_MIME, encodedData);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 return mimeData;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 void LayoutWidgetModel::append(LayoutWidget* layout_widget)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 if (m_layout_widgets_dict.contains(layout_widget->getName()))
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 qWarning() << "Layout widget of this name already exists, can't add an other one: " << layout_widget->getName();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 delete layout_widget;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 return;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 beginInsertRows(QModelIndex(), m_layout_widgets.size(), m_layout_widgets.size()+1);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 m_layout_widgets.append(layout_widget);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 m_layout_widgets_dict.insert(layout_widget->getName(), layout_widget);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 endInsertRows();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 const QList<LayoutWidget*>& LayoutWidgetModel::getLayoutWidgets() const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 return m_layout_widgets;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 const QHash<QString, LayoutWidget*>& LayoutWidgetModel::getLayoutWidgetsDict() const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return m_layout_widgets_dict;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 }