| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 |
* |
| 4 |
* Author: ZhangYong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: ZhangYong <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
#include "elidedlabel.h" |
| 22 |
|
| 23 |
#include <QApplication> |
| 24 |
#include <QDesktopWidget> |
| 25 |
#include <QFont> |
| 26 |
#include <QFontMetrics> |
| 27 |
#include <QPainter> |
| 28 |
#include <QDebug> |
| 29 |
|
| 30 |
#include "application.h" |
| 31 |
#include "controller/configsetter.h" |
| 32 |
|
| 33 |
//const int MAX_WIDTH = 600; |
| 34 |
//const int HEIGHT = 39; |
| 35 |
|
| 36 |
ElidedLabel::ElidedLabel(QWidget *parent) |
| 37 |
: QLabel(parent) |
| 38 |
, m_leftMargin(0) |
| 39 |
{ |
| 40 |
onThemeChanged(dApp->viewerTheme->getCurrentTheme()); |
| 41 |
connect(dApp->viewerTheme, &ViewerThemeManager::viewerThemeChanged, |
| 42 |
this, &ElidedLabel::onThemeChanged); |
| 43 |
} |
| 44 |
|
| 45 |
ElidedLabel::~ElidedLabel() |
| 46 |
{ |
| 47 |
} |
| 48 |
void ElidedLabel::setText(const QString &text, int leftMargin) |
| 49 |
{ |
| 50 |
m_text = text; |
| 51 |
m_leftMargin = leftMargin; |
| 52 |
update(); |
| 53 |
} |
| 54 |
|
| 55 |
void ElidedLabel::paintEvent(QPaintEvent *) |
| 56 |
{ |
| 57 |
QPainter painter(this); |
| 58 |
QFontMetrics fm(this->font()); |
| 59 |
painter.setPen(QPen(m_textColor)); |
| 60 |
painter.drawText(m_leftMargin, (this->height() - fm.height()) / 2, |
| 61 |
this->width() - m_leftMargin, this->height(), Qt::AlignLeft, m_text); |
| 62 |
} |
| 63 |
|
| 64 |
void ElidedLabel::onThemeChanged(ViewerThemeManager::AppTheme theme) |
| 65 |
{ |
| 66 |
if (theme == ViewerThemeManager::Dark) { |
| 67 |
m_textColor = QColor(255, 255, 255, 204); |
| 68 |
} else { |
| 69 |
m_textColor = QColor("#656565"); |
| 70 |
} |
| 71 |
update(); |
| 72 |
} |
| 73 |
|
| 74 |
void ElidedLabel::resizeEvent(QResizeEvent *event) |
| 75 |
{ |
| 76 |
Q_UNUSED(event); |
| 77 |
update(); |
| 78 |
} |