/* * toolbar.cpp * * Created on: 21.01.2013 * Author: sr */ #include "../../shared/settings.h" #include "../net/serverconnection.h" #include "../vnc/vncwindow.h" #include "../vnc/vncserver.h" #include "toolbar.h" #include "ui_toolbar.h" Toolbar::Toolbar(QWidget *parent) : QWidget(parent), _ui(new Ui::Toolbar), _location(POSITION_TOP_CENTER), _hideTimer(0), _connection(NULL) { // Initialize the GUI _ui->setupUi(this); // QWidget *firstPageWidget = new QWidget; // QWidget *secondPageWidget = new QWidget; // QStackedLayout *stackedLayout = new QStackedLayout; // stackedLayout->addWidget(firstPageWidget); // stackedLayout->addWidget(secondPageWidget); // QVBoxLayout *mainLayout = new QVBoxLayout; // mainLayout->addLayout(stackedLayout); // setLayout(mainLayout); setWindowFlags(Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint); setAttribute(Qt::WA_AlwaysShowToolTips); setAttribute(Qt::WA_QuitOnClose); setVisible(true); // VNC Window _vnc = new VncWindow(NULL); // Connect window _connectWindow = new ConnectWindow(NULL); connect(_connectWindow, SIGNAL(disconnect()), this, SLOT(onDoDisconnect())); connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(onConnected(ServerConnection*))); connect(VncServer::instance(), SIGNAL(started(int, QString&, QString&)), this, SLOT(onVncServerIsRunning(int, QString&, QString&))); // setupMenu(); setLocation(); hideBar(); } void Toolbar::setupMenu() { _menu = new QMenu(this); // setup actions _acnDisconnect = new QAction(tr("Set &session ID"), this); //_acnDisconnect->setEnabled(false); _acnQuit = new QAction(tr("&Quit"), this); // setup menu _menu->addAction(_acnDisconnect); _menu->addSeparator(); _menu->addAction(_acnQuit); _ui->cmdMenu->setMenu(_menu); connect(_acnQuit, SIGNAL(triggered()), this, SLOT(onQuit())); connect(_acnDisconnect, SIGNAL(triggered()), _connectWindow, SLOT(show())); } Toolbar::~Toolbar() { VncServer::instance()->stop(); _vnc->deleteLater(); _connectWindow->deleteLater(); delete _ui; } //###########\\/\/ void Toolbar::setLocation() { const QDesktopWidget desktop; const QRect primaryScreen = desktop.screenGeometry(); switch (_location) { case POSITION_TOP_LEFT: move(primaryScreen.left(), primaryScreen.top()); break; case POSITION_TOP_CENTER: move((primaryScreen.width() - this->width()) / 2 + primaryScreen.left(), primaryScreen.top()); break; case POSITION_TOP_RIGHT: move(primaryScreen.right() - width(), primaryScreen.top()); break; case POSITION_BOTTOM_LEFT: move(primaryScreen.left(), primaryScreen.bottom() - height()); break; case POSITION_BOTTOM_CENTER: move((primaryScreen.width() - this->width()) / 2 + primaryScreen.left(), primaryScreen.bottom() - height()); break; case POSITION_BOTTOM_RIGHT: move(primaryScreen.right() - width(), primaryScreen.bottom() - height()); break; default: break; } } void Toolbar::setBarVisible(bool shown) { const QDesktopWidget desktop; const QRect primaryScreen = desktop.screenGeometry(); if (!shown) { if (_location <= POSITION_TOP_RIGHT) move(x(), primaryScreen.top() + 2 - height()); else move(x(), primaryScreen.bottom() - 2); } else { if (_location <= POSITION_TOP_RIGHT) move(x(), primaryScreen.top()); else move(x(), primaryScreen.bottom() - height()); } } bool Toolbar::hideBar() { // Don't hide window if any menu is open or VNC Server is running from this client. if (_menu->isVisible() || VncServer::instance()->isVncServerRunning()) return false; setBarVisible(false); return true; } /* * Override */ void Toolbar::leaveEvent(QEvent* e) { if (_hideTimer == 0) _hideTimer = startTimer(100); _hideDelay = 6; QWidget::leaveEvent(e); } void Toolbar::enterEvent(QEvent* e) { if (_hideTimer != 0) { killTimer(_hideTimer); _hideTimer = 0; } setBarVisible(true); QWidget::enterEvent(e); } void Toolbar::timerEvent(QTimerEvent* event) { if (event->timerId() == _hideTimer) { if (--_hideDelay <= 0) { if (hideBar()) { killTimer(_hideTimer); _hideTimer = 0; } } } } /* * Slots */ void Toolbar::onVncServerIsRunning(int port, QString&, QString&) { if (port > 0) { setBarVisible(true); return; } hideBar(); return; } void Toolbar::onDisconnected() { _connectWindow->setConnected(false); if (_connection != NULL) _connection->blockSignals(true); _connection = NULL; _ui->lblStatus->setStyleSheet("color:red"); _ui->lblStatus->setText(tr("Offline")); } void Toolbar::onConnected(ServerConnection* connection) { _ui->lblStatus->setStyleSheet("color:green"); _ui->lblStatus->setText(tr("Online")); // if (_connection != NULL) { disconnect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected())); _connection->blockSignals(true); _connection->disconnectFromServer(); } _connection = connection; connect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected())); connect(_connection, SIGNAL(openVnc(const QString&, int, const QString&, bool, bool, const QString&, const int)), _vnc, SLOT(open(const QString&, int, const QString&, bool, bool, const QString&, const int))); connect(_connection, SIGNAL(closeVnc()), _vnc, SLOT(close())); connect(_vnc, SIGNAL(running(const bool, const int)), _connection, SLOT(onVncViewerStartStop(const bool, const int))); _connectWindow->setConnected(true); } void Toolbar::onDoDisconnect() { if (_connection != NULL) _connection->disconnectFromServer(); } void Toolbar::onQuit() { QApplication::exit(0); }