| 1 | #include "callwidget.h" |
|---|
| 2 | #include "ui_callwidget.h" |
|---|
| 3 | |
|---|
| 4 | CallWidget::CallWidget(TCallSession * tCallSession,QWidget *parent) |
|---|
| 5 | :QMainWindow(parent),_tCallSession(tCallSession),ui(new Ui::CallWidget) |
|---|
| 6 | { |
|---|
| 7 | _timerIdProgressAnimation = 0; |
|---|
| 8 | _timerIdSession = 0; |
|---|
| 9 | _durationSession = 0; |
|---|
| 10 | |
|---|
| 11 | _lastStatus = EnumPhoneCallState::PhoneCallStateIncoming; |
|---|
| 12 | |
|---|
| 13 | ui->setupUi(this); |
|---|
| 14 | |
|---|
| 15 | #if defined(OS_MACOSX) |
|---|
| 16 | setUnifiedTitleAndToolBarOnMac(true); |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | //_labelMsg = new QLabel; |
|---|
| 20 | //_labelMsg->setText("New Call..."); |
|---|
| 21 | QToolBar * _toolBar = new QToolBar; |
|---|
| 22 | //_toolBar->layout()->setMargin(0); |
|---|
| 23 | //_toolBar->layout()->setSpacing(0); |
|---|
| 24 | //_toolBar->addWidget(_labelMsg); |
|---|
| 25 | addToolBar(_toolBar); |
|---|
| 26 | |
|---|
| 27 | _remoteVideoFrame = QPixmap(":/qute.png"); |
|---|
| 28 | |
|---|
| 29 | connect(_tCallSession, |
|---|
| 30 | SIGNAL(videoFrameReceivedSignal(QImage, QImage)), |
|---|
| 31 | SLOT(videoFrameReceivedSlot(QImage, QImage))); |
|---|
| 32 | |
|---|
| 33 | connect(_tCallSession, |
|---|
| 34 | SIGNAL(phoneCallStateChangedSignal(EnumPhoneCallState::PhoneCallState)), |
|---|
| 35 | SLOT(phoneCallStateChangedSlot(EnumPhoneCallState::PhoneCallState))); |
|---|
| 36 | _tCallSession->enableVideo(true); |
|---|
| 37 | show(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | CallWidget::CallWidget(Contact contact,QWidget *parent) |
|---|
| 41 | :QMainWindow(parent),ui(new Ui::CallWidget) |
|---|
| 42 | { |
|---|
| 43 | _timerIdProgressAnimation = 0; |
|---|
| 44 | _timerIdSession = 0; |
|---|
| 45 | _durationSession = 0; |
|---|
| 46 | |
|---|
| 47 | _lastStatus = EnumPhoneCallState::PhoneCallStateUnknown; |
|---|
| 48 | |
|---|
| 49 | ui->setupUi(this); |
|---|
| 50 | |
|---|
| 51 | #if defined(OS_MACOSX) |
|---|
| 52 | setUnifiedTitleAndToolBarOnMac(true); |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | //_labelMsg = new QLabel; |
|---|
| 56 | //_labelMsg->setText("New Call..."); |
|---|
| 57 | QToolBar * _toolBar = new QToolBar; |
|---|
| 58 | //_toolBar->layout()->setMargin(0); |
|---|
| 59 | //_toolBar->layout()->setSpacing(0); |
|---|
| 60 | //_toolBar->addWidget(_labelMsg); |
|---|
| 61 | addToolBar(_toolBar); |
|---|
| 62 | |
|---|
| 63 | _remoteVideoFrame = QPixmap(":/qute.png"); |
|---|
| 64 | |
|---|
| 65 | _tCallSession = Coip::self()->getTCallSessionManager().createTCallSession(); |
|---|
| 66 | |
|---|
| 67 | connect(_tCallSession, |
|---|
| 68 | SIGNAL(videoFrameReceivedSignal(QImage, QImage)), |
|---|
| 69 | SLOT(videoFrameReceivedSlot(QImage, QImage))); |
|---|
| 70 | |
|---|
| 71 | connect(_tCallSession, |
|---|
| 72 | SIGNAL(phoneCallStateChangedSignal(EnumPhoneCallState::PhoneCallState)), |
|---|
| 73 | SLOT(phoneCallStateChangedSlot(EnumPhoneCallState::PhoneCallState))); |
|---|
| 74 | |
|---|
| 75 | _tCallSession->addContact(contact); |
|---|
| 76 | _tCallSession->enableVideo(true); |
|---|
| 77 | _tCallSession->start(); |
|---|
| 78 | |
|---|
| 79 | show(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | CallWidget::~CallWidget() |
|---|
| 83 | { |
|---|
| 84 | delete ui; |
|---|
| 85 | delete _tCallSession; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void CallWidget::changeEvent(QEvent *e) |
|---|
| 89 | { |
|---|
| 90 | QMainWindow::changeEvent(e); |
|---|
| 91 | switch (e->type()) { |
|---|
| 92 | case QEvent::LanguageChange: |
|---|
| 93 | ui->retranslateUi(this); |
|---|
| 94 | break; |
|---|
| 95 | default: |
|---|
| 96 | break; |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | void CallWidget::paintEvent(QPaintEvent*) |
|---|
| 101 | { |
|---|
| 102 | QPainter painter(this); |
|---|
| 103 | painter.setRenderHint(QPainter::Antialiasing, true); |
|---|
| 104 | |
|---|
| 105 | if(!_remoteVideoFrame.isNull()) |
|---|
| 106 | { |
|---|
| 107 | QPixmap pix = _remoteVideoFrame.scaled(ui->label->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation); |
|---|
| 108 | QRect re(0,0,pix.width(),pix.height()); |
|---|
| 109 | re.moveCenter(ui->label->rect().center()); |
|---|
| 110 | painter.drawPixmap(re,pix); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if(!_localVideoFrame.isNull()) |
|---|
| 114 | painter.drawPixmap(ui->label->pos(),_localVideoFrame.scaled(ui->label->size()/6,Qt::KeepAspectRatio,Qt::SmoothTransformation)); |
|---|
| 115 | |
|---|
| 116 | if(_lastStatus != EnumPhoneCallState::PhoneCallStateTalking) |
|---|
| 117 | { |
|---|
| 118 | painter.fillRect(centralWidget()->rect(),QColor(0,0,0,225)); |
|---|
| 119 | |
|---|
| 120 | if(!_timerIdProgressAnimation && (_lastStatus == EnumPhoneCallState::PhoneCallStateIncoming || _lastStatus == EnumPhoneCallState::PhoneCallStateHold)) |
|---|
| 121 | { |
|---|
| 122 | painter.setPen(QPen(QColor(255,255,255,200), 3)); |
|---|
| 123 | QRect re(0,0,50,50); |
|---|
| 124 | re.moveCenter(centralWidget()->rect().center()); |
|---|
| 125 | painter.drawEllipse(re); |
|---|
| 126 | |
|---|
| 127 | re.translate(re.width()/4, 0); |
|---|
| 128 | painter.setBrush(QColor(255,255,255,200)); |
|---|
| 129 | painter.drawPie(re, 150 * 16, 60 * 16); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if(_timerIdProgressAnimation) |
|---|
| 134 | { |
|---|
| 135 | for (int i=0; i<9; i++) |
|---|
| 136 | { |
|---|
| 137 | QColor color = Qt::white; |
|---|
| 138 | color.setAlphaF(1.0f - (i/9.0f)); |
|---|
| 139 | painter.setPen(Qt::NoPen); |
|---|
| 140 | painter.setBrush(color); |
|---|
| 141 | painter.save(); |
|---|
| 142 | painter.translate(centralWidget()->rect().center()); |
|---|
| 143 | painter.rotate(_angleProgressAnimation - i*40.0f); |
|---|
| 144 | painter.drawRect(QRectF(5.5, 0.9, 21.5, 5)); |
|---|
| 145 | painter.restore(); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void CallWidget::mouseReleaseEvent(QMouseEvent *) |
|---|
| 151 | { |
|---|
| 152 | if(_tCallSession->isStarted()) |
|---|
| 153 | { |
|---|
| 154 | if(_lastStatus == EnumPhoneCallState::PhoneCallStateTalking) |
|---|
| 155 | { |
|---|
| 156 | startProgressAnimation(); |
|---|
| 157 | _tCallSession->pause(); |
|---|
| 158 | } |
|---|
| 159 | else if(_lastStatus == EnumPhoneCallState::PhoneCallStateHold) |
|---|
| 160 | { |
|---|
| 161 | startProgressAnimation(); |
|---|
| 162 | _tCallSession->resume(); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | } |
|---|
| 166 | else if(_lastStatus == EnumPhoneCallState::PhoneCallStateIncoming) |
|---|
| 167 | { |
|---|
| 168 | startProgressAnimation(); |
|---|
| 169 | _tCallSession->start(); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void CallWidget::timerEvent(QTimerEvent* event) |
|---|
| 174 | { |
|---|
| 175 | if(event->timerId() == _timerIdProgressAnimation) |
|---|
| 176 | { |
|---|
| 177 | _angleProgressAnimation = (_angleProgressAnimation+30)%360; |
|---|
| 178 | update(); |
|---|
| 179 | } |
|---|
| 180 | else if(event->timerId() == _timerIdSession) |
|---|
| 181 | { |
|---|
| 182 | _durationSession++; |
|---|
| 183 | QTime time; |
|---|
| 184 | setWindowTitle(time.addSecs(_durationSession).toString()); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void CallWidget::startSessionTimer() |
|---|
| 189 | { |
|---|
| 190 | _timerIdSession = startTimer(1000); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void CallWidget::stopSessionTimer() |
|---|
| 194 | { |
|---|
| 195 | killTimer(_timerIdSession); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | void CallWidget::startProgressAnimation() |
|---|
| 199 | { |
|---|
| 200 | if(!_timerIdProgressAnimation) |
|---|
| 201 | _timerIdProgressAnimation = startTimer(100); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | void CallWidget::stopProgressAnimation() |
|---|
| 205 | { |
|---|
| 206 | if(_timerIdProgressAnimation) |
|---|
| 207 | { |
|---|
| 208 | killTimer(_timerIdProgressAnimation); |
|---|
| 209 | _timerIdProgressAnimation = 0; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | void CallWidget::hangUpButtonClicked() { |
|---|
| 215 | |
|---|
| 216 | _tCallSession->stop(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | void CallWidget::videoFrameReceivedSlot(QImage remoteVideoFrame, QImage localVideoFrame) { |
|---|
| 220 | |
|---|
| 221 | if(!remoteVideoFrame.isNull()) |
|---|
| 222 | _remoteVideoFrame = QPixmap::fromImage(remoteVideoFrame); |
|---|
| 223 | |
|---|
| 224 | if(!localVideoFrame.isNull()) |
|---|
| 225 | _localVideoFrame = QPixmap::fromImage(localVideoFrame); |
|---|
| 226 | |
|---|
| 227 | update(); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | void CallWidget::phoneCallStateChangedSlot(EnumPhoneCallState::PhoneCallState state) { |
|---|
| 231 | |
|---|
| 232 | _lastStatus = state; |
|---|
| 233 | |
|---|
| 234 | switch(state) { |
|---|
| 235 | //case EnumPhoneCallState::PhoneCallStateUnknown: |
|---|
| 236 | case EnumPhoneCallState::PhoneCallStateClosed: |
|---|
| 237 | stopProgressAnimation(); |
|---|
| 238 | stopSessionTimer(); |
|---|
| 239 | //_labelMsg->setText("Closed..."); |
|---|
| 240 | break; |
|---|
| 241 | |
|---|
| 242 | case EnumPhoneCallState::PhoneCallStateError: |
|---|
| 243 | stopProgressAnimation(); |
|---|
| 244 | stopSessionTimer(); |
|---|
| 245 | //_labelMsg->setText("Error..."); |
|---|
| 246 | break; |
|---|
| 247 | |
|---|
| 248 | case EnumPhoneCallState::PhoneCallStateTalking: |
|---|
| 249 | stopProgressAnimation(); |
|---|
| 250 | startSessionTimer(); |
|---|
| 251 | //_labelMsg->setText("Talking..."); |
|---|
| 252 | break; |
|---|
| 253 | |
|---|
| 254 | case EnumPhoneCallState::PhoneCallStateDialing: |
|---|
| 255 | startProgressAnimation(); |
|---|
| 256 | //_labelMsg->setText("Dialing..."); |
|---|
| 257 | break; |
|---|
| 258 | |
|---|
| 259 | case EnumPhoneCallState::PhoneCallStateRinging: |
|---|
| 260 | //_labelMsg->setText("Ringing..."); |
|---|
| 261 | break; |
|---|
| 262 | |
|---|
| 263 | case EnumPhoneCallState::PhoneCallStateHold: |
|---|
| 264 | stopProgressAnimation(); |
|---|
| 265 | //_labelMsg->setText("Hold..."); |
|---|
| 266 | break; |
|---|
| 267 | |
|---|
| 268 | case EnumPhoneCallState::PhoneCallStateResumed: |
|---|
| 269 | //_labelMsg->setText("Resumed..."); |
|---|
| 270 | break; |
|---|
| 271 | |
|---|
| 272 | case EnumPhoneCallState::PhoneCallStateMissed: |
|---|
| 273 | stopProgressAnimation(); |
|---|
| 274 | //_labelMsg->setText("Missed..."); |
|---|
| 275 | break; |
|---|
| 276 | |
|---|
| 277 | case EnumPhoneCallState::PhoneCallStateRedirected: |
|---|
| 278 | stopProgressAnimation(); |
|---|
| 279 | stopSessionTimer(); |
|---|
| 280 | //_labelMsg->setText("Redirected..."); |
|---|
| 281 | break; |
|---|
| 282 | |
|---|
| 283 | //case EnumPhoneCallState::PhoneCallStateIncoming: |
|---|
| 284 | |
|---|
| 285 | default: |
|---|
| 286 | break; |
|---|
| 287 | } |
|---|
| 288 | update(); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void CallWidget::closeEvent(QCloseEvent*) |
|---|
| 292 | { |
|---|
| 293 | stopCallSessionSignal(_tCallSession); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | void CallWidget::callButtonClickedSlot() |
|---|
| 297 | { |
|---|
| 298 | _tCallSession->start(); |
|---|
| 299 | } |
|---|