Commit fda09bbc authored by mae's avatar mae

introduce new helper class Core::Utils::TreeWidgetColumnStretcher

and use it for some options dialogs.

The class fixes QTreeWidget to resize all columns to contents, except one
stretching column. As opposed to standard QTreeWidget, all columns are
still interactively resizable.
parent 98e695e3
#include "treewidgetcolumnstretcher.h"
#include <QtGui/QTreeWidget>
#include <QtGui/QHideEvent>
#include <QtGui/QHeaderView>
using namespace Core::Utils;
TreeWidgetColumnStretcher::TreeWidgetColumnStretcher(QTreeWidget *treeWidget, int columnToStretch)
: QObject(treeWidget->header()), m_columnToStretch(columnToStretch)
{
parent()->installEventFilter(this);
QHideEvent fake;
TreeWidgetColumnStretcher::eventFilter(parent(), &fake);
}
bool TreeWidgetColumnStretcher::eventFilter(QObject *obj, QEvent *ev)
{
if (obj == parent()) {
if (ev->type() == QEvent::Show) {
QHeaderView *hv = qobject_cast<QHeaderView*>(obj);
for (int i = 0; i < hv->count(); ++i)
hv->setResizeMode(i, QHeaderView::Interactive);
} else if (ev->type() == QEvent::Hide) {
QHeaderView *hv = qobject_cast<QHeaderView*>(obj);
for (int i = 0; i < hv->count(); ++i)
hv->setResizeMode(i, i == m_columnToStretch ? QHeaderView::Stretch : QHeaderView::ResizeToContents);
} else if (ev->type() == QEvent::Resize) {
QHeaderView *hv = qobject_cast<QHeaderView*>(obj);
if (hv->resizeMode(m_columnToStretch) == QHeaderView::Interactive) {
QResizeEvent *re = static_cast<QResizeEvent*>(ev);
int diff = re->size().width() - re->oldSize().width() ;
hv->resizeSection(m_columnToStretch, qMax(32, hv->sectionSize(1) + diff));
}
}
}
return false;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/
#ifndef TREEWIDGETCOLUMNSTRETCHER_H
#define TREEWIDGETCOLUMNSTRETCHER_H
#include "utils_global.h"
#include <QObject>
QT_BEGIN_NAMESPACE
class QTreeWidget;
QT_END_NAMESPACE
namespace Core {
namespace Utils {
/*
The class fixes QTreeWidget to resize all columns to contents, except one
stretching column. As opposed to standard QTreeWidget, all columns are
still interactively resizable.
*/
class QTCREATOR_UTILS_EXPORT TreeWidgetColumnStretcher : public QObject
{
int m_columnToStretch;
public:
TreeWidgetColumnStretcher(QTreeWidget *treeWidget, int columnToStretch);
bool eventFilter(QObject *obj, QEvent *ev);
};
} // namespace Utils
} // namespace Core
#endif // TREEWIDGETCOLUMNSTRETCHER_H
TEMPLATE = lib
TARGET = Utils
QT += gui network
QT += gui \
network
DEFINES += QTCREATOR_UTILS_LIBRARY
include(../../qtcreatorlibrary.pri)
SOURCES += \
reloadpromptutils.cpp \
SOURCES += reloadpromptutils.cpp \
settingsutils.cpp \
filesearch.cpp \
pathchooser.cpp \
......@@ -30,19 +27,16 @@ SOURCES += \
submitfieldwidget.cpp \
consoleprocess.cpp \
uncommentselection.cpp \
parameteraction.cpp
win32 {
parameteraction.cpp \
treewidgetcolumnstretcher.cpp
win32 {
SOURCES += abstractprocess_win.cpp \
consoleprocess_win.cpp \
winutils.cpp
consoleprocess_win.cpp \
winutils.cpp
HEADERS += winutils.h
} else {
SOURCES += consoleprocess_unix.cpp
}
HEADERS += \
utils_global.h \
else:SOURCES += consoleprocess_unix.cpp
HEADERS += utils_global.h \
reloadpromptutils.h \
settingsutils.h \
filesearch.h \
......@@ -68,11 +62,10 @@ HEADERS += \
synchronousprocess.h \
submitfieldwidget.h \
uncommentselection.h \
parameteraction.h
parameteraction.h \
treewidgetcolumnstretcher.h
FORMS += filewizardpage.ui \
projectintropage.ui \
newclasswidget.ui \
submiteditorwidget.ui
projectintropage.ui \
newclasswidget.ui \
submiteditorwidget.ui
RESOURCES += utils.qrc
......@@ -37,6 +37,8 @@
#include "filemanager.h"
#include "icore.h"
#include "uniqueidmanager.h"
#include <utils/treewidgetcolumnstretcher.h>
#include <QtGui/QKeyEvent>
#include <QtGui/QShortcut>
......@@ -111,10 +113,7 @@ QWidget *ShortcutSettings::createPage(QWidget *parent)
this, SLOT(commandChanged(QTreeWidgetItem *)));
connect(m_page->shortcutEdit, SIGNAL(textChanged(QString)), this, SLOT(keyChanged()));
QHeaderView *hv = m_page->commandList->header();
hv->resizeSection(0, 210);
hv->resizeSection(1, 110);
hv->setStretchLastSection(true);
new Core::Utils::TreeWidgetColumnStretcher(m_page->commandList, 1);
commandChanged(0);
......
......@@ -4,6 +4,7 @@
#include "qt4projectmanagerconstants.h"
#include "qtversionmanager.h"
#include <coreplugin/coreconstants.h>
#include <utils/treewidgetcolumnstretcher.h>
#include <QtCore/QDebug>
#include <QtCore/QDir>
......@@ -60,6 +61,8 @@ void QtOptionsPage::apply()
}
//-----------------------------------------------------
QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent, QList<QtVersion *> versions, QtVersion *defaultVersion)
: QWidget(parent)
, m_defaultVersion(versions.indexOf(defaultVersion))
......@@ -82,6 +85,8 @@ QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent, QList<QtVersion *> ver
m_ui->addButton->setIcon(QIcon(Core::Constants::ICON_PLUS));
m_ui->delButton->setIcon(QIcon(Core::Constants::ICON_MINUS));
new Core::Utils::TreeWidgetColumnStretcher(m_ui->qtdirList, 1);
for (int i = 0; i < m_versions.count(); ++i) {
const QtVersion * const version = m_versions.at(i);
QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment