From ddf63cb924abc6f6aefea74a727dcf07de552b18 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Sat, 25 Jan 2014 01:09:39 +0100 Subject: [PATCH] ComboBox: added sizeHint() + coding style --- include/combobox.h | 32 +++++++++++++++++--------------- src/gui/widgets/combobox.cpp | 20 ++++++++++++++++++-- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/include/combobox.h b/include/combobox.h index d4d432640..76bb36163 100644 --- a/include/combobox.h +++ b/include/combobox.h @@ -1,7 +1,7 @@ /* - * combobox.h - class comboBox, a very cool combo-box + * combobox.h - class ComboBox, a combo box view for models * - * Copyright (c) 2006-2008 Tobias Doerffel + * Copyright (c) 2006-2014 Tobias Doerffel * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * @@ -38,31 +38,33 @@ class EXPORT comboBox : public QWidget, public IntModelView { Q_OBJECT public: - comboBox( QWidget * _parent, const QString & _name = QString() ); + comboBox( QWidget* parent = NULL, const QString& name = QString() ); virtual ~comboBox(); - ComboBoxModel * model() + ComboBoxModel* model() { - return( castModel() ); + return castModel(); } - const ComboBoxModel * model() const + const ComboBoxModel* model() const { - return( castModel() ); + return castModel(); } + virtual QSize sizeHint() const; + protected: - virtual void contextMenuEvent( QContextMenuEvent * _me ); - virtual void mousePressEvent( QMouseEvent * _me ); - virtual void paintEvent( QPaintEvent * _pe ); - virtual void wheelEvent( QWheelEvent * _we ); + virtual void contextMenuEvent( QContextMenuEvent* event ); + virtual void mousePressEvent( QMouseEvent* event ); + virtual void paintEvent( QPaintEvent* event ); + virtual void wheelEvent( QWheelEvent* event ); private: - static QPixmap * s_background; - static QPixmap * s_arrow; - static QPixmap * s_arrowSelected; + static QPixmap* s_background; + static QPixmap* s_arrow; + static QPixmap* s_arrowSelected; QMenu m_menu; @@ -70,7 +72,7 @@ private: private slots: - void setItem( QAction * _item ); + void setItem( QAction* item ); } ; diff --git a/src/gui/widgets/combobox.cpp b/src/gui/widgets/combobox.cpp index 9244344b9..c78e59ff7 100644 --- a/src/gui/widgets/combobox.cpp +++ b/src/gui/widgets/combobox.cpp @@ -1,7 +1,7 @@ /* - * combobox.cpp - implementation of LMMS-combobox + * combobox.cpp - implementation of LMMS combobox * - * Copyright (c) 2006-2011 Tobias Doerffel + * Copyright (c) 2006-2014 Tobias Doerffel * Copyright (c) 2008-2009 Paul Giblock * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net @@ -88,6 +88,22 @@ comboBox::~comboBox() +QSize comboBox::sizeHint() const +{ + int maxTextWidth = 0; + for( int i = 0; i < model()->size(); ++i ) + { + int w = fontMetrics().width( model()->itemText( i ) ); + if( w > maxTextWidth ) + { + maxTextWidth = w; + } + } + + return QSize( 32 + maxTextWidth, 22 ); +} + + void comboBox::contextMenuEvent( QContextMenuEvent * _me ) {