summaryrefslogtreecommitdiffstats
path: root/hoverwindow.cpp
blob: 4bfdae44af678cff938ca27c0a9eba5227034de1 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
  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 <QTextDocument>
#include <QSettings>
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include <QFontMetrics>
#include <QBrush>
#include <QPen>
#include <QColor>
#include <QApplication>

#include "hoverwindow.h"
#include "smglobals.h"

HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mAlignCenter(false), mDesktopHeight(-1){
	setVisible(false);
	QSettings s;
	int opacity = s.value("ui/hoveropacity", 7).toInt();
	setWindowOpacity(opacity / 10.0);
	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>");
	QTextDocument doc;
	doc.setHtml(curText);
	mLabel->setText(curText);
	setMinimumSize(doc.size().width() + 10, doc.size().height() + 10);
	setMaximumSize(doc.size().width() + 10, doc.size().height() + 10);
    raise();
}

void HoverWindow::setPixmap(const QPixmap &pm, bool scale){
	mAlignCenter = true;
	QPixmap curPm = pm;
	if(scale){
		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);
    raise();
}

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("&lt;empty&gt;");
	}
	setContent(data.at(0).toString(), dataList);
}

void HoverWindow::setCaption(const QString &caption){
	const QPixmap *pm = mLabel->pixmap();
	if(!pm){
		return;
	}
	QFontMetrics fm = qApp->fontMetrics();
	QString elidedCap = fm.elidedText(caption, Qt::ElideMiddle, pm->size().width() - 18, Qt::TextSingleLine);
	QSize captionSize = fm.size(Qt::TextSingleLine, elidedCap);
	QImage img = pm->toImage();
	QPainter *p = new QPainter(&img);
	QColor bgColor(Qt::white);
	p->setBrush(QBrush(bgColor));
	p->setPen(Qt::NoPen);
	qreal xStart = (pm->size().width() - captionSize.width()) / 2.0;
	QRectF bgRect(xStart, 5, captionSize.width() + 6, captionSize.height());
	p->drawRect(bgRect);
	p->setPen(Qt::black);
	p->drawText(bgRect.topLeft().x() + 3, bgRect.topLeft().y() + fm.ascent(), elidedCap);
	delete p;
	mLabel->setPixmap(QPixmap::fromImage(img));
}

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);
}