ebe2a90032ead060213d3329c50c60c782bf9f1c
[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
34                         = ToolBox::createLabel(aDefaultRoom->name(), importantTextFont);
35         iDefaultRoomLabel->setAlignment(Qt::AlignHCenter);
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
46                         = ToolBox::createLabel(tr( "until %1" ).arg(aUntil.toString(iTimeFormat) ), importantTextFont);
47         iUntilTextLabel->setAlignment(Qt::AlignHCenter);
48         iUntilTextLabel->setStyleSheet("background-color: transparent");
49         iUntilTextLabel->setHidden( true);
50
51         // No connection to server note
52         qDebug()
53                         << "RoomStatusIndicatorWidget::RoomStatusIndicatorWidget() creating connection label";
54         QFrame* connectionLabelFrame = new QFrame( this );
55         iConnectionLabel = new QLabel( tr( "No connection to server" ), connectionLabelFrame );
56         iConnectionLabel->setFont(importantTextFont);
57         iConnectionLabel->setAlignment(Qt::AlignHCenter);
58         iConnectionLabel->setWordWrap( true);
59         iConnectionLabel->setStyleSheet("background-color: transparent; color: red; text-decoration:blink; max-width: 250px");
60         connectionLabelFrame->setFixedSize(iConnectionLabel->sizeHint() );
61         if (connectedOnce && !connectionError)
62                 iConnectionLabel->setHidden( true);
63
64         QVBoxLayout *topLayout = new QVBoxLayout;
65         topLayout->addStretch();
66         topLayout->addWidget(iTimeDisplay);
67         topLayout->addSpacing( 28);
68         topLayout->addWidget(iDefaultRoomLabel);
69         topLayout->addWidget(iStatusLabel);
70         topLayout->addWidget(iUntilTextLabel);
71         topLayout->addSpacing( 28);
72         topLayout->addWidget(connectionLabelFrame);
73         topLayout->addStretch();
74
75         QHBoxLayout *mainLayout = new QHBoxLayout;
76         mainLayout->addLayout(topLayout);
77         mainLayout->addStretch();
78         //mainLayout->setMargin( 65 );
79         mainLayout->setContentsMargins( 65, 65, 65, 0);
80         setLayout(mainLayout);
81
82         statusChanged(aStatus, aUntil);
83
84         QPalette palette;
85         palette.setColor( QPalette::Window, Qt::white );
86         palette.setColor( QPalette::WindowText, Qt::darkGray );
87         setPalette( palette );
88
89         setAutoFillBackground( true );
90         setFocusPolicy(Qt::StrongFocus);
91         setEnabled( true); // enable mouse & key events
92 }
93
94 RoomStatusIndicatorWidget::~RoomStatusIndicatorWidget()
95 {
96         delete iTimeDisplay;
97         iTimeDisplay = 0;
98 }
99
100 QString RoomStatusIndicatorWidget::statusToText(const Room::Status aStatus)
101 {
102         return (aStatus == Room::BusyStatus ) ? tr("busy") : tr("free");
103 }
104
105 QPalette RoomStatusIndicatorWidget::createPalette(Room::Status aStatus)
106 {
107         QString
108                         image =
109                                         aStatus == Room::BusyStatus ? ":roomstatus_busy" : ":roomstatus_free";
110         QPixmap pixmap(image);
111
112         // The image needs to be moved in normal mode so the traffic light not partly outside the screen
113         const int xoffset( 60);
114         const int yoffset( 19);
115         int cropwidth(pixmap.width() - xoffset);
116         int cropheight(pixmap.height() - yoffset);
117
118         QBrush brush;
119         if (windowState() == Qt::WindowFullScreen)
120         {
121                 // Use the full image in full screen mode
122                 brush.setTexture(pixmap);
123         }
124         else
125         {
126                 // Take part of the image so the traffic lights are moved xoffset poxels to left 
127                 // and yoffset pixels to up
128                 brush.setTexture(pixmap.copy(xoffset, yoffset, cropwidth, cropheight) );
129         }
130
131         QPalette palette;
132         palette.setColor( QPalette::Window, Qt::white );
133         palette.setColor( QPalette::WindowText, Qt::darkGray );
134         palette.setBrush( QPalette::Window, brush );
135         return palette;
136 }
137
138 void RoomStatusIndicatorWidget::setCurrentTime(QTime aCurrentTime)
139 {
140         iTimeDisplay->setTime(aCurrentTime);
141 }
142
143 void RoomStatusIndicatorWidget::statusChanged(const Room::Status aStatus, const QTime aUntil)
144 {
145         iStatusLabel->setText(tr( "is %1" ).arg(statusToText(aStatus) ) );
146         if (aUntil == RoomStatusIndicatorWidget::endOfTheDay)
147         {
148                 iUntilTextLabel->setText(tr("whole day.") );
149         }
150         else
151         {
152                 iUntilTextLabel->setText(tr( "until %1" ).arg(aUntil.toString(iTimeFormat) ) );
153         }
154         setPalette(createPalette(aStatus) );
155 }
156
157 void RoomStatusIndicatorWidget::currentRoomChanged(Room *aRoom)
158 {
159         iDefaultRoomLabel->setText(aRoom->name() );
160 }
161
162 bool RoomStatusIndicatorWidget::event(QEvent *event)
163 {
164         switch (event->type())
165         {
166                 case QEvent::Paint:
167                         qDebug() << "[RoomStatusIndicatorWidget::event] <Paint event>";
168                         break;
169                 case QEvent::PaletteChange:
170                         qDebug()
171                                         << "[RoomStatusIndicatorWidget::event] <Palette change event>";
172                         break;
173                 default:
174                         break;
175         }
176
177         return ViewBase::event(event);
178 }
179
180 void RoomStatusIndicatorWidget::connectionEstablished()
181 {
182         if ( !connectedOnce)
183         { 
184                 // Just got the required meetings for the first time              
185                 qDebug() << "RoomStatusIndicatorWidget::connectionEstablished() first call";
186                 iDefaultRoomLabel->setHidden( false);
187                 iUntilTextLabel->setHidden( false);
188                 iStatusLabel->setHidden( false);
189         }
190         else
191         {
192                 qDebug() << "RoomStatusIndicatorWidget::connectionEstablished()";
193         }
194         ViewBase::connectionEstablished();
195         iConnectionLabel->setHidden( true);
196 }
197
198 void RoomStatusIndicatorWidget::connectionLost()
199 {
200         ViewBase::connectionLost();
201         iConnectionLabel->setHidden( false);
202 }