annotate layout_widget.cpp @ 11:98485ebbdb86 default tip

LayoutView: WidgetViewBox fix
author Goffi <goffi@goffi.org>
date Sat, 27 Aug 2011 00:31:20 +0200
parents 0d7875c26974
children
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
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 LayoutWidget::~LayoutWidget()
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 {
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 }
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 const QString& LayoutWidget::getName() const
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 return m_name;
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
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 LayoutWidgetModel::LayoutWidgetModel(QObject* parent)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 : QAbstractListModel(parent)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
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 LayoutWidgetModel::~LayoutWidgetModel()
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 while (!m_layout_widgets.isEmpty())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 delete m_layout_widgets.takeFirst();
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
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 int LayoutWidgetModel::rowCount(const QModelIndex &parent) const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 return m_layout_widgets.size();
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 QVariant LayoutWidgetModel::data(const QModelIndex &index, int role) 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 if (!index.isValid())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 return QVariant();
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 if (index.row() >= m_layout_widgets.size())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 return QVariant();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if (role == Qt::DisplayRole)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return m_layout_widgets[index.row()]->getName();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74
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
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 Qt::ItemFlags LayoutWidgetModel::flags (const QModelIndex& index) const
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 Qt::ItemFlags default_flags = QAbstractListModel::flags(index);
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 if (index.isValid())
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return default_flags | Qt::ItemIsDragEnabled;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return default_flags;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 }
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 QStringList LayoutWidgetModel::mimeTypes() const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 QStringList types;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 types << LAYOUT_WIDGET_MIME;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return types;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 }
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 QMimeData* LayoutWidgetModel::mimeData (const QModelIndexList& indexes ) const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 QMimeData *mimeData = new QMimeData();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 QByteArray encodedData;
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 QDataStream stream(&encodedData, QIODevice::WriteOnly);
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 foreach (const QModelIndex &index, indexes) {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 if (index.isValid()) {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 QString text = m_layout_widgets[index.row()]->getName();
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 stream << text;
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 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 mimeData->setData(LAYOUT_WIDGET_MIME, encodedData);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 return mimeData;
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 void LayoutWidgetModel::append(LayoutWidget* layout_widget)
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if (m_layout_widgets_dict.contains(layout_widget->getName()))
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 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
117 delete layout_widget;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 return;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 }
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 beginInsertRows(QModelIndex(), m_layout_widgets.size(), m_layout_widgets.size()+1);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 m_layout_widgets.append(layout_widget);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 m_layout_widgets_dict.insert(layout_widget->getName(), layout_widget);
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 endInsertRows();
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
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 const QList<LayoutWidget*>& LayoutWidgetModel::getLayoutWidgets() const
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 {
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return m_layout_widgets;
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 QHash<QString, LayoutWidget*>& LayoutWidgetModel::getLayoutWidgetsDict() 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_dict;
c63d67895cbe layout designer: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 }