summaryrefslogtreecommitdiffstats
path: root/application/ColumnResizer.cpp
blob: 494408308b7a0191ebe354dcb7ce1a98dc25e7b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2011 Aurélien Gâteau <agateau@kde.org>
* License: BSD-3-Clause
*/
#include <ColumnResizer.h>

#include <QDebug>
#include <QEvent>
#include <QFormLayout>
#include <QGridLayout>
#include <QTimer>
#include <QWidget>

class FormLayoutWidgetItem : public QWidgetItem
{
public:
	FormLayoutWidgetItem(QWidget* widget, QFormLayout* formLayout, QFormLayout::ItemRole itemRole)
	: QWidgetItem(widget)
	, m_width(-1)
	, m_formLayout(formLayout)
	, m_itemRole(itemRole)
	{}

	QSize sizeHint() const
	{
		QSize size = QWidgetItem::sizeHint();
		if (m_width != -1) {
			size.setWidth(m_width);
		}
		return size;
	}

	QSize minimumSize() const
	{
		QSize size = QWidgetItem::minimumSize();
		if (m_width != -1) {
			size.setWidth(m_width);
		}
		return size;
	}

	QSize maximumSize() const
	{
		QSize size = QWidgetItem::maximumSize();
		if (m_width != -1) {
			size.setWidth(m_width);
		}
		return size;
	}

	void setWidth(int width)
	{
		if (width != m_width) {
			m_width = width;
			invalidate();
		}
	}

	void setGeometry(const QRect& _rect)
	{
		QRect rect = _rect;
		int width = widget()->sizeHint().width();
		if (m_itemRole == QFormLayout::LabelRole && m_formLayout->labelAlignment() & Qt::AlignRight) {
			rect.setLeft(rect.right() - width);
		}
		QWidgetItem::setGeometry(rect);
	}

	QFormLayout* formLayout() const
	{
		return m_formLayout;
	}

private:
	int m_width;
	QFormLayout* m_formLayout;
	QFormLayout::ItemRole m_itemRole;
};

typedef QPair<QGridLayout*, int> GridColumnInfo;

class ColumnResizerPrivate
{
public:
	ColumnResizerPrivate(ColumnResizer* q_ptr)
	: q(q_ptr)
	, m_updateTimer(new QTimer(q))
	{
		m_updateTimer->setSingleShot(true);
		m_updateTimer->setInterval(0);
		QObject::connect(m_updateTimer, SIGNAL(timeout()), q, SLOT(updateWidth()));
	}

	void scheduleWidthUpdate()
	{
		m_updateTimer->start();
	}

	ColumnResizer* q;
	QTimer* m_updateTimer;
	QList<QWidget*> m_widgets;
	QList<FormLayoutWidgetItem*> m_wrWidgetItemList;
	QList<GridColumnInfo> m_gridColumnInfoList;
};

ColumnResizer::ColumnResizer(QObject* parent)
: QObject(parent)
, d(new ColumnResizerPrivate(this))
{}

ColumnResizer::~ColumnResizer()
{
	delete d;
}

void ColumnResizer::addWidget(QWidget* widget)
{
	d->m_widgets.append(widget);
	widget->installEventFilter(this);
	d->scheduleWidthUpdate();
}

void ColumnResizer::updateWidth()
{
	int width = 0;
	Q_FOREACH(QWidget* widget, d->m_widgets) {
		width = qMax(widget->sizeHint().width(), width);
	}
	Q_FOREACH(FormLayoutWidgetItem* item, d->m_wrWidgetItemList) {
		item->setWidth(width);
		item->formLayout()->update();
	}
	Q_FOREACH(GridColumnInfo info, d->m_gridColumnInfoList) {
		info.first->setColumnMinimumWidth(info.second, width);
	}
}

bool ColumnResizer::eventFilter(QObject*, QEvent* event)
{
	if (event->type() == QEvent::Resize) {
		d->scheduleWidthUpdate();
	}
	return false;
}

void ColumnResizer::addWidgetsFromLayout(QLayout* layout, int column)
{
	Q_ASSERT(column >= 0);
	QGridLayout* gridLayout = qobject_cast<QGridLayout*>(layout);
	QFormLayout* formLayout = qobject_cast<QFormLayout*>(layout);
	if (gridLayout) {
		addWidgetsFromGridLayout(gridLayout, column);
	} else if (formLayout) {
		if (column > QFormLayout::SpanningRole) {
			qCritical() << "column should not be more than" << QFormLayout::SpanningRole << "for QFormLayout";
			return;
		}
		QFormLayout::ItemRole role = static_cast<QFormLayout::ItemRole>(column);
		addWidgetsFromFormLayout(formLayout, role);
	} else {
		qCritical() << "Don't know how to handle layout" << layout;
	}
}

void ColumnResizer::addWidgetsFromGridLayout(QGridLayout* layout, int column)
{
	for (int row = 0; row < layout->rowCount(); ++row) {
		QLayoutItem* item = layout->itemAtPosition(row, column);
		if (!item) {
			continue;
		}
		QWidget* widget = item->widget();
		if (!widget) {
			continue;
		}
		addWidget(widget);
	}
	d->m_gridColumnInfoList << GridColumnInfo(layout, column);
}

void ColumnResizer::addWidgetsFromFormLayout(QFormLayout* layout, QFormLayout::ItemRole role)
{
	for (int row = 0; row < layout->rowCount(); ++row) {
		QLayoutItem* item = layout->itemAt(row, role);
		if (!item) {
			continue;
		}
		QWidget* widget = item->widget();
		if (!widget) {
			continue;
		}
		layout->removeItem(item);
		delete item;
		FormLayoutWidgetItem* newItem = new FormLayoutWidgetItem(widget, layout, role);
		layout->setItem(row, role, newItem);
		addWidget(widget);
		d->m_wrWidgetItemList << newItem;
	}
}

#include <ColumnResizer.moc>