2 ******************************************************************************
4 * @file treewidgetcolumnstretcher.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "treewidgetcolumnstretcher.h"
30 #include <QTreeWidget>
32 #include <QHeaderView>
33 using namespace Utils
;
35 TreeWidgetColumnStretcher::TreeWidgetColumnStretcher(QTreeWidget
*treeWidget
, int columnToStretch
)
36 : QObject(treeWidget
->header()), m_columnToStretch(columnToStretch
)
38 parent()->installEventFilter(this);
40 TreeWidgetColumnStretcher::eventFilter(parent(), &fake
);
43 bool TreeWidgetColumnStretcher::eventFilter(QObject
*obj
, QEvent
*ev
)
45 if (obj
== parent()) {
46 if (ev
->type() == QEvent::Show
) {
47 QHeaderView
*hv
= qobject_cast
<QHeaderView
*>(obj
);
48 for (int i
= 0; i
< hv
->count(); ++i
) {
49 hv
->setSectionResizeMode(i
, QHeaderView::Interactive
);
51 } else if (ev
->type() == QEvent::Hide
) {
52 QHeaderView
*hv
= qobject_cast
<QHeaderView
*>(obj
);
53 for (int i
= 0; i
< hv
->count(); ++i
) {
54 hv
->setSectionResizeMode(i
, i
== m_columnToStretch
? QHeaderView::Stretch
: QHeaderView::ResizeToContents
);
56 } else if (ev
->type() == QEvent::Resize
) {
57 QHeaderView
*hv
= qobject_cast
<QHeaderView
*>(obj
);
58 if (hv
->sectionResizeMode(m_columnToStretch
) == QHeaderView::Interactive
) {
59 QResizeEvent
*re
= static_cast<QResizeEvent
*>(ev
);
60 int diff
= re
->size().width() - re
->oldSize().width();
61 hv
->resizeSection(m_columnToStretch
, qMax(32, hv
->sectionSize(1) + diff
));