a1f75a13ffc855a44c283e4c69a565dce736e9be
[qtmeetings] / src / UserInterface / Views / RoomStatusIndicatorWidget.cpp
1 #include "RoomStatusIndicatorWidget.h"
2
3 #include <QLabel>
4 #include <QFont>
5 #include <QVBoxLayout>
6 #include "DigitalTimeDisplayWidget.h"
7 #include "ToolBox.h"
8
9 #include <QEvent>
10
11 #include <QtDebug>
12
13 QTime RoomStatusIndicatorWidget::endOfTheDay = QTime( 23, 59, 0, 0 );
14
15 RoomStatusIndicatorWidget::RoomStatusIndicatorWidget( Room *aDefaultRoom, Room::Status aStatus, QTime aUntil, QString aTimeFormat, QWidget *aParent ) :
16                 ViewBase( ViewBase::ObservedView, aParent ), iTimeFormat( aTimeFormat )
17 {
18         QFont importantTextFont;
19         //importantTextFont.setBold( true );
20         importantTextFont.setPointSize( 20 );
21
22         QFont regularTextFont;
23         //regularTextFont.setBold( true );
24         regularTextFont.setPointSize( 12 );
25
26         // display for current time
27         // Note: the time display receives current time info from Engine::clock()
28         iTimeDisplay = new DigitalTimeDisplayWidget( QTime::currentTime(), iTimeFormat, this );
29         iTimeDisplay->setFrameVisible( false );
30         iTimeDisplay->setSize( 250, 120 );
31
32         // Pegasus
33         iDefaultRoomLabel = ToolBox::createLabel( aDefaultRoom->name(), importantTextFont );
34         iDefaultRoomLabel->setAlignment( Qt::AlignHCenter );
35         iDefaultRoomLabel->setStyleSheet( "background-color: transparent" );
36         iDefaultRoomLabel->setHidden( true );
37         
38         // is busy
39         iStatusLabel = ToolBox::createLabel( tr( "is %1" ).arg( statusToText( aStatus ) ), importantTextFont );
40         iStatusLabel->setAlignment( Qt::AlignHCenter );
41         iStatusLabel->setStyleSheet( "background-color: transparent" );
42         iStatusLabel->setHidden( true );
43
44         // until 13:22
45         iUntilTextLabel = ToolBox::createLabel( tr( "until %1" ).arg( aUntil.toString( iTimeFormat ) ), importantTextFont );
46         iUntilTextLabel->setAlignment( Qt::AlignHCenter );
47         iUntilTextLabel->setStyleSheet( "background-color: transparent" );
48         iUntilTextLabel->setHidden( true );
49         
50         // No connection to server note
51         qDebug() << "RoomStatusIndicatorWidget::RoomStatusIndicatorWidget() creating connection label";
52         QFrame* connectionLabelFrame = new QFrame( this );
53         iConnectionLabel = new QLabel( tr( "No connection to server" ), connectionLabelFrame );
54         iConnectionLabel->setFont( importantTextFont ); 
55         iConnectionLabel->setAlignment( Qt::AlignHCenter );
56         iConnectionLabel->setWordWrap( true );
57         iConnectionLabel->setStyleSheet( "background-color: transparent; color: red; text-decoration:blink; max-width: 250px" );
58         connectionLabelFrame->setFixedSize( iConnectionLabel->sizeHint() );
59         if( connectedOnce && !connectionError ) iConnectionLabel->setHidden( true );
60                 
61
62         QVBoxLayout *topLayout = new QVBoxLayout;
63         topLayout->addStretch();
64         topLayout->addWidget( iTimeDisplay );
65         topLayout->addSpacing( 28 );
66         topLayout->addWidget( iDefaultRoomLabel );
67         topLayout->addWidget( iStatusLabel );
68         topLayout->addWidget( iUntilTextLabel );
69         topLayout->addSpacing( 28 );
70         topLayout->addWidget( connectionLabelFrame );
71         topLayout->addStretch();
72
73         QHBoxLayout *mainLayout = new QHBoxLayout;
74         mainLayout->addLayout( topLayout );
75         mainLayout->addStretch();
76         //mainLayout->setMargin( 65 );
77         mainLayout->setContentsMargins( 65, 65, 65, 0 );
78         setLayout( mainLayout );
79
80         statusChanged( aStatus, aUntil );
81
82         setFocusPolicy( Qt::StrongFocus );
83         setEnabled( true ); // enable mouse & key events
84 }
85
86 RoomStatusIndicatorWidget::~RoomStatusIndicatorWidget()
87 {
88         delete iTimeDisplay;
89         iTimeDisplay = 0;
90 }
91
92 QString RoomStatusIndicatorWidget::statusToText( const Room::Status aStatus )
93 {
94         return ( aStatus == Room::BusyStatus ) ? tr( "busy" ) : tr( "free" );
95 }
96
97 QPalette RoomStatusIndicatorWidget::createPalette( Room::Status aStatus )
98 {
99         QString image = aStatus == Room::BusyStatus ? ":roomstatus_busy" : ":roomstatus_free";
100         QPixmap pixmap( image );
101
102         // The image needs to be moved in normal mode so the traffic light not partly outside the screen
103         const int xoffset( 60 );
104         const int yoffset( 19 );
105         int cropwidth( pixmap.width() - xoffset );
106         int cropheight( pixmap.height() - yoffset );
107         
108         QBrush brush;
109         if ( windowState() == Qt::WindowFullScreen )
110         {
111                 // Use the full image in full screen mode
112                 brush.setTexture( pixmap );
113         }
114         else
115         {
116                 // Take part of the image so the traffic lights are moved xoffset poxels to left 
117                 // and yoffset pixels to up
118                 brush.setTexture( pixmap.copy( xoffset, yoffset, cropwidth, cropheight ) );
119         }
120
121         QPalette palette;
122         palette.setBrush( QPalette::Window, brush );
123         return palette;
124 }
125
126 void RoomStatusIndicatorWidget::setCurrentTime( QTime aCurrentTime )
127 {
128         iTimeDisplay->setTime( aCurrentTime );
129 }
130
131 void RoomStatusIndicatorWidget::statusChanged( const Room::Status aStatus, const QTime aUntil )
132 {
133         iStatusLabel->setText( tr( "is %1" ).arg( statusToText( aStatus ) ) );
134         if ( aUntil == RoomStatusIndicatorWidget::endOfTheDay )
135         {
136                 iUntilTextLabel->setText( tr( "whole day." ) );
137         }
138         else
139         {
140                 iUntilTextLabel->setText( tr( "until %1" ).arg( aUntil.toString( iTimeFormat ) ) );
141         }
142         setPalette( createPalette( aStatus ) );
143 }
144
145 void RoomStatusIndicatorWidget::currentRoomChanged( Room *aRoom )
146 {
147         iDefaultRoomLabel->setText( aRoom->name() );
148 }
149
150 bool RoomStatusIndicatorWidget::event(QEvent *event)
151 {
152         switch(event->type())
153         {
154                 case QEvent::Paint:
155                         qDebug() << "[RoomStatusIndicatorWidget::event] <Paint event>";
156                         break;
157                 case QEvent::PaletteChange:
158                         qDebug() << "[RoomStatusIndicatorWidget::event] <Palette change event>";
159                         break;
160                 default:
161                         break;
162         }
163         
164         return ViewBase::event( event );
165 }