goldendict-ng/mainstatusbar.cc
Tvangeste 354066c292 Reimplemented the status bar as internal widget instead of top-level borderless window.
Now the status bar behavior is much more reliable on X11 systems, no more
lags when moving GoldenDict window around or when resizing it. Works
better with compositing window managers as well.

Also, the new status bar also supports images, which is good for
warnings and error messages.

The status bar is fully styled and can change its appearence
based on user's CSS, if needed.
2011-07-09 13:55:44 +02:00

97 lines
2.1 KiB
C++

/* This file is (c) 2011 Tvangeste <i.4m.l33t@yandex.ru>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "mainstatusbar.hh"
#include <Qt>
#include <QFrame>
#include <QVBoxLayout>
#include <QDebug>
#include <QEvent>
#include <QApplication>
MainStatusBar::MainStatusBar(QWidget *parent) : QWidget(parent)
{
textWidget = new QLabel(QString(), this);
textWidget->setObjectName("text");
textWidget->setFont(QApplication::font("QStatusBar"));
textWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
picWidget = new QLabel(QString(), this);
picWidget->setObjectName("icon");
picWidget->setPixmap(QPixmap());
picWidget->setScaledContents(true);
picWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
timer = new QTimer(this);
// layout
QHBoxLayout * layout = new QHBoxLayout;
layout->setSpacing(0);
layout->setSizeConstraint(QLayout::SetFixedSize);
layout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(picWidget);
layout->addWidget(textWidget);
setLayout(layout);
connect( timer, SIGNAL( timeout() ), SLOT( clearMessage() ) );
}
void MainStatusBar::clearMessage()
{
textWidget->setText(QString());
picWidget->setPixmap(QPixmap());
timer->stop();
refresh();
}
QString MainStatusBar::currentMessage() const
{
return textWidget->text();
}
void MainStatusBar::showMessage(const QString & str, int timeout, const QPixmap & pixmap)
{
textWidget->setText( str );
picWidget->setPixmap( pixmap );
if ( timeout > 0 )
{
timer->start( timeout );
}
refresh();
}
void MainStatusBar::refresh()
{
if ( !currentMessage().isEmpty() )
{
if ( !picWidget->pixmap()->isNull() )
{
picWidget->setFixedSize( textWidget->height(), textWidget->height() );
}
else
{
picWidget->setFixedSize( 0, 0);
}
adjustSize();
move( QPoint( 0, parentWidget()->height() - height() ) );
show();
raise();
}
else
{
hide();
}
}
void MainStatusBar::mousePressEvent ( QMouseEvent * )
{
clearMessage();
}