1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
This program 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
2 of the License, or (at your option) any later version.
*/
#include <QHBoxLayout>
#include <QLabel>
#include <QApplication>
#include <QDesktopWidget>
#include <QSize>
#include "hoverwindow.h"
#include "smglobals.h"
HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mAlignCenter(false), mDesktopHeight(-1){
setVisible(false);
setWindowOpacity(0.7);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }");
QDesktopWidget *desktop = qApp->desktop();
mDesktopHeight = desktop->availableGeometry().height();
QSize curSize = SmGlobals::instance()->cursorSize();
setHoverOffset(QPoint(curSize.width() + 30, 0));
mMainLayout = new QHBoxLayout;
mMainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
mMainLayout->setContentsMargins(4, 4, 4, 4);
mLabel = new QLabel;
mMainLayout->addWidget(mLabel);
setLayout(mMainLayout);
}
void HoverWindow::setContent(const QString &parent, const QStringList &children){
mAlignCenter = false;
QString curText = QString("<p style=\"align: center; text-decoration: underline; font-weight: bold; margin-bottom: 0;\">%1</p>").arg(parent);
curText.append("<ul style=\"margin-left: -25; margin-top: 0px;\">");
int count = qMin(6, children.size());
int i = 0;
for(i = 0; i < count; ++i){
curText.append(QString("<li>%1</li>").arg(children.at(i)));
}
if(i < children.count()){
curText.append("<li>...</li>");
}
curText.append("</ul>");
mLabel->setText(curText);
}
void HoverWindow::setPixmap(const QPixmap &pm){
mAlignCenter = true;
QPixmap curPm = pm;
if(curPm.height() > 500){
curPm = curPm.scaledToHeight(500);
}
if(curPm.width() > 300){
curPm = curPm.scaledToWidth(300);
}
mLabel->setPixmap(curPm);
setMaximumSize(curPm.width() + 10, curPm.height() + 10);
setMinimumSize(curPm.width() + 10, curPm.height() + 10);
}
void HoverWindow::setData(const QList<QVariant> &data){
if(data.isEmpty()){
return;
}
if(data.at(0).canConvert(QVariant::Pixmap)){
setPixmap(data.at(0).value<QPixmap>());
return;
}
if(data.size() != 2){
return;
}
QStringList dataList = data.at(1).toStringList();
if(dataList.isEmpty()){
dataList << tr("<empty>");
}
setContent(data.at(0).toString(), dataList);
}
int HoverWindow::pixmapHeight() const{
const QPixmap *pm = mLabel->pixmap();
return pm ? pm->height() : 0;
}
void HoverWindow::setPos(){
const QPoint globalPos(QCursor::pos());
QPoint hoverPos(globalPos.x() + mHoverOffset.x(), globalPos.y() + mHoverOffset.y());
if(mAlignCenter){
hoverPos = QPoint(hoverPos.x(), hoverPos.y() - height() / 2);
}
if(hoverPos.y() < 0){
hoverPos = QPoint(hoverPos.x(), 0);
}
if(hoverPos.y() + height() > mDesktopHeight){
hoverPos = QPoint(hoverPos.x(), mDesktopHeight - height());
}
move(hoverPos);
}
|