2010-03-25 20:30:10 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "osdpretty.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "ui_osdpretty.h"
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
#include <QApplication>
|
2011-08-27 00:32:28 +02:00
|
|
|
#include <QBitmap>
|
|
|
|
#include <QColor>
|
2010-03-25 20:30:10 +01:00
|
|
|
#include <QDesktopWidget>
|
2011-08-27 00:32:28 +02:00
|
|
|
#include <QLayout>
|
2010-03-25 20:30:10 +01:00
|
|
|
#include <QMouseEvent>
|
2011-08-27 00:32:28 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QSettings>
|
2010-03-25 20:30:10 +01:00
|
|
|
#include <QTimer>
|
2010-03-25 22:24:19 +01:00
|
|
|
#include <QTimeLine>
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2010-03-25 20:57:52 +01:00
|
|
|
#ifdef Q_WS_X11
|
2014-02-07 16:34:20 +01:00
|
|
|
#include <QX11Info>
|
2010-03-25 20:57:52 +01:00
|
|
|
#endif
|
|
|
|
|
2010-09-25 16:55:36 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "qtwin.h"
|
|
|
|
#include <windows.h>
|
2010-09-25 16:55:36 +02:00
|
|
|
#endif
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
const char* OSDPretty::kSettingsGroup = "OSDPretty";
|
|
|
|
|
|
|
|
const int OSDPretty::kDropShadowSize = 13;
|
|
|
|
const int OSDPretty::kBorderRadius = 10;
|
|
|
|
const int OSDPretty::kMaxIconSize = 100;
|
|
|
|
|
2010-12-19 15:06:51 +01:00
|
|
|
const int OSDPretty::kSnapProximity = 20;
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
const QRgb OSDPretty::kPresetBlue = qRgb(102, 150, 227);
|
|
|
|
const QRgb OSDPretty::kPresetOrange = qRgb(254, 156, 67);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OSDPretty::OSDPretty(Mode mode, QWidget* parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
ui_(new Ui_OSDPretty),
|
|
|
|
mode_(mode),
|
|
|
|
background_color_(kPresetBlue),
|
|
|
|
background_opacity_(0.85),
|
|
|
|
popup_display_(0),
|
|
|
|
font_(QFont()),
|
|
|
|
disable_duration_(false),
|
|
|
|
timeout_(new QTimer(this)),
|
|
|
|
fading_enabled_(false),
|
|
|
|
fader_(new QTimeLine(300, this)),
|
|
|
|
toggle_mode_(false) {
|
2010-04-22 17:26:33 +02:00
|
|
|
Qt::WindowFlags flags = Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
|
|
|
|
Qt::X11BypassWindowManagerHint;
|
2010-04-20 21:43:56 +02:00
|
|
|
|
|
|
|
setWindowFlags(flags);
|
2010-03-25 20:30:10 +01:00
|
|
|
setAttribute(Qt::WA_TranslucentBackground, true);
|
2011-12-27 17:06:48 +01:00
|
|
|
setAttribute(Qt::WA_X11NetWmWindowTypeNotification, true);
|
2012-07-15 14:44:42 +02:00
|
|
|
setAttribute(Qt::WA_ShowWithoutActivating, true);
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->setupUi(this);
|
2010-04-20 21:43:56 +02:00
|
|
|
|
2012-07-15 14:44:42 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// Don't show the window in the taskbar. Qt::ToolTip does this too, but it
|
|
|
|
// adds an extra ugly shadow.
|
|
|
|
int ex_style = GetWindowLong(winId(), GWL_EXSTYLE);
|
|
|
|
ex_style |= WS_EX_NOACTIVATE;
|
|
|
|
SetWindowLong(winId(), GWL_EXSTYLE, ex_style);
|
|
|
|
#endif
|
|
|
|
|
2010-04-20 21:43:56 +02:00
|
|
|
// Mode settings
|
|
|
|
switch (mode_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Mode_Popup:
|
|
|
|
setCursor(QCursor(Qt::ArrowCursor));
|
|
|
|
break;
|
2010-04-20 21:43:56 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case Mode_Draggable:
|
|
|
|
setCursor(QCursor(Qt::OpenHandCursor));
|
|
|
|
break;
|
2010-04-20 21:43:56 +02:00
|
|
|
}
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2010-03-25 22:24:19 +01:00
|
|
|
// Timeout
|
2010-03-25 20:30:10 +01:00
|
|
|
timeout_->setSingleShot(true);
|
|
|
|
timeout_->setInterval(5000);
|
|
|
|
connect(timeout_, SIGNAL(timeout()), SLOT(hide()));
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->icon->setMaximumSize(kMaxIconSize, kMaxIconSize);
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2010-03-25 22:24:19 +01:00
|
|
|
// Fader
|
|
|
|
connect(fader_, SIGNAL(valueChanged(qreal)), SLOT(FaderValueChanged(qreal)));
|
|
|
|
connect(fader_, SIGNAL(finished()), SLOT(FaderFinished()));
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
set_fading_enabled(true);
|
|
|
|
#endif
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
// Load the show edges and corners
|
|
|
|
QImage shadow_edge(":osd_shadow_edge.png");
|
|
|
|
QImage shadow_corner(":osd_shadow_corner.png");
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
2010-03-25 20:30:10 +01:00
|
|
|
QTransform rotation = QTransform().rotate(90 * i);
|
|
|
|
shadow_edge_[i] = QPixmap::fromImage(shadow_edge.transformed(rotation));
|
|
|
|
shadow_corner_[i] = QPixmap::fromImage(shadow_corner.transformed(rotation));
|
|
|
|
}
|
2010-06-02 21:19:30 +02:00
|
|
|
background_ = QPixmap(":osd_background.png");
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
// Set the margins to allow for the drop shadow
|
2010-03-25 20:36:56 +01:00
|
|
|
QBoxLayout* l = static_cast<QBoxLayout*>(layout());
|
|
|
|
int margin = l->margin() + kDropShadowSize;
|
|
|
|
l->setMargin(margin);
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2011-05-29 21:44:03 +02:00
|
|
|
// Get current screen resolution
|
|
|
|
QRect screenResolution = QApplication::desktop()->screenGeometry();
|
|
|
|
// Leave 200 px for icon
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->summary->setMaximumWidth(screenResolution.width() - 200);
|
|
|
|
ui_->message->setMaximumWidth(screenResolution.width() - 200);
|
2011-05-29 21:44:03 +02:00
|
|
|
// Set maximum size for the OSD, a little margin here too
|
2014-02-07 16:34:20 +01:00
|
|
|
setMaximumSize(screenResolution.width() - 100,
|
|
|
|
screenResolution.height() - 100);
|
2011-05-29 21:44:03 +02:00
|
|
|
|
2011-05-28 10:50:29 +02:00
|
|
|
// Don't load settings here, they will be reloaded anyway on creation
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
OSDPretty::~OSDPretty() { delete ui_; }
|
2010-05-10 23:50:31 +02:00
|
|
|
|
2010-03-25 20:57:52 +01:00
|
|
|
bool OSDPretty::IsTransparencyAvailable() {
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
return QX11Info::isCompositingManagerRunning();
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
void OSDPretty::Load() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
foreground_color_ = QColor(s.value("foreground_color", 0).toInt());
|
|
|
|
background_color_ = QColor(s.value("background_color", kPresetBlue).toInt());
|
2010-03-25 20:36:56 +01:00
|
|
|
background_opacity_ = s.value("background_opacity", 0.85).toDouble();
|
2010-03-25 20:30:10 +01:00
|
|
|
popup_display_ = s.value("popup_display", -1).toInt();
|
|
|
|
popup_pos_ = s.value("popup_pos", QPoint(0, 0)).toPoint();
|
2011-05-28 10:50:29 +02:00
|
|
|
font_.fromString(s.value("font", "Verdana,9,-1,5,50,0,0,0,0,0").toString());
|
2011-05-29 14:44:38 +02:00
|
|
|
disable_duration_ = s.value("disable_duration", false).toBool();
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2011-05-28 10:50:29 +02:00
|
|
|
set_font(font());
|
2010-03-25 20:30:10 +01:00
|
|
|
set_foreground_color(foreground_color());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::ReloadSettings() {
|
|
|
|
Load();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (isVisible()) update();
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
2010-03-25 20:57:52 +01:00
|
|
|
QRect OSDPretty::BoxBorder() const {
|
2014-02-07 16:34:20 +01:00
|
|
|
return rect().adjusted(kDropShadowSize, kDropShadowSize, -kDropShadowSize,
|
|
|
|
-kDropShadowSize);
|
2010-03-25 20:57:52 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OSDPretty::paintEvent(QPaintEvent*) {
|
2010-03-25 20:30:10 +01:00
|
|
|
QPainter p(this);
|
|
|
|
p.setRenderHint(QPainter::Antialiasing);
|
|
|
|
p.setRenderHint(QPainter::HighQualityAntialiasing);
|
|
|
|
|
2010-03-25 20:57:52 +01:00
|
|
|
QRect box(BoxBorder());
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
// Shadow corners
|
|
|
|
const int kShadowCornerSize = kDropShadowSize + kBorderRadius;
|
|
|
|
p.drawPixmap(0, 0, shadow_corner_[0]);
|
|
|
|
p.drawPixmap(width() - kShadowCornerSize, 0, shadow_corner_[1]);
|
2014-02-07 16:34:20 +01:00
|
|
|
p.drawPixmap(width() - kShadowCornerSize, height() - kShadowCornerSize,
|
|
|
|
shadow_corner_[2]);
|
2010-03-25 20:30:10 +01:00
|
|
|
p.drawPixmap(0, height() - kShadowCornerSize, shadow_corner_[3]);
|
|
|
|
|
|
|
|
// Shadow edges
|
2014-02-07 16:34:20 +01:00
|
|
|
p.drawTiledPixmap(kShadowCornerSize, 0, width() - kShadowCornerSize * 2,
|
|
|
|
kDropShadowSize, shadow_edge_[0]);
|
2010-03-25 20:30:10 +01:00
|
|
|
p.drawTiledPixmap(width() - kDropShadowSize, kShadowCornerSize,
|
2014-02-07 16:34:20 +01:00
|
|
|
kDropShadowSize, height() - kShadowCornerSize * 2,
|
2010-03-25 20:30:10 +01:00
|
|
|
shadow_edge_[1]);
|
|
|
|
p.drawTiledPixmap(kShadowCornerSize, height() - kDropShadowSize,
|
2014-02-07 16:34:20 +01:00
|
|
|
width() - kShadowCornerSize * 2, kDropShadowSize,
|
2010-03-25 20:30:10 +01:00
|
|
|
shadow_edge_[2]);
|
2014-02-07 16:34:20 +01:00
|
|
|
p.drawTiledPixmap(0, kShadowCornerSize, kDropShadowSize,
|
|
|
|
height() - kShadowCornerSize * 2, shadow_edge_[3]);
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
// Box background
|
|
|
|
p.setBrush(background_color_);
|
|
|
|
p.setPen(QPen());
|
|
|
|
p.setOpacity(background_opacity_);
|
|
|
|
p.drawRoundedRect(box, kBorderRadius, kBorderRadius);
|
|
|
|
|
2010-06-02 21:19:30 +02:00
|
|
|
// Background pattern
|
|
|
|
QPainterPath background_path;
|
|
|
|
background_path.addRoundedRect(box, kBorderRadius, kBorderRadius);
|
|
|
|
p.setClipPath(background_path);
|
|
|
|
p.setOpacity(1.0);
|
|
|
|
p.drawPixmap(box.right() - background_.width(),
|
|
|
|
box.bottom() - background_.height(), background_);
|
|
|
|
p.setClipping(false);
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
// Gradient overlay
|
|
|
|
QLinearGradient gradient(0, 0, 0, height());
|
|
|
|
gradient.setColorAt(0, QColor(255, 255, 255, 130));
|
|
|
|
gradient.setColorAt(1, QColor(255, 255, 255, 50));
|
|
|
|
p.setBrush(gradient);
|
|
|
|
p.drawRoundedRect(box, kBorderRadius, kBorderRadius);
|
|
|
|
|
|
|
|
// Box border
|
|
|
|
p.setBrush(QBrush());
|
|
|
|
p.setPen(QPen(background_color_.darker(150), 2));
|
|
|
|
p.drawRoundedRect(box, kBorderRadius, kBorderRadius);
|
|
|
|
}
|
|
|
|
|
2011-08-27 00:32:28 +02:00
|
|
|
void OSDPretty::SetMessage(const QString& summary, const QString& message,
|
2010-03-25 20:30:10 +01:00
|
|
|
const QImage& image) {
|
|
|
|
|
|
|
|
if (!image.isNull()) {
|
|
|
|
QImage scaled_image =
|
2014-02-07 16:34:20 +01:00
|
|
|
image.scaled(kMaxIconSize, kMaxIconSize, Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation);
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->icon->setPixmap(QPixmap::fromImage(scaled_image));
|
|
|
|
ui_->icon->show();
|
2010-03-25 20:30:10 +01:00
|
|
|
} else {
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->icon->hide();
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->summary->setText(summary);
|
|
|
|
ui_->message->setText(message);
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (isVisible()) Reposition();
|
2011-08-27 00:32:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the desired message and then show the OSD
|
|
|
|
void OSDPretty::ShowMessage(const QString& summary, const QString& message,
|
|
|
|
const QImage& image) {
|
|
|
|
SetMessage(summary, message, image);
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2011-06-05 10:21:17 +02:00
|
|
|
if (isVisible() && mode_ == Mode_Popup) {
|
|
|
|
// The OSD is already visible, toggle or restart the timer
|
|
|
|
if (toggle_mode()) {
|
|
|
|
set_toggle_mode(false);
|
|
|
|
// If timeout is disabled, timer hadn't been started
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!disable_duration()) timeout_->stop();
|
2011-06-05 10:21:17 +02:00
|
|
|
hide();
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!disable_duration()) timeout_->start(); // Restart the timer
|
2011-06-05 10:21:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (toggle_mode()) set_toggle_mode(false);
|
2011-06-05 10:21:17 +02:00
|
|
|
// The OSD is not visible, show it
|
|
|
|
show();
|
|
|
|
}
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::showEvent(QShowEvent* e) {
|
2010-03-25 22:24:19 +01:00
|
|
|
setWindowOpacity(fading_enabled_ ? 0.0 : 1.0);
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
QWidget::showEvent(e);
|
|
|
|
|
|
|
|
Reposition();
|
|
|
|
|
2010-03-25 22:24:19 +01:00
|
|
|
if (fading_enabled_) {
|
|
|
|
fader_->setDirection(QTimeLine::Forward);
|
2014-02-07 16:34:20 +01:00
|
|
|
fader_->start(); // Timeout will be started in FaderFinished
|
|
|
|
} else if (mode_ == Mode_Popup) {
|
|
|
|
if (!disable_duration()) timeout_->start();
|
2011-05-28 10:50:29 +02:00
|
|
|
// Ensures it is above when showing the preview
|
|
|
|
raise();
|
|
|
|
}
|
2010-03-25 22:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::setVisible(bool visible) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!visible && fading_enabled_ &&
|
|
|
|
fader_->direction() == QTimeLine::Forward) {
|
2010-03-25 22:24:19 +01:00
|
|
|
fader_->setDirection(QTimeLine::Backward);
|
|
|
|
fader_->start();
|
|
|
|
} else {
|
|
|
|
QWidget::setVisible(visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::FaderFinished() {
|
|
|
|
if (fader_->direction() == QTimeLine::Backward)
|
|
|
|
hide();
|
2011-05-29 14:44:38 +02:00
|
|
|
else if (mode_ == Mode_Popup && !disable_duration())
|
2010-03-25 20:30:10 +01:00
|
|
|
timeout_->start();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OSDPretty::FaderValueChanged(qreal value) { setWindowOpacity(value); }
|
2010-03-25 22:24:19 +01:00
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
void OSDPretty::Reposition() {
|
|
|
|
QDesktopWidget* desktop = QApplication::desktop();
|
|
|
|
|
2010-04-19 15:16:53 +02:00
|
|
|
// Make the OSD the proper size
|
2010-03-25 20:30:10 +01:00
|
|
|
layout()->activate();
|
|
|
|
resize(sizeHint());
|
|
|
|
|
2010-04-19 15:16:53 +02:00
|
|
|
// Work out where to place the OSD. -1 for x or y means "on the right or
|
|
|
|
// bottom edge".
|
2010-03-25 20:36:56 +01:00
|
|
|
QRect geometry(desktop->availableGeometry(popup_display_));
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2010-04-19 15:16:53 +02:00
|
|
|
int x = popup_pos_.x() < 0 ? geometry.right() - width()
|
|
|
|
: geometry.left() + popup_pos_.x();
|
|
|
|
int y = popup_pos_.y() < 0 ? geometry.bottom() - height()
|
|
|
|
: geometry.top() + popup_pos_.y();
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
move(qBound(0, x, geometry.right() - width()),
|
|
|
|
qBound(0, y, geometry.bottom() - height()));
|
2010-03-25 20:57:52 +01:00
|
|
|
|
2010-07-08 01:03:35 +02:00
|
|
|
// Create a mask for the actual area of the OSD
|
|
|
|
QBitmap mask(size());
|
|
|
|
mask.clear();
|
|
|
|
|
|
|
|
QPainter p(&mask);
|
|
|
|
p.setBrush(Qt::color1);
|
2014-02-07 16:34:20 +01:00
|
|
|
p.drawRoundedRect(BoxBorder().adjusted(-1, -1, 0, 0), kBorderRadius,
|
|
|
|
kBorderRadius);
|
2010-07-08 01:03:35 +02:00
|
|
|
p.end();
|
|
|
|
|
2010-04-19 15:16:53 +02:00
|
|
|
// If there's no compositing window manager running then we have to set an
|
|
|
|
// XShape mask.
|
2010-03-25 20:57:52 +01:00
|
|
|
if (IsTransparencyAvailable())
|
|
|
|
clearMask();
|
|
|
|
else {
|
|
|
|
setMask(mask);
|
|
|
|
}
|
2010-07-08 01:03:35 +02:00
|
|
|
|
2010-09-25 16:55:36 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
2010-07-08 01:03:35 +02:00
|
|
|
// On windows, enable blurbehind on the masked area
|
|
|
|
QtWin::enableBlurBehindWindow(this, true, QRegion(mask));
|
2010-09-25 16:55:36 +02:00
|
|
|
#endif
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OSDPretty::enterEvent(QEvent*) {
|
|
|
|
if (mode_ == Mode_Popup) setWindowOpacity(0.25);
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OSDPretty::leaveEvent(QEvent*) { setWindowOpacity(1.0); }
|
2010-03-25 20:30:10 +01:00
|
|
|
|
|
|
|
void OSDPretty::mousePressEvent(QMouseEvent* e) {
|
|
|
|
if (mode_ == Mode_Popup)
|
|
|
|
hide();
|
|
|
|
else {
|
|
|
|
original_window_pos_ = pos();
|
|
|
|
drag_start_pos_ = e->globalPos();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::mouseMoveEvent(QMouseEvent* e) {
|
|
|
|
if (mode_ == Mode_Draggable) {
|
|
|
|
QPoint delta = e->globalPos() - drag_start_pos_;
|
|
|
|
QPoint new_pos = original_window_pos_ + delta;
|
|
|
|
|
|
|
|
// Keep it to the bounds of the desktop
|
|
|
|
QDesktopWidget* desktop = QApplication::desktop();
|
|
|
|
QRect geometry(desktop->availableGeometry(e->globalPos()));
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
new_pos.setX(
|
|
|
|
qBound(geometry.left(), new_pos.x(), geometry.right() - width()));
|
|
|
|
new_pos.setY(
|
|
|
|
qBound(geometry.top(), new_pos.y(), geometry.bottom() - height()));
|
2010-03-25 20:30:10 +01:00
|
|
|
|
2010-12-19 15:06:51 +01:00
|
|
|
// Snap to center
|
|
|
|
int snap_x = geometry.center().x() - width() / 2;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (new_pos.x() > snap_x - kSnapProximity &&
|
|
|
|
new_pos.x() < snap_x + kSnapProximity) {
|
2010-12-19 15:06:51 +01:00
|
|
|
new_pos.setX(snap_x);
|
|
|
|
}
|
|
|
|
|
2010-03-25 20:30:10 +01:00
|
|
|
move(new_pos);
|
2010-04-11 19:19:25 +02:00
|
|
|
|
|
|
|
popup_display_ = current_display();
|
|
|
|
popup_pos_ = current_pos();
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint OSDPretty::current_pos() const {
|
|
|
|
QDesktopWidget* desktop = QApplication::desktop();
|
|
|
|
QRect geometry(desktop->availableGeometry(current_display()));
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int x = pos().x() >= geometry.right() - width() ? -1
|
|
|
|
: pos().x() - geometry.left();
|
|
|
|
int y = pos().y() >= geometry.bottom() - height() ? -1 : pos().y() -
|
|
|
|
geometry.top();
|
2010-04-19 15:16:53 +02:00
|
|
|
|
|
|
|
return QPoint(x, y);
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int OSDPretty::current_display() const {
|
|
|
|
QDesktopWidget* desktop = QApplication::desktop();
|
|
|
|
return desktop->screenNumber(pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::set_background_color(QRgb color) {
|
|
|
|
background_color_ = color;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (isVisible()) update();
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::set_background_opacity(qreal opacity) {
|
|
|
|
background_opacity_ = opacity;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (isVisible()) update();
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSDPretty::set_foreground_color(QRgb color) {
|
|
|
|
foreground_color_ = QColor(color);
|
|
|
|
|
|
|
|
QPalette p;
|
|
|
|
p.setColor(QPalette::WindowText, foreground_color_);
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->summary->setPalette(p);
|
|
|
|
ui_->message->setPalette(p);
|
2010-03-25 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OSDPretty::set_popup_duration(int msec) { timeout_->setInterval(msec); }
|
2010-03-25 21:54:47 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void OSDPretty::mouseReleaseEvent(QMouseEvent*) {
|
2010-03-25 21:54:47 +01:00
|
|
|
if (mode_ == Mode_Draggable) {
|
|
|
|
popup_display_ = current_display();
|
|
|
|
popup_pos_ = current_pos();
|
|
|
|
}
|
|
|
|
}
|
2011-05-28 10:50:29 +02:00
|
|
|
|
|
|
|
void OSDPretty::set_font(QFont font) {
|
|
|
|
font_ = font;
|
|
|
|
|
|
|
|
// Update the UI
|
|
|
|
ui_->summary->setFont(font);
|
|
|
|
ui_->message->setFont(font);
|
|
|
|
// Now adjust OSD size so everything fits
|
|
|
|
ui_->verticalLayout->activate();
|
|
|
|
resize(sizeHint());
|
|
|
|
// Update the position after font change
|
|
|
|
Reposition();
|
|
|
|
}
|