summaryrefslogtreecommitdiffstats
path: root/hoverwindow.cpp
blob: 100f278f543d6380652eb61b37523d76834a3053 (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
/*
  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 "hoverwindow.h"

HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f){
	setVisible(false);
	setWindowOpacity(0.7);
	setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }");
	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->setContentsMargins(4, 4, 4, 4);
	mLabel = new QLabel;
	mainLayout->addWidget(mLabel);
	setLayout(mainLayout);
}

void HoverWindow::setContent(const QString &parent, const QStringList &children){
	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){
	QPixmap curPm = pm;
	if(curPm.height() > 500){
		curPm = curPm.scaledToHeight(500);
	}
	if(curPm.width() > 300){
		curPm = curPm.scaledToWidth(300);
	}
	mLabel->setPixmap(curPm);
}

int HoverWindow::pixmapHeight() const{
	const QPixmap *pm = mLabel->pixmap();
	return pm ? pm->height() : 0;
}

void HoverWindow::setPos(const QPoint &cursorPos){
	if(cursorPos.y() - height() / 2 < 0){
		move(QPoint(cursorPos.x(), 0));
		return;
	}
	QDesktopWidget *desktop = qApp->desktop();
	int desktopHeight = desktop->availableGeometry().height();
	if(cursorPos.y() + height() / 2 > desktopHeight){
		move(QPoint(cursorPos.x(), desktopHeight - height()));
		return;
	}
	move(cursorPos);
}