File indexing completed on 2024-04-28 04:50:50
0001 /* 0002 * osdwidget.cpp 0003 * 0004 * Copyright (C) 2009-2011 Christoph Pfister <christophpfister@gmail.com> 0005 * 0006 * This program is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU General Public License as published by 0008 * the Free Software Foundation; either version 2 of the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License along 0017 * with this program; if not, write to the Free Software Foundation, Inc., 0018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0019 */ 0020 0021 #include <QPainter> 0022 #include <QTimer> 0023 0024 #include "osdwidget.h" 0025 0026 OsdWidget::OsdWidget(QWidget *parent) : QWidget(parent), osdObject(NULL) 0027 { 0028 timer = new QTimer(this); 0029 connect(timer, SIGNAL(timeout()), this, SLOT(hideOsd())); 0030 0031 hide(); 0032 } 0033 0034 OsdWidget::~OsdWidget() 0035 { 0036 } 0037 0038 void OsdWidget::showText(const QString &text, int duration) 0039 { 0040 QFont osdFont = font(); 0041 osdFont.setPointSize(25); 0042 0043 rect = QRect(QPoint(0, 0), QFontMetrics(osdFont).size(Qt::AlignLeft, text)); 0044 rect.adjust(0, 0, 10, 0); 0045 0046 pixmap = QPixmap(rect.size()); 0047 0048 { 0049 QPainter painter(&pixmap); 0050 painter.fillRect(rect, Qt::black); 0051 painter.setFont(osdFont); 0052 painter.setPen(Qt::white); 0053 painter.drawText(rect.adjusted(5, 0, 0, 0), Qt::AlignLeft, text); 0054 } 0055 0056 osdObject = NULL; 0057 update(); 0058 show(); 0059 0060 timer->start(duration); 0061 } 0062 0063 void OsdWidget::showObject(OsdObject *osdObject_, int duration) 0064 { 0065 osdObject = osdObject_; 0066 update(); 0067 show(); 0068 0069 if (duration >= 0) { 0070 timer->start(duration); 0071 } else { 0072 timer->stop(); 0073 } 0074 } 0075 0076 void OsdWidget::hideObject() 0077 { 0078 if (osdObject != NULL) { 0079 osdObject = NULL; 0080 timer->stop(); 0081 hide(); 0082 } 0083 } 0084 0085 void OsdWidget::paintEvent(QPaintEvent *) 0086 { 0087 if (osdObject == NULL) { 0088 if (layoutDirection() == Qt::LeftToRight) { 0089 QPainter(this).drawPixmap(20, 20, pixmap); 0090 rect.moveTo(20, 20); 0091 } else { 0092 int x = width() - pixmap.width() - 20; 0093 QPainter(this).drawPixmap(x, 20, pixmap); 0094 rect.moveTo(x, 20); 0095 } 0096 } else { 0097 rect = QWidget::rect(); 0098 rect.adjust(0, 0, -40, -60); 0099 pixmap = osdObject->paintOsd(rect, font(), layoutDirection()); 0100 0101 int y = (4 * height()) / 5 - rect.height(); 0102 0103 if (y < 30) { 0104 y = 30; 0105 } 0106 0107 rect.moveTo(20, y); 0108 QPainter(this).drawPixmap(rect.left(), rect.top(), pixmap); 0109 } 0110 0111 setMask(rect); 0112 } 0113 0114 void OsdWidget::hideOsd() 0115 { 0116 timer->stop(); 0117 hide(); 0118 } 0119 0120 #include "moc_osdwidget.cpp"