198 lines
4.3 KiB
C++
198 lines
4.3 KiB
C++
|
/***************************************************************************
|
||
|
|
||
|
source::worx libWidgets
|
||
|
Copyright © 2021-2022 c.holzheuer
|
||
|
c.holzheuer@sourceworx.org
|
||
|
|
||
|
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.
|
||
|
|
||
|
***************************************************************************/
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
#include <QPaintEvent>
|
||
|
#include <QPainter>
|
||
|
#include <QDebug>
|
||
|
#include <QEvent>
|
||
|
#include <QStyleOption>
|
||
|
|
||
|
#include <swsidebar.h>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
SWSideBar::SWSideBar( QWidget *parent )
|
||
|
: QFrame( parent )
|
||
|
{
|
||
|
setMouseTracking( true );
|
||
|
}
|
||
|
|
||
|
|
||
|
SWSideBar::~SWSideBar()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void SWSideBar::appendAction( QAction* action )
|
||
|
{
|
||
|
Q_ASSERT( action != nullptr );
|
||
|
action->setCheckable( true );
|
||
|
//action->setEnabled( false );
|
||
|
QFrame::addAction( action );
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void SWSideBar::appendAction( const QIcon& icon, const QString& text )
|
||
|
{
|
||
|
return appendAction( new QAction( icon, text ) );
|
||
|
}
|
||
|
|
||
|
void SWSideBar::setCheckedAction( QAction* action )
|
||
|
{
|
||
|
/// PFUSCH! Prüfen!
|
||
|
_checkedAction = action;
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
void SWSideBar::paintEvent( QPaintEvent* event )
|
||
|
{
|
||
|
|
||
|
|
||
|
QPainter painter( this );
|
||
|
|
||
|
int actionY = 0;
|
||
|
for( auto& action : actions() )
|
||
|
{
|
||
|
|
||
|
QRect actionRect( 0, actionY, event->rect().width(), SWACTIONHEIGHT );
|
||
|
if( action->isEnabled() )
|
||
|
{
|
||
|
if( action == _hoveredAction )
|
||
|
painter.fillRect( actionRect, _hoveredColor );
|
||
|
|
||
|
if( action == _pressedAction )
|
||
|
painter.fillRect( actionRect, Qt::lightGray );
|
||
|
|
||
|
if( action == _checkedAction )
|
||
|
painter.fillRect( actionRect, _checkedColor );
|
||
|
|
||
|
}
|
||
|
|
||
|
QSize size = painter.fontMetrics().size( Qt::TextSingleLine, action->text() );
|
||
|
QRect textRect( QPoint( actionRect.width()/2 - size.width()/2, actionRect.bottom()-size.height() - 10 ), size );
|
||
|
painter.setPen( action->isEnabled() ? Qt::white : Qt::gray );
|
||
|
painter.drawText( textRect, Qt::AlignCenter, action->text() );
|
||
|
|
||
|
QRect pixmapRect( actionRect.width()/2 - 32, actionY+15 , 64, 64 );
|
||
|
QPixmap pixmap = action->icon().pixmap( QSize( 64, 64 ), action->isEnabled() ? QIcon::Normal : QIcon::Disabled, QIcon::On );
|
||
|
painter.drawPixmap( pixmapRect, pixmap );
|
||
|
|
||
|
actionY += actionRect.height();
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void SWSideBar::mousePressEvent( QMouseEvent* event )
|
||
|
{
|
||
|
|
||
|
QAction* tempAction = actionAt( event->pos() );
|
||
|
if( tempAction == nullptr )
|
||
|
return;
|
||
|
|
||
|
if( _hoveredAction == tempAction )
|
||
|
_hoveredAction = nullptr;
|
||
|
|
||
|
_pressedAction = tempAction;
|
||
|
|
||
|
update();
|
||
|
QFrame::mousePressEvent (event );
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void SWSideBar::mouseReleaseEvent( QMouseEvent *event )
|
||
|
{
|
||
|
|
||
|
QAction* tempAction = actionAt( event->pos() );
|
||
|
if( tempAction == nullptr )
|
||
|
return;
|
||
|
|
||
|
if( tempAction == _pressedAction )
|
||
|
{
|
||
|
_hoveredAction = _pressedAction;
|
||
|
_checkedAction = _pressedAction;
|
||
|
_pressedAction = nullptr;
|
||
|
update();
|
||
|
if( _checkedAction->isEnabled() )
|
||
|
_checkedAction->trigger();
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
//update();QGuiApplication::palette()) an QPalette::Highlight
|
||
|
QFrame::mouseReleaseEvent( event );
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void SWSideBar::mouseMoveEvent(QMouseEvent *event)
|
||
|
{
|
||
|
QAction* tempAction = actionAt( event->pos() );
|
||
|
/// nix gefunden
|
||
|
if( tempAction == nullptr)
|
||
|
{
|
||
|
/// dann ist auch nix gehovered
|
||
|
_hoveredAction = nullptr;
|
||
|
update();
|
||
|
return;
|
||
|
}
|
||
|
/// ist schon gehovered? auch weg
|
||
|
if( _hoveredAction == tempAction )
|
||
|
return;
|
||
|
|
||
|
_hoveredAction = tempAction;
|
||
|
update();
|
||
|
QFrame::mouseMoveEvent(event);
|
||
|
}
|
||
|
|
||
|
|
||
|
void SWSideBar::leaveEvent(QEvent * event)
|
||
|
{
|
||
|
_hoveredAction = _pressedAction = nullptr;
|
||
|
update();
|
||
|
QFrame::leaveEvent(event);
|
||
|
}
|
||
|
|
||
|
|
||
|
QSize SWSideBar::minimumSizeHint() const
|
||
|
{
|
||
|
return SWACTIONHEIGHT * QSize( 1, actions().size() );
|
||
|
}
|
||
|
|
||
|
|
||
|
QAction* SWSideBar::actionAt(const QPoint &at)
|
||
|
{
|
||
|
int actionY = 0;
|
||
|
|
||
|
for( QAction* action : actions() )
|
||
|
{
|
||
|
QRect actionRect( 0, actionY, rect().width(), SWACTIONHEIGHT );
|
||
|
if(actionRect.contains(at))
|
||
|
return action;
|
||
|
actionY += actionRect.height();
|
||
|
}
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
|