forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainstatusbar.cc
133 lines (109 loc) · 2.77 KB
/
mainstatusbar.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This file is (c) 2012 Tvangeste <[email protected]>
* 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::Ignored );
timer = new QTimer( this );
timer->setSingleShot( true );
// 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 );
parentWidget()->installEventFilter( this );
connect( timer, SIGNAL( timeout() ), SLOT( clearMessage() ) );
}
bool MainStatusBar::hasImage() const
{
return !picWidget->pixmap()->isNull();
}
void MainStatusBar::clearMessage()
{
message.clear();
textWidget->setText( backgroungMessage );
picWidget->setPixmap( QPixmap() );
timer->stop();
refresh();
}
QString MainStatusBar::currentMessage() const
{
return message;
}
void MainStatusBar::setBackgroundMessage(const QString & bkg_message )
{
backgroungMessage = bkg_message;
if( message.isEmpty() )
{
textWidget->setText( backgroungMessage );
refresh();
}
}
void MainStatusBar::showMessage(const QString & str, int timeout, const QPixmap & pixmap)
{
textWidget->setText( message = str );
picWidget->setPixmap( pixmap );
// reload stylesheet
setStyleSheet( styleSheet() );
if ( timeout > 0 )
{
timer->start( timeout );
}
refresh();
}
void MainStatusBar::refresh()
{
if ( !textWidget->text().isEmpty() )
{
adjustSize();
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();
}
bool MainStatusBar::eventFilter( QObject *, QEvent * e )
{
switch ( e->type() ) {
case QEvent::Resize:
refresh();
break;
default:
break;
};
return false;
}