Skip to content

Commit

Permalink
Merge pull request #129 from shundhammer/huha-auto-wrap
Browse files Browse the repository at this point in the history
Auto-wrapping for YLabel Widgets (Qt Part)
  • Loading branch information
shundhammer committed Jun 8, 2020
2 parents abfd7e8 + 745aa4c commit cf7abc3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
6 changes: 3 additions & 3 deletions VERSION.cmake
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
SET(VERSION_MAJOR "2")
SET(VERSION_MINOR "52")
SET(VERSION_PATCH "4")
SET(VERSION_MINOR "53")
SET(VERSION_PATCH "0")
SET( VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${GIT_SHA1_VERSION}" )

##### This is needed for the libyui-qt core ONLY.
##### These will be overridden from exports in LibyuiConfig.cmake
SET( SONAME_MAJOR "11" )
SET( SONAME_MAJOR "12" )
SET( SONAME_MINOR "0" )
SET( SONAME_PATCH "0" )
SET( SONAME "${SONAME_MAJOR}.${SONAME_MINOR}.${SONAME_PATCH}" )
4 changes: 2 additions & 2 deletions package/libyui-qt-doc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@


%define parent libyui-qt
%define so_version 11
%define so_version 12

Name: %{parent}-doc
# DO NOT manually bump the version here; instead, use rake version:bump
Version: 2.52.4
Version: 2.53.0
Release: 0
Source: %{parent}-%{version}.tar.bz2

Expand Down
8 changes: 8 additions & 0 deletions package/libyui-qt.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
-------------------------------------------------------------------
Thu Jun 4 11:58:01 UTC 2020 - Stefan Hundhammer <shundhammer@suse.com>

- Added autoWrap to label widget (bsc#1172513)
- Bumped SO version to 12
- 2.53.0


-------------------------------------------------------------------
Wed May 20 10:00:31 UTC 2020 - Stefan Hundhammer <shundhammer@suse.com>

Expand Down
6 changes: 3 additions & 3 deletions package/libyui-qt.spec
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@

Name: libyui-qt
# DO NOT manually bump the version here; instead, use rake version:bump
Version: 2.52.4
Version: 2.53.0
Release: 0
Source: %{name}-%{version}.tar.bz2

%define so_version 11
%define so_version 12
%define bin_name %{name}%{so_version}

BuildRequires: boost-devel
BuildRequires: cmake >= 2.8
BuildRequires: gcc-c++
BuildRequires: pkg-config

%define libyui_devel_version libyui-devel >= 3.9.1
%define libyui_devel_version libyui-devel >= 3.10.0
BuildRequires: %{libyui_devel_version}
BuildRequires: fontconfig-devel
BuildRequires: pkgconfig(Qt5Core)
Expand Down
85 changes: 83 additions & 2 deletions src/YQLabel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "YQApplication.h"
#include "YQLabel.h"

#define AUTO_WRAP_WIDTH 150
#define AUTO_WRAP_HEIGHT 10

using std::string;


Expand All @@ -43,6 +46,7 @@ YQLabel::YQLabel( YWidget * parent,
bool isOutputField )
: QLabel( (QWidget *) parent->widgetRep() )
, YLabel( parent, text, isHeading, isOutputField )
, _layoutPass1Width( 0 )
{
setWidgetRep( this );

Expand Down Expand Up @@ -90,6 +94,13 @@ void YQLabel::setUseBoldFont( bool useBold )
}


void YQLabel::setAutoWrap( bool autoWrap )
{
YLabel::setAutoWrap( autoWrap );
QLabel::setWordWrap( autoWrap );
}


void YQLabel::setEnabled( bool enabled )
{
QLabel::setEnabled( enabled );
Expand All @@ -99,17 +110,87 @@ void YQLabel::setEnabled( bool enabled )

int YQLabel::preferredWidth()
{
return sizeHint().width();
int width;

if ( autoWrap() )
{
if ( layoutPass() == 2 )
{
// Use the width passed down to us from the parent layout manager
// in the last setSize() call. This is the definitive width that
// will be used after taking all other children of the layout into
// consideration, also including making widgets smaller due to size
// restrictions, or redistributing excess space.
//
// Since this widget can auto-wrap its contents, we accept
// basically any width; we just need to adapt the preferred height
// accordingly.
width = _layoutPass1Width;
}
else
{
// Use a preliminary width. Typically, this widget should be
// wrapped into a MinSize or MinWidth which hopefully gives us a
// much more useful width than this.
//
// We would also just use 0 here, but that would make debugging
// really hard since the widget might completly disappear.
//
// The real width that will be used will be set in the setSize()
// call following the recursive preferredWidth() /
// preferredHeight() calls in the widget tree.
width = AUTO_WRAP_WIDTH;
}
}
else // ! autoWrap()
{
width = sizeHint().width();
}

return width;
}


int YQLabel::preferredHeight()
{
return sizeHint().height();
int height;

if ( autoWrap() )
{
if ( layoutPass() == 2 )
{
// This is where the magic happens:
//
// setSize() in the first layout pass gave us the real width which
// we stored in _layoutPass1Width. We can now calculate the height
// that is really needed based on that width. QLabel provides this
// handy function that takes word wrapping and font metrics into
// account (remember, we are using a proportional font, so every
// letter has a different width).

height = heightForWidth( _layoutPass1Width );
}
else
{
height = AUTO_WRAP_HEIGHT;
}
}
else // ! autoWrap()
{
height = sizeHint().height();
}

return height;
}


void YQLabel::setSize( int newWidth, int newHeight )
{
if ( autoWrap() )
{
_layoutPass1Width = layoutPass() == 1 ?
newWidth : 0;
}

resize( newWidth, newHeight );
}
11 changes: 11 additions & 0 deletions src/YQLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class YQLabel : public QLabel, public YLabel
**/
virtual void setUseBoldFont( bool bold );

/*
* Enable or disable automatic word wrapping.
*
* Reimplemented from YLabel.
**/
virtual void setAutoWrap( bool autoWrap = true );

/**
* Set enabled / disabled state.
*
Expand Down Expand Up @@ -90,6 +97,10 @@ class YQLabel : public QLabel, public YLabel
* Reimplemented from YWidget.
**/
virtual void setSize( int newWidth, int newHeight );

protected:

int _layoutPass1Width;
};


Expand Down

0 comments on commit cf7abc3

Please sign in to comment.