Merge pull request #6607 from michaelgregorius/ImproveReadabilityOfTextFloat

Improve readability of text floats
This commit is contained in:
Michael Gregorius
2023-04-29 16:53:58 +02:00
committed by GitHub
13 changed files with 233 additions and 182 deletions

View File

@@ -112,6 +112,7 @@ SET(LMMS_SRCS
gui/widgets/NStateButton.cpp
gui/widgets/Oscilloscope.cpp
gui/widgets/PixmapButton.cpp
gui/widgets/SimpleTextFloat.cpp
gui/widgets/TabBar.cpp
gui/widgets/TabWidget.cpp
gui/widgets/TempoSyncKnob.cpp

View File

@@ -66,6 +66,7 @@
#include "PatternStore.h"
#include "PianoView.h"
#include "PositionLine.h"
#include "SimpleTextFloat.h"
#include "SongEditor.h"
#include "StepRecorderWidget.h"
#include "TextFloat.h"
@@ -127,7 +128,7 @@ QPixmap * PianoRoll::s_toolMove = nullptr;
QPixmap * PianoRoll::s_toolOpen = nullptr;
QPixmap* PianoRoll::s_toolKnife = nullptr;
TextFloat * PianoRoll::s_textFloat = nullptr;
SimpleTextFloat * PianoRoll::s_textFloat = nullptr;
static std::array<QString, 12> s_noteStrings {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
@@ -290,7 +291,7 @@ PianoRoll::PianoRoll() :
// init text-float
if( s_textFloat == nullptr )
{
s_textFloat = new TextFloat;
s_textFloat = new SimpleTextFloat;
}
setAttribute( Qt::WA_OpaquePaintEvent, true );

View File

@@ -54,13 +54,13 @@
#include "embed.h"
#include "CaptionMenu.h"
#include "ConfigManager.h"
#include "TextFloat.h"
#include "SimpleTextFloat.h"
namespace lmms::gui
{
TextFloat * Fader::s_textFloat = nullptr;
SimpleTextFloat * Fader::s_textFloat = nullptr;
QPixmap * Fader::s_back = nullptr;
QPixmap * Fader::s_leds = nullptr;
QPixmap * Fader::s_knob = nullptr;
@@ -83,7 +83,7 @@ Fader::Fader( FloatModel * _model, const QString & _name, QWidget * _parent ) :
{
if( s_textFloat == nullptr )
{
s_textFloat = new TextFloat;
s_textFloat = new SimpleTextFloat;
}
if( ! s_back )
{
@@ -125,7 +125,7 @@ Fader::Fader( FloatModel * model, const QString & name, QWidget * parent, QPixma
{
if( s_textFloat == nullptr )
{
s_textFloat = new TextFloat;
s_textFloat = new SimpleTextFloat;
}
m_back = back;
@@ -328,7 +328,8 @@ void Fader::updateTextFloat()
{
s_textFloat->setText( m_description + " " + QString("%1 ").arg( model()->value() * m_conversionFactor ) + " " + m_unit );
}
s_textFloat->moveGlobal( this, QPoint( width() - ( *m_knob ).width() - 5, knobPosY() - 46 ) );
s_textFloat->moveGlobal(this, QPoint(width() + 2, knobPosY() - s_textFloat->height() / 2));
}
@@ -483,4 +484,4 @@ void Fader::setPeakYellow( const QColor & c )
}
} // namespace lmms::gui
} // namespace lmms::gui

View File

@@ -45,14 +45,14 @@
#include "LocaleHelper.h"
#include "MainWindow.h"
#include "ProjectJournal.h"
#include "SimpleTextFloat.h"
#include "StringPairDrag.h"
#include "TextFloat.h"
namespace lmms::gui
{
TextFloat * Knob::s_textFloat = nullptr;
SimpleTextFloat * Knob::s_textFloat = nullptr;
@@ -86,7 +86,7 @@ void Knob::initUi( const QString & _name )
{
if( s_textFloat == nullptr )
{
s_textFloat = new TextFloat;
s_textFloat = new SimpleTextFloat;
}
setWindowTitle( _name );

View File

@@ -0,0 +1,62 @@
/*
* TextFloat.cpp - class textFloat, a floating text-label
*
* Copyright (c) LMMS team
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "SimpleTextFloat.h"
#include <QTimer>
#include <QStyleOption>
#include <QHBoxLayout>
#include <QLabel>
#include "GuiApplication.h"
#include "MainWindow.h"
namespace lmms::gui
{
SimpleTextFloat::SimpleTextFloat() :
QWidget(getGUI()->mainWindow(), Qt::ToolTip)
{
QHBoxLayout * layout = new QHBoxLayout(this);
layout->setMargin(3);
setLayout(layout);
m_textLabel = new QLabel(this);
layout->addWidget(m_textLabel);
}
void SimpleTextFloat::setText(const QString & text)
{
m_textLabel->setText(text);
}
void SimpleTextFloat::setVisibilityTimeOut(int msecs)
{
QTimer::singleShot(msecs, this, SLOT(hide()));
show();
}
} // namespace lmms::gui

View File

@@ -22,12 +22,15 @@
*
*/
#include "TextFloat.h"
#include <QTimer>
#include <QPainter>
#include <QStyleOption>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include "TextFloat.h"
#include "gui_templates.h"
#include "GuiApplication.h"
#include "MainWindow.h"
@@ -36,170 +39,97 @@ namespace lmms::gui
TextFloat::TextFloat() :
QWidget( getGUI()->mainWindow(), Qt::ToolTip ),
m_title(),
m_text(),
m_pixmap()
TextFloat("", "", QPixmap())
{
resize( 20, 20 );
hide();
setAttribute( Qt::WA_TranslucentBackground, true );
setStyle( QApplication::style() );
setFont( pointSize<8>( font() ) );
}
void TextFloat::setTitle( const QString & _title )
TextFloat::TextFloat(const QString & title, const QString & text, const QPixmap & pixmap) :
QWidget(getGUI()->mainWindow(), Qt::ToolTip)
{
m_title = _title;
updateSize();
QHBoxLayout * mainLayout = new QHBoxLayout();
setLayout(mainLayout);
// Create the label that displays the pixmap
m_pixmapLabel = new QLabel(this);
mainLayout->addWidget(m_pixmapLabel);
// Create the widget that displays the title and the text
QWidget * titleAndTextWidget = new QWidget(this);
QVBoxLayout * titleAndTextLayout = new QVBoxLayout();
titleAndTextWidget->setLayout(titleAndTextLayout);
m_titleLabel = new QLabel(titleAndTextWidget);
m_titleLabel->setStyleSheet("font-weight: bold;");
titleAndTextLayout->addWidget(m_titleLabel);
m_textLabel = new QLabel(titleAndTextWidget);
titleAndTextLayout->addWidget(m_textLabel);
mainLayout->addWidget(titleAndTextWidget);
// Call the setters so that the hidden state is updated
setTitle(title);
setText(text);
setPixmap(pixmap);
}
void TextFloat::setText( const QString & _text )
void TextFloat::setTitle(const QString & title)
{
m_text = _text;
updateSize();
m_titleLabel->setText(title);
m_titleLabel->setHidden(title.isEmpty());
}
void TextFloat::setPixmap( const QPixmap & _pixmap )
void TextFloat::setText(const QString & text)
{
m_pixmap = _pixmap;
updateSize();
m_textLabel->setText(text);
m_textLabel->setHidden(text.isEmpty());
}
void TextFloat::setVisibilityTimeOut( int _msecs )
void TextFloat::setPixmap(const QPixmap & pixmap)
{
QTimer::singleShot( _msecs, this, SLOT(hide()));
m_pixmapLabel->setPixmap(pixmap);
m_pixmapLabel->setHidden(pixmap.isNull());
}
void TextFloat::setVisibilityTimeOut(int msecs)
{
QTimer::singleShot(msecs, this, SLOT(hide()));
show();
}
TextFloat * TextFloat::displayMessage( const QString & _msg, int _timeout,
QWidget * _parent, int _add_y_margin )
TextFloat * TextFloat::displayMessage(const QString & title,
const QString & msg,
const QPixmap & pixmap,
int timeout, QWidget * parent)
{
QWidget * mw = getGUI()->mainWindow();
auto tf = new TextFloat;
if( _parent != nullptr )
{
tf->moveGlobal( _parent, QPoint( _parent->width() + 2, 0 ) );
}
else
{
tf->moveGlobal( mw, QPoint( 32, mw->height() - tf->height() - 8 - _add_y_margin ) );
}
tf->setText( _msg );
auto tf = new TextFloat(title, msg, pixmap);
// Show the widget so that the correct height is calculated in the code that follows
tf->show();
if( _timeout > 0 )
if(parent != nullptr)
{
tf->setAttribute( Qt::WA_DeleteOnClose, true );
QTimer::singleShot( _timeout, tf, SLOT(close()));
}
return( tf );
}
TextFloat * TextFloat::displayMessage( const QString & _title,
const QString & _msg,
const QPixmap & _pixmap,
int _timeout, QWidget * _parent )
{
TextFloat * tf = displayMessage( _msg, _timeout, _parent, 16 );
tf->setTitle( _title );
tf->setPixmap( _pixmap );
return( tf );
}
void TextFloat::paintEvent( QPaintEvent * _pe )
{
QStyleOption opt;
opt.init( this );
QPainter p( this );
p.fillRect( 0, 0, width(), height(), QColor( 0, 0, 0, 0 ) );
/* p.setPen( p.pen().brush().color() );
p.setBrush( p.background() );*/
p.setFont( pointSize<8>( p.font() ) );
style()->drawPrimitive( QStyle::PE_Widget, &opt, &p, this );
/* p.drawRect( 0, 0, rect().right(), rect().bottom() );*/
if( m_title.isEmpty() )
{
p.drawText( opt.rect, Qt::AlignCenter, m_text );
tf->moveGlobal(parent, QPoint(parent->width() + 2, 0));
}
else
{
int text_x = opt.rect.left() + 2;
int text_y = opt.rect.top() + 12;
if( m_pixmap.isNull() == false )
{
p.drawPixmap( opt.rect.topLeft() + QPoint( 5, 5 ), m_pixmap );
text_x += m_pixmap.width() + 8;
}
p.drawText( text_x, text_y + 16, m_text );
QFont f = p.font();
f.setBold( true );
p.setFont( f );
p.drawText( text_x, text_y, m_title );
// If no parent is given move the window to the lower left area of the main window
QWidget * mw = getGUI()->mainWindow();
tf->moveGlobal(mw, QPoint(32, mw->height() - tf->height() - 8));
}
if (timeout > 0)
{
tf->setAttribute(Qt::WA_DeleteOnClose, true);
QTimer::singleShot(timeout, tf, SLOT(close()));
}
return tf;
}
void TextFloat::mousePressEvent( QMouseEvent * )
void TextFloat::mousePressEvent(QMouseEvent *)
{
close();
}
void TextFloat::updateSize()
{
QFontMetrics metrics( pointSize<8>( font() ) );
QRect textBound = metrics.boundingRect( m_text );
if( !m_title.isEmpty() )
{
QFont f = pointSize<8>( font() );
f.setBold( true );
int title_w = QFontMetrics( f ).boundingRect( m_title ).width();
if( title_w > textBound.width() )
{
textBound.setWidth( title_w );
}
textBound.setHeight( textBound.height() * 2 + 8 );
}
if( m_pixmap.isNull() == false )
{
textBound.setWidth( textBound.width() + m_pixmap.width() + 10 );
}
resize( textBound.width() + 5, textBound.height()+2 );
//move( QPoint( parentWidget()->width() + 5, 5 ) );
update();
}
} // namespace lmms::gui